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.test.ts 1.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /* tslint:disable:no-unused-expression */
  2. import { expect } from "chai";
  3. import * as AST from "../src/ast";
  4. import { isError } from "../src/error";
  5. import Lexer from "../src/lexer";
  6. import Parser from "../src/parser";
  7. const parse = (source: string): AST.Statement[] => {
  8. const tokens = new Lexer(source).scan();
  9. if (isError(tokens)) {
  10. throw tokens.message;
  11. } else {
  12. const tree = new Parser(tokens).parse();
  13. if (isError(tree)) {
  14. throw tree.message;
  15. }
  16. return tree;
  17. }
  18. };
  19. describe("Parser", () => {
  20. it("should parse a number selection", () => {
  21. const tree = parse("select 5");
  22. expect(tree).to.deep.equal([
  23. new AST.Statement({
  24. arguments: [new AST.Number(5)],
  25. type: AST.StatementType.SELECT,
  26. }),
  27. ]);
  28. });
  29. it("should parse a multiple number selection", () => {
  30. const tree = parse("select 5, 6");
  31. expect(tree).to.deep.equal([
  32. new AST.Statement({
  33. arguments: [
  34. new AST.Number(5),
  35. new AST.Number(6),
  36. ],
  37. type: AST.StatementType.SELECT,
  38. }),
  39. ]);
  40. });
  41. it("should parse a selection of identifiers", () => {
  42. const tree = parse("select a, b");
  43. expect(tree).to.deep.equal([
  44. new AST.Statement({
  45. arguments: [
  46. new AST.Identifier("a"),
  47. new AST.Identifier("b"),
  48. ],
  49. type: AST.StatementType.SELECT,
  50. }),
  51. ]);
  52. });
  53. it("should parse a selection of identifiers and numbers", () => {
  54. const tree = parse("select a, 5");
  55. expect(tree).to.deep.equal([
  56. new AST.Statement({
  57. arguments: [
  58. new AST.Identifier("a"),
  59. new AST.Number(5),
  60. ],
  61. type: AST.StatementType.SELECT,
  62. }),
  63. ]);
  64. });
  65. });