ima-parse
Version:
Easy Simple Parser, that only requires a Grammar JSON to output an AST.
107 lines (106 loc) • 4.77 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const definitionParsers_1 = require("../../definitionParsers");
describe("definitionParsers > parseInput()", () => {
const START_POS = { col: 0, ln: 0 };
const END_POS = { col: 1, ln: 0 };
const SIMPLE_PART_DATA = { type: "simple", startPos: START_POS, endPos: END_POS, index: 0 };
// Rules used for testing
let simpleRule;
let input;
let context;
let parts;
let parseInfoBase;
beforeEach(() => {
parts = [];
context = { parser: {}, grammar: {}, parts };
input = { chars: "x", startPos: START_POS, endPos: END_POS, phraseKind: "chars" };
simpleRule = { name: "importRule", definition: [{ type: "keyword", phrase: "import" }] };
parseInfoBase = { context, input, index: 0 };
});
it("should parse parts", () => {
parts.push({ type: "modifiers", phrases: ["abstract", "public"] }, { type: "keyword", phrase: "class" });
input.chars = "abstract";
const parsedModifiersPart = (0, definitionParsers_1.parseInput)(input, context);
expect(parsedModifiersPart).toEqual({ ...SIMPLE_PART_DATA, value: ["abstract"], isFinished: false });
input.chars = "class";
const parsedKeywordPart = (0, definitionParsers_1.parseInput)(input, context, parsedModifiersPart);
expect(parsedKeywordPart).toEqual({ ...SIMPLE_PART_DATA, index: 1, value: ["class"], isFinished: true });
});
it("should continue a previous parsed part", () => {
// Arrange
const pathA = [
{ type: "keyword", phrase: "." },
{ type: "keyword", phrase: "*" }
];
parts.push({ type: "paths", paths: [pathA] }, { type: "keyword", phrase: "=" });
// Act 1: Parse the first part
input.chars = ".";
const parsedPathPart1 = (0, definitionParsers_1.parseInput)(input, context);
// Assert
expect(parsedPathPart1).toEqual({
index: 0,
type: "paths",
overrideSamePart: true,
hasSatisfiedPath: false,
textMode: false,
pathsProgress: [{ parsedParts: [{ ...SIMPLE_PART_DATA, isFinished: true, value: ["."] }], path: pathA }]
});
// Act 2: Continue to parse the previous part
input.chars = "*";
const parsedPathPart2 = (0, definitionParsers_1.parseInput)(input, context, parsedPathPart1);
// Assert
expect(parsedPathPart2).toEqual({
index: 0,
type: "paths",
overrideSamePart: true,
hasSatisfiedPath: true,
textMode: false,
pathsProgress: [
{
parsedParts: [
{ ...SIMPLE_PART_DATA, index: 0, isFinished: true, value: ["."] },
{ ...SIMPLE_PART_DATA, index: 1, isFinished: true, value: ["*"] }
],
path: pathA
}
]
});
// Act 3: Parse the keyword after the first paths part
input.chars = "=";
const parsedKeywordPart = (0, definitionParsers_1.parseInput)(input, context, parsedPathPart2);
// Assert
expect(parsedKeywordPart).toEqual({ ...SIMPLE_PART_DATA, index: 1, isFinished: true, value: ["="] });
});
it("should not allow a new part if there was no satistied path in a paths part", () => {
// Arrange
const pathA = [
{ type: "keyword", phrase: "." },
{ type: "keyword", phrase: "*" }
];
const pathB = [
{ type: "keyword", phrase: "." },
{ type: "identifier", key: "subject" }
];
parts.push({ type: "paths", paths: [pathA, pathB] }, { type: "keyword", phrase: "=" });
// Act 1: parse a paths part
input.chars = ".";
const parsedModifiersPart = (0, definitionParsers_1.parseInput)(input, context);
// Assert
expect(parsedModifiersPart).toEqual({
index: 0,
type: "paths",
overrideSamePart: true,
hasSatisfiedPath: false,
textMode: false,
pathsProgress: [
{ parsedParts: [{ ...SIMPLE_PART_DATA, isFinished: true, value: ["."] }], path: pathA },
{ parsedParts: [{ ...SIMPLE_PART_DATA, isFinished: true, value: ["."] }], path: pathB }
]
});
// Act 2: Try to skip the next required parts and parse the keyword after the paths part, which should not succeed
input.chars = "=";
const parsedKeywordPart = (0, definitionParsers_1.parseInput)(input, context, parsedModifiersPart);
expect(parsedKeywordPart).toBeUndefined();
});
});