Skip to content

Commit

Permalink
BaseParsers: init with some simple parsers
Browse files Browse the repository at this point in the history
  • Loading branch information
fabianhjr committed Oct 2, 2024
1 parent 3675df4 commit a2396b3
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/parsers/BaseParsers.ts
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"),
);
109 changes: 109 additions & 0 deletions test/parsers/BaseParsers.test.ts
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);
}
});
});

0 comments on commit a2396b3

Please sign in to comment.