grammar.pegjs 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. {
  2. const {tc, term} = options;
  3. }
  4. term
  5. = maybeFactor:maybe_factor remainingAdds:remaining_adds {
  6. return tc.tokenTerm(maybeFactor, remainingAdds);
  7. }
  8. remaining_adds = add_sub*
  9. add_sub
  10. = operator:add_sub_operator rhs:maybe_factor {
  11. return tc.tokenOperation(operator, rhs);
  12. }
  13. maybe_factor
  14. = spaces term:(factor / primary) spaces {
  15. return term;
  16. }
  17. factor
  18. = primary:primary remaining:remaining_muls {
  19. return tc.tokenFactor(primary, remaining);
  20. }
  21. remaining_muls = mul_div+
  22. mul_div
  23. = operator:mul_div_operator rhs:primary {
  24. return tc.tokenOperation(operator, rhs);
  25. }
  26. add_sub_operator
  27. = spaces operator:(plus / minus) spaces {
  28. return operator;
  29. }
  30. mul_div_operator
  31. = spaces operator:(multiply / divide) spaces {
  32. return operator;
  33. }
  34. primary
  35. = spaces primary:(parens / numeric_value / function_value / field_value) spaces {
  36. return primary;
  37. }
  38. parens
  39. = open_paren term:term closed_paren {
  40. return term;
  41. }
  42. plus
  43. = "+" {
  44. return "plus";
  45. }
  46. minus
  47. = "-" {
  48. return "minus";
  49. }
  50. multiply
  51. = "*" {
  52. return "multiply";
  53. }
  54. divide
  55. = [/÷] {
  56. return "divide";
  57. }
  58. function_value "function"
  59. = function_name open_paren spaces function_args? spaces closed_paren {
  60. return tc.tokenFunction(text(), location());
  61. }
  62. numeric_value "number"
  63. = [+-]?[0-9]+ ("." [0-9]*)? {
  64. return text();
  65. }
  66. field_value "field"
  67. = [a-zA-Z_\.]+ {
  68. return tc.tokenField(text(), location());
  69. }
  70. function_args
  71. = aggregate_param (spaces comma spaces aggregate_param)*
  72. aggregate_param
  73. = quoted_aggregate_param / raw_aggregate_param
  74. raw_aggregate_param
  75. = param:[^()\t\n, \"]+
  76. quoted_aggregate_param
  77. = '"' param:('\\"' / [^\t\n\"])* '"'
  78. function_name = [a-zA-Z_0-9]+
  79. comma = ","
  80. open_paren = "("
  81. closed_paren = ")"
  82. spaces = " "*