Options
All
  • Public
  • Public/Protected
  • All
Menu

Class Parser

A Recognizer capable of self analysis to determine it's grammar structure This is used for more advanced features requiring such information. for example: Error Recovery, Automatic lookahead calculation

Hierarchy

  • Parser

Index

Constructors

Properties

Accessors

Methods

Constructors

constructor

  • new Parser(input: Token[], tokensMapOrArr: object | Function[], isErrorRecoveryEnabled?: boolean): Parser

Properties

Protected RULE_OCCURRENCE_STACK

RULE_OCCURRENCE_STACK: number[]

Protected RULE_STACK

RULE_STACK: string[]

Protected _input

_input: Token[]

Private atLeastOneLookaheadKeys

atLeastOneLookaheadKeys: HashTable<string>[]

Private atLeastOneSepLookaheadKeys

atLeastOneSepLookaheadKeys: HashTable<string>[]

Private classLAFuncs

classLAFuncs: any

Protected className

className: string

Private definedRulesNames

definedRulesNames: string[]

Private definitionErrors

definitionErrors: IParserDefinitionError[]

errors

Private firstAfterRepMap

firstAfterRepMap: any

Protected inputIdx

inputIdx: number

Protected isBackTrackingStack

isBackTrackingStack: Array<any>

isErrorRecoveryEnabled

isErrorRecoveryEnabled: any

This flag enables or disables error recovery (fault tolerance) of the parser. If this flag is disabled the parser will halt on the first error.

Private manyLookaheadKeys

manyLookaheadKeys: HashTable<string>[]

Private manySepLookaheadKeys

manySepLookaheadKeys: HashTable<string>[]

Private optionLookaheadKeys

optionLookaheadKeys: HashTable<string>[]

Private orLookaheadKeys

orLookaheadKeys: HashTable<string>[]

Protected tokensMap

tokensMap: object

Type declaration

  • [fqn: string]: Function

Static DEFER_DEFINITION_ERRORS_HANDLING

DEFER_DEFINITION_ERRORS_HANDLING: boolean

Static IGNORE_AMBIGUITIES

IGNORE_AMBIGUITIES: boolean

Static NO_RESYNC

NO_RESYNC: boolean

Accessors

input

  • get input(): Token[]
  • set input(newInput: Token[]): void

Methods

Protected AT_LEAST_ONE

Protected AT_LEAST_ONE1

  • convenience method, same as MANY but the repetition is of one or more. failing to match at least one repetition will result in a parsing error and cause the parser to attempt error recovery.

    see

    MANY1

    Parameters

    • laFuncOrAction: LookAheadFunc | GrammarAction

      The lookahead function that 'decides' whether or not the AT_LEAST_ONE's action will be invoked or the action to optionally invoke

    • Optional action: GrammarAction | string
    • Optional errMsg: string

    Returns void

Protected AT_LEAST_ONE2

Protected AT_LEAST_ONE3

Protected AT_LEAST_ONE4

Protected AT_LEAST_ONE5

Protected AT_LEAST_ONE_SEP

Protected AT_LEAST_ONE_SEP1

  • convenience method, same as MANY_SEP but the repetition is of one or more. failing to match at least one repetition will result in a parsing error and cause the parser to attempt error recovery.

    see

    MANY_SEP1

    Parameters

    Returns Token[]

Protected AT_LEAST_ONE_SEP2

Protected AT_LEAST_ONE_SEP3

Protected AT_LEAST_ONE_SEP4

Protected AT_LEAST_ONE_SEP5

Protected BACKTRACK

  • BACKTRACK<T>(grammarRule: function, isValid: function): function
  • Type parameters

    • T

    Parameters

    • grammarRule: function

      the rule to try and parse in backtracking mode

        • (...args: Array<any>): T
        • Parameters

          • Rest ...args: Array<any>

          Returns T

    • isValid: function

      a predicate that given the result of the parse attempt will "decide" if the parse was successfully or not

        • (T: any): boolean
        • Parameters

          • T: any

          Returns boolean

    Returns function

    a lookahead function that will try to parse the given grammarRule and will return true if succeed

      • (): boolean
      • Returns boolean

