-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
86 additions
and
5 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,6 +19,7 @@ | |
"src" | ||
], | ||
"dependencies": { | ||
"moo": "^0.5", | ||
"yargs": "^17" | ||
}, | ||
"scripts": { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { expect, test } from "vitest"; | ||
import { lexer } from "./lex.js"; | ||
|
||
const tokens = ( | ||
source: string, | ||
): { type: string | undefined; text: string }[] => { | ||
const lex = lexer(); | ||
lex.reset(source); | ||
return [...lex].map(({ type, text }) => ({ type, text })); | ||
}; | ||
|
||
test("comment", () => { | ||
expect( | ||
tokens(`# one comment | ||
# another comment`), | ||
).toEqual([ | ||
{ type: "comment", text: "# one comment" }, | ||
{ type: "space", text: "\n\n" }, | ||
{ type: "comment", text: "# another comment" }, | ||
]); | ||
}); | ||
|
||
test("string", () => { | ||
expect(tokens('"one string" "another string"')).toEqual([ | ||
{ type: "str", text: '"one string"' }, | ||
{ type: "space", text: " " }, | ||
{ type: "str", text: '"another string"' }, | ||
]); | ||
}); | ||
|
||
test("integer", () => { | ||
expect(tokens("42")).toEqual([{ type: "num", text: "42" }]); | ||
}); | ||
|
||
test("float", () => { | ||
expect(tokens("42.0")).toEqual([{ type: "num", text: "42.0" }]); | ||
}); | ||
|
||
test("number followed by letter", () => { | ||
expect(() => tokens("1a")).toThrow("invalid syntax"); | ||
}); | ||
|
||
test("letter followed by number", () => { | ||
expect(tokens("a1")).toEqual([{ type: "id", text: "a1" }]); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import moo from "moo"; | ||
|
||
export const lexer = () => | ||
moo.compile({ | ||
op: /[^\s#\w"\(\)\[\]\{\},\.]+/, | ||
space: { match: /\s+/, lineBreaks: true }, | ||
comment: /#.*?$/, | ||
str: { match: /".*?"/, lineBreaks: true }, | ||
num: /\d+(?:\.\d+)?\b/, | ||
id: /(?!\d)\w+/, | ||
lparen: "(", | ||
rparen: ")", | ||
lbracket: "[", | ||
rbracket: "]", | ||
lbrace: "{", | ||
rbrace: "}", | ||
comma: ",", | ||
dot: ".", | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters