Skip to content

Commit

Permalink
Added support for parse tree pattern matchin
Browse files Browse the repository at this point in the history
Close #12 How to perform tree pattern matching?

Signed-off-by: Mike Lischke <mike@lischke-online.de>
  • Loading branch information
mike-lischke committed Feb 11, 2024
1 parent 1bf40fc commit 4c97fe8
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 12 deletions.
33 changes: 33 additions & 0 deletions src/Parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ import { RuleContext } from "./RuleContext.js";
import { TraceListener } from "./TraceListener.js";
import { ProfilingATNSimulator } from "./atn/ProfilingATNSimulator.js";
import type { IntStream } from "./IntStream.js";
import type { ParseTreePattern } from "./tree/pattern/ParseTreePattern.js";
import { Lexer } from "./Lexer.js";
import { ParseTreePatternMatcher } from "./tree/pattern/ParseTreePatternMatcher.js";

export interface IDebugPrinter {
println(s: string): void;
Expand Down Expand Up @@ -304,6 +307,36 @@ export abstract class Parser extends Recognizer<ParserATNSimulator> {
this.inputStream!.tokenSource.tokenFactory = factory;
}

/**
* The preferred method of getting a tree pattern. For example, here's a
* sample use:
*
* ```
* const t = parser.expr();
* const p = parser.compileParseTreePattern("<ID>+0", MyParser.RULE_expr);
* const m = p.match(t);
* const id = m.get("ID");
* ```
*/
public compileParseTreePattern(pattern: string, patternRuleIndex: number, lexer?: Lexer): ParseTreePattern {
if (!lexer) {
if (this.tokenStream !== null) {
const tokenSource = this.tokenStream.tokenSource;
if (tokenSource instanceof Lexer) {
lexer = tokenSource;
}
}
}

if (!lexer) {
throw new Error("Parser can't discover a lexer to use");
}

const m = new ParseTreePatternMatcher(lexer, this);

return m.compile(pattern, patternRuleIndex);
}

/**
* The ATN with bypass alternatives is expensive to create so we create it
* lazily.
Expand Down
20 changes: 8 additions & 12 deletions src/atn/ATNDeserializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
* can be found in the LICENSE.txt file in the project root.
*/

/* eslint-disable @typescript-eslint/naming-convention */

import { Token } from "../Token.js";
import { ATN } from "./ATN.js";
import { ATNType } from "./ATNType.js";
Expand Down Expand Up @@ -54,13 +52,6 @@ import { ATNState } from "./ATNState.js";
import { LexerAction } from "./LexerAction.js";
import { Transition } from "./Transition.js";

const initArray = <T>(length: number, value: T) => {
const tmp = new Array<T>(length - 1);
tmp[length - 1] = value;

return tmp.map(() => { return value; });
};

export class ATNDeserializer {
public static readonly SERIALIZED_VERSION = 4;

Expand Down Expand Up @@ -183,9 +174,12 @@ export class ATNDeserializer {
let i;
const ruleCount = this.readInt();
if (atn.grammarType === ATNType.LEXER) {
atn.ruleToTokenType = initArray(ruleCount, 0);
atn.ruleToTokenType = new Array(ruleCount);
atn.ruleToTokenType.fill(0);
}
atn.ruleToStartState = initArray<RuleStartState | null>(ruleCount, null);

atn.ruleToStartState = new Array(ruleCount);
atn.ruleToStartState.fill(null);
for (i = 0; i < ruleCount; i++) {
const s = this.readInt();
atn.ruleToStartState[i] = atn.states[s] as RuleStartState;
Expand All @@ -194,7 +188,9 @@ export class ATNDeserializer {
atn.ruleToTokenType[i] = tokenType;
}
}
atn.ruleToStopState = initArray<RuleStopState | null>(ruleCount, null);

atn.ruleToStopState = new Array(ruleCount);
atn.ruleToStopState.fill(null);
for (i = 0; i < atn.states.length; i++) {
const state = atn.states[i];
if (!(state instanceof RuleStopState)) {
Expand Down

0 comments on commit 4c97fe8

Please sign in to comment.