1 | var dict = require('./dict');
|
2 | var fs = require('fs');
|
3 | var grammar = {
|
4 |
|
5 | lex: {
|
6 |
|
7 | macros: {
|
8 | esc: "\\\\",
|
9 | int: dict.integer
|
10 | },
|
11 |
|
12 | rules: [
|
13 | ["\\$", "return 'DOLLAR'"],
|
14 | ["\\.\\.", "return 'DOT_DOT'"],
|
15 | ["\\.", "return 'DOT'"],
|
16 | ["\\*", "return 'STAR'"],
|
17 | [dict.identifier, "return 'IDENTIFIER'"],
|
18 | ["\\[", "return '['"],
|
19 | ["\\]", "return ']'"],
|
20 | [",", "return ','"],
|
21 | ["({int})?\\:({int})?(\\:({int})?)?", "return 'ARRAY_SLICE'"],
|
22 | ["{int}", "return 'INTEGER'"],
|
23 | [dict.qq_string, "yytext = yytext.substr(1,yyleng-2); return 'QQ_STRING';"],
|
24 | [dict.q_string, "yytext = yytext.substr(1,yyleng-2); return 'Q_STRING';"],
|
25 | ["\\(.+?\\)(?=\\])", "return 'SCRIPT_EXPRESSION'"],
|
26 | ["\\?\\(.+?\\)(?=\\])", "return 'FILTER_EXPRESSION'"]
|
27 | ]
|
28 | },
|
29 |
|
30 | start: "JSON_PATH",
|
31 |
|
32 | bnf: {
|
33 |
|
34 | JSON_PATH: [
|
35 | [ 'DOLLAR', 'yy.ast.set({ expression: { type: "root", value: $1 } }); yy.ast.unshift(); return yy.ast.yield()' ],
|
36 | [ 'DOLLAR PATH_COMPONENTS', 'yy.ast.set({ expression: { type: "root", value: $1 } }); yy.ast.unshift(); return yy.ast.yield()' ],
|
37 | [ 'LEADING_CHILD_MEMBER_EXPRESSION', 'yy.ast.unshift(); return yy.ast.yield()' ],
|
38 | [ 'LEADING_CHILD_MEMBER_EXPRESSION PATH_COMPONENTS', 'yy.ast.set({ operation: "member", scope: "child", expression: { type: "identifier", value: $1 }}); yy.ast.unshift(); return yy.ast.yield()' ] ],
|
39 |
|
40 | PATH_COMPONENTS: [
|
41 | [ 'PATH_COMPONENT', '' ],
|
42 | [ 'PATH_COMPONENTS PATH_COMPONENT', '' ] ],
|
43 |
|
44 | PATH_COMPONENT: [
|
45 | [ 'MEMBER_COMPONENT', 'yy.ast.set({ operation: "member" }); yy.ast.push()' ],
|
46 | [ 'SUBSCRIPT_COMPONENT', 'yy.ast.set({ operation: "subscript" }); yy.ast.push() ' ] ],
|
47 |
|
48 | MEMBER_COMPONENT: [
|
49 | [ 'CHILD_MEMBER_COMPONENT', 'yy.ast.set({ scope: "child" })' ],
|
50 | [ 'DESCENDANT_MEMBER_COMPONENT', 'yy.ast.set({ scope: "descendant" })' ] ],
|
51 |
|
52 | CHILD_MEMBER_COMPONENT: [
|
53 | [ 'DOT MEMBER_EXPRESSION', '' ] ],
|
54 |
|
55 | LEADING_CHILD_MEMBER_EXPRESSION: [
|
56 | [ 'MEMBER_EXPRESSION', 'yy.ast.set({ scope: "child", operation: "member" })' ] ],
|
57 |
|
58 | DESCENDANT_MEMBER_COMPONENT: [
|
59 | [ 'DOT_DOT MEMBER_EXPRESSION', '' ] ],
|
60 |
|
61 | MEMBER_EXPRESSION: [
|
62 | [ 'STAR', 'yy.ast.set({ expression: { type: "wildcard", value: $1 } })' ],
|
63 | [ 'IDENTIFIER', 'yy.ast.set({ expression: { type: "identifier", value: $1 } })' ],
|
64 | [ 'SCRIPT_EXPRESSION', 'yy.ast.set({ expression: { type: "script_expression", value: $1 } })' ],
|
65 | [ 'INTEGER', 'yy.ast.set({ expression: { type: "numeric_literal", value: parseInt($1) } })' ],
|
66 | [ 'END', '' ] ],
|
67 |
|
68 | SUBSCRIPT_COMPONENT: [
|
69 | [ 'CHILD_SUBSCRIPT_COMPONENT', 'yy.ast.set({ scope: "child" })' ],
|
70 | [ 'DESCENDANT_SUBSCRIPT_COMPONENT', 'yy.ast.set({ scope: "descendant" })' ] ],
|
71 |
|
72 | CHILD_SUBSCRIPT_COMPONENT: [
|
73 | [ '[ SUBSCRIPT ]', '' ] ],
|
74 |
|
75 | DESCENDANT_SUBSCRIPT_COMPONENT: [
|
76 | [ 'DOT_DOT [ SUBSCRIPT ]', '' ] ],
|
77 |
|
78 | SUBSCRIPT: [
|
79 | [ 'SUBSCRIPT_EXPRESSION', '' ],
|
80 | [ 'SUBSCRIPT_EXPRESSION_LIST', '$1.length > 1? yy.ast.set({ expression: { type: "union", value: $1 } }) : $$ = $1' ] ],
|
81 |
|
82 | SUBSCRIPT_EXPRESSION_LIST: [
|
83 | [ 'SUBSCRIPT_EXPRESSION_LISTABLE', '$$ = [$1]'],
|
84 | [ 'SUBSCRIPT_EXPRESSION_LIST , SUBSCRIPT_EXPRESSION_LISTABLE', '$$ = $1.concat($3)' ] ],
|
85 |
|
86 | SUBSCRIPT_EXPRESSION_LISTABLE: [
|
87 | [ 'INTEGER', '$$ = { expression: { type: "numeric_literal", value: parseInt($1) } }; yy.ast.set($$)' ],
|
88 | [ 'STRING_LITERAL', '$$ = { expression: { type: "string_literal", value: $1 } }; yy.ast.set($$)' ],
|
89 | [ 'ARRAY_SLICE', '$$ = { expression: { type: "slice", value: $1 } }; yy.ast.set($$)' ] ],
|
90 |
|
91 | SUBSCRIPT_EXPRESSION: [
|
92 | [ 'STAR', '$$ = { expression: { type: "wildcard", value: $1 } }; yy.ast.set($$)' ],
|
93 | [ 'SCRIPT_EXPRESSION', '$$ = { expression: { type: "script_expression", value: $1 } }; yy.ast.set($$)' ],
|
94 | [ 'FILTER_EXPRESSION', '$$ = { expression: { type: "filter_expression", value: $1 } }; yy.ast.set($$)' ] ],
|
95 |
|
96 | STRING_LITERAL: [
|
97 | [ 'QQ_STRING', "$$ = $1" ],
|
98 | [ 'Q_STRING', "$$ = $1" ] ]
|
99 | }
|
100 | };
|
101 | if (fs.readFileSync) {
|
102 | grammar.moduleInclude = fs.readFileSync(require.resolve("../include/module.js"));
|
103 | grammar.actionInclude = fs.readFileSync(require.resolve("../include/action.js"));
|
104 | }
|
105 |
|
106 | module.exports = grammar;
|