Skip to content

Commit

Permalink
Merge pull request #8 from ajmarcus/one-char-tokens
Browse files Browse the repository at this point in the history
add parsing for one character tokens
  • Loading branch information
ajmarcus authored Jan 12, 2024
2 parents 2f2be2b + 75f2fc7 commit 4fda004
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 41 deletions.
53 changes: 26 additions & 27 deletions .github/workflows/actions.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,33 @@
name: Node.js CI

on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]
push:
branches: ["main"]
pull_request:
branches: ["main"]

jobs:
build:
build:
runs-on: ubuntu-latest

runs-on: ubuntu-latest
strategy:
matrix:
node-version: [20.x]
# See supported Node.js release schedule at https://nodejs.org/en/about/releases/

strategy:
matrix:
node-version: [20.x]
# See supported Node.js release schedule at https://nodejs.org/en/about/releases/

steps:
- name: Checkout
uses: actions/checkout@v3
- name: Setup Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
cache: 'npm'
- name: Install Dependencies
run: npm ci
- name: Lint
run: npm run lint
- name: Build
run: npm run build --if-present
- name: Test
run: npm test
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Setup Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
cache: "npm"
- name: Install Dependencies
run: npm ci
- name: Lint
run: npm run lint
- name: Build
run: npm run build --if-present
- name: Test
run: npm test
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
"main": "dist/repl.js",
"scripts": {
"start": "node dist/repl.js",
"dev": "rm -Rf dist && tsc && node dist/repl.js",
"build": "tsc",
"dev": "rm -Rf dist && tsc -p tsconfig-build.json && node dist/repl.js",
"build": "tsc -p tsconfig-build.json",
"clean": "rm -R dist",
"fmt": "prettier . --write",
"fmt-check": "prettier . --check",
Expand Down
8 changes: 0 additions & 8 deletions src/parser.test.ts

This file was deleted.

37 changes: 34 additions & 3 deletions src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,48 @@ const tokens = new Set([
"WITH",
]);

function parse(input: string): string[] {
const ONE_CHAR_TOKENS = new Map<string, string>([
[",", "COMMA"],
[";", "SEMI"],
["(", "LPAREN"],
[")", "RPAREN"],
["*", "STAR"],
["+", "PLUS"],
["-", "MINUS"],
["/", "SLASH"],
["<", "LT"],
[">", "GT"],
["=", "EQ"],
]);

function tokenize(input: string): string[] {
const result = [];
const inputTokens = input.toUpperCase().split(/\s+/);
for (const t of inputTokens) {
if (tokens.has(t)) {
result.push(t);
result.push(`TKN<${t}>`);
} else {
result.push(`VAL<${t}>`);
// look for one character tokens
let remaining = [];
for (const c of t) {
if (ONE_CHAR_TOKENS.has(c)) {
result.push(`VAL<${remaining.join("")}>`);
remaining = [];
result.push(`TKN<${ONE_CHAR_TOKENS.get(c)}>`);
} else {
remaining.push(c);
}
}
if (remaining.length > 0) {
result.push(`VAL<${remaining.join("")}>`);
}
}
}
return result;
}

function parse(input: string): string[] {
return tokenize(input);
}

export { parse };
23 changes: 23 additions & 0 deletions test/parser.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { describe, expect, test } from "@jest/globals";
import { parse } from "../src/parser";

describe("select statement work in the parser", () => {
test('select returns ["SELECT"]', () => {
expect(parse("select")).toStrictEqual(["TKN<SELECT>"]);
});
test("select name from employees where tenure=5;", () => {
expect(
parse("select name from employees where tenure=5;"),
).toStrictEqual([
"TKN<SELECT>",
"VAL<NAME>",
"TKN<FROM>",
"VAL<EMPLOYEES>",
"TKN<WHERE>",
"VAL<TENURE>",
"TKN<EQ>",
"VAL<5>",
"TKN<SEMI>",
]);
});
});
4 changes: 4 additions & 0 deletions tsconfig-build.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"extends": "./tsconfig.json",
"exclude": ["test/**/*.test.ts"]
}
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
// input files
"include": ["src/**/*"],
"include": ["src/**/*", "test/**/*.test.ts"],
"exclude": ["node_modules", "dist"],
"compilerOptions": {
// output compiled js files to dist folder
Expand Down

0 comments on commit 4fda004

Please sign in to comment.