antlr4-helper > "parser/mutable-antlr-parser" > 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
MutableAntlrParser
- addCustomRuleValidator
- addEnterRuleListener
- addExitRuleListener
- addParserCompleteListener
- addParserStartListener
- addTokenListener
- addValidator
- checkForErrors
- comparePositions
- createRuleError
- doesRuleExist
- doesTokenExist
- every
- filter
- find
- findAll
- findLast
- findRuleByName
- findRuleByPath
- findRulesByName
- findRulesByPath
- forEach
- getAllRules
- getAllTokens
- getCharacterAt
- getColumnCount
- getErrorRuleAt
- getErrors
- getFactory
- getInputStream
- getLine
- getLineCount
- getRelevantError
- getRoot
- getRuleAt
- getRuleBefore
- getRuleName
- getRuleParent
- getRulePositionTable
- getRuleRange
- getRuleStack
- getRuleText
- getRulesInLine
- getSiblings
- getText
- getTextRange
- getTokenAt
- getTokenName
- getTokenPositionTable
- getTokenRange
- getTokenText
- getTokensInLine
- getWarnings
- hasErrorNode
- hasErrors
- hasTextChanged
- hasWarnings
- map
- onParseComplete
- onParseStart
- onRuleEnter
- onRuleExit
- parse
- reduce
- reparse
- replaceRange
- setLexer
- setRuleText
- setTokenText
- siblingsHaveNoErrors
⊕ new MutableAntlrParser(parser: AntlrParser): MutableAntlrParser
Provide an AntlrFactory to construct
Parameters:
Param | Type | Description |
---|---|---|
parser | AntlrParser |
Returns: MutableAntlrParser
● functionalRuleParser: FunctionalRuleParser
● parser: AntlrParser
● ruleTable: RuleTable
● textBuffer: TextBuffer
● textChanged: boolean
● tokenTable: TokenTable
▸ addCustomRuleValidatorT(ruleClass: AntlrRuleClass<ParserRuleContext
>, validator: function
): void
Type parameters:
Parameters:
Param | Type |
---|---|
ruleClass | AntlrRuleClass<ParserRuleContext > |
validator | function |
Returns: void
▸ 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:
Parameters:
Param | Type | Description |
---|---|---|
ruleClass | AntlrRuleClass<ParserRuleContext > |
- |
listener | function |
Returns: void
▸ 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:
Parameters:
Param | Type | Description |
---|---|---|
ruleClass | AntlrRuleClass<ParserRuleContext > |
- |
listener | function |
Returns: void
▸ addParserCompleteListener(listener: function
): void
Parameters:
Param | Type |
---|---|
listener | function |
Returns: void
▸ addParserStartListener(listener: function
): void
Parameters:
Param | Type |
---|---|
listener | function |
Returns: void
▸ addTokenListener(listener: function
): void
Parameters:
Param | Type |
---|---|
listener | function |
Returns: void
▸ addValidator(ruleName: string
, validator: function
): void
Parameters:
Param | Type |
---|---|
ruleName | string |
validator | function |
Returns: void
▸ checkForErrors(): void
Returns: void
▸ comparePositions(a: object
, b: object
): number
Parameters:
Param | Type |
---|---|
a | object |
b | object |
Returns: number
▸ createRuleError(rule: ParserRuleContext
): AntlrRuleError
Parameters:
Param | Type |
---|---|
rule | ParserRuleContext |
Returns: AntlrRuleError
▸ doesRuleExist(rule: ParserRuleContext
): boolean
Parameters:
Param | Type |
---|---|
rule | ParserRuleContext |
Returns: boolean
▸ doesTokenExist(token: Token
): boolean
Parameters:
Param | Type |
---|---|
token | Token |
Returns: boolean
▸ every(predicate: function
): boolean
Parameters:
Param | Type |
---|---|
predicate | function |
Returns: boolean
▸ filter(filterFunction: function
): AntlrParser
Parameters:
Param | Type |
---|---|
filterFunction | function |
Returns: AntlrParser
▸ find(filterFunction: function
): AntlrRuleWrapper
Parameters:
Param | Type |
---|---|
filterFunction | function |
Returns: AntlrRuleWrapper
▸ findAll(filterFunction: function
): AntlrRuleWrapper[]
Parameters:
Param | Type |
---|---|
filterFunction | function |
Returns: AntlrRuleWrapper[]
▸ findLast(filterFunction: function
): AntlrRuleWrapper
Parameters:
Param | Type |
---|---|
filterFunction | function |
Returns: AntlrRuleWrapper
▸ findRuleByName(ruleName: string
): AntlrRuleWrapper
Parameters:
Param | Type |
---|---|
ruleName | string |
Returns: AntlrRuleWrapper
▸ findRuleByPath(path: string
): AntlrRuleWrapper
Parameters:
Param | Type |
---|---|
path | string |
Returns: AntlrRuleWrapper
▸ findRulesByName(ruleName: string
): AntlrRuleWrapper[]
Parameters:
Param | Type |
---|---|
ruleName | string |
Returns: AntlrRuleWrapper[]
▸ findRulesByPath(path: string
): AntlrRuleWrapper[]
Parameters:
Param | Type |
---|---|
path | string |
Returns: AntlrRuleWrapper[]
▸ forEachT(eachFunction: function
): void
Type parameters:
Parameters:
Param | Type |
---|---|
eachFunction | function |
Returns: void
▸ getAllRules(): AntlrRuleWrapper[]
Returns: AntlrRuleWrapper[]
▸ getAllTokens(): AntlrTokenWrapper[]
Returns: AntlrTokenWrapper[]
▸ getCharacterAt(column: number
, line: number
): string
Parameters:
Param | Type |
---|---|
column | number |
line | number |
Returns: string
▸ getColumnCount(line: number
): number
Parameters:
Param | Type |
---|---|
line | number |
Returns: number
▸ getErrorRuleAt(column: number
, line: number
): AntlrRuleWrapper
Parameters:
Param | Type |
---|---|
column | number |
line | number |
Returns: AntlrRuleWrapper
▸ getErrors(): AntlrRuleError[]
Returns: AntlrRuleError[]
▸ getFactory(): AntlrFactory
Returns: AntlrFactory
▸ getInputStream(): InputStream
Returns: InputStream
▸ getLine(line: number
): string
Parameters:
Param | Type |
---|---|
line | number |
Returns: string
▸ getLineCount(): number
Returns: number
▸ getRelevantError(): AntlrRuleError
Returns: AntlrRuleError
▸ getRoot(): AntlrRuleWrapper
Returns: AntlrRuleWrapper
▸ getRuleAt(column: number
, line: number
): AntlrRuleWrapper
Parameters:
Param | Type |
---|---|
column | number |
line | number |
Returns: AntlrRuleWrapper
▸ getRuleBefore(rule: ParserRuleContext
): ParserRuleContext
Parameters:
Param | Type |
---|---|
rule | ParserRuleContext |
Returns: ParserRuleContext
▸ getRuleName(rule: ParserRuleContext
): string
Parameters:
Param | Type |
---|---|
rule | ParserRuleContext |
Returns: string
▸ getRuleParent(rule: ParserRuleContext
): ParserRuleContext
Parameters:
Param | Type |
---|---|
rule | ParserRuleContext |
Returns: ParserRuleContext
▸ getRulePositionTable(): ParserRuleContext
[][]
Returns: ParserRuleContext
[][]
▸ getRuleRange(rule: ParserRuleContext
): AntlrRange
Parameters:
Param | Type |
---|---|
rule | ParserRuleContext |
Returns: AntlrRange
▸ getRuleStack(): ReadonlyArray
<ParserRuleContext
>
Returns: ReadonlyArray
<ParserRuleContext
>
▸ getRuleText(rule: ParserRuleContext
): string
Get the text of the completely parsed rule
Parameters:
Param | Type | Description |
---|---|---|
rule | ParserRuleContext |
- |
Returns: string
▸ getRulesInLine(line: number
): Set
<AntlrRuleWrapper>
Parameters:
Param | Type |
---|---|
line | number |
Returns: Set
<AntlrRuleWrapper>
▸ getSiblings(rule: ParserRuleContext
): ParserRuleContext
[]
Parameters:
Param | Type |
---|---|
rule | ParserRuleContext |
Returns: ParserRuleContext
[]
▸ getText(): string
Get the complete parsed and transformed text
Returns: string
▸ getTextRange(range: AntlrRange): string
Parameters:
Param | Type |
---|---|
range | AntlrRange |
Returns: string
▸ getTokenAt(column: number
, line: number
): AntlrTokenWrapper | undefined
Parameters:
Param | Type |
---|---|
column | number |
line | number |
Returns: AntlrTokenWrapper | undefined
▸ getTokenName(token: Token
): string
Parameters:
Param | Type |
---|---|
token | Token |
Returns: string
▸ getTokenPositionTable(): Token
[][]
Returns: Token
[][]
▸ 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(token: Token
): string
Get the text of a given token
Parameters:
Param | Type | Description |
---|---|---|
token | Token |
- |
Returns: string
▸ getTokensInLine(line: number
): Set
<Token
>
Parameters:
Param | Type |
---|---|
line | number |
Returns: Set
<Token
>
▸ getWarnings(): AntlrRuleError[]
Returns: AntlrRuleError[]
▸ hasErrorNode(ctx: ParserRuleContext
): boolean
Parameters:
Param | Type |
---|---|
ctx | ParserRuleContext |
Returns: boolean
▸ hasErrors(): boolean
Returns: boolean
▸ hasTextChanged(): boolean
Returns: boolean
▸ hasWarnings(): boolean
Returns: boolean
▸ mapT(mapFunction: function
): T
[]
Type parameters:
Parameters:
Param | Type |
---|---|
mapFunction | function |
Returns: T
[]
▸ onParseComplete(callback: function
): void
Parameters:
Param | Type |
---|---|
callback | function |
Returns: void
▸ onParseStart(callback: function
): void
Parameters:
Param | Type |
---|---|
callback | function |
Returns: void
▸ onRuleEnter(ruleName: string
, callback: function
): void
Parameters:
Param | Type |
---|---|
ruleName | string |
callback | function |
Returns: void
▸ onRuleExit(ruleName: string
, callback: function
): void
Parameters:
Param | Type |
---|---|
ruleName | string |
callback | function |
Returns: void
▸ 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
▸ reduceT(reduceFunction: function
, accumulator: T
): T
Type parameters:
Parameters:
Param | Type |
---|---|
reduceFunction | function |
accumulator | T |
Returns: T
▸ reparse(): ParserRuleContext
Returns: ParserRuleContext
▸ replaceRange(range: AntlrRange, text: string
): AntlrRange
Parameters:
Param | Type |
---|---|
range | AntlrRange |
text | string |
Returns: AntlrRange
▸ setLexer(lexer: Lexer
): void
Parameters:
Param | Type |
---|---|
lexer | Lexer |
Returns: void
▸ 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(token: Token
, text: string
): void
Change the text of the token
Parameters:
Param | Type | Description |
---|---|---|
token | Token |
- |
text | string |
Returns: void
▸ siblingsHaveNoErrors(rule: ParserRuleContext
): boolean
Parameters:
Param | Type |
---|---|
rule | ParserRuleContext |
Returns: boolean