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