formula.pegjs 882 B

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