formula.pegjs 898 B

123456789101112131415161718192021
  1. expression = term (_ expr_op _ term)*
  2. expr_op = plus / minus
  3. term = unary (_ term_op _ unary)*
  4. term_op = multiply / divide
  5. unary = minus? coefficient
  6. coefficient = number / variable / open_paren _ expression _ close_paren
  7. number = [0-9]+('.'[0-9]+)? { return { type: "number", content: text()}}
  8. variable = [a-z]+ { return { type: "variable", content: text()}}
  9. _ = " "* { return { type: "whitespace", content: text()}}
  10. open_paren = "(" { return { type: "openParen", content: text()}}
  11. close_paren = ")" { return { type: "closeParen", content: text()}}
  12. plus = "+" { return { type: "plus", content: text()}}
  13. minus = "-" { return { type: "minus", content: text()}}
  14. multiply = "*" { return { type: "multiply", content: text()}}
  15. divide = "/" { return { type: "divide", content: text()}}