Skip to content

Commit

Permalink
fix parser
Browse files Browse the repository at this point in the history
  • Loading branch information
mattnibs authored and nwt committed May 31, 2024
1 parent fd1aaf7 commit 2b6bc76
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 16 deletions.
12 changes: 6 additions & 6 deletions compiler/ast/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -312,28 +312,28 @@ func (f *FString) End() int {
}

type FStringElem interface {
FStringElem()
Node
FStringElem()
}

type FStringText struct {
Kind string `json:"kind" unpack:""`
TextPos int `json:"text_pos"`
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:""`
Lbrack int `json:"lbrack"`
Lbrace int `json:"lbrack"`
Expr Expr `json:"expr"`
Rbrack int `json:"rbrack"`
Rbrace int `json:"rbrack"`
}

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

func (*FStringText) FStringElem() {}
func (*FStringExpr) FStringElem() {}
Expand Down
4 changes: 2 additions & 2 deletions compiler/parser/parser.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions compiler/parser/parser.peg
Original file line number Diff line number Diff line change
Expand Up @@ -1291,8 +1291,8 @@ ComplexType
}

StringLiteral
= '"' v:DoubleQuotedChar* '"' { return newPrimitive(c, "string", joinChars(v), nil }
/ "'" v:SingleQuotedChar* "'" { return newPrimitive(c, "string", joinChars(V), nil }
= '"' v:DoubleQuotedChar* '"' { return newPrimitive(c, "string", joinChars(v)), nil }
/ "'" v:SingleQuotedChar* "'" { return newPrimitive(c, "string", joinChars(v)), nil }

FString
= "f\"" v:FStringDoubleQuotedElem* '"' {
Expand Down Expand Up @@ -1334,9 +1334,9 @@ FStringExpr
= "{" __ e:Expr __ "}" {
return &ast.FStringExpr{
Kind: "FStringExpr",
Lbrack: c.pos.offset,
Lbrace: c.pos.offset,
Expr: e.(ast.Expr),
Rbrack: lastPos(c, "}"),
Rbrace: lastPos(c, "}"),
}, nil
}

Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion docs/language/expressions.md
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ in a string the `$` character must be escaped, i.e., `\$`.

A formatted string literal (or f-string) is a string literal prefixed with `f`.
These strings may include replacement expressions which are delimited by curly
braces
braces:
```
f"{ <expr> }"
```
Expand Down
2 changes: 1 addition & 1 deletion docs/tutorials/zq.md
Original file line number Diff line number Diff line change
Expand Up @@ -929,7 +929,7 @@ DATE NUMBER TITLE
2019-11-12T16:49:07Z PR #6 a few clarifications to the zson spec
...
```
Note that we used [formatted string literals](../language/expressions.md#formatted-string-literals)
Note that we used a [formatted string literal](../language/expressions.md#formatted-string-literals)
to convert the field `number` into a string and format it with surrounding text.

Instead of old PRs, we can get the latest list of PRs using the
Expand Down
6 changes: 4 additions & 2 deletions zfmt/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ func (c *canon) expr(e ast.Expr, parent string) {
c.flush()
c.write(")")
case *ast.FString:
c.write("f\"")
c.write(`f"`)
for _, elem := range e.Elems {
switch elem := elem.(type) {
case *ast.FStringExpr:
Expand All @@ -217,9 +217,11 @@ func (c *canon) expr(e ast.Expr, parent string) {
c.write("}")
case *ast.FStringText:
c.write(elem.Text)
default:
c.write("(unknown f-strint element %T)", elem)
}
}
c.write("\"")
c.write(`"`)
default:
c.write("(unknown expr %T)", e)
}
Expand Down

0 comments on commit 2b6bc76

Please sign in to comment.