Skip to content

Latest commit

 

History

History
1199 lines (747 loc) · 27.1 KB

_parser_mutable_antlr_parser_.mutableantlrparser.md

File metadata and controls

1199 lines (747 loc) · 27.1 KB

antlr4-helper > "parser/mutable-antlr-parser" > MutableAntlrParser

Class: MutableAntlrParser

The MutableAntlrParser allows for text manipulation at the rule and token level. Once the parse is complete the getText method enables one to see the complete text of all the changes.

example:

...
const parser = antlrHelper.createParser(factory);
...
parser.addExitRuleListener(ExpressionContext, (rule)=>{
    parser.setRuleText(rule, 'replaced expression');
    console.log(parser.getRuleText(rule));
});
parser.parse(text);
console.log(parser.getText());

class: MutableAntlrParser

Hierarchy

MutableAntlrParser

Implements

Index

Constructors

Properties

Methods


Constructors

constructor

new MutableAntlrParser(parser: AntlrParser): MutableAntlrParser

Provide an AntlrFactory to construct

Parameters:

Param Type Description
parser AntlrParser

Returns: MutableAntlrParser


Properties

<Private> functionalRuleParser

● functionalRuleParser: FunctionalRuleParser


<Private> parser

● parser: AntlrParser


<Private> ruleTable

● ruleTable: RuleTable


<Private> textBuffer

● textBuffer: TextBuffer


<Private> textChanged

● textChanged: boolean


<Private> tokenTable

● tokenTable: TokenTable


Methods

addCustomRuleValidator

addCustomRuleValidatorT(ruleClass: AntlrRuleClass<ParserRuleContext>, validator: function): void

Type parameters:

T : ParserRuleContext

Parameters:

Param Type
ruleClass AntlrRuleClass<ParserRuleContext>
validator function

Returns: void


addEnterRuleListener

addEnterRuleListenerT(ruleClass: AntlrRuleClass<ParserRuleContext>, listener: function): void

Add a listener for when the parser enters a given rule. See example below

example:

parser.addEnterRuleListener(ExpressionContext, (rule)=>{
    console.log("entering an expression rule");
});

Type parameters:

T : ParserRuleContext

Parameters:

Param Type Description
ruleClass AntlrRuleClass<ParserRuleContext> -
listener function

Returns: void


addExitRuleListener

addExitRuleListenerT(ruleClass: AntlrRuleClass<ParserRuleContext>, listener: function): void

Add a listener for when the parser exits a given rule. Within this listener is where you would typically call methods such as setRuleText, getRuleText ,setTokenText and getTokenText

See example below: example:

parser.addExitRuleListener(ExpressionContext, (rule)=>{
    console.log(parser.getRuleText(rule));
});

Type parameters:

T : ParserRuleContext

Parameters:

Param Type Description
ruleClass AntlrRuleClass<ParserRuleContext> -
listener function

Returns: void


addParserCompleteListener

addParserCompleteListener(listener: function): void

Parameters:

Param Type
listener function

Returns: void


addParserStartListener

addParserStartListener(listener: function): void

Parameters:

Param Type
listener function

Returns: void


addTokenListener

addTokenListener(listener: function): void

Parameters:

Param Type
listener function

Returns: void


addValidator

addValidator(ruleName: string, validator: function): void

Parameters:

Param Type
ruleName string
validator function

Returns: void


checkForErrors

checkForErrors(): void

Returns: void


comparePositions

comparePositions(a: object, b: object): number

Parameters:

Param Type
a object
b object

Returns: number


createRuleError

createRuleError(rule: ParserRuleContext): AntlrRuleError

Parameters:

Param Type
rule ParserRuleContext

Returns: AntlrRuleError


doesRuleExist

doesRuleExist(rule: ParserRuleContext): boolean

Parameters:

Param Type
rule ParserRuleContext

Returns: boolean


doesTokenExist

