-
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
2 changed files
with
38 additions
and
0 deletions.
There are no files selected for viewing
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,22 @@ | ||
import { describe, expect, test } from "@jest/globals"; | ||
import { EOI } from "../src"; | ||
import { SExpression } from "./s-expression"; | ||
|
||
describe("S-expression", () => { | ||
test("Hello world!", () => { | ||
const source = '(print "Hello world!")'; | ||
expect(SExpression.skip(EOI).parse(source)).toEqual({ | ||
success: true, | ||
index: expect.any(Number), | ||
value: ["print", '"Hello world!"'], | ||
}); | ||
}); | ||
test("quote list", () => { | ||
const source = "'(1 2 3 4)"; | ||
expect(SExpression.skip(EOI).parse(source)).toEqual({ | ||
success: true, | ||
index: expect.any(Number), | ||
value: ["quote", "1", "2", "3", "4"], | ||
}); | ||
}); | ||
}); |
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,16 @@ | ||
import { type Parser, choice, el, lazy, many, regex } from "../src"; | ||
|
||
export type SExpression = string | readonly SExpression[]; | ||
|
||
const list = lazy(() => SExpression) | ||
.apply(many) | ||
.between(el("("), el(")")); | ||
|
||
export const SExpression: Parser<SExpression, string> = choice([ | ||
el("'") | ||
.option() | ||
.and(list) | ||
.map(([quote, list]) => (quote ? ["quote", ...list] : list)), | ||
regex(/"([^"]|\\.)*"/), | ||
Check failure Code scanning / CodeQL Inefficient regular expression High
This part of the regular expression may cause exponential backtracking on strings starting with '"' and containing many repetitions of '\!'.
|
||
regex(/[^\s()"]+/), | ||
]).between(regex(/\s*/)); |