/query-parser

parse advanced search queries using antlr4

Primary LanguageJavaScript

Query Parser

An antlr4-based parser for a query syntax similar to Github's advanced search.

When running index.js, input can be passed in the following ways:

  • arguments
  • stdin
  • file, with -i

For instance:

./index.js 'key:value'

echo 'key:value' | ./index.js

./index.js -i <(echo 'key:value')

Syntax

Keys and values are separated by :. Keys cannot have spaces or be quoted. Values can either follow the same formats as keys, or be quoted and be JSON strings.

For example, the following file:

hi
"hi there"
hello:world
hello:"brave new world"

Will produce this output:

┌───────┬─────────────────┐
│ Key   │ Value           │
├───────┼─────────────────┤
│       │ hi              │
├───────┼─────────────────┤
│       │ hi there        │
├───────┼─────────────────┤
│ hello │ world           │
├───────┼─────────────────┤
│ hello │ brave new world │
└───────┴─────────────────┘

Test

npm test

Generate Grammar Code

antlr4 -Dlanguage=JavaScript -o lib antlr4/Query.g4