mirror of
https://github.com/mermaid-js/mermaid.git
synced 2025-09-16 05:49:43 +02:00
chore(parser): rearrange rules to make imported rules at the beginning
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import type { GrammarAST, Stream, TokenBuilderOptions } from 'langium';
|
||||
import type { TokenType } from 'chevrotain';
|
||||
import type { TokenType, TokenVocabulary } from 'chevrotain';
|
||||
|
||||
import { DefaultTokenBuilder } from 'langium';
|
||||
|
||||
@@ -11,6 +11,14 @@ export abstract class MermaidTokenBuilder extends DefaultTokenBuilder {
|
||||
this.keywords = new Set<string>(keywords);
|
||||
}
|
||||
|
||||
public override buildTokens(
|
||||
grammar: GrammarAST.Grammar,
|
||||
options?: TokenBuilderOptions | undefined
|
||||
): TokenVocabulary {
|
||||
this.rearrangeRules(grammar.rules);
|
||||
return super.buildTokens(grammar, options);
|
||||
}
|
||||
|
||||
protected override buildKeywordTokens(
|
||||
rules: Stream<GrammarAST.AbstractRule>,
|
||||
terminalTokens: TokenType[],
|
||||
@@ -25,4 +33,12 @@ export abstract class MermaidTokenBuilder extends DefaultTokenBuilder {
|
||||
});
|
||||
return tokenTypes;
|
||||
}
|
||||
|
||||
private rearrangeRules(rules: GrammarAST.AbstractRule[]): GrammarAST.AbstractRule[] {
|
||||
const pivotIndex = rules.findIndex((rule) => rule.name === 'TitleAndAccessibilities');
|
||||
if (pivotIndex === -1) {
|
||||
return rules;
|
||||
}
|
||||
return [...rules.slice(pivotIndex), ...rules.slice(0, pivotIndex)];
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user