Handling indentation

This commit is contained in:
Knut Sveidqvist
2025-04-23 14:29:54 +02:00
parent d2ce80be10
commit 8f0703bdc2
3 changed files with 17 additions and 3 deletions

View File

@@ -46,6 +46,9 @@ IconDecoration:
ClassDecoration: ClassDecoration:
CLASS_KEYWORD content=(ID|STRING); CLASS_KEYWORD content=(ID|STRING);
// This should be processed before whitespace is ignored
terminal INDENTATION: /[ \t]{2,}/; // Two or more spaces/tabs for indentation
// Keywords with fixed text patterns // Keywords with fixed text patterns
terminal MINDMAP_KEYWORD: 'mindmap'; terminal MINDMAP_KEYWORD: 'mindmap';
terminal ICON_KEYWORD: '::icon('; terminal ICON_KEYWORD: '::icon(';
@@ -57,11 +60,13 @@ terminal ID: /[a-zA-Z0-9_\-\.\/]+/;
terminal CIRCLE_STR: /\(\(([\s\S]*?)\)\)/; terminal CIRCLE_STR: /\(\(([\s\S]*?)\)\)/;
// terminal CIRCLE_STR: /(?!\(\()[\s\S]+?(?!\(\()/; // terminal CIRCLE_STR: /(?!\(\()[\s\S]+?(?!\(\()/;
terminal STRING: /"[^"]*"|'[^']*'/; terminal STRING: /"[^"]*"|'[^']*'/;
terminal INDENTATION: /[ \t]{2,}/; // Two or more spaces/tabs for indentation
// Modified indentation rule to have higher priority than WS
terminal NL: /\r?\n/; terminal NL: /\r?\n/;
// Hidden tokens // Hidden tokens
hidden terminal WS: /[ \t]/; // Single space or tab for hidden whitespace terminal WS: /[ \t]/; // Single space or tab for hidden whitespace
hidden terminal ML_COMMENT: /\%\%[^\n]*/; hidden terminal ML_COMMENT: /\%\%[^\n]*/;
// Type definition for node types // Type definition for node types

View File

@@ -8,13 +8,15 @@ export class MindmapValueConverter extends AbstractMermaidValueConverter {
input: string, input: string,
_cstNode: CstNode _cstNode: CstNode
): ValueType | undefined { ): ValueType | undefined {
console.debug('MermaidValueConverter', rule.name, input); console.debug('MermaidValueConverter', rule.name);
if (rule.name === 'CIRCLE_STR') { if (rule.name === 'CIRCLE_STR') {
return input.replace('((', '').replace('))', '').trim(); return input.replace('((', '').replace('))', '').trim();
} else if (rule.name === 'ARCH_TEXT_ICON') { } else if (rule.name === 'ARCH_TEXT_ICON') {
return input.replace(/["()]/g, ''); return input.replace(/["()]/g, '');
} else if (rule.name === 'ARCH_TITLE') { } else if (rule.name === 'ARCH_TITLE') {
return input.replace(/[[\]]/g, '').trim(); return input.replace(/[[\]]/g, '').trim();
} else if (rule.name === 'INDENTATION') {
return input.length;
} }
return undefined; return undefined;
} }

View File

@@ -40,7 +40,13 @@ describe('MindMap Parser Tests', () => {
const rows = result.value.MindmapRows; const rows = result.value.MindmapRows;
const r0 = rows[0]; const r0 = rows[0];
expect(r0.indent).toBe(undefined);
const r1 = rows[1]; const r1 = rows[1];
expect(r1.indent).toBe(2);
const r2 = rows[2];
expect(r2.indent).toBe(2);
const r3 = rows[3];
expect(r3.indent).toBe(4);
expect(r0.$type).toBe('MindmapRow'); expect(r0.$type).toBe('MindmapRow');
const node0 = r0.item as CircleNode; const node0 = r0.item as CircleNode;
@@ -49,6 +55,7 @@ describe('MindMap Parser Tests', () => {
expect(node0.id).toBe('root'); expect(node0.id).toBe('root');
expect(r1.$type).toBe('MindmapRow'); expect(r1.$type).toBe('MindmapRow');
// console.debug('R1:', r1);
const node1 = r1.item as CircleNode; const node1 = r1.item as CircleNode;
expect(node1.$type).toBe('CircleNode'); expect(node1.$type).toBe('CircleNode');
expect(node1.id).toBe('child1'); expect(node1.id).toBe('child1');