UNPKG

ima-parse

Version:

Easy Simple Parser, that only requires a Grammar JSON to output an AST.

76 lines (75 loc) 3.54 kB
import { RuleParser } from "./RuleParser"; import { Grammar, DefinitionRules, DefinitionPaths, DefinitionPart, SimpleDefinitionPart } from "./grammarTypes"; import { Cursor, PhraseKind } from "../helpers/helpers"; export type ParsedPartBase = { type: string; /** Since a definition parts can be duplicated, we also store the index within the array this part is */ index: number; /** Indicates that the previous part can be overwritten if it targetted the same definition part */ overrideSamePart?: boolean; /** Indicates that this part cannot be expanded, like: end of a comment has been reached, all optional options, etc */ isFinished?: boolean; /** Indicates the current part will accept all characters */ textMode?: boolean; /** Indicates that the last parsed phrase was purposfully ignored and can be reused for other rules */ ignoredPhrase?: boolean; }; export type ParsedRule = ParsedPartBase & { type: "rule"; /** Direct child parser object, that can have its own structure and parsed parts. */ childParser: RuleParser; /** The actual (deepest) parser that was able to process the input that was parsed by this part. Can be 'unlimited' deep in the tree. */ successfulParser: RuleParser; separatorOptional?: boolean; separatorSatisfied?: boolean; }; export type ParsedSimplePart = ParsedPartBase & { type: "simple"; value: string[]; overrideSamePart?: boolean; /** Cursor position at which the part starts */ startPos: Cursor; /** Cursor position at which the part ends */ endPos: Cursor; }; export type ParsedPath = ParsedPartBase & { type: "paths"; pathsProgress: { path: DefinitionPaths["paths"][number]; parsedParts: ParsedSimplePart[]; }[]; hasSatisfiedPath: boolean; overrideSamePart: true; }; export type ParsedPart = ParsedRule | ParsedSimplePart | ParsedPath; export type ParseContext = { grammar: Grammar; parts: DefinitionPart[]; parser: RuleParser; }; export type Input = { chars: string; phraseKind: PhraseKind; startPos: Cursor; endPos: Cursor; }; export type ParseInfo<D extends DefinitionPart, P extends ParsedPartBase = ParsedPart> = { definition: D; index: number; input: Input; context: ParseContext; previousParsedPart?: P; }; export declare function parseInput(input: Input, context: ParseContext, previousParsedPart?: ParsedPart): ParsedPart | undefined; export declare function matchDefinition(definition: DefinitionPart, index: number, input: Input, context: ParseContext, previousParsedPart?: ParsedPart): ParsedRule | ParsedSimplePart | ParsedPath | undefined; type PathParseInfo = ParseInfo<DefinitionPaths, ParsedPath>; /** * @returns parse information if parsing was possible for one of the paths in the definition. If not possible, undefined. */ export declare function matchPathsPart({ definition, input, index, previousParsedPart, context }: PathParseInfo): ParsedPath | undefined; type SimpleParseInfo = ParseInfo<SimpleDefinitionPart, ParsedSimplePart>; export declare function matchSimplePart({ definition, input, index, previousParsedPart }: SimpleParseInfo): ParsedSimplePart | undefined; export declare function isDefinitionSatisfied(parts: DefinitionPart[], parsedParts: ParsedPart[]): boolean; type RuleParseInfo = ParseInfo<DefinitionRules, ParsedRule>; export declare function matchRulePart({ definition, input, index, context, previousParsedPart }: RuleParseInfo): ParsedRule | undefined; export {};