doesTokenExist(token: Token): boolean

Parameters:

Param Type
token Token

Returns: boolean


every

every(predicate: function): boolean

Parameters:

Param Type
predicate function

Returns: boolean


filter

filter(filterFunction: function): AntlrParser

Parameters:

Param Type
filterFunction function

Returns: AntlrParser


find

find(filterFunction: function): AntlrRuleWrapper

Parameters:

Param Type
filterFunction function

Returns: AntlrRuleWrapper


findAll

findAll(filterFunction: function): AntlrRuleWrapper[]

Parameters:

Param Type
filterFunction function

Returns: AntlrRuleWrapper[]


findLast

findLast(filterFunction: function): AntlrRuleWrapper

Parameters:

Param Type
filterFunction function

Returns: AntlrRuleWrapper


findRuleByName

findRuleByName(ruleName: string): AntlrRuleWrapper

Parameters:

Param Type
ruleName string

Returns: AntlrRuleWrapper


findRuleByPath

findRuleByPath(path: string): AntlrRuleWrapper

Parameters:

Param Type
path string

Returns: AntlrRuleWrapper


findRulesByName

findRulesByName(ruleName: string): AntlrRuleWrapper[]

Parameters:

Param Type
ruleName string

Returns: AntlrRuleWrapper[]


findRulesByPath

findRulesByPath(path: string): AntlrRuleWrapper[]

Parameters:

Param Type
path string

Returns: AntlrRuleWrapper[]


forEach

forEachT(eachFunction: function): void

Type parameters:

T

Parameters:

Param Type
eachFunction function

Returns: void


getAllRules

getAllRules(): AntlrRuleWrapper[]

Returns: AntlrRuleWrapper[]


getAllTokens

getAllTokens(): AntlrTokenWrapper[]

Returns: AntlrTokenWrapper[]


getCharacterAt

getCharacterAt(column: number, line: number): string

Parameters:

Param Type
column number
line number

Returns: string


getColumnCount

getColumnCount(line: number): number

Parameters:

Param Type
line number

Returns: number


getErrorRuleAt

getErrorRuleAt(column: number, line: number): AntlrRuleWrapper

Parameters:

Param Type
column number
line number

Returns: AntlrRuleWrapper


getErrors

getErrors(): AntlrRuleError[]

Returns: AntlrRuleError[]


getFactory

getFactory(): AntlrFactory

Returns: AntlrFactory


getInputStream

getInputStream(): InputStream

Returns: InputStream


getLine

getLine(line: number): string

Parameters:

Param Type
line number

Returns: string


getLineCount

getLineCount(): number

Returns: number


getRelevantError

getRelevantError(): AntlrRuleError

Returns: AntlrRuleError


getRoot

getRoot(): AntlrRuleWrapper

Returns: AntlrRuleWrapper


getRuleAt

getRuleAt(column: number, line: number): AntlrRuleWrapper

Parameters:

Param Type
column number
line number

Returns: AntlrRuleWrapper


getRuleBefore

getRuleBefore(rule: ParserRuleContext): ParserRuleContext

Parameters:

Param Type
rule ParserRuleContext

Returns: ParserRuleContext


getRuleName

getRuleName(rule: ParserRuleContext): string

Parameters:

Param Type
rule ParserRuleContext

Returns: string


getRuleParent

getRuleParent(rule: ParserRuleContext): ParserRuleContext

Parameters:

Param Type
rule ParserRuleContext

Returns: ParserRuleContext


getRulePositionTable

getRulePositionTable(): ParserRuleContext[][]

Returns: ParserRuleContext[][]


getRuleRange

getRuleRange(rule: ParserRuleContext): AntlrRange

Parameters:

Param Type
rule ParserRuleContext

Returns: AntlrRange


getRuleStack

getRuleStack(): ReadonlyArray<ParserRuleContext>

Returns: ReadonlyArray<ParserRuleContext>


getRuleText

