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 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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. if (this.match(TokenKind.BACKTICK)) {
  70. return this.alias(this.backtick);
  71. }
  72. return this.alias(this.identifier);
  73. }
  74. private where(): AST.Expr | Error {
  75. this.eat(TokenKind.WHERE);
  76. return this.expr();
  77. }
  78. private expr(): AST.Expr | Error {
  79. return this.or();
  80. }
  81. private or(): AST.Expr | Error {
  82. let left = this.and();
  83. if (isError(left)) {
  84. return left;
  85. }
  86. while (this.match(TokenKind.OR)) {
  87. this.eat(TokenKind.OR);
  88. const right = this.and();
  89. if (isError(right)) {
  90. return right;
  91. }
  92. left = new AST.Binary({
  93. left,
  94. right,
  95. type: AST.BinaryExpressionTypes.OR,
  96. });
  97. }
  98. return left;
  99. }
  100. private and(): AST.Expr | Error {
  101. let left = this.equality();
  102. if (isError(left)) {
  103. return left;
  104. }
  105. while (this.match(TokenKind.AND)) {
  106. this.eat(TokenKind.AND);
  107. const right = this.equality();
  108. if (isError(right)) {
  109. return right;
  110. }
  111. left = new AST.Binary({
  112. left,
  113. right,
  114. type: AST.BinaryExpressionTypes.AND,
  115. });
  116. }
  117. return left;
  118. }
  119. private equality(): AST.Expr | Error {
  120. const left = this.addition();
  121. if (isError(left)) {
  122. return left;
  123. }
  124. if (this.match(TokenKind.EQUALS)) {
  125. this.eat(TokenKind.EQUALS);
  126. const right = this.addition();
  127. if (isError(right)) {
  128. return right;
  129. }
  130. return new AST.Binary({
  131. left,
  132. right,
  133. type: AST.BinaryExpressionTypes.EQUALITY,
  134. });
  135. }
  136. return left;
  137. }
  138. private addition(): AST.Expr | Error {
  139. const left = this.multiplication();
  140. if (isError(left)) {
  141. return left;
  142. }
  143. if (this.match(TokenKind.PLUS)) {
  144. this.eat(TokenKind.PLUS);
  145. const right = this.multiplication();
  146. if (isError(right)) {
  147. return right;
  148. }
  149. return new AST.Binary({
  150. left,
  151. right,
  152. type: AST.BinaryExpressionTypes.ADDITION,
  153. });
  154. }
  155. if (this.match(TokenKind.MINUS)) {
  156. this.eat(TokenKind.MINUS);
  157. const right = this.multiplication();
  158. if (isError(right)) {
  159. return right;
  160. }
  161. return new AST.Binary({
  162. left,
  163. right,
  164. type: AST.BinaryExpressionTypes.SUBTRACTION,
  165. });
  166. }
  167. return left;
  168. }
  169. private multiplication(): AST.Expr | Error {
  170. const left = this.primary();
  171. if (isError(left)) {
  172. return left;
  173. }
  174. if (this.match(TokenKind.STAR)) {
  175. this.eat(TokenKind.STAR);
  176. const right = this.primary();
  177. if (isError(right)) {
  178. return right;
  179. }
  180. return new AST.Binary({
  181. left,
  182. right,
  183. type: AST.BinaryExpressionTypes.MULTIPLICATION,
  184. });
  185. }
  186. if (this.match(TokenKind.SLASH)) {
  187. this.eat(TokenKind.SLASH);
  188. const right = this.primary();
  189. if (isError(right)) {
  190. return right;
  191. }
  192. return new AST.Binary({
  193. left,
  194. right,
  195. type: AST.BinaryExpressionTypes.DIVISION,
  196. });
  197. }
  198. return left;
  199. }
  200. private alias(fn: () => AST.Primary | Error = this.primary): AST.Secondary | Error {
  201. const primary = fn.bind(this)();
  202. if (isError(primary)) {
  203. return primary;
  204. }
  205. if (this.match(TokenKind.AS)) {
  206. this.eat(TokenKind.AS);
  207. const name = this.match(TokenKind.BACKTICK) ? this.backtick() : this.identifier();
  208. if (isError(name)) {
  209. return name;
  210. }
  211. return new AST.Alias(primary, name);
  212. }
  213. return primary;
  214. }
  215. private primary(): AST.Primary | Error {
  216. const token = this.currentToken();
  217. switch (token.kind) {
  218. case TokenKind.NUMBER:
  219. return this.number();
  220. case TokenKind.IDENTIFIER:
  221. return this.identifier();
  222. case TokenKind.BACKTICK:
  223. return this.backtick();
  224. }
  225. return new Error(
  226. `Unexpected token ${token.kind} ${token.value}`,
  227. token.line,
  228. );
  229. }
  230. private backtick(): AST.Backtick | Error {
  231. this.eat(TokenKind.BACKTICK);
  232. const identifier = this.identifier();
  233. if (isError(identifier)) {
  234. return identifier;
  235. }
  236. const closeTick = this.eat(TokenKind.BACKTICK);
  237. if (isError(closeTick)) {
  238. return closeTick;
  239. }
  240. return new AST.Backtick(identifier);
  241. }
  242. private identifier(): AST.Identifier | Error {
  243. const identifier = this.eat(TokenKind.IDENTIFIER);
  244. if (isError(identifier)) {
  245. return identifier;
  246. }
  247. return new AST.Identifier(identifier.value || "");
  248. }
  249. private number(): AST.Number | Error {
  250. const n = this.eat(TokenKind.NUMBER);
  251. if (isError(n)) {
  252. return n;
  253. }
  254. return new AST.Number(parseFloat(n.value || ""));
  255. }
  256. private eat(kind: TokenKind) {
  257. const token = this.currentToken();
  258. if (token.kind === kind) {
  259. this.advance();
  260. return token;
  261. }
  262. const repr = `{ kind: ${token.kind}${token.value ? `, value: ${token.value} }` : " }"}`;
  263. return new Error(`Unexpected token: ${repr}`, token.line);
  264. }
  265. private match(kind: TokenKind): boolean {
  266. return this.currentToken().kind === kind;
  267. }
  268. private currentToken(): Token {
  269. return this.tokens[this.position];
  270. }
  271. private advance() {
  272. this.position += 1;
  273. }
  274. private atEnd(): boolean {
  275. return this.match(TokenKind.EOF);
  276. }
  277. }