Browse Source

Scan AND and OR

master
Dylan Baker 4 years ago
parent
commit
b2fe682f92
3 changed files with 14 additions and 2 deletions
  1. 6
    0
      src/lexer.ts
  2. 2
    0
      src/token.ts
  3. 6
    2
      test/lexer.test.ts

+ 6
- 0
src/lexer.ts View File

@@ -43,6 +43,12 @@ export default class Lexer {
43 43
     } else if (source.match(/^as/i)) {
44 44
       this.advance(2);
45 45
       return new Token(TokenKind.AS, null, this.line);
46
+    } else if (source.match(/^and/i)) {
47
+      this.advance(3);
48
+      return new Token(TokenKind.AND, null, this.line);
49
+    } else if (source.match(/^or/i)) {
50
+      this.advance(2);
51
+      return new Token(TokenKind.OR, null, this.line);
46 52
     } else if (source.match(/^\+/)) {
47 53
       this.advance();
48 54
       return new Token(TokenKind.PLUS, null, this.line);

+ 2
- 0
src/token.ts View File

@@ -1,4 +1,5 @@
1 1
 export enum TokenKind {
2
+  AND = "AND",
2 3
   AS = "AS",
3 4
   BACKTICK = "BACKTICK",
4 5
   COMMA = "COMMA",
@@ -9,6 +10,7 @@ export enum TokenKind {
9 10
   IDENTIFIER = "IDENTIFIER",
10 11
   MINUS = "MINUS",
11 12
   NUMBER = "NUMBER",
13
+  OR = "OR",
12 14
   PLUS = "PLUS",
13 15
   SELECT = "SELECT",
14 16
   SEMICOLON = "SEMICOLON",

+ 6
- 2
test/lexer.test.ts View File

@@ -11,23 +11,27 @@ const scan = (source: string): Token[] | Error => {
11 11
 
12 12
 describe("Lexer", () => {
13 13
   it("scans uppercase keywords", () => {
14
-    const tokens = scan("SELECT FROM WHERE AS");
14
+    const tokens = scan("SELECT FROM WHERE AS AND OR");
15 15
     expect(tokens).to.deep.equal([
16 16
       new Token(TokenKind.SELECT, null, 1),
17 17
       new Token(TokenKind.FROM, null, 1),
18 18
       new Token(TokenKind.WHERE, null, 1),
19 19
       new Token(TokenKind.AS, null, 1),
20
+      new Token(TokenKind.AND, null, 1),
21
+      new Token(TokenKind.OR, null, 1),
20 22
       new Token(TokenKind.EOF, null, 1),
21 23
     ]);
22 24
   });
23 25
 
24 26
   it("scans lowercase keywords", () => {
25
-    const tokens = scan("select from where as");
27
+    const tokens = scan("select from where as and or");
26 28
     expect(tokens).to.deep.equal([
27 29
       new Token(TokenKind.SELECT, null, 1),
28 30
       new Token(TokenKind.FROM, null, 1),
29 31
       new Token(TokenKind.WHERE, null, 1),
30 32
       new Token(TokenKind.AS, null, 1),
33
+      new Token(TokenKind.AND, null, 1),
34
+      new Token(TokenKind.OR, null, 1),
31 35
       new Token(TokenKind.EOF, null, 1),
32 36
     ]);
33 37
   });

Loading…
Cancel
Save