getRuleText(rule: ParserRuleContext): string

Get the text of the completely parsed rule

Parameters:

Param Type Description
rule ParserRuleContext -

Returns: string


getRulesInLine

getRulesInLine(line: number): Set<AntlrRuleWrapper>

Parameters:

Param Type
line number

Returns: Set<AntlrRuleWrapper>


getSiblings

getSiblings(rule: ParserRuleContext): ParserRuleContext[]

Parameters:

Param Type
rule ParserRuleContext

Returns: ParserRuleContext[]


getText

getText(): string

Get the complete parsed and transformed text

Returns: string


getTextRange

getTextRange(range: AntlrRange): string

Parameters:

Param Type
range AntlrRange

Returns: string


getTokenAt

getTokenAt(column: number, line: number): AntlrTokenWrapper | undefined

Parameters:

Param Type
column number
line number

Returns: AntlrTokenWrapper | undefined


getTokenName

getTokenName(token: Token): string

Parameters:

Param Type
token Token

Returns: string


getTokenPositionTable

getTokenPositionTable(): Token[][]

Returns: Token[][]


getTokenRange

getTokenRange(token: Token): AntlrRange

Get the range of a given token, where the first object is the start position and the last is the end position

Parameters:

Param Type Description
token Token

Returns: AntlrRange


getTokenText

getTokenText(token: Token): string

Get the text of a given token

Parameters:

Param Type Description
token Token -

Returns: string


getTokensInLine

getTokensInLine(line: number): Set<Token>

Parameters:

Param Type
line number

Returns: Set<Token>


getWarnings

getWarnings(): AntlrRuleError[]

Returns: AntlrRuleError[]


hasErrorNode

hasErrorNode(ctx: ParserRuleContext): boolean

Parameters:

Param Type
ctx ParserRuleContext

Returns: boolean


hasErrors

hasErrors(): boolean

Returns: boolean


hasTextChanged

hasTextChanged(): boolean

Returns: boolean


hasWarnings

hasWarnings(): boolean

Returns: boolean


map

mapT(mapFunction: function): T[]

Type parameters:

T

Parameters:

Param Type
mapFunction function

Returns: T[]


onParseComplete

onParseComplete(callback: function): void

Parameters:

Param Type
callback function

Returns: void


onParseStart

onParseStart(callback: function): void

Parameters:

Param Type
callback function

Returns: void


onRuleEnter

onRuleEnter(ruleName: string, callback: function): void

Parameters:

Param Type
ruleName string
callback function

Returns: void


onRuleExit

onRuleExit(ruleName: string, callback: function): void

Parameters:

Param Type
ruleName string
callback function

Returns: void


parse

parse(input: string): ParserRuleContext

Parse the input string (The internal text model is changed on each parse)

Parameters:

Param Type Description
input string -

Returns: ParserRuleContext

  • the invoked root rule

reduce

reduceT(reduceFunction: function, accumulator: T): T

Type parameters:

T

Parameters:

Param Type
reduceFunction function
accumulator T

Returns: T


reparse

reparse(): ParserRuleContext

Returns: ParserRuleContext


replaceRange

replaceRange(range: AntlrRange, text: string): AntlrRange

Parameters:

Param Type
range AntlrRange
text string

Returns: AntlrRange


setLexer

setLexer(lexer: Lexer): void

Parameters:

Param Type
lexer Lexer

Returns: void


setRuleText

setRuleText(rule: ParserRuleContext, text: string): void

Change the text of the completely parsed rule

Parameters:

Param Type Description
rule ParserRuleContext -
text string

Returns: void


setTokenText

setTokenText(token: Token, text: string): void

Change the text of the token

Parameters:

Param Type Description
token Token -
text string

Returns: void


siblingsHaveNoErrors

siblingsHaveNoErrors(rule: ParserRuleContext): boolean

Parameters:

Param Type
rule ParserRuleContext

Returns: boolean