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 14, 2024
1 parent 789ddf2 commit 30065a7
Show file tree
Hide file tree
Showing 18 changed files with 1,257 additions and 1,091 deletions.
42 changes: 42 additions & 0 deletions compiler/ast/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,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 {
FStringElem()
Node
}

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

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:""`
Lbrack int `json:"lbrack"`
Expr Expr `json:"expr"`
Rbrack int `json:"rbrack"`
}

func (f *FStringExpr) Pos() int { return f.Lbrack }
func (f *FStringExpr) End() int { return f.Rbrack + 1 }

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

func (*UnaryExpr) ExprAST() {}
func (*BinaryExpr) ExprAST() {}
func (*Conditional) ExprAST() {}
Expand All @@ -321,6 +362,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 30065a7

Please sign in to comment.