Skip to content

Commit

Permalink
Formatted String Literals
Browse files Browse the repository at this point in the history
The commit removes the template literals and replaces them with
formatted string literals. F-strings have a similar functionality but
the expression substitution only happens on strings prefixed with the
'f' character.
  • Loading branch information
mattnibs committed May 31, 2024
1 parent 68f35c9 commit ff4643a
Show file tree
Hide file tree
Showing 20 changed files with 1,277 additions and 1,089 deletions.
42 changes: 42 additions & 0 deletions compiler/ast/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,47 @@ type OverExpr struct {
func (o *OverExpr) Pos() int { return o.KeywordPos }
func (o *OverExpr) End() int { return o.Body.End() }

type FString struct {
Kind string `json:"kind" unpack:""`
StartPos int `json:"start_pos"`
Elems []FStringElem `json:"elems"`
}

func (f *FString) Pos() int { return f.StartPos }
func (f *FString) End() int {
if n := len(f.Elems); n > 0 {
return f.Elems[n-1].End() + 1
}
return f.StartPos + 3
}

type FStringElem interface {
Node
FStringElem()
}

type FStringText struct {
Kind string `json:"kind" unpack:""`
Text string `json:"text"`
TextPos int `json:"text_pos"`
}

func (f *FStringText) Pos() int { return f.TextPos }
func (f *FStringText) End() int { return f.TextPos + len(f.Text) }

type FStringExpr struct {
Kind string `json:"kind" unpack:""`
Lbrace int `json:"lbrace"`
Expr Expr `json:"expr"`
Rbrace int `json:"rbrace"`
}

func (f *FStringExpr) Pos() int { return f.Lbrace }
func (f *FStringExpr) End() int { return f.Rbrace + 1 }

func (*FStringText) FStringElem() {}
func (*FStringExpr) FStringElem() {}

func (*UnaryExpr) ExprAST() {}
func (*BinaryExpr) ExprAST() {}
func (*Conditional) ExprAST() {}
Expand All @@ -318,6 +359,7 @@ func (*ArrayExpr) ExprAST() {}
func (*SetExpr) ExprAST() {}
func (*MapExpr) ExprAST() {}
func (*OverExpr) ExprAST() {}
func (*FString) ExprAST() {}

type ConstDecl struct {
Kind string `json:"kind" unpack:""`
Expand Down
3 changes: 3 additions & 0 deletions compiler/ast/unpack.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ var unpacker = unpack.New(
Field{},
File{},
From{},
FString{},
FStringExpr{},
FStringText{},
FuncDecl{},
Fuse{},
Summarize{},
Expand Down
11 changes: 10 additions & 1 deletion compiler/ast/zed/value.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,5 +97,14 @@ func (*TypeValue) ExprDAG() {}
func (x *Primitive) Pos() int { return x.TextPos }
func (x *TypeValue) Pos() int { return x.Lbrack }

func (x *Primitive) End() int { return x.TextPos + len(x.Text) }
func (x *Primitive) End() int {
// If Primitive type is string we need to adjust the end for quotations
// marks since they are currently not captured in the Value string. They
// should be but this will require a change to the ZSON lexer as well as
// the Zed parser.
if x.Type == "string" {
return x.TextPos + len(x.Text) + 2
}
return x.TextPos + len(x.Text)
}
func (x *TypeValue) End() int { return x.Rbrack + 1 }
Loading

0 comments on commit ff4643a

Please sign in to comment.