Protected CONSUME

  • CONSUME(tokClass: Function): Token

Protected CONSUME1

  • CONSUME1(tokClass: Function): Token
  • A Parsing DSL method use to consume a single terminal Token. a Token will be consumed, IFF the next token in the token vector is an instanceof tokClass. otherwise the parser will attempt to perform error recovery.

    The index in the method name indicates the unique occurrence of a terminal consumption inside a the top level rule. What this means is that if a terminal appears more than once in a single rule, each appearance must have a difference index.

    for example:

    function parseQualifiedName() { this.CONSUME1(Identifier); this.MANY(()=> { this.CONSUME1(Dot); this.CONSUME2(Identifier); // <-- here we use CONSUME2 because the terminal }); // 'Identifier' has already appeared previously in the // the rule 'parseQualifiedName' }

    Parameters

    • tokClass: Function

      A constructor function specifying the type of token to be consumed.

    Returns Token

    The consumed token.

Protected CONSUME2

  • CONSUME2(tokClass: Function): Token

Protected CONSUME3

  • CONSUME3(tokClass: Function): Token

Protected CONSUME4

  • CONSUME4(tokClass: Function): Token

Protected CONSUME5

  • CONSUME5(tokClass: Function): Token

Protected LA

  • LA(howMuch: number): Token

Protected MANY

Protected MANY1

  • Parsing DSL method, that indicates a repetition of zero or more. This is equivalent to EBNF repetition {...}

    note that the 'action' param is optional. so both of the following forms are valid:

    short: this.MANY(()=>{ this.CONSUME(Comma}; this.CONSUME(Digit}); long: this.MANY(isComma, ()=>{ this.CONSUME(Comma}; this.CONSUME(Digit});

    using the short form is recommended as it will compute the lookahead function automatically. however this currently has one limitation: It only works if the lookahead for the grammar is one.

    As in CONSUME the index in the method name indicates the occurrence of the repetition production in it's top rule.

    Parameters

    • laFuncOrAction: LookAheadFunc | GrammarAction

      The lookahead function that 'decides' whether or not the MANY's action will be invoked or the action to optionally invoke

    • Optional action: GrammarAction

    Returns void

Protected MANY2

Protected MANY3

Protected MANY4

Protected MANY5

Protected MANY_SEP

Protected MANY_SEP1

  • Parsing DSL method, that indicates a repetition of zero or more with a separator Token between the repetitions.

    note that the 'action' param is optional. so both of the following forms are valid:

    short: this.MANY_SEP(Comma, ()=>{ this.CONSUME(Number}; ... );

    long: this.MANY(Comma, isNumber, ()=>{ this.CONSUME(Number} ... );

    using the short form is recommended as it will compute the lookahead function (for the first iteration) automatically. however this currently has one limitation: It only works if the lookahead for the grammar is one.

    As in CONSUME the index in the method name indicates the occurrence of the repetition production in it's top rule.

    Parameters

    • separator: TokenConstructor

      The Token to use as a separator between repetitions.

    • laFuncOrAction: LookAheadFunc | GrammarAction

      The lookahead function that 'decides' whether or not the MANY_SEP's action will be invoked or the action to optionally invoke

    • Optional action: GrammarAction

    Returns Token[]

    • The consumed separator Tokens.

Protected MANY_SEP2

Protected MANY_SEP3

Protected MANY_SEP4

Protected MANY_SEP5

Protected NEXT_TOKEN

Protected OPTION

Protected OPTION1

  • Parsing DSL Method that Indicates an Optional production in EBNF notation: [...]

    note that the 'action' param is optional. so both of the following forms are valid:

    short: this.OPTION(()=>{ this.CONSUME(Digit}); long: this.OPTION(isDigit, ()=>{ this.CONSUME(Digit});

    using the short form is recommended as it will compute the lookahead function automatically. however this currently has one limitation: It only works if the lookahead for the grammar is one.

    As in CONSUME the index in the method name indicates the occurrence of the optional production in it's top rule.

    Parameters

    • laFuncOrAction: LookAheadFunc | GrammarAction

      The lookahead function that 'decides' whether or not the OPTION's action will be invoked or the action to optionally invoke

    • Optional action: GrammarAction

    Returns boolean

    true iff the OPTION's action has been invoked

Protected OPTION2

Protected OPTION3

Protected OPTION4

Protected OPTION5

Protected OR

  • OR<T>(alts: IOrAlt<T>[] | IOrAltImplicit<T>[], errMsgTypes?: string, ignoreAmbiguities?: boolean): T
  • Convenience method equivalent to OR1

    see

    OR1

    Type parameters

    • T

    Parameters

    • alts: IOrAlt<T>[] | IOrAltImplicit<T>[]
    • Optional errMsgTypes: string
    • Default value ignoreAmbiguities: boolean = false

    Returns T

Protected OR1

  • OR1<T>(alts: IOrAlt<T>[] | IOrAltImplicit<T>[], errMsgTypes?: string, ignoreAmbiguities?: boolean): T
  • Parsing DSL method that indicates a choice between a set of alternatives must be made. This is equivalent to EBNF alternation (A | B | C | D ...)

    There are two forms:

    short: this.OR([ {ALT:()=>{this.CONSUME(One)}}, {ALT:()=>{this.CONSUME(Two)}}, {ALT:()=>{this.CONSUME(Three)}}, ], "a number")

    long: this.OR([ {WHEN: isOne, THEN_DO:()=>{this.CONSUME(One)}}, {WHEN: isTwo, THEN_DO:()=>{this.CONSUME(Two)}}, {WHEN: isThree, THEN_DO:()=>{this.CONSUME(Three)}}, ], "a number")

    using the short form is recommended as it will compute the lookahead function automatically. however this currently has one limitation: It only works if the lookahead for the grammar is one LL(1).

    As in CONSUME the index in the method name indicates the occurrence of the alternation production in it's top rule.

    Type parameters

    • T

    Parameters

    • alts: IOrAlt<T>[] | IOrAltImplicit<T>[]
    • Optional errMsgTypes: string
    • Default value ignoreAmbiguities: boolean = false

    Returns T

    The result of invoking the chosen alternative

Protected OR2

  • OR2<T>(alts: IOrAlt<T>[] | IOrAltImplicit<T>[], errMsgTypes?: string, ignoreAmbiguities?: boolean): T

Protected OR3

  • OR3<T>(alts: IOrAlt<T>[] | IOrAltImplicit<T>[], errMsgTypes?: string, ignoreAmbiguities?: boolean): T

Protected OR4

  • OR4<T>(alts: IOrAlt<T>[] | IOrAltImplicit<T>[], errMsgTypes?: string, ignoreAmbiguities?: boolean): T

Protected OR5

  • OR5<T>(alts: IOrAlt<T>[] | IOrAltImplicit<T>[], errMsgTypes?: string, ignoreAmbiguities?: boolean): T

Protected OVERRIDE_RULE

  • OVERRIDE_RULE<T>(ruleName: string, impl: function, invalidRet?: function, doReSync?: boolean): function
  • see

    RULE same as RULE, but should only be used in "extending" grammars to override rules/productions from the super grammar.

    Type parameters

    • T

    Parameters

    • ruleName: string
    • impl: function
        • (...implArgs: any[]): T
        • Parameters

          • Rest ...implArgs: any[]

          Returns T

    • Default value invalidRet: function = this.defaultInvalidReturn
        • (): T
        • Returns T

    • Default value doReSync: boolean = true

    Returns function

      • (idxInCallingRule?: number, ...args: any[]): T
      • Parameters

        • Optional idxInCallingRule: number
        • Rest ...args: any[]

        Returns T

Protected RULE

  • RULE<T>(ruleName: string, impl: function, invalidRet?: function, doReSync?: boolean): function
  • Type parameters

    • T

    Parameters

    • ruleName: string

      The name of the Rule. must match the let it is assigned to.

    • impl: function

      The implementation of the Rule

        • (...implArgs: any[]): T
        • Parameters

          • Rest ...implArgs: any[]

          Returns T

    • Default value invalidRet: function = this.defaultInvalidReturn
        • (): T
        • Returns T

    • Default value doReSync: boolean = true

    Returns function

    The parsing rule which is the impl Function wrapped with the parsing logic that handles Parser state / error recovery / ...

      • (idxInCallingRule?: number, ...args: any[]): T
      • Parameters

        • Optional idxInCallingRule: number
        • Rest ...args: any[]

        Returns T

Protected RULE_NO_RESYNC

  • RULE_NO_RESYNC<T>(ruleName: string, impl: function, invalidRet: function): function
  • Convenience method, same as RULE with doReSync=false

    see

    RULE

    Type parameters

    • T

    Parameters

    • ruleName: string
    • impl: function
        • (): T
        • Returns T

    • invalidRet: function
        • (): T
        • Returns T

    Returns function

      • (idxInCallingRule: number, isEntryPoint?: boolean): T
      • Parameters

        • idxInCallingRule: number
        • Optional isEntryPoint: boolean

        Returns T

Protected SAVE_ERROR

Protected SKIP_TOKEN

Protected SUBRULE

  • SUBRULE<T>(ruleToCall: function, args?: any[]): T
  • Convenience method equivalent to SUBRULE1

    see

    SUBRULE1

    Type parameters

    • T

    Parameters

    • ruleToCall: function
        • (number: any): T
        • Parameters

          • number: any

          Returns T

    • Default value args: any[] = []

    Returns T

Protected SUBRULE1

  • SUBRULE1<T>(ruleToCall: function, args?: any[]): T
  • The Parsing DSL Method is used by one rule to call another.

    This may seem redundant as it does not actually do much. However using it is mandatory for all sub rule invocations. calling another rule without wrapping in SUBRULE(...) will cause errors/mistakes in the Recognizer's self analysis which will lead to errors in error recovery/automatic lookahead calculation and any other functionality relying on the Recognizer's self analysis output.

    As in CONSUME the index in the method name indicates the occurrence of the sub rule invocation in its rule.

    Type parameters

    • T

    Parameters

    • ruleToCall: function

      the rule to invoke

        • (number: any): T
        • Parameters

          • number: any

          Returns T

    • Default value args: any[] = []

      the arguments to pass to the invoked subrule

    Returns T

    the result of invoking ruleToCall

Protected SUBRULE2

  • SUBRULE2<T>(ruleToCall: function, args?: any[]): T
  • see

    SUBRULE1

    Type parameters

    • T

    Parameters

    • ruleToCall: function
        • (number: any): T
        • Parameters

          • number: any

          Returns T

    • Default value args: any[] = []

    Returns T

Protected SUBRULE3

  • SUBRULE3<T>(ruleToCall: function, args?: any[]): T
  • see

    SUBRULE1

    Type parameters

    • T

    Parameters

    • ruleToCall: function
        • (number: any): T
        • Parameters

          • number: any

          Returns T

    • Default value args: any[] = []

    Returns T

Protected SUBRULE4

  • SUBRULE4<T>(ruleToCall: function, args?: any[]): T
  • see

    SUBRULE1

    Type parameters

    • T

    Parameters

    • ruleToCall: function
        • (number: any): T
        • Parameters

          • number: any

          Returns T

    • Default value args: any[] = []

    Returns T

Protected SUBRULE5

  • SUBRULE5<T>(ruleToCall: function, args?: any[]): T
  • see

    SUBRULE1

    Type parameters

    • T

    Parameters

    • ruleToCall: function
        • (number: any): T
        • Parameters

          • number: any

          Returns T

    • Default value args: any[] = []

    Returns T

Private addToResyncTokens

Private atLeastOneInternal

Private atLeastOneSepFirstInternal

Private attemptInRepetitionRecovery

  • attemptInRepetitionRecovery(prodFunc: Function, args: any[], lookaheadFunc: function, prodName: string, prodOccurrence: number, nextToksWalker: AbstractNextTerminalAfterProductionWalker, prodKeys: HashTable<string>[]): void
  • Parameters

    • prodFunc: Function
    • args: any[]
    • lookaheadFunc: function
        • (): boolean
        • Returns boolean

    • prodName: string
    • prodOccurrence: number
    • nextToksWalker: AbstractNextTerminalAfterProductionWalker
    • prodKeys: HashTable<string>[]

    Returns void

Private buildFullFollowKeyStack

Private canPerformInRuleRecovery

  • canPerformInRuleRecovery(expectedToken: Function, follows: Function[]): boolean

Private canRecoverWithSingleTokenDeletion

  • canRecoverWithSingleTokenDeletion(expectedTokType: Function): boolean

Private canRecoverWithSingleTokenInsertion

  • canRecoverWithSingleTokenInsertion(expectedTokType: Function, follows: Function[]): boolean

Protected canTokenTypeBeInsertedInRecovery

  • canTokenTypeBeInsertedInRecovery(tokClass: Function): boolean
  • By default all tokens type may be inserted. This behavior may be overridden in inheriting Recognizers for example: One may decide that only punctuation tokens may be inserted automatically as they have no additional semantic value. (A mandatory semicolon has no additional semantic meaning, but an Integer may have additional meaning depending on its int value and context (Inserting an integer 0 in cardinality: "[1..]" will cause semantic issues as the max of the cardinality will be greater than the min value. (and this is a false error!)

    Parameters

    • tokClass: Function

    Returns boolean

Private consumeInternal

  • consumeInternal(tokClass: Function, idx: number): Token
  • Parameters

    • tokClass: Function

      The Type of Token we wish to consume (Reference to its constructor function)

    • idx: number

      occurrence index of consumed token in the invoking parser rule text for example: IDENT (DOT IDENT)* the first ident will have idx 1 and the second one idx 2

          * note that for the second ident the idx is always 2 even if its invoked 30 times in the same rule
            the idx is about the position in grammar (source code) and has nothing to do with a specific invocation
            details
      

    Returns Token

    the consumed Token

Private consumeInternalOptimized

  • consumeInternalOptimized(expectedTokClass: Function): Token

Private defaultInvalidReturn

  • defaultInvalidReturn(): any

Private defineRule

  • defineRule<T>(ruleName: string, impl: function, invalidRet: function, doReSync: any): function
  • Type parameters

    • T

    Parameters

    • ruleName: string
    • impl: function
        • (...implArgs: any[]): T
        • Parameters

          • Rest ...implArgs: any[]

          Returns T

    • invalidRet: function
        • (): T
        • Returns T

    • doReSync: any

    Returns function

      • (idxInCallingRule?: number, ...args: any[]): T
      • Parameters

        • Optional idxInCallingRule: number
        • Rest ...args: any[]

        Returns T

Private findReSyncTokenType

  • findReSyncTokenType(): Function

Private flattenFollowSet

  • flattenFollowSet(): Function[]

Private getCurrFollowKey

Private getFollowSetFromFollowKey

  • getFollowSetFromFollowKey(followKey: IFollowKey): Function[]

Private getFollowsForInRuleRecovery

  • getFollowsForInRuleRecovery(tokClass: Function, tokIdxInRule: any): Function[]

getGAstProductions

  • getGAstProductions(): HashTable<Rule>

Private getKeyForAutomaticLookahead

  • getKeyForAutomaticLookahead(prodName: string, prodKeys: HashTable<string>[], occurrence: number): string

Private getLookaheadFuncFor

  • getLookaheadFuncFor<T>(key: string, occurrence: number, laFuncBuilder: function, extraArgs?: any[]): function
  • Type parameters

    • T

    Parameters

    • key: string
    • occurrence: number
    • laFuncBuilder: function
        • (number: any, any: any): function
        • Parameters

          • number: any
          • any: any

          Returns function

            • (): T
            • Returns T

    • Default value extraArgs: any[] = []

    Returns function

      • (): T
      • Returns T

Private getLookaheadFuncForAtLeastOne

  • getLookaheadFuncForAtLeastOne(occurence: number): function

Private getLookaheadFuncForAtLeastOneSep

  • getLookaheadFuncForAtLeastOneSep(occurence: number): function

Private getLookaheadFuncForMany

  • getLookaheadFuncForMany(occurence: number): function

Private getLookaheadFuncForManySep

  • getLookaheadFuncForManySep(occurence: number): function

Private getLookaheadFuncForOption

  • getLookaheadFuncForOption(occurence: number): function

Private getLookaheadFuncForOr

  • getLookaheadFuncForOr(occurence: number, ignoreErrors: boolean): function

Protected getMisMatchTokenErrorMessage

  • getMisMatchTokenErrorMessage(expectedTokType: Function, actualToken: Token): string
  • Parameters

    • expectedTokType: Function

      The Class of the expected Token.

    • actualToken: Token

      The actual unexpected (mismatched) Token instance encountered.

    Returns string

    The error message saved as part of a MismatchedTokenException.

Protected getTokenToInsert

  • getTokenToInsert(tokClass: Function): Token
  • Returns an "imaginary" Token to insert when Single Token Insertion is done Override this if you require special behavior in your grammar for example if an IntegerToken is required provide one with the image '0' so it would be valid syntactically

    Parameters

    • tokClass: Function

    Returns Token

isAtEndOfInput

  • isAtEndOfInput(): boolean

Protected isBackTracking

  • isBackTracking(): boolean

Private isInCurrentRuleReSyncSet

  • isInCurrentRuleReSyncSet(token: Function): boolean

Protected isNextRule

  • isNextRule<T>(ruleName: string): boolean

Private manyInternal

Private manySepFirstInternal

Private optionInternal

Private orInternal

  • orInternal<T>(alts: IOrAlt<T>[] | IOrAltImplicit<T>[], errMsgTypes: string, occurrence: number, ignoreAmbiguities: boolean): T

Private raiseEarlyExitException

  • raiseEarlyExitException(occurrence: number, nextWalkerConstructor: AbstractNextPossibleTokensWalker, userDefinedErrMsg: string): void
  • Parameters

    • occurrence: number
    • nextWalkerConstructor: AbstractNextPossibleTokensWalker
    • userDefinedErrMsg: string

    Returns void

Private raiseNoAltException

  • raiseNoAltException(occurrence: number, errMsgTypes: string): void

Private reSyncTo

  • reSyncTo(tokClass: Function): Token[]

Private reloadRecogState

Private repetitionSepSecondInternal

  • repetitionSepSecondInternal(prodName: string, prodOccurrence: number, separator: TokenConstructor, separatorLookAheadFunc: function, action: GrammarAction, separatorsResult: Token[], laKeys: HashTable<string>[], nextTerminalAfterWalker: AbstractNextTerminalAfterProductionWalker): void
  • Parameters

    • prodName: string
    • prodOccurrence: number
    • separator: TokenConstructor
    • separatorLookAheadFunc: function
        • (): boolean
        • Returns boolean

    • action: GrammarAction
    • separatorsResult: Token[]
    • laKeys: HashTable<string>[]
    • nextTerminalAfterWalker: AbstractNextTerminalAfterProductionWalker

    Returns void

reset

  • reset(): void

Protected ruleFinallyStateUpdate

  • ruleFinallyStateUpdate(): void

Protected ruleInvocationStateUpdate

  • ruleInvocationStateUpdate(ruleName: string, idxInCallingRule: number): void

Private saveRecogState

Private shouldInRepetitionRecoveryBeTried

  • shouldInRepetitionRecoveryBeTried(expectTokAfterLastMatch?: Function, nextTokIdx?: number): boolean
  • Parameters

    • Optional expectTokAfterLastMatch: Function
    • Optional nextTokIdx: number

    Returns boolean

Private tryInRepetitionRecovery

  • tryInRepetitionRecovery(grammarRule: Function, grammarRuleArgs: any[], lookAheadFunc: function, expectedTokType: Function): void
  • Parameters

    • grammarRule: Function
    • grammarRuleArgs: any[]
    • lookAheadFunc: function
        • (): boolean
        • Returns boolean

    • expectedTokType: Function

    Returns void

Private tryInRuleRecovery

  • tryInRuleRecovery(expectedTokType: Function, follows: Function[]): Token

Static Protected performSelfAnalysis

  • performSelfAnalysis(classInstance: Parser): void

Generated using TypeDoc