import PValidationError from "./PValidationError"; import GRule, { GElement } from "./grammar/GRule"; import IObjectMap from "./IObjectMap"; import { ISourceList, UIDBase, IContentList, SimpleTextContent } from "@cafetextual/util"; import SourceLocation from "@cafetextual/util/dist/src/source/SourceLocation"; /** * Top level construct that maps objects to results, validation errors etc */ export default class PObjectMap implements IObjectMap { constructor(); content: ISourceList; private _objectMap; private _objectPropertiesMap; private _objectCollectionMap; private _objectPropertiesList; private ensureReferenceable(o); pset(o: any, ref: any, v: any): void; private pref(o, ref); static itemIsNonReferenceable(item: any): boolean; mapObject(element: IPResult, o: any, parent: any, propertyName: string, isTopOrCollection?: boolean): void; mapCollectionObject(element: PElementResult, collection: Array, index: number, o: any): void; /** * o - parent object (if childObject is non referenceable, ie a String) * childObject - use this * propertyName - only needed when we have a (non referenceable childObject) * index - when the parent object is an array * */ objectToLoc(o: any, includeToken?: boolean, propertyName?: string, index?: number, childObject?: any): SourceLocation; objectToGStack(o: any, includeToken?: boolean, propertyName?: string, index?: number, childObject?: any): Array; toResult(o: any, propertyName?: string, index?: number, childObject?: any): IPResult; toLoc(r: IPResult, includeToken?: boolean): SourceLocation; static resultToLocation(r: IPResult, includeToken?: boolean): SourceLocation; private _validationHash; private _validationErrors; logValidationError(err: PValidationError): void; readonly validationErrors: Array; validationErrorsByElement(e: IPResult): Array; hasError(element?: IPResult, type?: string, err?: string): boolean; toErrString(content?: IContentList): string; /** * * iterates mappings over the following callbacks * */ eachProperty(o: any, depth: number, it: IObjectTreeFactoryIterator, topName?: string, includeTop?: boolean): void; clear(): void; static showLoc(src: SimpleTextContent, loc: SourceLocation): string; /** * To a babel-friendly ast /w extracting complete source location information for objects. * * Note: * * - we're mapping class to Node.type * rule(RuleType): ... <--- RuleType is the Node.type * * - properties named "type", "loc" , "__cls" ,"uid" will break this * * rule(MyType): X(type):etc <--- conflict breaks data integrity * * o straightforward to create a more general mechanism * o might reasonably put a validation error on the grammar's property names * when such an ast is targeted * * * - similarly, grammar types named: "StringLiteral" , "BooleanLiteral" "ArrayLiteral" * will cause similar problems * * ie myRule(StringLiteral): property_that_inst_value(etc) <--- will confict /w this ast mechanism * * - All leaf properties mapped to StringLiteral, BooleanLiteral, or ArrayNode * * rule(MyType): X(v) <-- maps to: * * { * type: "MyType", * loc: ..., * v: {type: StringLiteral, value: ...} * } * * * - this is meant to run on plain objects, not instances of classes * ie out = { __cls, uid/__uid v:"SomeResult"} * * * * (although we do have the data to do this by looking up all the registered values) * * * - grammar parser tree * each item has a parent rule state [RuleResult , ElementResult .... RuleResult] * mapping to grammar rules/elements. * gstack:[list of uids] * * https://github.com/babel/babylon/blob/master/ast/spec.md * * interface Node { * type: string; * loc: SourceLocation | null; * } * * interface SourceLocation { * source: string | null; * start: Position; * end: Position; * } * * interface Position { * line: number; // >= 1 * column: number; // >= 0 * } * */ toAST(topObject: Object): Object; private static IGNORE; private objectToAST(o, includeGrammarStack?); private itemToAST(o, value, key, index); private addUID(o); private toLeafAST(value); } export interface IPResult { isValid(parentMap: PObjectMap, lineID: number): boolean; firstElement(): PElementResult; firstRule(): PRuleResult; lastElement(): PElementResult; show(): string; getUid(): string; } export declare class PElementResult extends UIDBase implements IPResult { /** * Result of a single element match. * */ constructor(element?: GElement, valid?: boolean, err?: string, childResult?: PRuleResult, prefixStart?: number, contentStartIndex?: number, contentEndIndex?: number, suffixStart?: number, endIndex?: number); element: GElement; varStr: string; /** * result when we have a composite rule X[Y(var1) Z(var2)] */ childResult: PRuleResult; valid: boolean; err: string; errCode: number; /** * May have children when we have a multiply occuring values */ children: Array; startLineIndex: number; tokenPrefixIndex: number; startIndex: number; contentIndex: number; contentEndIndex: number; tokenSuffixIndex: number; endIndex: number; parentRuleResult: PRuleResult; /** * This is the the parent line result * * Although when this is the first element in a line child * a) This is parsed as a direct child of a (more or less throw away rule result) * b) .. it is added as a child of an effective parent * c) it's immediate parent is a child (direct or part of a collection) of the line rule result * */ parentLineRuleResult: PRuleResult; parentLineCollectionIndex: number; effectiveParentResult: PRuleResult; parentElementIndex: number; parentElement: GElement; parentCollectionIndex: number; cutpointFailElement: PElementResult; /** * return the match index of the parent line. * * Not necessarily equal to the index of the grammar child line element (unless all child line rules * are non-optional). */ lineMatchIndex(): number; isLineChild(): boolean; child(index: number): PElementResult; toStringLow(prefix: string): string; isValid(parentMap?: PObjectMap, lineID?: number): boolean; static create(): PElementResult; release(): void; tr(childIndex?: number): string; show(): string; /** * result trace - for debug, useful for quickly identifying an element * * Iterates over actually present element results, as opposed to tr() which iterates over all grammar constructs */ rtr(): string; private lastChild(); private firstChild(); /** * * */ firstRule(): PRuleResult; firstElement(): PElementResult; lastElement(): PElementResult; } /** * Details of a match parser rule */ export declare class PRuleResult extends UIDBase implements IPResult { constructor(source?: string, r?: GRule, element?: PElementResult); source: string; /** * Array of secondary grammar (ie managing blank lines, maybe comment etc) */ preResults: Array; preResultsLength(): number; secondaryResults: Array; /** * Used in incremental parsing/tokenising/debugging when a parsing may pause in the middle of a rule * * (this could also be inferred form the call stack) */ lineIterationInProgress: boolean; /** * Reference to the rule that was matched */ matchedRule: GRule; /** * details of the match. * *

Includes resul elements that document errors

*/ results: Array; /** * True if the last matched element has matched WS */ matchedWS: boolean; /** * Extracted values of variables */ data: any; startIndex: number; endIndex: number; /** * result starts on this line in the content. */ startLineIndex: number; /** * Index of match, not the index of the rule in the grammar. */ lineMatchIndex: number; lineChildren: Array; lineChild(index: number, collectionIndex?: number): PRuleResult; /** * Convenience method - return the element of a line child, */ lineElement(index: number, collectionIndex?: number): PElementResult; firstRule(): PRuleResult; firstElement(): PElementResult; lastElement(): PElementResult; parentElementResult: PElementResult; parentLineResult: PRuleResult; preResultIndex: number; secondaryResultIndex: number; /** * should only be called by rule state * @private */ addResult_low(v: PElementResult): void; removeResult_low(v: PElementResult): void; private _matchedElements; readonly matchedElements: number; quickErrorStr(): string; /** * Recursively determine if this rule is valid. * * Note - this explicitly recurses, and is not an efficient way to determine valiidity. * * @param includeLines recurse on child lines only if true */ isValid(parentMap?: PObjectMap, lineID?: number): boolean; contentString(): string; child(index: number): PElementResult; /** * */ toString(): string; toStringLow(prefix: string): string; getSecondaryResult(index: number): PRuleResult; getResult(index: number): PElementResult; map: PObjectMap; static create(): PRuleResult; tr(): string; show(): string; rtr(elementIndex?: number): string; /** * Distinguishes between * rule1: ==> isLineTopRule = true * --> X :: ... * * and * * rule2: * --> X :: ... ==> isLineTopRule = false * --> Y :: ... * * note that this includes * * rule1: ... :rule2 ... * */ isLineTopRule(): boolean; /** * Documents the relattion of child line rules (as maintained in lineRules) and the elements as they are kept * in a parent rules's results property. */ lineMainElement(lineResultIndex: number, expectedRuleResult: PRuleResult): PElementResult; eachElement(f: Function): void; } export interface IObjectTreeFactoryIterator { objectF(o: any, depth: number, propertyName: string, pr: IPResult, loc: SourceLocation): void; leafF(o: any, depth: number, propertyName: string, value: any, pr: IPResult, loc: SourceLocation): void; arrayF(array: Array, depth: number, propertyName: string, pr: IPResult, loc: SourceLocation): void; arrayLeafF(index: number, depth: number, value: any, pr: IPResult, loc: SourceLocation): void; arrayItemF(index: number, depth: number, o: Object, p: IPResult, loc: SourceLocation): void; }