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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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 =
  38. this.currentToken().kind === TokenKind.FROM ? this.from() : null;
  39. if (isError(from)) {
  40. return from;
  41. }
  42. return new AST.SelectStatement({
  43. arguments: args,
  44. from,
  45. });
  46. }
  47. private args(): AST.Secondary[] | Error {
  48. const args = [];
  49. while (true) {
  50. const arg = this.alias();
  51. if (isError(arg)) {
  52. return arg;
  53. }
  54. args.push(arg);
  55. if (this.currentToken().kind === TokenKind.COMMA) {
  56. this.eat(TokenKind.COMMA);
  57. } else {
  58. break;
  59. }
  60. }
  61. return args;
  62. }
  63. private from(): AST.Secondary | Error {
  64. this.eat(TokenKind.FROM);
  65. return this.alias();
  66. }
  67. private alias(): AST.Secondary | Error {
  68. const primary = this.primary();
  69. if (isError(primary)) {
  70. return primary;
  71. }
  72. if (this.currentToken().kind === TokenKind.AS) {
  73. this.eat(TokenKind.AS);
  74. const name = this.identifier();
  75. if (isError(name)) {
  76. return name;
  77. }
  78. return new AST.Alias(primary, name);
  79. }
  80. return primary;
  81. }
  82. private primary(): AST.Primary | Error {
  83. const token = this.currentToken();
  84. switch (token.kind) {
  85. case TokenKind.NUMBER:
  86. return this.number();
  87. case TokenKind.IDENTIFIER:
  88. return this.identifier();
  89. }
  90. return new Error(
  91. `Unexpected token ${token.kind} ${token.value}`,
  92. token.line,
  93. );
  94. }
  95. private identifier(): AST.Identifier | Error {
  96. const identifier = this.eat(TokenKind.IDENTIFIER);
  97. if (isError(identifier)) {
  98. return identifier;
  99. }
  100. return new AST.Identifier(identifier.value || "");
  101. }
  102. private number(): AST.Number | Error {
  103. const n = this.eat(TokenKind.NUMBER);
  104. if (isError(n)) {
  105. return n;
  106. }
  107. return new AST.Number(parseFloat(n.value || ""));
  108. }
  109. private eat(kind: TokenKind) {
  110. const token = this.currentToken();
  111. if (token.kind === kind) {
  112. this.advance();
  113. return token;
  114. }
  115. return new Error(`Unexpected token ${token}`, token.line);
  116. }
  117. private currentToken(): Token {
  118. return this.tokens[this.position];
  119. }
  120. private advance() {
  121. this.position += 1;
  122. }
  123. private atEnd(): boolean {
  124. return this.currentToken().kind === TokenKind.EOF;
  125. }
  126. }