A work-in-progress SQL parser written in TypeScript
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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.SelectStatement({
  24. arguments: [new AST.Number(5)],
  25. }),
  26. ]);
  27. });
  28. it("should parse a multiple number selection", () => {
  29. const tree = parse("select 5, 6");
  30. expect(tree).to.deep.equal([
  31. new AST.SelectStatement({
  32. arguments: [
  33. new AST.Number(5),
  34. new AST.Number(6),
  35. ],
  36. }),
  37. ]);
  38. });
  39. it("should parse a selection of identifiers", () => {
  40. const tree = parse("select a, b");
  41. expect(tree).to.deep.equal([
  42. new AST.SelectStatement({
  43. arguments: [
  44. new AST.Identifier("a"),
  45. new AST.Identifier("b"),
  46. ],
  47. }),
  48. ]);
  49. });
  50. it("should parse a selection of identifiers and numbers", () => {
  51. const tree = parse("select a, 5");
  52. expect(tree).to.deep.equal([
  53. new AST.SelectStatement({
  54. arguments: [
  55. new AST.Identifier("a"),
  56. new AST.Number(5),
  57. ],
  58. }),
  59. ]);
  60. });
  61. it("should parse the from of a selection", () => {
  62. const tree = parse("select a from t");
  63. expect(tree).to.deep.equal([
  64. new AST.SelectStatement({
  65. arguments: [
  66. new AST.Identifier("a"),
  67. ],
  68. from: new AST.Identifier("t"),
  69. }),
  70. ]);
  71. });
  72. });