Skip to content

Commit

Permalink
improve compiler to abort with error if query is missing
Browse files Browse the repository at this point in the history
  • Loading branch information
itchyny committed Apr 18, 2024
1 parent 422cc9d commit 0cd3a66
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 10 deletions.
37 changes: 29 additions & 8 deletions cli/test.yaml
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion parser.go

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

2 changes: 1 addition & 1 deletion parser.go.y
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ meta
body
: funcdefs
{
$$ = &Query{FuncDefs: reverseFuncDef($1.([]*FuncDef)), Term: &Term{Type: TermTypeIdentity}}
$$ = &Query{FuncDefs: reverseFuncDef($1.([]*FuncDef))}
}
| query

Expand Down

0 comments on commit 0cd3a66

Please sign in to comment.