Advertisement
Guest User

Untitled

a guest
Jun 23rd, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
JSON 6.39 KB | None | 0 0
  1. start
  2.   = PrimaryExpression
  3.  
  4. PrimaryExpression
  5.   = ArithmeticExpression
  6.   / CallExpression
  7.   / Identifier
  8.   / Literal
  9.  
  10. LeftHandSideExpression
  11.   = UnaryOperator expression:LeftHandSideExpression {
  12.      return {
  13.       type: "callExpression",
  14.       id: "NOT",
  15.       arguments: expression
  16.     }
  17.   }
  18.   / LeftHandSidePart
  19.  
  20. LeftHandSidePart
  21.  = CallExpression
  22.   / Identifier
  23.   / Literal
  24.   / "(" __ expression:ArithmeticExpression __ ")" { return expression; }
  25.  
  26. CallExpression
  27.   = FunctionExpression
  28.  
  29. FunctionExpression
  30.   = id:FunctionIdentifier __ args:Arguments {
  31.     return {
  32.       type: "callExpression",
  33.       id: id,
  34.       arguments: args !== null ? args[0] : []
  35.     }
  36.   }
  37.  
  38. ArithmeticExpression
  39.   = LogicalAndExpression
  40.  
  41. LogicalAndExpression
  42.   = head:(LogicalOrExpression) __ (LogicalOperatorAnd) __ tail:LogicalAndExpression
  43.   {
  44.     return {
  45.       type: "callExpression",
  46.       id: "AND",
  47.       arguments: [head, tail]
  48.     }
  49.   }
  50.   / LogicalOrExpression
  51.  
  52. LogicalOrExpression
  53.   = head:(LogicalCompareExpression) __ (LogicalOperatorOr) __ tail:LogicalOrExpression
  54.   {
  55.     return {
  56.       type: "callExpression",
  57.       id: "OR",
  58.       arguments: [head, tail]
  59.     }
  60.   }
  61.   / LogicalCompareExpression
  62.  
  63. LogicalCompareExpression
  64.   = head:(ConcatinationExpression) __ op:(LogicalCompareOperator) __ tail:LogicalCompareExpression
  65.   {
  66.     var name;
  67.     switch(op) {
  68.       case "<":
  69.         name = "lessThan"
  70.         break;
  71.       case "<=":
  72.         name = "lessThanOrEqual"
  73.         break;
  74.       case ">":
  75.         name = "greaterThan"
  76.         break;
  77.       case ">=":
  78.         name = "greaterThanOrEqual"
  79.         break;
  80.       case "==":
  81.       case "=":
  82.         name = "equal"
  83.         break;
  84.       case "!=":
  85.       case "<>":
  86.         name = "unequal"
  87.         break;
  88.       default:
  89.     }
  90.  
  91.     return {
  92.       type: "callExpression",
  93.       id: name,
  94.       arguments: [head, tail]
  95.     }
  96.   }
  97.   / ConcatinationExpression
  98.  
  99. ConcatinationExpression
  100.   = head:(AdditiveExpression) __ op:(ConcatinationOperator) __ tail:ConcatinationExpression
  101.   {
  102.     return {
  103.       type: "callExpression",
  104.       id: "add",
  105.       arguments: [head, tail]
  106.     }
  107.   }
  108.   / AdditiveExpression
  109.  
  110. AdditiveExpression
  111.   = head:MultiplicativeExpression __ op:("+" / "-" ) __ tail:AdditiveExpression
  112.   {
  113.     return {
  114.       type: "callExpression",
  115.       id: op == "+" ? "add" : "negate",
  116.       arguments: [head, tail]
  117.     }
  118.   }
  119.   / MultiplicativeExpression
  120.  
  121. MultiplicativeExpression
  122.   = head:ExponentiateExpression __ op:("*" / "/" ) __ tail:MultiplicativeExpression
  123.   {
  124.     return {
  125.       type: "callExpression",
  126.       id: op == "*" ? "multiply" : "invert",
  127.       arguments: [head, tail]
  128.     }
  129.   }
  130.   / ExponentiateExpression
  131.  
  132. ExponentiateExpression
  133.   = head:(LeftHandSideExpression) __ "^" __ tail:ExponentiateExpression
  134.   {
  135.     return {
  136.       type: "callExpression",
  137.       id: "exponentiate",
  138.       arguments: [head, tail]
  139.     }
  140.   }
  141.   / LeftHandSideExpression
  142.  
  143. LogicalCompareOperator
  144.   = "<="
  145.   / ">="
  146.   / "<>"
  147.   / "<"
  148.   / ">"
  149.   / "=="
  150.   / "="
  151.   / "!="
  152.  
  153. ConcatinationOperator
  154.   = "&"
  155.  
  156. UnaryOperator
  157.   = "!"
  158.  
  159. Arguments
  160.   = "(" __ args:(ArgumentList __)? ")" {
  161.       return args;
  162.     }
  163.  
  164. ArgumentList
  165.   = args:(
  166.     arg:PrimaryExpression __ ","? __ {
  167.       return arg;
  168.     }
  169.   )+ {
  170.     return args;
  171.   }
  172.  
  173. FunctionIdentifier
  174.   = id:[A-Za-z]+ { return id.join("").toLowerCase(); }
  175.  
  176. Identifier
  177.   = !ReservedKeyword name:IdentifierName { return name; }
  178.  
  179. IdentifierName
  180.   = IdentifierExpression
  181.  
  182. IdentifierExpression
  183.   = head:IdentifierStart tail:IdentifierPart* {
  184.       return {
  185.         type: "identifier",
  186.         name: head + tail.join("")
  187.       };
  188.     }
  189. LogicalOperatorOr
  190.   = "||"
  191. LogicalOperatorAnd
  192.   = "&&"
  193.  
  194. IdentifierStart
  195.   = "_"
  196.   / [$A-Za-z\.]
  197.  
  198. IdentifierPart
  199.   = IdentifierStart
  200.   / DecimalDigit
  201.  
  202. Literal
  203.   = StringLiteral
  204.   / NumericLiteral
  205.   / BooleanLiteral
  206.   / NullLiteral
  207.  
  208. StringLiteral
  209.   = chars:Character {
  210.       return {
  211.         type: "literal",
  212.         value: chars,
  213.         dataType: "text",
  214.         options: {
  215.           length: chars.length
  216.         }
  217.       };
  218.     }
  219.  
  220. NumericLiteral
  221.   = DecimalLiteral
  222.  
  223. DecimalLiteral
  224.   = sign:(__ "+" / "-" __)? DecimalIntegerLiteral "." DecimalDigit* {
  225.       var splitted = text().replace(/[\+\-]/g, "").split(".")
  226.       return {
  227.         type: "literal",
  228.         value: parseFloat(text()),
  229.         dataType: "number",
  230.         options: {
  231.           length: splitted[0].length,
  232.           scale: splitted[1].length,
  233.         }
  234.       };
  235.     }
  236.   / sign:(__ "+" / "-" __)? DecimalIntegerLiteral {
  237.       return {
  238.         type: "literal",
  239.         value: parseInt(text()),
  240.         dataType: "number",
  241.         options: {
  242.           length: text().replace(/[\+\-]/g, "").length,
  243.           scale: 0,
  244.         }
  245.       };
  246.   }
  247.  
  248. DecimalIntegerLiteral
  249.   = "0"
  250.   / NonZeroDigit DecimalDigit*
  251.  
  252. DecimalDigit
  253.   = [0-9]
  254.  
  255. NonZeroDigit
  256.   = [1-9]
  257.  
  258. BooleanLiteral
  259.   = "TRUE"i !(IdentifierStart) {
  260.     return {
  261.       type: "literal",
  262.       value: true,
  263.       dataType: "checkbox",
  264.       options: {}
  265.     }
  266.   }
  267.   / "FALSE"i !(IdentifierStart) {
  268.     return {
  269.       type: "literal",
  270.       value: false,
  271.       dataType: "checkbox",
  272.       options: {}
  273.     }
  274.   }
  275.  
  276. NullLiteral
  277.   = "NULL"i !(IdentifierStart) {
  278.     return {
  279.       type: "literal",
  280.       value: null,
  281.       dataType: "null",
  282.       options: {}
  283.     }
  284.   }
  285.  
  286. Character
  287.   = DoubleQuote chars:DoubleStringCharacter* DoubleQuote { return chars.join(''); }
  288.   / SingleQuote chars:SingleStringCharacter* SingleQuote { return chars.join(''); }
  289.  
  290. DoubleStringCharacter
  291.   = !(DoubleQuote / Backslash) char:. { return char; }
  292.   / Backslash sequence:EscapeSequence { return sequence; }
  293.  
  294. SingleStringCharacter
  295.   = !(SingleQuote / Backslash) char:. { return char; }
  296.   / Backslash sequence:EscapeSequence { return sequence; }
  297.  
  298. EscapeSequence
  299.   = SingleQuote
  300.   / DoubleQuote
  301.   / Backslash
  302.   / "b"  { return "\b";   }
  303.   / "f"  { return "\f";   }
  304.   / "n"  { return "\n";   }
  305.   / "r"  { return "\r";   }
  306.   / "t"  { return "\t";   }
  307.   / "v"  { return "\x0B"; }
  308.  
  309. SingleQuote
  310.   = '\''
  311.  
  312. DoubleQuote
  313.   = '"'
  314.  
  315. Backslash
  316.   = '\\'
  317.  
  318. LineTerminator
  319.   = [\n\r\u2028\u2029]
  320.  
  321. ReservedKeyword
  322.   = BooleanLiteral / NullLiteral
  323.  
  324. __
  325.   = (WhiteSpace)*
  326.  
  327. WhiteSpace
  328.   = "\t"
  329.   / "\v"
  330.   / "\f"
  331.   / " "
  332.   / "\u00A0"
  333.   / "\uFEFF"
  334.   / "\r"
  335.   / "\n"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement