chore: apply review suggestions

* pref: optimize `common` rules and matchers
* chore: rename diagrams services to short form
* chore: sort imports
This commit is contained in:
Reda Al Sulais 2023-09-07 00:00:38 +03:00
parent 795baedbb1
commit 24d43849a0
22 changed files with 165 additions and 188 deletions

View File

@ -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]*/;

View File

@ -1,14 +0,0 @@
/**
* Matches single and multiline accessible description
*/
export const accessibilityDescrRegex = /accDescr(?:[\t ]*:[\t ]*([^\n\r]*)|\s*{([^}]*)})/;
/**
* Matches single line accessible title
*/
export const accessibilityTitleRegex = /accTitle[\t ]*:[\t ]*([^\n\r]*)/;
/**
* Matches a single line title
*/
export const titleRegex = /title([\t ]+([^\n\r]*)|)/;

View File

@ -1,74 +0,0 @@
import type { CstNode, GrammarAST, ValueType } from 'langium';
import { DefaultValueConverter } from 'langium';
import { accessibilityDescrRegex, accessibilityTitleRegex, titleRegex } from './commonMatcher.js';
export class CommonValueConverter extends DefaultValueConverter {
protected override runConverter(
rule: GrammarAST.AbstractRule,
input: string,
cstNode: CstNode
): ValueType {
const value: ValueType | undefined = CommonValueConverter.customRunConverter(
rule,
input,
cstNode
);
if (value === undefined) {
return super.runConverter(rule, input, cstNode);
} else {
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 common rule or `undefined` if it's not.
*/
public static customRunConverter(
rule: GrammarAST.AbstractRule,
input: string,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
_cstNode: CstNode
): ValueType | undefined {
let regex: RegExp | undefined;
switch (rule.name) {
case 'ACC_DESCR': {
regex = new RegExp(accessibilityDescrRegex.source);
break;
}
case 'ACC_TITLE': {
regex = new RegExp(accessibilityTitleRegex.source);
break;
}
case 'TITLE': {
regex = new RegExp(titleRegex.source);
break;
}
}
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().replaceAll(/[\t ]{2,}/gm, ' ');
}
// multi line accDescr
if (match[2] !== undefined) {
return match[2]
.replaceAll(/^\s*/gm, '')
.replaceAll(/\s+$/gm, '')
.replaceAll(/[\t ]{2,}/gm, ' ')
.replaceAll(/[\n\r]{2,}/gm, '\n');
}
return undefined;
}
}

View File

@ -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';

View File

@ -0,0 +1,14 @@
/**
* Matches single and multi line accessible description
*/
export const accessibilityDescrRegex = /accDescr(?:[\t ]*:([^\n\r]*)|\s*{([^}]*)})/;
/**
* Matches single line accessible title
*/
export const accessibilityTitleRegex = /accTitle[\t ]*:([^\n\r]*)/;
/**
* Matches a single line title
*/
export const titleRegex = /title([\t ][^\n\r]*|)/;

View File

@ -3,7 +3,14 @@ import { DefaultTokenBuilder } from 'langium';
import type { TokenType } from '../chevrotainWrapper.js';
export class InfoTokenBuilder extends DefaultTokenBuilder {
export class CommonTokenBuilder extends DefaultTokenBuilder {
private keywords: Set<string>;
public constructor(keywords: string[]) {
super();
this.keywords = new Set<string>(keywords);
}
protected override buildKeywordTokens(
rules: Stream<GrammarAST.AbstractRule>,
terminalTokens: TokenType[],
@ -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)');
}
});

View File

@ -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;
}
}

View File

@ -1 +1 @@
export * from './infoModule.js';
export * from './module.js';

View File

@ -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.
@ -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(),
},

View File

@ -0,0 +1,7 @@
import { CommonTokenBuilder } from '../common/index.js';
export class InfoTokenBuilder extends CommonTokenBuilder {
public constructor() {
super(['info', 'showInfo']);
}
}

View File

@ -1 +1 @@
export * from './pieModule.js';
export * from './module.js';

View File

@ -7,10 +7,10 @@ import type {
} from 'langium';
import { EmptyFileSystem, createDefaultModule, createDefaultSharedModule, inject } from 'langium';
import { CommonLexer } from '../common/lexer.js';
import { MermaidGeneratedSharedModule, PieGeneratedModule } from '../generated/module.js';
import { CommonLexer } from '../common/commonLexer.js';
import { PieTokenBuilder } from './pieTokenBuilder.js';
import { PieValueConverter } from './pieValueConverter.js';
import { PieTokenBuilder } from './tokenBuilder.js';
import { PieValueConverter } from './valueConverter.js';
/**
* Declaration of `Pie` services.
@ -32,9 +32,9 @@ export type PieServices = LangiumServices & PieAddedServices;
* Dependency injection module that overrides Langium default services and
* contributes the declared `Pie` services.
*/
const PieModule: Module<PieServices, PartialLangiumServices & PieAddedServices> = {
export const PieModule: Module<PieServices, PartialLangiumServices & PieAddedServices> = {
parser: {
Lexer: (services) => new CommonLexer(services),
Lexer: (services: PieServices) => new CommonLexer(services),
TokenBuilder: () => new PieTokenBuilder(),
ValueConverter: () => new PieValueConverter(),
},

View File

@ -12,8 +12,7 @@ entry Pie:
;
PieSection:
label=PIE_SECTION_LABEL ":" value=PIE_SECTION_VALUE
NEWLINE+
label=PIE_SECTION_LABEL ":" value=PIE_SECTION_VALUE NEWLINE+
;
terminal PIE_SECTION_LABEL: /"[^"]+"/;

View File

@ -1,23 +0,0 @@
import type { GrammarAST, Stream, TokenBuilderOptions } from 'langium';
import { DefaultTokenBuilder } from 'langium';
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;
}
}

View File

@ -1,49 +0,0 @@
import type { CstNode, GrammarAST, ValueType } from 'langium';
import { DefaultValueConverter } from 'langium';
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(
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, ' ');
}
return undefined;
}
}

View File

@ -0,0 +1,7 @@
import { CommonTokenBuilder } from '../common/index.js';
export class PieTokenBuilder extends CommonTokenBuilder {
public constructor() {
super(['pie', 'showData']);
}
}

View File

@ -0,0 +1,17 @@
import type { CstNode, GrammarAST, ValueType } from 'langium';
import { MermaidValueConverter } from '../common/valueConverter.js';
export class PieValueConverter extends MermaidValueConverter {
protected 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 undefined;
}
return input.replace(/"/g, '').trim();
}
}

View File

@ -1,4 +1,5 @@
import type { LangiumParser, ParseResult } from 'langium';
import type { Info, Pie } from './index.js';
import { createInfoServices, createPieServices } from './language/index.js';

View File

@ -1,5 +1,5 @@
import { describe, expect, it } from 'vitest';
import type { LangiumParser, ParseResult } from 'langium';
import { describe, expect, it } from 'vitest';
import type { InfoServices } from '../src/language/index.js';
import { Info, createInfoServices } from '../src/language/index.js';

View File

@ -1,5 +1,5 @@
import { describe, expect, it } from 'vitest';
import type { LangiumParser, ParseResult } from 'langium';
import { describe, expect, it } from 'vitest';
import type { PieServices } from '../src/language/index.js';
import { Pie, createPieServices } from '../src/language/index.js';

View File

@ -1,5 +1,5 @@
import { expect, vi } from 'vitest';
import type { ParseResult } from 'langium';
import { expect, vi } from 'vitest';
const consoleMock = vi.spyOn(console, 'log').mockImplementation(() => undefined);