From 1c5e97b93c7eaa8ad8cdd7dee9cdc4c575907cfe Mon Sep 17 00:00:00 2001 From: John Kerl Date: Fri, 3 Mar 2023 21:42:10 -0500 Subject: [PATCH] Fix bug on DSL comment with no final newline (#1216) --- internal/pkg/dsl/cst/root.go | 8 ++++++++ test/cases/dsl-parse/0121/cmd | 1 + test/cases/dsl-parse/0121/experr | 0 test/cases/dsl-parse/0121/expout | 10 ++++++++++ 4 files changed, 19 insertions(+) create mode 100644 test/cases/dsl-parse/0121/cmd create mode 100644 test/cases/dsl-parse/0121/experr create mode 100644 test/cases/dsl-parse/0121/expout diff --git a/internal/pkg/dsl/cst/root.go b/internal/pkg/dsl/cst/root.go index 1f592f5f4..979c82ad1 100644 --- a/internal/pkg/dsl/cst/root.go +++ b/internal/pkg/dsl/cst/root.go @@ -127,6 +127,14 @@ func buildASTFromString(dslString string) (*dsl.AST, error) { dslString = dslString[1 : len(dslString)-1] } + // The comment-stripping lex expression in Miller's GOCC grammar matches from '#' to '\n' ... in + // the case where there is a '#', then comment text, then end-of-string without any final + // newline, the comment text does _not_ get stripped out and is a parse error. + // It's simplest to ensure, here, that DSL-expression strings have a final newline. + if !strings.HasSuffix(dslString, "\n") { + dslString += "\n" + } + theLexer := lexer.NewLexer([]byte(dslString)) theParser := parser.NewParser() interfaceAST, err := theParser.Parse(theLexer) diff --git a/test/cases/dsl-parse/0121/cmd b/test/cases/dsl-parse/0121/cmd new file mode 100644 index 000000000..52455015a --- /dev/null +++ b/test/cases/dsl-parse/0121/cmd @@ -0,0 +1 @@ +mlr --from test/input/abixy put '$z = NR # test' diff --git a/test/cases/dsl-parse/0121/experr b/test/cases/dsl-parse/0121/experr new file mode 100644 index 000000000..e69de29bb diff --git a/test/cases/dsl-parse/0121/expout b/test/cases/dsl-parse/0121/expout new file mode 100644 index 000000000..801269bd2 --- /dev/null +++ b/test/cases/dsl-parse/0121/expout @@ -0,0 +1,10 @@ +a=pan,b=pan,i=1,x=0.34679014,y=0.72680286,z=1 +a=eks,b=pan,i=2,x=0.75867996,y=0.52215111,z=2 +a=wye,b=wye,i=3,x=0.20460331,y=0.33831853,z=3 +a=eks,b=wye,i=4,x=0.38139939,y=0.13418874,z=4 +a=wye,b=pan,i=5,x=0.57328892,y=0.86362447,z=5 +a=zee,b=pan,i=6,x=0.52712616,y=0.49322129,z=6 +a=eks,b=zee,i=7,x=0.61178406,y=0.18788492,z=7 +a=zee,b=wye,i=8,x=0.59855401,y=0.97618139,z=8 +a=hat,b=wye,i=9,x=0.03144188,y=0.74955076,z=9 +a=pan,b=wye,i=10,x=0.50262601,y=0.95261836,z=10