Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: override langium TokenBuilder for custom terminal rules #37

Merged
merged 1 commit into from
May 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/mermaid-parser/src/language/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './generated/ast';
export * from './generated/grammar';
export * from './generated/module';
export * from './mermaid-token-builder';
73 changes: 73 additions & 0 deletions packages/mermaid-parser/src/language/mermaid-token-builder.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import { CustomPatternMatcherFunc, EOF, TokenType } from 'chevrotain';
import { DefaultTokenBuilder } from 'langium';
import { TerminalRule } from 'langium/lib/grammar/generated/ast';

const titleRegex = /(?:^|[ \t]+)title(?:[ \t]+([^\n]*)|$)/;
const matchTitle: CustomPatternMatcherFunc = (str: string) => {
const match = titleRegex.exec(str);
if (match) {
// single line title
if (match[1] !== undefined) {
return [match[1].trim()];
}
}
return null;
};

/**
* Matches a single accessible title
*/
const accTitleRegex = /(?:^|[ \t]+)accTitle[ \t]*:[ \t]*([^\n]*)?/;
const matchAccTitle: CustomPatternMatcherFunc = (str: string) => {
const match = accTitleRegex.exec(str);
if (match) {
// single line title
if (match[1] !== undefined) {
return [match[1].trim()];
}
}
return null;
};

/**
* Matches single and multiline accessible description
*/
const accDescrRegex =
/(?:^|[ \t]+)accDescr(?:[ \t]*:[ \t]*([^\n]*)?|\s*{([^}]*)?})/;
const matchAccDescr: CustomPatternMatcherFunc = (str: string) => {
const match = accDescrRegex.exec(str);
if (match) {
// single line description
if (match[1] !== undefined) {
return [match[1].trim()];
}

// multi line description
if (match[2] !== undefined) {
return [match[2].replace(/^(\n+|[ \t]+)|\s+$/gm, '')];
}
}
return null;
};

export class MermiadTokenBuilder extends DefaultTokenBuilder {
override buildTerminalToken(terminal: TerminalRule): TokenType {
let tokenType = super.buildTerminalToken(terminal);
if (tokenType.name === 'EOF') {
tokenType = EOF;
} else if (tokenType.name === 'TITLE') {
tokenType.LINE_BREAKS = false;
tokenType.PATTERN = matchTitle;
tokenType.START_CHARS_HINT = ['title'];
} else if (tokenType.name === 'ACC_TITLE') {
tokenType.LINE_BREAKS = false;
tokenType.PATTERN = matchAccTitle;
tokenType.START_CHARS_HINT = ['accTitle'];
} else if (tokenType.name === 'ACC_DESCR') {
tokenType.LINE_BREAKS = true;
tokenType.PATTERN = matchAccDescr;
tokenType.START_CHARS_HINT = ['accDescr'];
}
return tokenType;
}
}