diff --git a/packages/mermaid-parser/src/language/mermaid-token-builder.ts b/packages/mermaid-parser/src/language/mermaid-token-builder.ts index 3e16c8b..c1d804a 100644 --- a/packages/mermaid-parser/src/language/mermaid-token-builder.ts +++ b/packages/mermaid-parser/src/language/mermaid-token-builder.ts @@ -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 { @@ -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; } diff --git a/packages/mermaid-parser/src/matchers/accdescr.ts b/packages/mermaid-parser/src/matchers/accdescr.ts new file mode 100644 index 0000000..179c795 --- /dev/null +++ b/packages/mermaid-parser/src/matchers/accdescr.ts @@ -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 */ diff --git a/packages/mermaid-parser/src/matchers/acctitle.ts b/packages/mermaid-parser/src/matchers/acctitle.ts new file mode 100644 index 0000000..116b9bb --- /dev/null +++ b/packages/mermaid-parser/src/matchers/acctitle.ts @@ -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 */ diff --git a/packages/mermaid-parser/src/matchers/index.ts b/packages/mermaid-parser/src/matchers/index.ts new file mode 100644 index 0000000..a3492e0 --- /dev/null +++ b/packages/mermaid-parser/src/matchers/index.ts @@ -0,0 +1,3 @@ +export * from './accdescr'; +export * from './acctitle'; +export * from './title'; diff --git a/packages/mermaid-parser/src/matchers/title.ts b/packages/mermaid-parser/src/matchers/title.ts new file mode 100644 index 0000000..06b59ac --- /dev/null +++ b/packages/mermaid-parser/src/matchers/title.ts @@ -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 */