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

Properties

Protected RULE_OCCURRENCE_STACK

RULE_OCCURRENCE_STACK: number[] = []

Protected RULE_STACK

RULE_STACK: string[] = []

Protected _errors

_errors: IRecognitionException[] = []

Protected _input

_input: Token[] = []

Private _productions

_productions: HashTable<Rule> = new HashTable<gast.Rule>()

Only used internally for storing productions as they are built for the first time. The final productions should be accessed from the static cache.

Private classLAFuncs

classLAFuncs: any

Protected className

className: string

Private definedRulesNames

definedRulesNames: string[] = []

Private definitionErrors

definitionErrors: IParserDefinitionError[]

Protected dynamicTokensEnabled

dynamicTokensEnabled: boolean

Private firstAfterRepMap

firstAfterRepMap: any

Protected ignoredIssues

ignoredIssues: IgnoredParserIssues

Protected inputIdx

inputIdx: number = -1

Protected isBackTrackingStack

isBackTrackingStack: Array<any> = []

Protected maxLookahead

maxLookahead: number

Protected recoveryEnabled

recoveryEnabled: boolean

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 ruleShortNameIdx

ruleShortNameIdx: number = 1

Protected savedTokenIdx

savedTokenIdx: number = -1

Private shortRuleNameToFull

shortRuleNameToFull: HashTable<string> = new HashTable<string>()

Private tokenClassIdentityFunc

tokenClassIdentityFunc: TokenClassIdentityFunc

Private tokenInstanceIdentityFunc

tokenInstanceIdentityFunc: TokenInstanceIdentityFunc

Private tokenMatcher

tokenMatcher: TokenMatcher

Protected tokensMap

tokensMap: object = undefined

Type declaration

  • [fqn: string]: Function

Static DEFER_DEFINITION_ERRORS_HANDLING

DEFER_DEFINITION_ERRORS_HANDLING: boolean = false

Static NO_RESYNC

NO_RESYNC: boolean = false

Accessors

errors

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

    • predicateOrAction: Predicate | GrammarAction

      The predicate / gate function that implements the constraint on the grammar or the grammar action to invoke at least once.

    • 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

    • separator: TokenConstructor

      The Token class which will be used as a separator between repetitions.

    • action: GrammarAction | string
    • Optional errMsg: string

    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

Protected CONSUME1

  • 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 ISimpleTokenOrIToken

    • The consumed token.

Protected CONSUME2

Protected CONSUME3

Protected CONSUME4

Protected CONSUME5

