Browse Source

Disallow numbers as from targets

master
Dylan Baker 4 years ago
parent
commit
1bb4549390
2 changed files with 13 additions and 6 deletions
  1. 8
    6
      src/parser.ts
  2. 5
    0
      test/parser.test.ts

+ 8
- 6
src/parser.ts View File

@@ -44,8 +44,8 @@ export default class Parser {
44 44
       return args;
45 45
     }
46 46
 
47
-    const from =
48
-      this.currentToken().kind === TokenKind.FROM ? this.from() : null;
47
+    const currentToken = this.currentToken();
48
+    const from = currentToken.kind === TokenKind.FROM ? this.from() : null;
49 49
     if (isError(from)) {
50 50
       return from;
51 51
     }
@@ -78,11 +78,11 @@ export default class Parser {
78 78
 
79 79
   private from(): AST.Secondary | Error {
80 80
     this.eat(TokenKind.FROM);
81
-    return this.alias();
81
+    return this.alias(this.identifier);
82 82
   }
83 83
 
84
-  private alias(): AST.Secondary | Error {
85
-    const primary = this.primary();
84
+  private alias(fn: () => AST.Primary | Error = this.primary): AST.Secondary | Error {
85
+    const primary = fn.bind(this)();
86 86
     if (isError(primary)) {
87 87
       return primary;
88 88
     }
@@ -137,7 +137,9 @@ export default class Parser {
137 137
       return token;
138 138
     }
139 139
 
140
-    return new Error(`Unexpected token ${token}`, token.line);
140
+    const repr = `{ kind: ${token.kind}${token.value ? `, value: ${token.value} }` : ""}`;
141
+
142
+    return new Error(`Unexpected token: ${repr}`, token.line);
141 143
   }
142 144
 
143 145
   private currentToken(): Token {

+ 5
- 0
test/parser.test.ts View File

@@ -86,4 +86,9 @@ describe("Parser", () => {
86 86
       }),
87 87
     ]);
88 88
   });
89
+
90
+  it("should not allow a number as a from target", () => {
91
+    const fn = () => parse("select 1 from 2");
92
+    expect(fn).to.throw("Unexpected token: { kind: NUMBER, value: 2 }");
93
+  });
89 94
 });

Loading…
Cancel
Save