Removed warnings in the grammar oand some console logging

This commit is contained in:
Knut Sveidqvist
2022-09-07 12:38:26 +02:00
parent c0dd6f9e35
commit c50a7533f6
3 changed files with 69 additions and 61 deletions

View File

@@ -1,4 +1,5 @@
import * as mindmapDB from './mindmapDb'; import * as mindmapDB from './mindmapDb';
import { setLogLevel } from '../../logger';
describe('when parsing a mindmap ', function () { describe('when parsing a mindmap ', function () {
let mindmap; let mindmap;
@@ -6,9 +7,10 @@ describe('when parsing a mindmap ', function () {
mindmap = require('./parser/mindmap').parser; mindmap = require('./parser/mindmap').parser;
mindmap.yy = require('./mindmapDb'); mindmap.yy = require('./mindmapDb');
mindmap.yy.clear(); mindmap.yy.clear();
setLogLevel('trace');
}); });
describe('hiearchy', function () { describe('hiearchy', function () {
it('should handle a simple root definition', function () { it('MMP-1 should handle a simple root definition abc122', function () {
let str = `mindmap let str = `mindmap
root`; root`;
@@ -16,7 +18,7 @@ describe('when parsing a mindmap ', function () {
// console.log('Time for checks', mindmap.yy.getMindmap().descr); // console.log('Time for checks', mindmap.yy.getMindmap().descr);
expect(mindmap.yy.getMindmap().descr).toEqual('root'); expect(mindmap.yy.getMindmap().descr).toEqual('root');
}); });
it('should handle a hierachial mindmap definition', function () { it('MMP-2 should handle a hierachial mindmap definition', function () {
let str = `mindmap let str = `mindmap
root root
child1 child1
@@ -31,7 +33,7 @@ describe('when parsing a mindmap ', function () {
expect(mm.children[1].descr).toEqual('child2'); expect(mm.children[1].descr).toEqual('child2');
}); });
it('should handle a simple root definition with a shape and without an id abc123', function () { it('3 should handle a simple root definition with a shape and without an id abc123', function () {
let str = `mindmap let str = `mindmap
(root)`; (root)`;
@@ -40,7 +42,7 @@ describe('when parsing a mindmap ', function () {
expect(mindmap.yy.getMindmap().descr).toEqual('root'); expect(mindmap.yy.getMindmap().descr).toEqual('root');
}); });
it('should handle a deeper hierachial mindmap definition', function () { it('MMP-4 should handle a deeper hierachial mindmap definition', function () {
let str = `mindmap let str = `mindmap
root root
child1 child1
@@ -55,7 +57,7 @@ describe('when parsing a mindmap ', function () {
expect(mm.children[0].children[0].descr).toEqual('leaf1'); expect(mm.children[0].children[0].descr).toEqual('leaf1');
expect(mm.children[1].descr).toEqual('child2'); expect(mm.children[1].descr).toEqual('child2');
}); });
it('Multiple roots are illegal', function () { it('5 Multiple roots are illegal', function () {
let str = `mindmap let str = `mindmap
root root
fakeRoot`; fakeRoot`;
@@ -70,7 +72,7 @@ describe('when parsing a mindmap ', function () {
); );
} }
}); });
it('real root in wrong place', function () { it('MMP-6 real root in wrong place', function () {
let str = `mindmap let str = `mindmap
root root
fakeRoot fakeRoot
@@ -88,7 +90,7 @@ describe('when parsing a mindmap ', function () {
}); });
}); });
describe('nodes', function () { describe('nodes', function () {
it('should handle an id and type for a node definition', function () { it('MMP-7 should handle an id and type for a node definition', function () {
let str = `mindmap let str = `mindmap
root[The root] root[The root]
`; `;
@@ -99,7 +101,7 @@ describe('when parsing a mindmap ', function () {
expect(mm.descr).toEqual('The root'); expect(mm.descr).toEqual('The root');
expect(mm.type).toEqual(mindmap.yy.nodeType.RECT); expect(mm.type).toEqual(mindmap.yy.nodeType.RECT);
}); });
it('should handle an id and type for a node definition', function () { it('MMP-8 should handle an id and type for a node definition', function () {
let str = `mindmap let str = `mindmap
root root
theId(child1)`; theId(child1)`;
@@ -113,7 +115,7 @@ describe('when parsing a mindmap ', function () {
expect(child.nodeId).toEqual('theId'); expect(child.nodeId).toEqual('theId');
expect(child.type).toEqual(mindmap.yy.nodeType.ROUNDED_RECT); expect(child.type).toEqual(mindmap.yy.nodeType.ROUNDED_RECT);
}); });
it('should handle an id and type for a node definition', function () { it('MMP-9 should handle an id and type for a node definition', function () {
let str = `mindmap let str = `mindmap
root root
theId(child1)`; theId(child1)`;
@@ -127,10 +129,10 @@ root
expect(child.nodeId).toEqual('theId'); expect(child.nodeId).toEqual('theId');
expect(child.type).toEqual(mindmap.yy.nodeType.ROUNDED_RECT); expect(child.type).toEqual(mindmap.yy.nodeType.ROUNDED_RECT);
}); });
it('mutiple types (circle)', function () { it('MMP-10 mutiple types (circle)', function () {
let str = `mindmap let str = `mindmap
root((the root)) root((the root))
`; `;
mindmap.parse(str); mindmap.parse(str);
const mm = mindmap.yy.getMindmap(); const mm = mindmap.yy.getMindmap();
@@ -139,7 +141,7 @@ root
expect(mm.type).toEqual(mindmap.yy.nodeType.CIRCLE); expect(mm.type).toEqual(mindmap.yy.nodeType.CIRCLE);
}); });
it('mutiple types (cloud)', function () { it('MMP-11 mutiple types (cloud)', function () {
let str = `mindmap let str = `mindmap
root)the root( root)the root(
`; `;
@@ -150,7 +152,7 @@ root
expect(mm.children.length).toEqual(0); expect(mm.children.length).toEqual(0);
expect(mm.type).toEqual(mindmap.yy.nodeType.CLOUD); expect(mm.type).toEqual(mindmap.yy.nodeType.CLOUD);
}); });
it('mutiple types (bang)', function () { it('MMP-12 mutiple types (bang)', function () {
let str = `mindmap let str = `mindmap
root))the root(( root))the root((
`; `;
@@ -163,7 +165,7 @@ root
}); });
}); });
describe('decorations', function () { describe('decorations', function () {
it('should be possible to set an icon for the node', function () { it('MMP-13 should be possible to set an icon for the node', function () {
let str = `mindmap let str = `mindmap
root[The root] root[The root]
::icon(bomb) ::icon(bomb)
@@ -177,7 +179,7 @@ root
expect(mm.type).toEqual(mindmap.yy.nodeType.RECT); expect(mm.type).toEqual(mindmap.yy.nodeType.RECT);
expect(mm.icon).toEqual('bomb'); expect(mm.icon).toEqual('bomb');
}); });
it('should be possible to set classes for the node', function () { it('MMP-14 should be possible to set classes for the node', function () {
let str = `mindmap let str = `mindmap
root[The root] root[The root]
:::m-4 p-8 :::m-4 p-8
@@ -191,7 +193,7 @@ root
expect(mm.type).toEqual(mindmap.yy.nodeType.RECT); expect(mm.type).toEqual(mindmap.yy.nodeType.RECT);
expect(mm.class).toEqual('m-4 p-8'); expect(mm.class).toEqual('m-4 p-8');
}); });
it('should be possible to set both classes and icon for the node', function () { it('MMP-15 should be possible to set both classes and icon for the node', function () {
let str = `mindmap let str = `mindmap
root[The root] root[The root]
:::m-4 p-8 :::m-4 p-8
@@ -207,7 +209,7 @@ root
expect(mm.class).toEqual('m-4 p-8'); expect(mm.class).toEqual('m-4 p-8');
expect(mm.icon).toEqual('bomb'); expect(mm.icon).toEqual('bomb');
}); });
it('should be possible to set both classes and icon for the node', function () { it('MMP-16 should be possible to set both classes and icon for the node', function () {
let str = `mindmap let str = `mindmap
root[The root] root[The root]
::icon(bomb) ::icon(bomb)
@@ -225,7 +227,7 @@ root
}); });
}); });
describe('descriptions', function () { describe('descriptions', function () {
it('should be possible to use node syntax in the descriptions', function () { it('MMP-17 should be possible to use node syntax in the descriptions', function () {
let str = `mindmap let str = `mindmap
root["String containing []"] root["String containing []"]
`; `;
@@ -234,7 +236,7 @@ root
expect(mm.nodeId).toEqual('root'); expect(mm.nodeId).toEqual('root');
expect(mm.descr).toEqual('String containing []'); expect(mm.descr).toEqual('String containing []');
}); });
it('should be possible to use node syntax in the descriptions in children', function () { it('MMP-18 should be possible to use node syntax in the descriptions in children', function () {
let str = `mindmap let str = `mindmap
root["String containing []"] root["String containing []"]
child1["String containing ()"] child1["String containing ()"]
@@ -246,7 +248,7 @@ root
expect(mm.children.length).toEqual(1); expect(mm.children.length).toEqual(1);
expect(mm.children[0].descr).toEqual('String containing ()'); expect(mm.children[0].descr).toEqual('String containing ()');
}); });
it('should be possible to have a child after a class assignment', function () { it('MMP-19 should be possible to have a child after a class assignment', function () {
let str = `mindmap let str = `mindmap
root(Root) root(Root)
Child(Child) Child(Child)
@@ -266,7 +268,7 @@ root
expect(child.children[1].nodeId).toEqual('b'); expect(child.children[1].nodeId).toEqual('b');
}); });
}); });
it('should be possible to have meaningless empty rows in a mindmap abc124', function () { it('MMP-20 should be possible to have meaningless empty rows in a mindmap abc124', function () {
let str = `mindmap let str = `mindmap
root(Root) root(Root)
Child(Child) Child(Child)
@@ -285,7 +287,7 @@ root
expect(child.children.length).toEqual(2); expect(child.children.length).toEqual(2);
expect(child.children[1].nodeId).toEqual('b'); expect(child.children[1].nodeId).toEqual('b');
}); });
it('should be possible to have comments in a mindmap', function () { it('MMP-21 should be possible to have comments in a mindmap', function () {
let str = `mindmap let str = `mindmap
root(Root) root(Root)
Child(Child) Child(Child)
@@ -306,7 +308,7 @@ root
expect(child.children[1].nodeId).toEqual('b'); expect(child.children[1].nodeId).toEqual('b');
}); });
it('should be possible to have comments at the end of a line', function () { it('MMP-22 should be possible to have comments at the end of a line', function () {
let str = `mindmap let str = `mindmap
root(Root) root(Root)
Child(Child) Child(Child)

View File

@@ -1,6 +1,6 @@
/** Created by knut on 15-01-14. */ /** Created by knut on 15-01-14. */
import { sanitizeText, getConfig } from '../../diagram-api/diagramAPI'; import { sanitizeText, getConfig } from '../../diagram-api/diagramAPI';
import { log } from '../../logger'; import { log as _log } from '../../logger';
let nodes = []; let nodes = [];
let cnt = 0; let cnt = 0;
@@ -25,7 +25,7 @@ export const getMindmap = () => {
return nodes.length > 0 ? nodes[0] : null; return nodes.length > 0 ? nodes[0] : null;
}; };
export const addNode = (level, id, descr, type) => { export const addNode = (level, id, descr, type) => {
console.info('addNode', level, id, descr, type); log.info('addNode', level, id, descr, type);
const conf = getConfig(); const conf = getConfig();
const node = { const node = {
id: cnt++, id: cnt++,
@@ -132,7 +132,8 @@ export const type2Str = (type) => {
return 'no-border'; return 'no-border';
} }
}; };
// Expose logger to grammar
export const log = _log;
export const getNodeById = (id) => nodes[id]; export const getNodeById = (id) => nodes[id];
export const getElementById = (id) => elements[id]; export const getElementById = (id) => elements[id];
// export default { // export default {

View File

@@ -17,20 +17,21 @@
%% %%
\s*\%\%.*\n {console.log('Found comment',yytext);} \s*\%\%.* {yy.log.trace('Found comment',yytext);}
// \%\%[^\n]*\n /* skip comments */ // \%\%[^\n]*\n /* skip comments */
"mindmap" return 'MINDMAP'; "mindmap" return 'MINDMAP';
":::" { this.begin('CLASS'); } ":::" { this.begin('CLASS'); }
<CLASS>.+ { this.popState();return 'CLASS'; } <CLASS>.+ { this.popState();return 'CLASS'; }
<CLASS>\n { this.popState();} <CLASS>\n { this.popState();}
[\n\s]*"::icon(" { this.begin('ICON'); } // [\s]*"::icon(" { this.begin('ICON'); }
[\n]+ /* return 'NL'; */ "::icon(" { yy.log.trace('Begin icon');this.begin('ICON'); }
[\n]+ return 'NL';
<ICON>[^\)]+ { return 'ICON'; } <ICON>[^\)]+ { return 'ICON'; }
<ICON>\) {this.popState();} <ICON>\) {yy.log.trace('end icon');this.popState();}
"-)" { console.log('Exploding node'); this.begin('NODE');return 'NODE_DSTART'; } "-)" { yy.log.trace('Exploding node'); this.begin('NODE');return 'NODE_DSTART'; }
"(-" { console.log('Cloud'); this.begin('NODE');return 'NODE_DSTART'; } "(-" { yy.log.trace('Cloud'); this.begin('NODE');return 'NODE_DSTART'; }
"))" { console.log('Explosion Bang'); this.begin('NODE');return 'NODE_DSTART'; } "))" { yy.log.trace('Explosion Bang'); this.begin('NODE');return 'NODE_DSTART'; }
")" { console.log('Cloud Bang'); this.begin('NODE');return 'NODE_DSTART'; } ")" { yy.log.trace('Cloud Bang'); this.begin('NODE');return 'NODE_DSTART'; }
"((" { this.begin('NODE');return 'NODE_DSTART'; } "((" { this.begin('NODE');return 'NODE_DSTART'; }
"(" { this.begin('NODE');return 'NODE_DSTART'; } "(" { this.begin('NODE');return 'NODE_DSTART'; }
"[" { this.begin('NODE');return 'NODE_DSTART'; } "[" { this.begin('NODE');return 'NODE_DSTART'; }
@@ -38,18 +39,18 @@
// !(-\() return 'NODE_ID'; // !(-\() return 'NODE_ID';
[^\(\[\n\-\)]+ return 'NODE_ID'; [^\(\[\n\-\)]+ return 'NODE_ID';
<<EOF>> return 'EOF'; <<EOF>> return 'EOF';
<NODE>["] { console.log('Starting NSTR');this.begin("NSTR");} <NODE>["] { yy.log.trace('Starting NSTR');this.begin("NSTR");}
<NSTR>[^"]+ { console.log('description:', yytext); return "NODE_DESCR";} <NSTR>[^"]+ { yy.log.trace('description:', yytext); return "NODE_DESCR";}
<NSTR>["] {this.popState();} <NSTR>["] {this.popState();}
<NODE>[\)]\) {this.popState();console.log('node end ))');return "NODE_DEND";} <NODE>[\)]\) {this.popState();yy.log.trace('node end ))');return "NODE_DEND";}
<NODE>[\)] {this.popState();console.log('node end )');return "NODE_DEND";} <NODE>[\)] {this.popState();yy.log.trace('node end )');return "NODE_DEND";}
<NODE>[\]] {this.popState();console.log('node end ...');return "NODE_DEND";} <NODE>[\]] {this.popState();yy.log.trace('node end ...',yytext);return "NODE_DEND";}
<NODE>"(-" {this.popState();console.log('node end (-');return "NODE_DEND";} <NODE>"(-" {this.popState();yy.log.trace('node end (-');return "NODE_DEND";}
<NODE>"-)" {this.popState();console.log('node end (-');return "NODE_DEND";} <NODE>"-)" {this.popState();yy.log.trace('node end (-');return "NODE_DEND";}
<NODE>"((" {this.popState();console.log('node end ((');return "NODE_DEND";} <NODE>"((" {this.popState();yy.log.trace('node end ((');return "NODE_DEND";}
<NODE>"(" {this.popState();console.log('node end ((');return "NODE_DEND";} <NODE>"(" {this.popState();yy.log.trace('node end ((');return "NODE_DEND";}
<NODE>[^\)\]\(]+ { console.log('Long description:', yytext); return 'NODE_DESCR';} <NODE>[^\)\]\(]+ { yy.log.trace('Long description:', yytext); return 'NODE_DESCR';}
<NODE>.+(?!\(\() { console.log('Long description:', yytext); return 'NODE_DESCR';} <NODE>.+(?!\(\() { yy.log.trace('Long description:', yytext); return 'NODE_DESCR';}
// [\[] return 'NODE_START'; // [\[] return 'NODE_START';
// .+ return 'TXT' ; // .+ return 'TXT' ;
@@ -62,29 +63,33 @@
start start
// %{ : info document 'EOF' { return yy; } } // %{ : info document 'EOF' { return yy; } }
: MINDMAP document { return yy; } : MINDMAP document { return yy; }
| MINDMAP NL document { return yy; }
| SPACELIST MINDMAP document { return yy; } | SPACELIST MINDMAP document { return yy; }
; ;
stop
: NL {yy.log.trace('Stop NL ');}
| EOF {yy.log.trace('Stop EOF ');}
| stop NL {yy.log.trace('Stop NL2 ');}
| stop EOF {yy.log.trace('Stop EOF2 ');}
;
document document
: document line : document statement stop
| line | statement stop
;
line
: statement { }
; ;
statement statement
: SPACELIST node { yy.addNode($1.length, $2.id, $2.descr, $2.type); } : SPACELIST node { yy.log.trace('Node: ',$2.id);yy.addNode($1.length, $2.id, $2.descr, $2.type); }
| SPACELIST ICON { yy.decorateNode({icon: $2}); } | SPACELIST ICON { yy.log.trace('Icon: ',$2);yy.decorateNode({icon: $2}); }
| SPACELIST EOF | SPACELIST CLASS { yy.decorateNode({class: $2}); }
| SPACELIST NL | node { yy.log.trace('Node: ',$1.id);yy.addNode(0, $1.id, $1.descr, $1.type); }
| node { console.log($1.id);yy.addNode(0, $1.id, $1.descr, $1.type); }
| ICON { yy.decorateNode({icon: $1}); } | ICON { yy.decorateNode({icon: $1}); }
| SPACELIST CLASS { yy.decorateNode({class: $2}); } | CLASS { yy.decorateNode({class: $1}); }
| CLASS { yy.decorateNode({class: $1}); } | SPACELIST
| EOF
; ;
node node
:nodeWithId :nodeWithId
|nodeWithoutId |nodeWithoutId
@@ -92,12 +97,12 @@ node
nodeWithoutId nodeWithoutId
: NODE_DSTART NODE_DESCR NODE_DEND : NODE_DSTART NODE_DESCR NODE_DEND
{ console.log("node found ..", $1); $$ = { id: $2, descr: $2, type: yy.getType($1, $3) }; } { yy.log.trace("node found ..", $1); $$ = { id: $2, descr: $2, type: yy.getType($1, $3) }; }
; ;
nodeWithId nodeWithId
: NODE_ID { $$ = { id: $1, descr: $1, type: yy.nodeType.DEFAULT }; } : NODE_ID { $$ = { id: $1, descr: $1, type: yy.nodeType.DEFAULT }; }
| NODE_ID NODE_DSTART NODE_DESCR NODE_DEND | NODE_ID NODE_DSTART NODE_DESCR NODE_DEND
{ console.log("node found ..", $1); $$ = { id: $1, descr: $3, type: yy.getType($2, $4) }; } { yy.log.trace("node found ..", $1); $$ = { id: $1, descr: $3, type: yy.getType($2, $4) }; }
; ;
%% %%