Protected LA

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(predicateFunc, () => { this.CONSUME(Comma}; this.CONSUME(Digit});

    The 'predicateFunc' in the long form can be used to add constraints (none grammar related) taking another iteration.

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

    Parameters

    • predicateOrAction: Predicate | GrammarAction

      The predicate / gate function that implements the constraint on the grammar or the grammar action to optionally invoke multiple times.

    • 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.

    Example:

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

    Note that for the purposes of deciding on whether or not another iteration exists Only a single Token is examined (The separator). Therefore if the grammar being implemented is so "crazy" to require multiple tokens to identify an item separator please use the basic DSL methods to implement it.

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

    Parameters

    Returns Token[]

    • The consumed separator Tokens.

Protected MANY_SEP2

Protected MANY_SEP3

Protected MANY_SEP4

Protected MANY_SEP5

Protected NEXT_TOKEN

  • Convenience method equivalent to LA(1) It is no longer used directly in chevrotain due to performance considerations (avoid the need for inlining optimizations).

    But it is maintained for backward compatibility reasons.

    deprecated

    Returns ISimpleTokenOrIToken

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(predicateFunc, ()=>{ this.CONSUME(Digit});

    The 'predicateFunc' in the long form can be used to add constraints (none grammar related) to optionally invoking the grammar action.

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

    Parameters

    • predicateOrAction: Predicate | GrammarAction

      The predicate / gate function that implements the constraint on the grammar or the grammar action to optionally invoke once.

    • 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: IAnyOrAlt[], errMsgTypes?: string): T

Protected OR1

  • OR1<T>(alts: IAnyOrAlt[], errMsgTypes?: string): 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([

          {GATE: predicateFunc1, ALT:()=>{this.CONSUME(One)}},
          {GATE: predicateFuncX, ALT:()=>{this.CONSUME(Two)}},
          {GATE: predicateFuncX, ALT:()=>{this.CONSUME(Three)}},
       ], "a number")
      

    They can also be mixed: mixed: this.OR([ {GATE: predicateFunc1, ALT:()=>{this.CONSUME(One)}}, {ALT:()=>{this.CONSUME(Two)}}, {ALT:()=>{this.CONSUME(Three)}} ], "a number")

    The 'predicateFuncX' in the long form can be used to add constraints (none grammar related) to choosing the alternative.

    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: IAnyOrAlt[]
    • Optional errMsgTypes: string

    Returns T

    • The result of invoking the chosen alternative.

Protected OR2

  • OR2<T>(alts: IAnyOrAlt[], errMsgTypes?: string): T

Protected OR3

  • OR3<T>(alts: IAnyOrAlt[], errMsgTypes?: string): T

Protected OR4

  • OR4<T>(alts: IAnyOrAlt[], errMsgTypes?: string): T

Protected OR5

  • OR5<T>(alts: IAnyOrAlt[], errMsgTypes?: string): T

Protected OVERRIDE_RULE

  • OVERRIDE_RULE<T>(name: string, impl: function, config?: IRuleConfig<T>): 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

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

          • Rest ...implArgs: any[]

          Returns T

    • Default value config: IRuleConfig<T> = DEFAULT_RULE_CONFIG

    Returns function

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

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

        Returns T

Protected RULE

  • RULE<T>(name: string, implementation: function, config?: IRuleConfig<T>): function
  • Type parameters

    • T

    Parameters

    • name: string

      The name of the rule.

    • implementation: function

      The implementation of the rule.

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

          • Rest ...implArgs: any[]

          Returns T

    • Default value config: IRuleConfig<T> = DEFAULT_RULE_CONFIG

    Returns function

    • The parsing rule which is the production implementation wrapped with the parsing logic that handles
                    Parser state / error recovery&reporting/ ...
      
      • (idxInCallingRule?: number, ...args: any[]): T
      • Parameters

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

        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

  • atLeastOneSepFirstInternal(prodFunc: Function, prodOccurrence: number, separator: TokenConstructor, action: GrammarAction | string, userDefinedErrMsg?: string): Token[]

Private attemptInRepetitionRecovery

  • attemptInRepetitionRecovery(prodFunc: Function, args: any[], lookaheadFunc: function, dslMethodIdx: number, prodOccurrence: number, nextToksWalker: AbstractNextTerminalAfterProductionWalker): void
  • Parameters

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

    • dslMethodIdx: number
    • prodOccurrence: number
    • nextToksWalker: AbstractNextTerminalAfterProductionWalker

    Returns void

Private buildFullFollowKeyStack

Private canPerformInRuleRecovery

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

Private canRecoverWithSingleTokenDeletion

  • canRecoverWithSingleTokenDeletion(expectedTokType: Function): boolean

Private canRecoverWithSingleTokenInsertion

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

Protected canTokenTypeBeInsertedInRecovery

  • 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

    Returns boolean

Protected consumeInternal

  • 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 ISimpleTokenOrIToken

    • The consumed Token.

Private consumeInternalOptimized

Protected consumeInternalWithTryCatch

Protected consumeToken

  • consumeToken(): void

Private defineRule

  • defineRule<T>(ruleName: string, impl: function, config: IRuleConfig<T>): function
  • Type parameters

    • T

    Parameters

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

          • Rest ...implArgs: any[]

          Returns T

    • config: IRuleConfig<T>

    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

Protected getCurrRuleFullName

  • getCurrRuleFullName(): string

Protected getCurrentGrammarPath

Private getFollowSetFromFollowKey

  • getFollowSetFromFollowKey(followKey: IFollowKey): Function[]

Private getFollowsForInRuleRecovery

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

getGAstProductions

  • getGAstProductions(): HashTable<Rule>

Protected getHumanReadableRuleStack

  • getHumanReadableRuleStack(): Array<string>

Private getKeyForAutomaticLookahead

  • getKeyForAutomaticLookahead(dslMethodIdx: number, occurrence: number): number

Private getLookaheadFuncFor

  • getLookaheadFuncFor<T>(key: number, occurrence: number, laFuncBuilder: function, maxLookahead: number): function
  • Type parameters

    • T

    Parameters

    • key: number
    • occurrence: number
    • laFuncBuilder: function
        • (number: any, rule: any, k: any, tokenMatcher: any, tokenClassIdentityFunc: any, tokenInstanceIdentityFunc: any, dynamicTokensEnabled: any): function
        • Parameters

          • number: any
          • rule: any
          • k: any
          • tokenMatcher: any
          • tokenClassIdentityFunc: any
          • tokenInstanceIdentityFunc: any
          • dynamicTokensEnabled: any

          Returns function

            • (): T
            • Returns T

    • maxLookahead: number

    Returns function

      • (): T
      • Returns T

Private getLookaheadFuncForAtLeastOne

  • getLookaheadFuncForAtLeastOne(occurrence: number): function

Private getLookaheadFuncForAtLeastOneSep

  • getLookaheadFuncForAtLeastOneSep(occurrence: number): function

Private getLookaheadFuncForMany

  • getLookaheadFuncForMany(occurrence: number): function

Private getLookaheadFuncForManySep

  • getLookaheadFuncForManySep(occurrence: number): function

Private getLookaheadFuncForOption

  • getLookaheadFuncForOption(occurrence: number): function

Private getLookaheadFuncForOr

  • getLookaheadFuncForOr(occurrence: number, alts: IAnyOrAlt[]): function

Protected getMisMatchTokenErrorMessage

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

    • expectedTokType: Function

      The Class of the expected Token.

    • actualToken: ISimpleTokenOrIToken

      The actual unexpected (mismatched) Token instance encountered.

    Returns string

    • The error message saved as part of a MismatchedTokenException.

Protected getNextPossibleTokenTypes

Protected getTokenToInsert

  • 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

    Returns Token

isAtEndOfInput

  • isAtEndOfInput(): boolean

Protected isBackTracking

  • isBackTracking(): boolean

Private isInCurrentRuleReSyncSet

  • isInCurrentRuleReSyncSet(token: Function): boolean

Private manyInternal

Private manySepFirstInternal

Private optionInternal

Private orInternal

  • orInternal<T>(alts: IAnyOrAlt[], errMsgTypes: string, occurrence: number): T

Private raiseEarlyExitException

  • raiseEarlyExitException(occurrence: number, prodType: PROD_TYPE, userDefinedErrMsg: string): void

Private raiseNoAltException

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

Private reSyncTo

  • reSyncTo(tokClass: Function): Token[]

Private reloadRecogState

Private repetitionSepSecondInternal

  • repetitionSepSecondInternal(prodOccurrence: number, separator: TokenConstructor, separatorLookAheadFunc: function, action: GrammarAction, separatorsResult: ISimpleTokenOrIToken[], nextTerminalAfterWalker: AbstractNextTerminalAfterProductionWalker): void

reset

  • reset(): void
  • Resets the parser state, should be overridden for custom parsers which "carry" additional state. When overriding, remember to also invoke the super implementation!

    Returns void

Protected resetLexerState

  • resetLexerState(): void

Protected restoreLexerState

  • restoreLexerState(): void

Protected ruleFinallyStateUpdate

  • ruleFinallyStateUpdate(): void

Protected ruleInvocationStateUpdate

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

Protected saveLexerState

  • saveLexerState(): void

Private saveRecogState

Protected shortRuleNameToFullName

  • shortRuleNameToFullName(shortName: string): string

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

Static Protected performSelfAnalysis

  • performSelfAnalysis(parserInstance: Parser): void

Generated using TypeDoc