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

style: split matchers from mermaid-token-builder #62

Merged
merged 1 commit into from
May 22, 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
65 changes: 8 additions & 57 deletions packages/mermaid-parser/src/language/mermaid-token-builder.ts
Original file line number Diff line number Diff line change
@@ -1,62 +1,13 @@
/* eslint-disable unicorn/no-null */
import {
CustomPatternMatcherFunc,
CustomPatternMatcherReturn,
EOF,
TokenType,
} from 'chevrotain';
import { EOF, TokenType } from 'chevrotain';
import { DefaultTokenBuilder } from 'langium';
import { TerminalRule } from 'langium/lib/grammar/generated/ast';

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

/**
* Matches a single accessible title
*/
const accumulatorTitleRegex = /(?:^|[\t ]+)accTitle[\t ]*:[\t ]*([^\n]+)?/;
const matchAccumulatorTitle: CustomPatternMatcherFunc = (string_: string) => {
let result = null;
let match = accumulatorTitleRegex.exec(string_);
while (match !== null) {
result = match[1] === undefined ? null : [match[1].trim()];
string_ = string_.replace(match[0], '');
match = accumulatorTitleRegex.exec(string_);
}
return result as CustomPatternMatcherReturn;
};

/**
* Matches single and multiline accessible description
*/
const accumulatorDescrRegex =
// eslint-disable-next-line regexp/strict
/(?:^|[\t ]+)accDescr(?:[\t ]*:[\t ]*([^\n]+)?|\s*{([^}]+)?})/;
const matchAccumulatorDescr: CustomPatternMatcherFunc = (string_: string) => {
const match = accumulatorDescrRegex.exec(string_);
if (match) {
// single line description
if (match[1] !== undefined) {
return [match[1].trim()];
}

// multi line description
if (match[2] !== undefined) {
const result = match[2].replaceAll(/^\s*|\s+$/gm, '');
return result ? [result] : null;
}
}
return null;
};
import {
matchAccessibilityDescr,
matchAccessibilityTitle,
matchTitle,
} from '../matchers';

export class MermiadTokenBuilder extends DefaultTokenBuilder {
override buildTerminalToken(terminal: TerminalRule): TokenType {
Expand All @@ -68,13 +19,13 @@ export class MermiadTokenBuilder extends DefaultTokenBuilder {
}
case 'ACC_DESCR': {
tokenType.LINE_BREAKS = true;
tokenType.PATTERN = matchAccumulatorDescr;
tokenType.PATTERN = matchAccessibilityDescr;
tokenType.START_CHARS_HINT = ['accDescr'];
break;
}
case 'ACC_TITLE': {
tokenType.LINE_BREAKS = false;
tokenType.PATTERN = matchAccumulatorTitle;
tokenType.PATTERN = matchAccessibilityTitle;
tokenType.START_CHARS_HINT = ['accTitle'];
break;
}
Expand Down
28 changes: 28 additions & 0 deletions packages/mermaid-parser/src/matchers/accdescr.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/* eslint-disable unicorn/no-null */
import { CustomPatternMatcherFunc } from 'chevrotain';

/**
* Matches single and multiline accessible description
*/
const accessibilityDescrRegex =
// eslint-disable-next-line regexp/strict
/(?:^|[\t ]+)accDescr(?:[\t ]*:[\t ]*([^\n]+)?|\s*{([^}]+)?})/;
export const matchAccessibilityDescr: CustomPatternMatcherFunc = (
string_: string,
) => {
const match = accessibilityDescrRegex.exec(string_);
if (match) {
// single line description
if (match[1] !== undefined) {
return [match[1].trim()];
}

// multi line description
if (match[2] !== undefined) {
const result = match[2].replaceAll(/^\s*|\s+$/gm, '');
return result ? [result] : null;
}
}
return null;
};
/* eslint-enable unicorn/no-null */
23 changes: 23 additions & 0 deletions packages/mermaid-parser/src/matchers/acctitle.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/* eslint-disable unicorn/no-null */
import {
CustomPatternMatcherFunc,
CustomPatternMatcherReturn,
} from 'chevrotain';

/**
* Matches a single accessible title
*/
const accessibilityTitleRegex = /(?:^|[\t ]+)accTitle[\t ]*:[\t ]*([^\n]+)?/;
export const matchAccessibilityTitle: CustomPatternMatcherFunc = (
string_: string,
) => {
let result: CustomPatternMatcherReturn | null = null;
let match = accessibilityTitleRegex.exec(string_);
while (match !== null) {
result = match[1] === undefined ? null : [match[1].trim()];
string_ = string_.replace(match[0], '');
match = accessibilityTitleRegex.exec(string_);
}
return result;
};
/* eslint-enable unicorn/no-null */
3 changes: 3 additions & 0 deletions packages/mermaid-parser/src/matchers/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from './accdescr';
export * from './acctitle';
export * from './title';
15 changes: 15 additions & 0 deletions packages/mermaid-parser/src/matchers/title.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/* eslint-disable unicorn/no-null */
import { CustomPatternMatcherFunc } from 'chevrotain';

/**
* Matches a single title
*/
const titleRegex = /(?:^|[\t ]+)title(?:[\t ]+([^\n]*)|$)/;
export const matchTitle: CustomPatternMatcherFunc = (string_: string) => {
const match = titleRegex.exec(string_);
if (match && match[1] !== undefined) {
return [match[1].trim()];
}
return null;
};
/* eslint-enable unicorn/no-null */