浏览代码

Scan AND and OR

master
Dylan Baker 5 年前
父节点
当前提交
b2fe682f92
共有 3 个文件被更改,包括 14 次插入2 次删除
  1. 6
    0
      src/lexer.ts
  2. 2
    0
      src/token.ts
  3. 6
    2
      test/lexer.test.ts

+ 6
- 0
src/lexer.ts 查看文件

43
     } else if (source.match(/^as/i)) {
43
     } else if (source.match(/^as/i)) {
44
       this.advance(2);
44
       this.advance(2);
45
       return new Token(TokenKind.AS, null, this.line);
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
     } else if (source.match(/^\+/)) {
52
     } else if (source.match(/^\+/)) {
47
       this.advance();
53
       this.advance();
48
       return new Token(TokenKind.PLUS, null, this.line);
54
       return new Token(TokenKind.PLUS, null, this.line);

+ 2
- 0
src/token.ts 查看文件

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

+ 6
- 2
test/lexer.test.ts 查看文件

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

正在加载...
取消
保存