-
-
Notifications
You must be signed in to change notification settings - Fork 7.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: Simplify TokenBuilder and ValueConverter
- Loading branch information
1 parent
52b33f6
commit 271b779
Showing
10 changed files
with
117 additions
and
92 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,14 +1,19 @@ | ||
interface Common { | ||
accDescr?: string; | ||
accTitle?: string; | ||
title?: string; | ||
} | ||
|
||
fragment TitleAndAccessibilities: | ||
((accDescr=ACC_DESCR | accTitle=ACC_TITLE | title=TITLE) NEWLINE+)+ | ||
; | ||
|
||
terminal NEWLINE: /\r?\n/; | ||
terminal ACC_DESCR: /accDescr(?:[\t ]*:[\t ]*[^\n\r]*?(?=%%)|\s*{[^}]*})|accDescr(?:[\t ]*:[\t ]*[^\n\r]*|\s*{[^}]*})/; | ||
terminal ACC_TITLE: /accTitle[\t ]*:[\t ]*[^\n\r]*?(?=%%)|accTitle[\t ]*:[\t ]*[^\n\r]*/; | ||
terminal TITLE: /title(?:[\t ]+[^\n\r]*?|)(?=%%)|title(?:[\t ]+[^\n\r]*|)/; | ||
terminal ACC_DESCR: /accDescr([\t ]*:[^\n\r]*(?=%%)|\s*{[^}]*})|accDescr([\t ]*:[^\n\r]*|\s*{[^}]*})/; | ||
terminal ACC_TITLE: /accTitle[\t ]*:[^\n\r]*(?=%%)|accTitle[\t ]*:[^\n\r]*/; | ||
terminal TITLE: /title([\t ][^\n\r]*|)(?=%%)|title([\t ][^\n\r]*|)/; | ||
|
||
hidden terminal WHITESPACE: /[\t ]+/; | ||
// TODO: add YAML_COMMENT hidden rule without interfere actual grammar | ||
hidden terminal YAML: /---[\t ]*\r?\n[\S\s]*?---[\t ]*(?!.)/; | ||
hidden terminal DIRECTIVE: /[\t ]*%%{[\S\s]*?}%%\s*/; | ||
hidden terminal SINGLE_LINE_COMMENT: /[\t ]*%%[^\n\r]*/; |
74 changes: 0 additions & 74 deletions
74
packages/parser/src/language/common/commonValueConverters.ts
This file was deleted.
Oops, something went wrong.
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,3 @@ | ||
export * from './commonLexer.js'; | ||
export * from './commonValueConverters.js'; | ||
export * from './lexer.js'; | ||
export * from './tokenBuilder.js'; | ||
export { MermaidValueConverter } from './valueConverter.js'; |
File renamed without changes.
File renamed without changes.
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 |
---|---|---|
@@ -0,0 +1,82 @@ | ||
/* eslint-disable @typescript-eslint/no-unused-vars */ | ||
import type { CstNode, GrammarAST, ValueType } from 'langium'; | ||
import { DefaultValueConverter } from 'langium'; | ||
|
||
import { accessibilityDescrRegex, accessibilityTitleRegex, titleRegex } from './matcher.js'; | ||
|
||
const rulesRegexes: Record<string, RegExp> = { | ||
ACC_DESCR: accessibilityDescrRegex, | ||
ACC_TITLE: accessibilityTitleRegex, | ||
TITLE: titleRegex, | ||
}; | ||
|
||
export abstract class MermaidValueConverter extends DefaultValueConverter { | ||
/** | ||
* A method contains convert logic to be used by class. | ||
* | ||
* @param rule - Parsed rule. | ||
* @param input - Matched string. | ||
* @param cstNode - Node in the Concrete Syntax Tree (CST). | ||
* @returns converted the value if it's available or `undefined` if it's not. | ||
*/ | ||
protected abstract runCustomConverter( | ||
rule: GrammarAST.AbstractRule, | ||
input: string, | ||
cstNode: CstNode | ||
): ValueType | undefined; | ||
|
||
protected override runConverter( | ||
rule: GrammarAST.AbstractRule, | ||
input: string, | ||
cstNode: CstNode | ||
): ValueType { | ||
let value: ValueType | undefined = this.runCommonConverter(rule, input, cstNode); | ||
|
||
if (value === undefined) { | ||
value = this.runCustomConverter(rule, input, cstNode); | ||
} | ||
if (value === undefined) { | ||
return super.runConverter(rule, input, cstNode); | ||
} | ||
|
||
return value; | ||
} | ||
|
||
private runCommonConverter( | ||
rule: GrammarAST.AbstractRule, | ||
input: string, | ||
_cstNode: CstNode | ||
): ValueType | undefined { | ||
const regex: RegExp | undefined = rulesRegexes[rule.name]; | ||
if (regex === undefined) { | ||
return undefined; | ||
} | ||
const match = regex.exec(input); | ||
if (match === null) { | ||
return undefined; | ||
} | ||
// single line title, accTitle, accDescr | ||
if (match[1] !== undefined) { | ||
return match[1].trim().replace(/[\t ]{2,}/gm, ' '); | ||
} | ||
// multi line accDescr | ||
if (match[2] !== undefined) { | ||
return match[2] | ||
.replace(/^\s*/gm, '') | ||
.replace(/\s+$/gm, '') | ||
.replace(/[\t ]{2,}/gm, ' ') | ||
.replace(/[\n\r]{2,}/gm, '\n'); | ||
} | ||
return undefined; | ||
} | ||
} | ||
|
||
export class CommonValueConverter extends MermaidValueConverter { | ||
protected runCustomConverter( | ||
_rule: GrammarAST.AbstractRule, | ||
_input: string, | ||
_cstNode: CstNode | ||
): ValueType | undefined { | ||
return undefined; | ||
} | ||
} |
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 +1 @@ | ||
export * from './infoModule.js'; | ||
export * from './module.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
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,7 @@ | ||
import { MermaidTokenBuilder } from '../common/index.js'; | ||
|
||
export class InfoTokenBuilder extends MermaidTokenBuilder { | ||
public constructor() { | ||
super(['info', 'showInfo']); | ||
} | ||
} |