A work-in-progress SQL parser written in TypeScript
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

parser.ts 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. import * as AST from "./ast";
  2. import Error, { isError } from "./error";
  3. import Token, { TokenKind } from "./token";
  4. export default class Parser {
  5. public tokens: Token[];
  6. public position: number;
  7. constructor(tokens: Token[]) {
  8. this.tokens = tokens;
  9. this.position = 0;
  10. }
  11. public parse(): AST.Statement[] | Error {
  12. const tree: AST.Statement[] = [];
  13. while (!this.atEnd()) {
  14. const stmt = this.statement();
  15. if (isError(stmt)) {
  16. return stmt;
  17. } else {
  18. tree.push(stmt);
  19. }
  20. }
  21. return tree;
  22. }
  23. private statement(): AST.Statement | Error {
  24. const token = this.currentToken();
  25. switch (token.kind) {
  26. case TokenKind.SELECT:
  27. return this.select();
  28. }
  29. return new Error(`Unexpected token ${token.kind}`, token.line);
  30. }
  31. private select(): AST.Statement | Error {
  32. this.eat(TokenKind.SELECT);
  33. const args = this.args();
  34. if (isError(args)) {
  35. return args;
  36. }
  37. const from = this.match(TokenKind.FROM) ? this.from() : null;
  38. if (isError(from)) {
  39. return from;
  40. }
  41. const where = this.match(TokenKind.WHERE) ? this.where() : null;
  42. if (isError(where)) {
  43. return where;
  44. }
  45. return new AST.SelectStatement({
  46. arguments: args,
  47. from,
  48. where,
  49. });
  50. }
  51. private args(): AST.Secondary[] | Error {
  52. const args = [];
  53. while (true) {
  54. const arg = this.alias();
  55. if (isError(arg)) {
  56. return arg;
  57. }
  58. args.push(arg);
  59. if (this.match(TokenKind.COMMA)) {
  60. this.eat(TokenKind.COMMA);
  61. } else {
  62. break;
  63. }
  64. }
  65. return args;
  66. }
  67. private from(): AST.Secondary | Error {
  68. this.eat(TokenKind.FROM);
  69. return this.alias(this.identifier);
  70. }
  71. private where(): AST.Expr | Error {
  72. this.eat(TokenKind.WHERE);
  73. return this.expr();
  74. }
  75. private expr(): AST.Expr | Error {
  76. return this.equality();
  77. }
  78. private equality(): AST.Expr | Error {
  79. const left = this.addition();
  80. if (isError(left)) {
  81. return left;
  82. }
  83. if (this.match(TokenKind.EQUALS)) {
  84. this.eat(TokenKind.EQUALS);
  85. const right = this.addition();
  86. if (isError(right)) {
  87. return right;
  88. }
  89. return new AST.Binary({
  90. left,
  91. right,
  92. type: AST.BinaryExpressionTypes.EQUALITY,
  93. });
  94. }
  95. return left;
  96. }
  97. private addition(): AST.Expr | Error {
  98. const left = this.multiplication();
  99. if (isError(left)) {
  100. return left;
  101. }
  102. if (this.match(TokenKind.PLUS)) {
  103. this.eat(TokenKind.PLUS);
  104. const right = this.multiplication();
  105. if (isError(right)) {
  106. return right;
  107. }
  108. return new AST.Binary({
  109. left,
  110. right,
  111. type: AST.BinaryExpressionTypes.ADDITION,
  112. });
  113. }
  114. if (this.match(TokenKind.MINUS)) {
  115. this.eat(TokenKind.MINUS);
  116. const right = this.multiplication();
  117. if (isError(right)) {
  118. return right;
  119. }
  120. return new AST.Binary({
  121. left,
  122. right,
  123. type: AST.BinaryExpressionTypes.SUBTRACTION,
  124. });
  125. }
  126. return left;
  127. }
  128. private multiplication(): AST.Expr | Error {
  129. const left = this.primary();
  130. if (isError(left)) {
  131. return left;
  132. }
  133. if (this.match(TokenKind.STAR)) {
  134. this.eat(TokenKind.STAR);
  135. const right = this.primary();
  136. if (isError(right)) {
  137. return right;
  138. }
  139. return new AST.Binary({
  140. left,
  141. right,
  142. type: AST.BinaryExpressionTypes.MULTIPLICATION,
  143. });
  144. }
  145. if (this.match(TokenKind.SLASH)) {
  146. this.eat(TokenKind.SLASH);
  147. const right = this.primary();
  148. if (isError(right)) {
  149. return right;
  150. }
  151. return new AST.Binary({
  152. left,
  153. right,
  154. type: AST.BinaryExpressionTypes.DIVISION,
  155. });
  156. }
  157. return left;
  158. }
  159. private alias(fn: () => AST.Primary | Error = this.primary): AST.Secondary | Error {
  160. const primary = fn.bind(this)();
  161. if (isError(primary)) {
  162. return primary;
  163. }
  164. if (this.match(TokenKind.AS)) {
  165. this.eat(TokenKind.AS);
  166. const name = this.identifier();
  167. if (isError(name)) {
  168. return name;
  169. }
  170. return new AST.Alias(primary, name);
  171. }
  172. return primary;
  173. }
  174. private primary(): AST.Primary | Error {
  175. const token = this.currentToken();
  176. switch (token.kind) {
  177. case TokenKind.NUMBER:
  178. return this.number();
  179. case TokenKind.IDENTIFIER:
  180. return this.identifier();
  181. }
  182. return new Error(
  183. `Unexpected token ${token.kind} ${token.value}`,
  184. token.line,
  185. );
  186. }
  187. private identifier(): AST.Identifier | Error {
  188. const identifier = this.eat(TokenKind.IDENTIFIER);
  189. if (isError(identifier)) {
  190. return identifier;
  191. }
  192. return new AST.Identifier(identifier.value || "");
  193. }
  194. private number(): AST.Number | Error {
  195. const n = this.eat(TokenKind.NUMBER);
  196. if (isError(n)) {
  197. return n;
  198. }
  199. return new AST.Number(parseFloat(n.value || ""));
  200. }
  201. private eat(kind: TokenKind) {
  202. const token = this.currentToken();
  203. if (token.kind === kind) {
  204. this.advance();
  205. return token;
  206. }
  207. const repr = `{ kind: ${token.kind}${token.value ? `, value: ${token.value} }` : ""}`;
  208. return new Error(`Unexpected token: ${repr}`, token.line);
  209. }
  210. private match(kind: TokenKind): boolean {
  211. return this.currentToken().kind === kind;
  212. }
  213. private currentToken(): Token {
  214. return this.tokens[this.position];
  215. }
  216. private advance() {
  217. this.position += 1;
  218. }
  219. private atEnd(): boolean {
  220. return this.match(TokenKind.EOF);
  221. }
  222. }