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.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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. });