From 0cd3a662ced371ca01cd21e89d8225501cc415f9 Mon Sep 17 00:00:00 2001 From: itchyny Date: Thu, 18 Apr 2024 13:05:43 +0900 Subject: [PATCH] improve compiler to abort with error if query is missing --- cli/test.yaml | 37 +++++++++++++++++++++++++++++-------- compiler.go | 2 ++ parser.go | 2 +- parser.go.y | 2 +- 4 files changed, 33 insertions(+), 10 deletions(-) diff --git a/cli/test.yaml b/cli/test.yaml index fedecde8..c8d0a770 100644 --- a/cli/test.yaml +++ b/cli/test.yaml @@ -1,22 +1,36 @@ - name: empty input + args: + - '.' input: '' expected: '' -- name: empty query +- name: empty args + input: 'null' + expected: | + null + +- name: missing query args: - '' - input: '128' + error: | + compile error: missing query (try ".") + exit_code: 3 + +- name: query with comment + args: + - '. # comment' + input: 'null' expected: | - 128 + null -- name: comment only query +- name: missing query with comment args: - '# comment' - input: '128' - expected: | - 128 + error: | + compile error: missing query (try ".") + exit_code: 3 -- name: query with comment +- name: query with comment with newline args: - | # comment @@ -2574,6 +2588,13 @@ ^ unexpected token "$f" exit_code: 3 +- name: function declaration without query + args: + - 'def f: 0;' + error: | + compile error: missing query (try ".") + exit_code: 3 + - name: add, subtract, multiply, divide, modulo numbers args: - -c diff --git a/compiler.go b/compiler.go index d114f27b..b2788c05 100644 --- a/compiler.go +++ b/compiler.go @@ -398,6 +398,8 @@ func (c *compiler) compileQuery(e *Query) error { return c.compileTerm(e.Term) } switch e.Op { + case Operator(0): + return errors.New(`missing query (try ".")`) case OpPipe: if err := c.compileQuery(e.Left); err != nil { return err diff --git a/parser.go b/parser.go index 61501f87..561ed396 100644 --- a/parser.go +++ b/parser.go @@ -837,7 +837,7 @@ yydefault: yyDollar = yyS[yypt-1 : yypt+1] //line parser.go.y:104 { - yyVAL.value = &Query{FuncDefs: reverseFuncDef(yyDollar[1].value.([]*FuncDef)), Term: &Term{Type: TermTypeIdentity}} + yyVAL.value = &Query{FuncDefs: reverseFuncDef(yyDollar[1].value.([]*FuncDef))} } case 12: yyDollar = yyS[yypt-0 : yypt+1] diff --git a/parser.go.y b/parser.go.y index 7354c645..29bed07c 100644 --- a/parser.go.y +++ b/parser.go.y @@ -102,7 +102,7 @@ meta body : funcdefs { - $$ = &Query{FuncDefs: reverseFuncDef($1.([]*FuncDef)), Term: &Term{Type: TermTypeIdentity}} + $$ = &Query{FuncDefs: reverseFuncDef($1.([]*FuncDef))} } | query