WIP - fixing grammar added CircleNode, string handling

This commit is contained in:
Knut Sveidqvist
2025-04-19 07:14:05 -04:00
parent 6bcfb4df3a
commit d2ce80be10
5 changed files with 158 additions and 112 deletions

View File

@@ -9,7 +9,9 @@ entry MindmapDoc:
(MindmapRows+=MindmapRow)*;
MindmapRow:
(indent=INDENTATION)? item=Item (terminator=NL)?;
// indent=(INDENTATION | '0') item=Item (terminator=NL)?;
(indent=INDENTATION)? item=Item (terminator=NL)?;
Item:
Node | IconDecoration | ClassDecoration;
@@ -20,7 +22,9 @@ Node:
// Specifically handle double parentheses case - highest priority
CircleNode:
id=ID '((' desc=(ID | STRING) '))';
id=ID desc=(CIRCLE_STR);
// id=ID '((' desc=(CIRCLE_STR) '))';
// id=ID '((' desc=(ID|STRING) '))';
// Handle other complex node variants
OtherComplex:
@@ -49,6 +53,9 @@ terminal CLASS_KEYWORD: ':::';
// Basic token types
terminal ID: /[a-zA-Z0-9_\-\.\/]+/;
// terminal CIRCLE_STR: /[\s\S]*?\)\)/;
terminal CIRCLE_STR: /\(\(([\s\S]*?)\)\)/;
// terminal CIRCLE_STR: /(?!\(\()[\s\S]+?(?!\(\()/;
terminal STRING: /"[^"]*"|'[^']*'/;
terminal INDENTATION: /[ \t]{2,}/; // Two or more spaces/tabs for indentation
terminal NL: /\r?\n/;

View File

@@ -14,7 +14,7 @@ import {
import { MermaidGeneratedSharedModule, MindmapGeneratedModule } from '../generated/module.js';
import { MindmapTokenBuilder } from './tokenBuilder.js';
import { CommonValueConverter } from '../common/valueConverter.js';
import { MindmapValueConverter } from './valueConverter.js';
/**
* Declaration of `Mindmap` services.
@@ -22,7 +22,7 @@ import { CommonValueConverter } from '../common/valueConverter.js';
interface MindmapAddedServices {
parser: {
TokenBuilder: MindmapTokenBuilder;
ValueConverter: CommonValueConverter;
ValueConverter: MindmapValueConverter;
};
}
@@ -41,7 +41,7 @@ export const MindmapModule: Module<
> = {
parser: {
TokenBuilder: () => new MindmapTokenBuilder(),
ValueConverter: () => new CommonValueConverter(),
ValueConverter: () => new MindmapValueConverter(),
},
};

View File

@@ -0,0 +1,21 @@
import type { CstNode, GrammarAST, ValueType } from 'langium';
import { AbstractMermaidValueConverter } from '../common/index.js';
export class MindmapValueConverter extends AbstractMermaidValueConverter {
protected runCustomConverter(
rule: GrammarAST.AbstractRule,
input: string,
_cstNode: CstNode
): ValueType | undefined {
console.debug('MermaidValueConverter', rule.name, input);
if (rule.name === 'CIRCLE_STR') {
return input.replace('((', '').replace('))', '').trim();
} else if (rule.name === 'ARCH_TEXT_ICON') {
return input.replace(/["()]/g, '');
} else if (rule.name === 'ARCH_TITLE') {
return input.replace(/[[\]]/g, '').trim();
}
return undefined;
}
}

View File

@@ -50,37 +50,22 @@ describe('MindMap Parser Tests', () => {
expect(r1.$type).toBe('MindmapRow');
const node1 = r1.item as CircleNode;
console.debug('NODE1:', node1);
expect(node1.$type).toBe('CircleNode');
expect(result.value.rows[1].element.ID).toBe('Root');
expect(result.value.rows[1].element.desc).toBe('Root');
expect(Object.keys(result.value.rows[1].element)).toBe('root');
expect(result.value.rows[1].indent).toBe('indent');
expect(Object.keys(result.value.rows[1].element)).toBe(true);
expect(result.value.rows[1].element.id).toBe('SimpleNode');
expect(node1.id).toBe('child1');
expect(node1.desc).toBe('Child 1');
// expect(Object.keys(r1)).toBe(2);
// Temporarily commenting out failing assertions
// expect(result.successful).toBe(true);
// Check that there are 4 rows: mindmap, root, child1, child2, grandchild
expect(result.value.rows.length).toBe(5);
// Check that the first statement is the mindmap
expect(result.value.rows[0].type).toBe('mindmap');
// Check that the second statement is the root
expect(result.value.rows[1].type.type).toBe('circle');
expect(result.value.rows[1].text).toBe('Root');
expect(result.value.rows[1].depth).toBe(0);
// Check that the third statement is the first child
expect(result.value.rows[2].type.type).toBe('circle');
expect(result.value.rows[2].text).toBe('Child 1');
expect(result.value.rows[2].depth).toBe(1);
// Check that the fourth statement is the second child
expect(result.value.rows[3].type.type).toBe('circle');
expect(result.value.rows[3].text).toBe('Child 2');
expect(result.value.rows[3].depth).toBe(1);
// Check that the fifth statement is the grandchild
expect(result.value.rows[4].type.type).toBe('circle');
expect(result.value.rows[4].text).toBe('Grand Child');
expect(result.value.rows[4].depth).toBe(2);
const child2 = rows[2].item as CircleNode;
// expect(result.value.rows[1].indent).toBe('indent');
// expect(Object.keys(node1)).toBe(true);
expect(child2.id).toBe('child2');
expect(child2.desc).toBe('Child 2');
const grandChild = rows[3].item as CircleNode;
// expect(result.value.rows[1].indent).toBe('indent');
// expect(Object.keys(node1)).toBe(true);
expect(grandChild.id).toBe('grandchild');
expect(grandChild.desc).toBe('Grand Child');
});
});