summaryrefslogtreecommitdiff
path: root/src/lang/js-lang/core/lexer.ts
diff options
context:
space:
mode:
authorKai Stevenson <kai@kaistevenson.com>2025-11-03 23:41:31 -0800
committerKai Stevenson <kai@kaistevenson.com>2025-11-03 23:41:31 -0800
commit8b610f2bcfc223333254ce9679730c42dce6d26e (patch)
treeac1eab726395523f8725bda3d040e22214cba409 /src/lang/js-lang/core/lexer.ts
parent56040f3ff85e77311f0c864a89afd63fcf1bdb50 (diff)
add createFn
Diffstat (limited to 'src/lang/js-lang/core/lexer.ts')
-rw-r--r--src/lang/js-lang/core/lexer.ts46
1 files changed, 46 insertions, 0 deletions
diff --git a/src/lang/js-lang/core/lexer.ts b/src/lang/js-lang/core/lexer.ts
new file mode 100644
index 0000000..95e0e19
--- /dev/null
+++ b/src/lang/js-lang/core/lexer.ts
@@ -0,0 +1,46 @@
+import { TokenType, Lex, Token } from "../../ts-lang";
+
+const WHITESPACE_TOKENS = [
+ TokenType.SPACE,
+ TokenType.COMMA,
+ TokenType.SEMICOLON,
+] as string[];
+
+export const lex = <const Raw extends string>(raw: Raw): Lex<Raw> => {
+ let _raw: string = raw;
+ let nameCollection = "";
+ const tokens: Token[] = [];
+
+ while (_raw) {
+ const head = _raw[0];
+ _raw = _raw.slice(1);
+
+ const processNameCollection = () => {
+ if (nameCollection) {
+ tokens.push({ type: TokenType.NAME, name: nameCollection });
+ nameCollection = "";
+ }
+ };
+
+ if (WHITESPACE_TOKENS.includes(head)) {
+ processNameCollection();
+ continue;
+ }
+
+ if (head === TokenType.OPEN_PAREN) {
+ processNameCollection();
+ tokens.push({ type: TokenType.OPEN_PAREN, name: "" });
+ continue;
+ }
+
+ if (head === TokenType.CLOSE_PAREN) {
+ processNameCollection();
+ tokens.push({ type: TokenType.CLOSE_PAREN, name: "" });
+ continue;
+ }
+
+ nameCollection += head;
+ }
+
+ return tokens as Lex<Raw>;
+};