From 2306534248e771487d8205a4633d8981fe616ac3 Mon Sep 17 00:00:00 2001 From: knsv Date: Sun, 22 Sep 2019 03:30:36 -0700 Subject: [PATCH] #945 Handling of dimples state definitions --- src/diagrams/state/parser/stateDiagram.jison | 17 +++++- src/diagrams/state/stateDiagram.spec.js | 64 +++++++++++++------- 2 files changed, 57 insertions(+), 24 deletions(-) diff --git a/src/diagrams/state/parser/stateDiagram.jison b/src/diagrams/state/parser/stateDiagram.jison index e2cd40946..0e73cee4a 100644 --- a/src/diagrams/state/parser/stateDiagram.jison +++ b/src/diagrams/state/parser/stateDiagram.jison @@ -23,6 +23,8 @@ %x NOTE %x NOTE_ID %x NOTE_TEXT +%x FLOATING_NOTE +%x FLOATING_NOTE_ID %x struct // A special state for grabbing text up to the first comment/newline @@ -45,17 +47,23 @@ .*"<>" {this.popState();console.log('Join: ',yytext);return 'JOIN';} ["] this.begin("STATE_STRING"); "as"\s* {this.popState();this.pushState('STATE_ID');return "AS";} -[^\n]* {this.popState();console.log('ID');return "ID";} +[^\n\{]* {this.popState();console.log('STATE_ID', yytext);return "ID";} ["] this.popState(); [^"]* { console.log('Long description:', yytext);return "STATE_DESCR";} [^\n\s\{]+ {console.log('COMPOSIT_STATE', yytext);return 'COMPOSIT_STATE';} -\{ {this.popState();this.pushState('struct'); console.log('begin struct', yytext);return 'STRUCT_START';} +\n {this.popState();} +\{ {this.popState();this.pushState('struct'); console.log('begin struct', yytext);return 'STRUCT_START';} \} { console.log('Ending struct'); this.popState(); return 'STRUCT_STOP';}} [\n] /* nothing */ "note"\s+ { this.begin('NOTE'); return 'note'; } "left of" { this.popState();this.pushState('NOTE_ID');console.log('Got dir');return 'left_of';} "right of" { this.popState();this.pushState('NOTE_ID');return 'right_of';} +\" { this.popState();this.pushState('FLOATING_NOTE');} +\s*"as"\s* {this.popState();this.pushState('FLOATING_NOTE_ID');return "AS";} +["] /**/ +[^"]* { console.log('Floating note text: ', yytext);return "NOTE_TEXT";} +[^\n]* {this.popState();console.log('Floating note ID', yytext);return "ID";} \s*[^:\n\s\-]+ { this.popState();this.pushState('NOTE_TEXT');console.log('Got ID for note', yytext);return 'ID';} \s*":"[^\+\-:\n,;]+ { this.popState();console.log('Got NOTE_TEXT for note',yytext);return 'NOTE_TEXT';} \s*[^\+\-:,;]+"end note" { this.popState();console.log('Got NOTE_TEXT for note',yytext);return 'NOTE_TEXT';} @@ -72,7 +80,7 @@ // [^#\n;]* { this.popState(); return 'restOfLine'; } // "end" return 'end'; "[*]" { console.log('EDGE_STATE=',yytext); return 'EDGE_STATE';} -[^:\n\s\-]+ { console.log('ID=',yytext); return 'ID';} +[^:\n\s\-\{]+ { console.log('=>ID=',yytext); return 'ID';} \s*":"[^\+\->:\n,;]+ { yytext = yytext.trim(); console.log('Descr = ', yytext); return 'DESCR'; } // "over" return 'over'; // "note" return 'note'; @@ -121,12 +129,15 @@ statement | idStatement '-->' idStatement DESCR | HIDE_EMPTY | scale WIDTH + | COMPOSIT_STATE | COMPOSIT_STATE STRUCT_START document STRUCT_STOP | STATE_DESCR AS ID + | STATE_DESCR AS ID STRUCT_START document STRUCT_STOP | FORK | JOIN | CONCURRENT | note notePosition ID NOTE_TEXT + | note NOTE_TEXT AS ID ; idStatement diff --git a/src/diagrams/state/stateDiagram.spec.js b/src/diagrams/state/stateDiagram.spec.js index 0b0ef4244..58399bfdf 100644 --- a/src/diagrams/state/stateDiagram.spec.js +++ b/src/diagrams/state/stateDiagram.spec.js @@ -128,6 +128,20 @@ describe('state diagram, ', function() { parser.parse(str); }); + it('should handle state deifintions with separation of id', function() { + const str = `stateDiagram + state "Not Shooting State" as NotShooting { + state "Idle mode" as Idle + state "Configuring mode" as Configuring + [*] --> Idle + Idle --> Configuring : EvConfig + Configuring --> Idle : EvConfig + } + `; + + parser.parse(str); + }); + it('should State definition with quotes', function() { const str = `stateDiagram\n scale 600 width @@ -229,30 +243,38 @@ describe('state diagram, ', function() { parser.parse(str); }); - // it('should handle relation definitions', function() { - // const str = `stateDiagram\n - // state foo - // note "This is a floating note" as N1 - // `; + it('should handle floating notes', function() { + const str = `stateDiagram + foo: bar + note "This is a floating note" as N1 + `; - // parser.parse(str); - // }); - // it('should handle relation definitions', function() { - // const str = `stateDiagram\n - // [*] --> NotShooting + parser.parse(str); + }); + it('should handle floating notes', function() { + const str = `stateDiagram\n + state foo + note "This is a floating note" as N1 + `; - // state "Not Shooting State" as NotShooting { - // state "Idle mode" as Idle - // state "Configuring mode" as Configuring - // [*] --> Idle - // Idle --> Configuring : EvConfig - // Configuring --> Idle : EvConfig - // } + parser.parse(str); + }); + it('should handle notes for composit states', function() { + const str = `stateDiagram\n + [*] --> NotShooting - // note right of NotShooting : This is a note on a composite state - // `; + state "Not Shooting State" as NotShooting { + state "Idle mode" as Idle + state "Configuring mode" as Configuring + [*] --> Idle + Idle --> Configuring : EvConfig + Configuring --> Idle : EvConfig + } - // parser.parse(str); - // }); + note right of NotShooting : This is a note on a composite state + `; + + parser.parse(str); + }); }); });