From 3a59ea58b5c953e2dad6e3bd784cce9350ef691a Mon Sep 17 00:00:00 2001 From: Matthew Nibecker Date: Fri, 24 May 2024 00:07:41 -0700 Subject: [PATCH] fix parser --- compiler/ast/ast.go | 12 ++++++------ compiler/parser/parser.go | 4 ++-- compiler/parser/parser.peg | 8 ++++---- compiler/ztests/{fstring.yaml => f-string.yaml} | 0 docs/language/expressions.md | 2 +- docs/tutorials/zq.md | 2 +- zfmt/ast.go | 6 ++++-- 7 files changed, 18 insertions(+), 16 deletions(-) rename compiler/ztests/{fstring.yaml => f-string.yaml} (100%) diff --git a/compiler/ast/ast.go b/compiler/ast/ast.go index df3c505da6..ec2a82f916 100644 --- a/compiler/ast/ast.go +++ b/compiler/ast/ast.go @@ -312,14 +312,14 @@ 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 } @@ -327,13 +327,13 @@ 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() {} diff --git a/compiler/parser/parser.go b/compiler/parser/parser.go index e541e1560c..860e4fbd89 100644 --- a/compiler/parser/parser.go +++ b/compiler/parser/parser.go @@ -15238,9 +15238,9 @@ func (p *parser) callonFStringSingleQuotedChar7() (any, error) { func (c *current) onFStringExpr1(e any) (any, error) { return &ast.FStringExpr{ Kind: "FStringExpr", - Lbrack: c.pos.offset, + Lbrace: c.pos.offset, Expr: e.(ast.Expr), - Rbrack: lastPos(c, "}"), + Rbrace: lastPos(c, "}"), }, nil } diff --git a/compiler/parser/parser.peg b/compiler/parser/parser.peg index 953545da49..be77d9faf3 100644 --- a/compiler/parser/parser.peg +++ b/compiler/parser/parser.peg @@ -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* '"' { @@ -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 } diff --git a/compiler/ztests/fstring.yaml b/compiler/ztests/f-string.yaml similarity index 100% rename from compiler/ztests/fstring.yaml rename to compiler/ztests/f-string.yaml diff --git a/docs/language/expressions.md b/docs/language/expressions.md index ffc335e022..51428ae848 100644 --- a/docs/language/expressions.md +++ b/docs/language/expressions.md @@ -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"{ }" ``` diff --git a/docs/tutorials/zq.md b/docs/tutorials/zq.md index b4f394cc03..2a121e5f03 100644 --- a/docs/tutorials/zq.md +++ b/docs/tutorials/zq.md @@ -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 diff --git a/zfmt/ast.go b/zfmt/ast.go index 090ca8752c..7e4ca13ad3 100644 --- a/zfmt/ast.go +++ b/zfmt/ast.go @@ -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: @@ -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) }