From 42ef0352417268a48e0e89d70f348f3c48a8db24 Mon Sep 17 00:00:00 2001 From: Knut Sveidqvist Date: Wed, 18 Dec 2019 18:36:34 +0100 Subject: [PATCH] #834 Using space as separator, simplfifying the grammar some more, reomving some logging --- src/diagrams/flowchart/flowDb.js | 1 - .../flowchart/parser/flow-singlenode.spec.js | 10 ++++ src/diagrams/flowchart/parser/flow.jison | 49 +++++-------------- .../flowchart/parser/subgraph.spec.js | 9 ++++ src/diagrams/pie/parser/pie.jison | 2 +- 5 files changed, 31 insertions(+), 40 deletions(-) diff --git a/src/diagrams/flowchart/flowDb.js b/src/diagrams/flowchart/flowDb.js index ccd2fbbb3..bfd304e96 100644 --- a/src/diagrams/flowchart/flowDb.js +++ b/src/diagrams/flowchart/flowDb.js @@ -498,7 +498,6 @@ export const indexNodes = function() { }; export const getSubGraphs = function() { - console.warn(subGraphs); return subGraphs; }; diff --git a/src/diagrams/flowchart/parser/flow-singlenode.spec.js b/src/diagrams/flowchart/parser/flow-singlenode.spec.js index ba9f23925..15f7cc169 100644 --- a/src/diagrams/flowchart/parser/flow-singlenode.spec.js +++ b/src/diagrams/flowchart/parser/flow-singlenode.spec.js @@ -22,6 +22,16 @@ describe('[Singlenodes] when parsing', () => { expect(edges.length).toBe(0); expect(vert['A'].styles.length).toBe(0); }); + it('should handle a single node with white space after it (SN1)', function() { + // Silly but syntactically correct + const res = flow.parser.parse('graph TD;A ;'); + + const vert = flow.parser.yy.getVertices(); + const edges = flow.parser.yy.getEdges(); + + expect(edges.length).toBe(0); + expect(vert['A'].styles.length).toBe(0); + }); it('should handle a single square node', function() { // Silly but syntactically correct diff --git a/src/diagrams/flowchart/parser/flow.jison b/src/diagrams/flowchart/parser/flow.jison index e3fd550ed..6a282b93b 100644 --- a/src/diagrams/flowchart/parser/flow.jison +++ b/src/diagrams/flowchart/parser/flow.jison @@ -20,7 +20,6 @@ "interpolate" return 'INTERPOLATE'; "classDef" return 'CLASSDEF'; "class" return 'CLASS'; -"EPA" return 'EPA'; "click" return 'CLICK'; "graph" {if(yy.lex.firstGraph()){this.begin("dir");} return 'GRAPH';} "subgraph" return 'subgraph'; @@ -250,7 +249,7 @@ spaceList statement : verticeStatement separator - { console.warn('finat vs', $1.nodes); $$=$1.nodes} + { /* console.warn('finat vs', $1.nodes); */ $$=$1.nodes} | styleStatement separator {$$=[];} | linkStyleStatement separator @@ -289,74 +288,48 @@ separator: NEWLINE | SEMI | EOF ; // ; -verticeStatement: verticeStatement link node { console.warn('vs',$1.stmt,$3); yy.addLink($1.stmt,$3,$2); $$ = { stmt: $3, nodes: $3.concat($1.nodes) } } - |node {console.warn('noda', $1); $$ = {stmt: $1, nodes:$1 }} +verticeStatement: verticeStatement link node + { /* console.warn('vs',$1.stmt,$3); */ yy.addLink($1.stmt,$3,$2); $$ = { stmt: $3, nodes: $3.concat($1.nodes) } } + | verticeStatement link node spaceList + { /* console.warn('vs',$1.stmt,$3); */ yy.addLink($1.stmt,$3,$2); $$ = { stmt: $3, nodes: $3.concat($1.nodes) } } + |node spaceList {/*console.warn('noda', $1);*/ $$ = {stmt: $1, nodes:$1 }} + |node { /*console.warn('noda', $1);*/ $$ = {stmt: $1, nodes:$1 }} ; node: vertex - { console.warn('nod', $1);$$ = [$1];} - | node PIPE vertex - { $$ = [$1[0], $3]; console.warn('pip', $1, $3, $$); } + { /* console.warn('nod', $1); */ $$ = [$1];} + | node spaceList vertex + { $$ = [$1[0], $3]; /*console.warn('pip', $1, $3, $$);*/ } | vertex STYLE_SEPARATOR idString {$$ = [$1];yy.setClass($1,$3)} ; vertex: idString SQS text SQE {$$ = $1;yy.addVertex($1,$3,'square');} - | idString SQS text SQE spaceList - {$$ = $1;yy.addVertex($1,$3,'square');} | idString PS PS text PE PE {$$ = $1;yy.addVertex($1,$4,'circle');} - | idString PS PS text PE PE spaceList - {$$ = $1;yy.addVertex($1,$4,'circle');} | idString '(-' text '-)' {$$ = $1;yy.addVertex($1,$3,'ellipse');} - | idString '(-' text '-)' spaceList - {$$ = $1;yy.addVertex($1,$3,'ellipse');} | idString STADIUMSTART text STADIUMEND {$$ = $1;yy.addVertex($1,$3,'stadium');} - | idString STADIUMSTART text STADIUMEND spaceList - {$$ = $1;yy.addVertex($1,$3,'stadium');} | idString PS text PE {$$ = $1;yy.addVertex($1,$3,'round');} - | idString PS text PE spaceList - {$$ = $1;yy.addVertex($1,$3,'round');} | idString DIAMOND_START text DIAMOND_STOP {$$ = $1;yy.addVertex($1,$3,'diamond');} - | idString DIAMOND_START text DIAMOND_STOP spaceList - {$$ = $1;yy.addVertex($1,$3,'diamond');} | idString DIAMOND_START DIAMOND_START text DIAMOND_STOP DIAMOND_STOP {$$ = $1;yy.addVertex($1,$4,'hexagon');} - | idString DIAMOND_START DIAMOND_START text DIAMOND_STOP DIAMOND_STOP spaceList - {$$ = $1;yy.addVertex($1,$4,'hexagon');} | idString TAGEND text SQE {$$ = $1;yy.addVertex($1,$3,'odd');} - | idString TAGEND text SQE spaceList - {$$ = $1;yy.addVertex($1,$3,'odd');} | idString TRAPSTART text TRAPEND {$$ = $1;yy.addVertex($1,$3,'trapezoid');} - | idString TRAPSTART text TRAPEND spaceList - {$$ = $1;yy.addVertex($1,$3,'trapezoid');} | idString INVTRAPSTART text INVTRAPEND {$$ = $1;yy.addVertex($1,$3,'inv_trapezoid');} - | idString INVTRAPSTART text INVTRAPEND spaceList - {$$ = $1;yy.addVertex($1,$3,'inv_trapezoid');} | idString TRAPSTART text INVTRAPEND {$$ = $1;yy.addVertex($1,$3,'lean_right');} - | idString TRAPSTART text INVTRAPEND spaceList - {$$ = $1;yy.addVertex($1,$3,'lean_right');} | idString INVTRAPSTART text TRAPEND {$$ = $1;yy.addVertex($1,$3,'lean_left');} - | idString INVTRAPSTART text TRAPEND spaceList - {$$ = $1;yy.addVertex($1,$3,'lean_left');} -/* | idString SQS text TAGSTART - {$$ = $1;yy.addVertex($1,$3,'odd_right');} - | idString SQS text TAGSTART spaceList - {$$ = $1;yy.addVertex($1,$3,'odd_right');} */ | idString - {console.warn('h: ', $1);$$ = $1;yy.addVertex($1);} - | idString spaceList - {$$ = $1;yy.addVertex($1);} + { /*console.warn('h: ', $1);*/$$ = $1;yy.addVertex($1);} ; diff --git a/src/diagrams/flowchart/parser/subgraph.spec.js b/src/diagrams/flowchart/parser/subgraph.spec.js index bd97f154c..8bb02fe38 100644 --- a/src/diagrams/flowchart/parser/subgraph.spec.js +++ b/src/diagrams/flowchart/parser/subgraph.spec.js @@ -192,6 +192,15 @@ describe('when parsing subgraphs', function() { expect(edges[0].type).toBe('arrow'); }); + it('should handle subgraphs3', function() { + const res = flow.parser.parse('graph TD\nA-->B\nsubgraph myTitle \n\n c-->d \nend\n'); + + const vert = flow.parser.yy.getVertices(); + const edges = flow.parser.yy.getEdges(); + + expect(edges[0].type).toBe('arrow'); + }); + it('should handle nested subgraphs', function() { const str = 'graph TD\n' + diff --git a/src/diagrams/pie/parser/pie.jison b/src/diagrams/pie/parser/pie.jison index be6fcf214..2cc4045ff 100644 --- a/src/diagrams/pie/parser/pie.jison +++ b/src/diagrams/pie/parser/pie.jison @@ -49,7 +49,7 @@ line statement : STR VALUE { - console.log('str:'+$1+' value: '+$2) + /*console.log('str:'+$1+' value: '+$2)*/ yy.addSection($1,yy.cleanupValue($2)); } | title {yy.setTitle($1.substr(6));$$=$1.substr(6);} ;