formulaFormatting.pegjs 1.2 KB

123456789101112131415161718192021
  1. expression = token*
  2. token = number / variable / _ / open_paren / close_paren / plus / minus / multiply / divide / generic_token
  3. number = [0-9]+('.'[0-9]+)? { return { type: "number", content: text()}}
  4. variable = [a-z]+ { return { type: "variable", content: text()}}
  5. _ = " "+ { return { type: "whitespace", content: text()}}
  6. open_paren = "(" { return { type: "openParen", content: text()}}
  7. close_paren = ")" { return { type: "closeParen", content: text()}}
  8. plus = "+" { return { type: "plus", content: text()}}
  9. minus = "-" { return { type: "minus", content: text()}}
  10. multiply = "*" { return { type: "multiply", content: text()}}
  11. divide = "/" { return { type: "divide", content: text()}}
  12. // \u00A0-\uFFFF is the entire Unicode BMP _including_ surrogate pairs and
  13. // unassigned code points, which aren't parse-able naively. A more precise
  14. // approach would be to define all valid Unicode ranges exactly but for
  15. // permissive parsing we don't mind the lack of precision.
  16. generic_token
  17. = [a-zA-Z0-9\u00A0-\uFFFF"'`_\-.=><:,*;!\[\]?$%|/\\@#&~^+{}]+ { return { type: 'generic', content: text() } }