grammar.js 4.9 KB

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