sqlish.pegjs 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. Expression
  2. = tokens:Token*
  3. Token
  4. = LeftParenthesis / RightParenthesis / Whitespace / Keyword / Parameter / CollapsedColumns / GenericToken
  5. LeftParenthesis
  6. = "(" { return { type: 'LeftParenthesis', content: '(' } }
  7. RightParenthesis
  8. = ")" { return { type: 'RightParenthesis', content: ')' } }
  9. Keyword
  10. = Keyword:("ADD"i / "ALL"i / "ALTER"i / "AND"i / "ANY"i / "AS"i / "ASC"i / "BACKUP"i / "BETWEEN"i / "BY"i / "CASE"i / "CHECK"i / "COLUMN"i / "CONSTRAINT"i / "COUNT"i / "CREATE"i / "DATABASE"i / "DEFAULT"i / "DELETE"i / "DESC"i / "DISTINCT"i / "DROP"i / "EXEC"i / "EXISTS"i / "FOREIGN"i / "FROM"i / "FROM"i / "FULL"i / "GROUP"i / "HAVING"i / "INNER"i / "INSERT"i / "JOIN"i / "KEY"i / "LEFT"i / "LIMIT"i / "OFFSET"i / "ON"i / "ORDER"i / "OUTER"i / "RETURNING"i / "RIGHT"i / "SELECT"i / "SELECT"i / "SET"i / "TABLE"i / "UPDATE"i / "VALUES"i / "WHERE"i / JoinKeyword) & (Whitespace / LeftParenthesis / RightParenthesis) {
  11. return { type: 'Keyword', content: Keyword.toUpperCase() }
  12. }
  13. JoinKeyword
  14. = JoinDirection:JoinDirection? Whitespace? JoinType:JoinType? Whitespace "JOIN"i {
  15. return (JoinDirection || '') + (JoinDirection ? " " : '') + JoinType + " " + "JOIN"
  16. }
  17. JoinDirection
  18. = "LEFT"i / "RIGHT"i / "FULL"i
  19. JoinType
  20. = "OUTER"i / "INNER"i
  21. Parameter
  22. = Parameter:("%s" / ":c" [0-9]) { return { type: 'Parameter', content: Array.isArray(Parameter) ? Parameter.join('') : Parameter } }
  23. CollapsedColumns
  24. = ".." { return { type: 'CollapsedColumns', content: '..' } }
  25. Whitespace
  26. = Whitespace:[\n\t\r ]+ { return { type: 'Whitespace', content: Whitespace.join("") } }
  27. // \u00A0-\uFFFF is the entire Unicode BMP _including_ surrogate pairs and
  28. // unassigned code points, which aren't parse-able naively. A more precise
  29. // approach would be to define all valid Unicode ranges exactly but for
  30. // permissive parsing we don't mind the lack of precision.
  31. GenericToken
  32. = GenericToken:[a-zA-Z0-9\u00A0-\uFFFF"'`_\-.=><:,*;!\[\]?$%|/\\@#&~^+{}]+ { return { type: 'GenericToken', content: GenericToken.join('') } }