-
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.
BaseParsers: init with some simple parsers
- Loading branch information
Showing
2 changed files
with
120 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,11 @@ | ||
import { CondCharParser } from "./CondCharParser.ts"; | ||
|
||
export const space = CondCharParser.new((input) => input === " "); | ||
export const tab = CondCharParser.new((input) => input === "\t"); | ||
export const newline = CondCharParser.new((input) => input === "\n"); | ||
export const digit = CondCharParser.new( | ||
(input) => input >= "0" && input <= "9", | ||
); | ||
export const letter = CondCharParser.new( | ||
(input) => (input >= "a" && input <= "z") || (input >= "A" && input <= "Z"), | ||
); |
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,109 @@ | ||
import { describe, expect, it } from "@jest/globals"; | ||
import { assert, property, string } from "fast-check"; | ||
|
||
import { | ||
space, | ||
tab, | ||
newline, | ||
digit, | ||
letter, | ||
} from "../../src/parsers/BaseParsers.ts"; | ||
import { | ||
CondFailure, | ||
EndOfFileFailure, | ||
Success, | ||
} from "../../src/parsers/ParserResult.ts"; | ||
|
||
describe("BaseParsers Unit", () => { | ||
it("space should successfully parse matching input", () => { | ||
const result = space.parse(" "); | ||
expect(result).toBeInstanceOf(Success); | ||
}); | ||
it("space should fail with EndOfFileFailure on empty input", () => { | ||
const result = space.parse(""); | ||
expect(result).toBeInstanceOf(EndOfFileFailure); | ||
}); | ||
it("space should fail with CondFailure on non-matching input", () => { | ||
const result = space.parse("a"); | ||
expect(result).toBeInstanceOf(CondFailure); | ||
if (result instanceof CondFailure) { | ||
expect(result.cond).toBe('(input) => input === " "'); | ||
expect(result.actual).toBe("a"); | ||
expect(result.pos).toBe(1); | ||
} | ||
}); | ||
|
||
it("tab should successfully parse matching input", () => { | ||
const result = tab.parse("\t"); | ||
expect(result).toBeInstanceOf(Success); | ||
}); | ||
it("tab should fail with EndOfFileFailure on empty input", () => { | ||
const result = tab.parse(""); | ||
expect(result).toBeInstanceOf(EndOfFileFailure); | ||
}); | ||
it("tab should fail with CondFailure on non-matching input", () => { | ||
const result = tab.parse("a"); | ||
expect(result).toBeInstanceOf(CondFailure); | ||
if (result instanceof CondFailure) { | ||
expect(result.cond).toBe('(input) => input === "\\t"'); | ||
expect(result.actual).toBe("a"); | ||
expect(result.pos).toBe(1); | ||
} | ||
}); | ||
|
||
it("newline should successfully parse matching input", () => { | ||
const result = newline.parse("\n"); | ||
expect(result).toBeInstanceOf(Success); | ||
}); | ||
it("newline should fail with EndOfFileFailure on empty input", () => { | ||
const result = newline.parse(""); | ||
expect(result).toBeInstanceOf(EndOfFileFailure); | ||
}); | ||
it("newline should fail with CondFailure on non-matching input", () => { | ||
const result = newline.parse("a"); | ||
expect(result).toBeInstanceOf(CondFailure); | ||
if (result instanceof CondFailure) { | ||
expect(result.cond).toBe('(input) => input === "\\n"'); | ||
expect(result.actual).toBe("a"); | ||
expect(result.pos).toBe(1); | ||
} | ||
}); | ||
|
||
it("digit should successfully parse matching input", () => { | ||
const result = digit.parse("1"); | ||
expect(result).toBeInstanceOf(Success); | ||
}); | ||
it("digit should fail with EndOfFileFailure on empty input", () => { | ||
const result = digit.parse(""); | ||
expect(result).toBeInstanceOf(EndOfFileFailure); | ||
}); | ||
it("digit should fail with CondFailure on non-matching input", () => { | ||
const result = digit.parse("a"); | ||
expect(result).toBeInstanceOf(CondFailure); | ||
if (result instanceof CondFailure) { | ||
expect(result.cond).toBe('(input) => input >= "0" && input <= "9"'); | ||
expect(result.actual).toBe("a"); | ||
expect(result.pos).toBe(1); | ||
} | ||
}); | ||
|
||
it("letter should successfully parse matching input", () => { | ||
const result = letter.parse("a"); | ||
expect(result).toBeInstanceOf(Success); | ||
}); | ||
it("letter should fail with EndOfFileFailure on empty input", () => { | ||
const result = letter.parse(""); | ||
expect(result).toBeInstanceOf(EndOfFileFailure); | ||
}); | ||
it("letter should fail with CondFailure on non-matching input", () => { | ||
const result = letter.parse("1"); | ||
expect(result).toBeInstanceOf(CondFailure); | ||
if (result instanceof CondFailure) { | ||
expect(result.cond).toBe( | ||
'(input) => input >= "a" && input <= "z" || input >= "A" && input <= "Z"', | ||
); | ||
expect(result.actual).toBe("1"); | ||
expect(result.pos).toBe(1); | ||
} | ||
}); | ||
}); |