-
-
Notifications
You must be signed in to change notification settings - Fork 6.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: Simplify langium by abstracting common functions
- Loading branch information
1 parent
795baed
commit 11b60ce
Showing
7 changed files
with
78 additions
and
100 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
export * from './commonLexer.js'; | ||
export * from './commonValueConverters.js'; | ||
export * from './valueConverter.js'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import type { GrammarAST, Stream, TokenBuilderOptions } from 'langium'; | ||
import type { TokenType } from '../chevrotainWrapper.js'; | ||
|
||
import { DefaultTokenBuilder } from 'langium'; | ||
|
||
export class MermaidTokenBuilder extends DefaultTokenBuilder { | ||
private keywords: Set<string>; | ||
constructor(public _keywords: string[]) { | ||
super(); | ||
this.keywords = new Set<string>(_keywords); | ||
} | ||
|
||
protected override buildKeywordTokens( | ||
rules: Stream<GrammarAST.AbstractRule>, | ||
terminalTokens: TokenType[], | ||
options?: TokenBuilderOptions | ||
): TokenType[] { | ||
const tokenTypes: TokenType[] = super.buildKeywordTokens(rules, terminalTokens, options); | ||
tokenTypes.forEach((tokenType: TokenType): void => { | ||
if (this.keywords.has(tokenType.name) && tokenType.PATTERN !== undefined) { | ||
tokenType.PATTERN = new RegExp(tokenType.PATTERN.toString() + '(?!\\S)'); | ||
} | ||
}); | ||
return tokenTypes; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,7 @@ | ||
import type { GrammarAST, Stream, TokenBuilderOptions } from 'langium'; | ||
import { DefaultTokenBuilder } from 'langium'; | ||
import { MermaidTokenBuilder } from '../common/tokenBuilder.js'; | ||
|
||
import type { TokenType } from '../chevrotainWrapper.js'; | ||
|
||
export class InfoTokenBuilder extends DefaultTokenBuilder { | ||
protected override buildKeywordTokens( | ||
rules: Stream<GrammarAST.AbstractRule>, | ||
terminalTokens: TokenType[], | ||
options?: TokenBuilderOptions | ||
): TokenType[] { | ||
const tokenTypes: TokenType[] = super.buildKeywordTokens(rules, terminalTokens, options); | ||
// to restrict users, they mustn't have any non-whitespace characters after the keyword. | ||
tokenTypes.forEach((tokenType: TokenType): void => { | ||
if ( | ||
(tokenType.name === 'info' || tokenType.name === 'showInfo') && | ||
tokenType.PATTERN !== undefined | ||
) { | ||
tokenType.PATTERN = new RegExp(tokenType.PATTERN.toString() + '(?!\\S)'); | ||
} | ||
}); | ||
return tokenTypes; | ||
export class InfoTokenBuilder extends MermaidTokenBuilder { | ||
constructor() { | ||
super(['info', 'showInfo']); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,7 @@ | ||
import type { GrammarAST, Stream, TokenBuilderOptions } from 'langium'; | ||
import { DefaultTokenBuilder } from 'langium'; | ||
import { MermaidTokenBuilder } from '../common/tokenBuilder.js'; | ||
|
||
import type { TokenType } from '../chevrotainWrapper.js'; | ||
|
||
export class PieTokenBuilder extends DefaultTokenBuilder { | ||
protected override buildKeywordTokens( | ||
rules: Stream<GrammarAST.AbstractRule>, | ||
terminalTokens: TokenType[], | ||
options?: TokenBuilderOptions | ||
): TokenType[] { | ||
const tokenTypes: TokenType[] = super.buildKeywordTokens(rules, terminalTokens, options); | ||
tokenTypes.forEach((tokenType: TokenType): void => { | ||
if ( | ||
(tokenType.name === 'pie' || tokenType.name === 'showData') && | ||
tokenType.PATTERN !== undefined | ||
) { | ||
tokenType.PATTERN = new RegExp(tokenType.PATTERN.toString() + '(?!\\S)'); | ||
} | ||
}); | ||
return tokenTypes; | ||
export class PieTokenBuilder extends MermaidTokenBuilder { | ||
constructor() { | ||
super(['pie', 'showData']); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,49 +1,17 @@ | ||
import type { CstNode, GrammarAST, ValueType } from 'langium'; | ||
import { DefaultValueConverter } from 'langium'; | ||
import { MermaidValueConverter } from '../common/valueConverter.js'; | ||
|
||
import { CommonValueConverter } from '../common/commonValueConverters.js'; | ||
|
||
export class PieValueConverter extends DefaultValueConverter { | ||
protected override runConverter( | ||
rule: GrammarAST.AbstractRule, | ||
input: string, | ||
cstNode: CstNode | ||
): ValueType { | ||
let value: ValueType | undefined = CommonValueConverter.customRunConverter( | ||
rule, | ||
input, | ||
cstNode | ||
); | ||
if (value === undefined) { | ||
value = PieValueConverter.customRunConverter(rule, input, cstNode); | ||
} | ||
|
||
if (value === undefined) { | ||
return super.runConverter(rule, input, cstNode); | ||
} | ||
return value; | ||
} | ||
|
||
/** | ||
* A method contains convert logic to be used by class itself or `MermaidValueConverter`. | ||
* | ||
* @param rule - Parsed rule. | ||
* @param input - Matched string. | ||
* @param _cstNode - Node in the Concrete Syntax Tree (CST). | ||
* @returns converted the value if it's pie rule or `null` if it's not. | ||
*/ | ||
public static customRunConverter( | ||
export class PieValueConverter extends MermaidValueConverter { | ||
override runCustomConverter( | ||
rule: GrammarAST.AbstractRule, | ||
input: string, | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
_cstNode: CstNode | ||
): ValueType | undefined { | ||
if (rule.name === 'PIE_SECTION_LABEL') { | ||
return input | ||
.replace(/"/g, '') | ||
.trim() | ||
.replaceAll(/[\t ]{2,}/gm, ' '); | ||
if (rule.name !== 'PIE_SECTION_LABEL') { | ||
return undefined; | ||
} | ||
return undefined; | ||
|
||
return input.replace(/"/g, '').trim(); | ||
} | ||
} |