Skip to content

Instantly share code, notes, and snippets.

@davejhilton
Created October 1, 2016 16:12
Show Gist options
  • Save davejhilton/61b169cd7ed4381435c4260bae8c7588 to your computer and use it in GitHub Desktop.
Save davejhilton/61b169cd7ed4381435c4260bae8c7588 to your computer and use it in GitHub Desktop.
Jison Grammar for grafana kairosdb-datasource queries
/*
The grammar below is intended for use with Jison Parser Generator (http://zaa.ch/jison/)
Paste the contents below int http://zaa.ch/jison/try/ to see it in action
*/
/* ====== START GRAMMAR ====== */
/* description: Parses kairosdb-datasource 'tag_values' queries */
/* lexical grammar */
%lex
%%
\s+ /* skip whitespace */
[A-Za-z0-9_]+ return 'IDENTIFIER'
"(" return 'LEFT_PAREN'
")" return 'RIGHT_PAREN'
"," return 'COMMA'
"=" return 'EQUALS'
"{" return 'CURLY_OPEN'
"}" return 'CURLY_CLOSE'
<<EOF>> return 'EOF'
. return 'INVALID'
/lex
/* operator associations and precedence */
%start expressions
%% /* language grammar */
expressions
: IDENTIFIER LEFT_PAREN arguments RIGHT_PAREN EOF
{return {"function": $1, "args": $3};}
;
arguments
: IDENTIFIER COMMA IDENTIFIER
{$$ = {"metric": $1, "tagKey": $3};}
| IDENTIFIER COMMA IDENTIFIER COMMA filters
{$$ = {"metric": $1, "tagKey": $3, "filters": $5};}
;
filters
: filter
{$$ = [$1];}
| filter COMMA filters
{$$ = [$1].concat($3);}
;
filter
: IDENTIFIER EQUALS operand
{$$ = {"name":$1, "value":$3};}
;
operand
: IDENTIFIER
{$$ = [$1];}
| CURLY_OPEN operand_list CURLY_CLOSE
{$$ = $2;}
;
operand_list
: IDENTIFIER
{$$ = [$1];}
| IDENTIFIER COMMA operand_list
{$$ = [$1].concat($3);}
;
/* ====== END GRAMMAR ====== */
/*
The above grammar can be used to parse queries like the following:
tag_values(metric,tag_name)
tag_values(metric,tag_name,tag1=val1)
tag_values(metric,tag_name,tag1=val1,tag2=val2,tag3=val3)
tag_values(metric,tag_name,tag1=val1,tag2={val2,val3,val4},tag3={val5})
etc.
EXAMPLE:
The generated parser for this grammar reads the following query:
tag_values(metric,tag_name,t1=val1,t2={val2},t3={val3,val4,val5})
and produces the following output:
{
"function": "tag_values",
"args": {
"metric": "metric",
"tagKey": "tag_name",
"filters": [
{
"name": "t1",
"value": [
"val1"
]
},
{
"name": "t2",
"value": [
"val2"
]
},
{
"name": "t3",
"value": [
"val3",
"val4",
"val5"
]
}
]
}
}
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment