Browse Source

Initial commit/lexer

master
Dylan Baker 4 years ago
commit
e7684c4f70
10 changed files with 1539 additions and 0 deletions
  1. 2
    0
      .gitignore
  2. 5
    0
      .mocharc.json
  3. 1203
    0
      package-lock.json
  4. 23
    0
      package.json
  5. 13
    0
      src/error.ts
  6. 105
    0
      src/lexer.ts
  7. 26
    0
      src/token.ts
  8. 82
    0
      test/lexer.test.ts
  9. 62
    0
      tsconfig.json
  10. 18
    0
      tslint.json

+ 2
- 0
.gitignore View File

@@ -0,0 +1,2 @@
1
+node_modules
2
+src/index.ts

+ 5
- 0
.mocharc.json View File

@@ -0,0 +1,5 @@
1
+{
2
+  "extension": ["ts"],
3
+  "spec": "test/*.test.ts",
4
+  "require": "ts-node/register"
5
+}

+ 1203
- 0
package-lock.json
File diff suppressed because it is too large
View File


+ 23
- 0
package.json View File

@@ -0,0 +1,23 @@
1
+{
2
+  "name": "sql",
3
+  "version": "1.0.0",
4
+  "description": "",
5
+  "main": "index.js",
6
+  "scripts": {
7
+    "test": "mocha",
8
+    "format": "tslint --fix ./{src,test}/*.ts"
9
+  },
10
+  "keywords": [],
11
+  "author": "Dylan Baker <dylan@simulacrum.party>",
12
+  "license": "MIT",
13
+  "devDependencies": {
14
+    "@types/chai": "^4.1.7",
15
+    "@types/mocha": "^5.2.7",
16
+    "@types/node": "^12.6.9",
17
+    "chai": "^4.2.0",
18
+    "mocha": "^6.2.0",
19
+    "ts-node": "^8.3.0",
20
+    "tslint": "^5.18.0",
21
+    "typescript": "^3.5.3"
22
+  }
23
+}

+ 13
- 0
src/error.ts View File

@@ -0,0 +1,13 @@
1
+export default class Error {
2
+  public message: string;
3
+  public line: number;
4
+
5
+  constructor(message: string, line: number) {
6
+    this.message = message;
7
+    this.line = line;
8
+  }
9
+}
10
+
11
+export const isError = (obj: any): obj is Error => {
12
+  return obj && obj.message && obj.line;
13
+};

+ 105
- 0
src/lexer.ts View File

@@ -0,0 +1,105 @@
1
+import Error, { isError } from "./error";
2
+import Token, { TokenKind } from "./token";
3
+
4
+export default class Lexer {
5
+  public source: string;
6
+  public position: number;
7
+  public line: number;
8
+
9
+  constructor(source: string) {
10
+    this.source = source;
11
+    this.position = 0;
12
+    this.line = 1;
13
+  }
14
+  public scan(): Token[] | Error {
15
+    const tokens = [];
16
+
17
+    while (!this.atEnd()) {
18
+      const result = this.getToken();
19
+      if (isError(result)) {
20
+        return result;
21
+      } else if (result) {
22
+        tokens.push(result);
23
+      }
24
+    }
25
+
26
+    tokens.push(new Token(TokenKind.EOF, null, this.line));
27
+
28
+    return tokens;
29
+  }
30
+
31
+  private getToken(): Token | Error | null {
32
+    const source = this.source.slice(this.position);
33
+
34
+    if (source.match(/^select/i)) {
35
+      this.advance(6);
36
+      return new Token(TokenKind.SELECT, null, this.line);
37
+    } else if (source.match(/^where/i)) {
38
+      this.advance(5);
39
+      return new Token(TokenKind.WHERE, null, this.line);
40
+    } else if (source.match(/^from/i)) {
41
+      this.advance(4);
42
+      return new Token(TokenKind.FROM, null, this.line);
43
+    } else if (source.match(/^\*/)) {
44
+      this.advance();
45
+      return new Token(TokenKind.STAR, null, this.line);
46
+    } else if (source.match(/^=/)) {
47
+      this.advance();
48
+      return new Token(TokenKind.EQUALS, null, this.line);
49
+    } else if (source.match(/^,/)) {
50
+      this.advance();
51
+      return new Token(TokenKind.COMMA, null, this.line);
52
+    } else if (source.match(/^`/)) {
53
+      this.advance();
54
+      return new Token(TokenKind.BACKTICK, null, this.line);
55
+    } else if (source.match(/^\.([^0-9]|$)/)) {
56
+      this.advance();
57
+      return new Token(TokenKind.DOT, null, this.line);
58
+    } else if (source.match(/^;/)) {
59
+      this.advance();
60
+      return new Token(TokenKind.SEMICOLON, null, this.line);
61
+    } else if (source.match(/^[0-9]+(\.[0-9]+)?/)) {
62
+      const match = source.match(/^[0-9]+(\.[0-9]+)?/);
63
+      if (match) {
64
+        const numeric = match[0];
65
+        this.advance(numeric.length);
66
+        return new Token(TokenKind.NUMBER, numeric, this.line);
67
+      }
68
+    } else if (source.match(/^\.[0-9]+/)) {
69
+      const match = source.match(/^\.[0-9]+/);
70
+      if (match) {
71
+        const numeric = match[0];
72
+        this.advance(numeric.length);
73
+        return new Token(TokenKind.NUMBER, numeric, this.line);
74
+      }
75
+    } else if (source.match(/^[a-zA-Z_][a-zA-Z0-9_]*/)) {
76
+      const match = source.match(/^[a-zA-Z_][a-zA-Z0-9_]*/);
77
+      if (match) {
78
+        const identifier = match[0];
79
+        this.advance(identifier.length);
80
+        return new Token(TokenKind.IDENTIFIER, identifier, this.line);
81
+      }
82
+    } else if (source.match(/^\n/)) {
83
+      this.advance();
84
+      this.nextLine();
85
+      return null;
86
+    } else if (source.match(/^\s/)) {
87
+      this.advance();
88
+      return null;
89
+    }
90
+
91
+    return new Error(`Unrecognized character ${source[0]}`, this.line);
92
+  }
93
+
94
+  private advance(step: number = 1) {
95
+    this.position += step;
96
+  }
97
+
98
+  private nextLine() {
99
+    this.line += 1;
100
+  }
101
+
102
+  private atEnd(): boolean {
103
+    return this.position === this.source.length;
104
+  }
105
+}

+ 26
- 0
src/token.ts View File

@@ -0,0 +1,26 @@
1
+export enum TokenKind {
2
+  BACKTICK = "BACKTICK",
3
+  COMMA = "COMMA",
4
+  DOT = "DOT",
5
+  EOF = "EOF",
6
+  EQUALS = "EQUALS",
7
+  FROM = "FROM",
8
+  IDENTIFIER = "IDENTIFIER",
9
+  NUMBER = "NUMBER",
10
+  SELECT = "SELECT",
11
+  SEMICOLON = "SEMICOLON",
12
+  STAR = "STAR",
13
+  WHERE = "WHERE",
14
+}
15
+
16
+export default class Token {
17
+  public kind: TokenKind;
18
+  public value: string | null;
19
+  public line: number;
20
+
21
+  constructor(kind: TokenKind, value: string | null, line: number) {
22
+    this.kind = kind;
23
+    this.value = value;
24
+    this.line = line;
25
+  }
26
+}

+ 82
- 0
test/lexer.test.ts View File

@@ -0,0 +1,82 @@
1
+/* tslint:disable:no-unused-expression */
2
+import { expect } from "chai";
3
+
4
+import Error, { isError } from "../src/error";
5
+import Lexer from "../src/lexer";
6
+import Token, { TokenKind } from "../src/token";
7
+
8
+const scan = (source: string): Token[] | Error => {
9
+  return new Lexer(source).scan();
10
+};
11
+
12
+describe("Lexer", () => {
13
+  it("scans uppercase keywords", () => {
14
+    const tokens = scan("SELECT FROM WHERE");
15
+    expect(tokens).to.deep.equal([
16
+      new Token(TokenKind.SELECT, null, 1),
17
+      new Token(TokenKind.FROM, null, 1),
18
+      new Token(TokenKind.WHERE, null, 1),
19
+      new Token(TokenKind.EOF, null, 1),
20
+    ]);
21
+  });
22
+
23
+  it("scans lowercase keywords", () => {
24
+    const tokens = scan("select from where");
25
+    expect(tokens).to.deep.equal([
26
+      new Token(TokenKind.SELECT, null, 1),
27
+      new Token(TokenKind.FROM, null, 1),
28
+      new Token(TokenKind.WHERE, null, 1),
29
+      new Token(TokenKind.EOF, null, 1),
30
+    ]);
31
+  });
32
+
33
+  it("scans symbols", () => {
34
+    const tokens = scan("*=,`;.");
35
+    expect(tokens).to.deep.equal([
36
+      new Token(TokenKind.STAR, null, 1),
37
+      new Token(TokenKind.EQUALS, null, 1),
38
+      new Token(TokenKind.COMMA, null, 1),
39
+      new Token(TokenKind.BACKTICK, null, 1),
40
+      new Token(TokenKind.SEMICOLON, null, 1),
41
+      new Token(TokenKind.DOT, null, 1),
42
+      new Token(TokenKind.EOF, null, 1),
43
+    ]);
44
+  });
45
+
46
+  it("scans identifiers", () => {
47
+    const tokens = scan("ident ident_ifier Ident");
48
+    expect(tokens).to.deep.equal([
49
+      new Token(TokenKind.IDENTIFIER, "ident", 1),
50
+      new Token(TokenKind.IDENTIFIER, "ident_ifier", 1),
51
+      new Token(TokenKind.IDENTIFIER, "Ident", 1),
52
+      new Token(TokenKind.EOF, null, 1),
53
+    ]);
54
+  });
55
+
56
+  it("scans numbers", () => {
57
+    const tokens = scan("1 123.45 .123");
58
+    expect(tokens).to.deep.equal([
59
+      new Token(TokenKind.NUMBER, "1", 1),
60
+      new Token(TokenKind.NUMBER, "123.45", 1),
61
+      new Token(TokenKind.NUMBER, ".123", 1),
62
+      new Token(TokenKind.EOF, null, 1),
63
+    ]);
64
+  });
65
+
66
+  it("keeps track of line numbers", () => {
67
+    const tokens = scan("foo\nbar");
68
+    expect(tokens).to.deep.equal([
69
+      new Token(TokenKind.IDENTIFIER, "foo", 1),
70
+      new Token(TokenKind.IDENTIFIER, "bar", 2),
71
+      new Token(TokenKind.EOF, null, 2),
72
+    ]);
73
+  });
74
+
75
+  it("returns an error for an unrecognized character", () => {
76
+    const error = scan("~");
77
+    expect(isError(error)).to.be.ok;
78
+    if (isError(error)) {
79
+      expect(error.message).to.equal("Unrecognized character ~");
80
+    }
81
+  });
82
+});

+ 62
- 0
tsconfig.json View File

@@ -0,0 +1,62 @@
1
+{
2
+  "compilerOptions": {
3
+    /* Basic Options */
4
+    "target": "es5",                          /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019' or 'ESNEXT'. */
5
+    "module": "commonjs",                     /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */
6
+    // "lib": [],                             /* Specify library files to be included in the compilation. */
7
+    // "allowJs": true,                       /* Allow javascript files to be compiled. */
8
+    // "checkJs": true,                       /* Report errors in .js files. */
9
+    // "jsx": "preserve",                     /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */
10
+    // "declaration": true,                   /* Generates corresponding '.d.ts' file. */
11
+    // "declarationMap": true,                /* Generates a sourcemap for each corresponding '.d.ts' file. */
12
+    // "sourceMap": true,                     /* Generates corresponding '.map' file. */
13
+    // "outFile": "./",                       /* Concatenate and emit output to single file. */
14
+    // "outDir": "./",                        /* Redirect output structure to the directory. */
15
+    // "rootDir": "./",                       /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */
16
+    // "composite": true,                     /* Enable project compilation */
17
+    // "incremental": true,                   /* Enable incremental compilation */
18
+    // "tsBuildInfoFile": "./",               /* Specify file to store incremental compilation information */
19
+    // "removeComments": true,                /* Do not emit comments to output. */
20
+    // "noEmit": true,                        /* Do not emit outputs. */
21
+    // "importHelpers": true,                 /* Import emit helpers from 'tslib'. */
22
+    // "downlevelIteration": true,            /* Provide full support for iterables in 'for-of', spread, and destructuring when targeting 'ES5' or 'ES3'. */
23
+    // "isolatedModules": true,               /* Transpile each file as a separate module (similar to 'ts.transpileModule'). */
24
+
25
+    /* Strict Type-Checking Options */
26
+    "strict": true,                           /* Enable all strict type-checking options. */
27
+    // "noImplicitAny": true,                 /* Raise error on expressions and declarations with an implied 'any' type. */
28
+    // "strictNullChecks": true,              /* Enable strict null checks. */
29
+    // "strictFunctionTypes": true,           /* Enable strict checking of function types. */
30
+    // "strictBindCallApply": true,           /* Enable strict 'bind', 'call', and 'apply' methods on functions. */
31
+    // "strictPropertyInitialization": true,  /* Enable strict checking of property initialization in classes. */
32
+    // "noImplicitThis": true,                /* Raise error on 'this' expressions with an implied 'any' type. */
33
+    // "alwaysStrict": true,                  /* Parse in strict mode and emit "use strict" for each source file. */
34
+
35
+    /* Additional Checks */
36
+    // "noUnusedLocals": true,                /* Report errors on unused locals. */
37
+    // "noUnusedParameters": true,            /* Report errors on unused parameters. */
38
+    // "noImplicitReturns": true,             /* Report error when not all code paths in function return a value. */
39
+    // "noFallthroughCasesInSwitch": true,    /* Report errors for fallthrough cases in switch statement. */
40
+
41
+    /* Module Resolution Options */
42
+    // "moduleResolution": "node",            /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
43
+    // "baseUrl": "./",                       /* Base directory to resolve non-absolute module names. */
44
+    // "paths": {},                           /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */
45
+    // "rootDirs": [],                        /* List of root folders whose combined content represents the structure of the project at runtime. */
46
+    // "typeRoots": [],                       /* List of folders to include type definitions from. */
47
+    // "types": [],                           /* Type declaration files to be included in compilation. */
48
+    // "allowSyntheticDefaultImports": true,  /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */
49
+    "esModuleInterop": true                   /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
50
+    // "preserveSymlinks": true,              /* Do not resolve the real path of symlinks. */
51
+
52
+    /* Source Map Options */
53
+    // "sourceRoot": "",                      /* Specify the location where debugger should locate TypeScript files instead of source locations. */
54
+    // "mapRoot": "",                         /* Specify the location where debugger should locate map files instead of generated locations. */
55
+    // "inlineSourceMap": true,               /* Emit a single file with source maps instead of having a separate file. */
56
+    // "inlineSources": true,                 /* Emit the source alongside the sourcemaps within a single file; requires '--inlineSourceMap' or '--sourceMap' to be set. */
57
+
58
+    /* Experimental Options */
59
+    // "experimentalDecorators": true,        /* Enables experimental support for ES7 decorators. */
60
+    // "emitDecoratorMetadata": true,         /* Enables experimental support for emitting type metadata for decorators. */
61
+  }
62
+}

+ 18
- 0
tslint.json View File

@@ -0,0 +1,18 @@
1
+{
2
+  "defaultSeverity": "error",
3
+  "extends": ["tslint:recommended"],
4
+  "jsRules": {},
5
+  "rules": {},
6
+  "rulesDirectory": [],
7
+  "linterOptions": {
8
+    "exclude": ["src/index.ts"]
9
+  },
10
+  "overrides": [
11
+    {
12
+      "files": ["*.test.ts"],
13
+      "rules": {
14
+        "no-unused-expressions": "off"
15
+      }
16
+    }
17
+  ]
18
+}

Loading…
Cancel
Save