Skip to content

Commit

Permalink
refactor: Simplify TokenBuilder and ValueConverter
Browse files Browse the repository at this point in the history
  • Loading branch information
sidharthv96 committed Sep 14, 2023
1 parent 52b33f6 commit 271b779
Show file tree
Hide file tree
Showing 10 changed files with 117 additions and 92 deletions.
13 changes: 9 additions & 4 deletions packages/parser/src/language/common/common.langium
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 packages/parser/src/language/common/commonValueConverters.ts

This file was deleted.

5 changes: 3 additions & 2 deletions packages/parser/src/language/common/index.ts
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';
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
import type { GrammarAST, Stream, TokenBuilderOptions } from 'langium';
import type { TokenType } from '../chevrotainWrapper.js';

import { DefaultTokenBuilder } from 'langium';

import type { TokenType } from '../chevrotainWrapper.js';
export abstract class MermaidTokenBuilder extends DefaultTokenBuilder {
private keywords: Set<string>;

public constructor(keywords: string[]) {
super();
this.keywords = new Set<string>(keywords);
}

export class InfoTokenBuilder extends DefaultTokenBuilder {
protected override buildKeywordTokens(
rules: Stream<GrammarAST.AbstractRule>,
terminalTokens: TokenType[],
Expand All @@ -12,10 +19,7 @@ export class InfoTokenBuilder extends DefaultTokenBuilder {
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
) {
if (this.keywords.has(tokenType.name) && tokenType.PATTERN !== undefined) {
tokenType.PATTERN = new RegExp(tokenType.PATTERN.toString() + '(?!\\S)');
}
});
Expand Down
82 changes: 82 additions & 0 deletions packages/parser/src/language/common/valueConverter.ts
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;
}
}
2 changes: 1 addition & 1 deletion packages/parser/src/language/info/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export * from './infoModule.js';
export * from './module.js';
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import type {
} from 'langium';
import { EmptyFileSystem, createDefaultModule, createDefaultSharedModule, inject } from 'langium';

import { MermaidGeneratedSharedModule, InfoGeneratedModule } from '../generated/module.js';
import { CommonLexer } from '../common/commonLexer.js';
import { CommonValueConverter } from '../common/commonValueConverters.js';
import { InfoTokenBuilder } from './infoTokenBuilder.js';
import { CommonLexer } from '../common/lexer.js';
import { CommonValueConverter } from '../common/valueConverter.js';
import { InfoGeneratedModule, MermaidGeneratedSharedModule } from '../generated/module.js';
import { InfoTokenBuilder } from './tokenBuilder.js';

/**
* Declaration of `Info` services.
Expand All @@ -34,7 +34,7 @@ export type InfoServices = LangiumServices & InfoAddedServices;
*/
export const InfoModule: Module<InfoServices, PartialLangiumServices & InfoAddedServices> = {
parser: {
Lexer: (services) => new CommonLexer(services),
Lexer: (services: InfoServices) => new CommonLexer(services),
TokenBuilder: () => new InfoTokenBuilder(),
ValueConverter: () => new CommonValueConverter(),
},
Expand Down
7 changes: 7 additions & 0 deletions packages/parser/src/language/info/tokenBuilder.ts
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']);
}
}

0 comments on commit 271b779

Please sign in to comment.