diff --git a/src/diagrams/sequenceDiagram/parser/sequenceDiagram.jison b/src/diagrams/sequenceDiagram/parser/sequenceDiagram.jison index f98f935fea..cf37e7fd66 100644 --- a/src/diagrams/sequenceDiagram/parser/sequenceDiagram.jison +++ b/src/diagrams/sequenceDiagram/parser/sequenceDiagram.jison @@ -40,11 +40,13 @@ "right of" return 'right_of'; "over" return 'over'; "note" return 'note'; +"activate" { this.begin('ID'); return 'activate'; } +"deactivate" { this.begin('ID'); return 'deactivate'; } "title" return 'title'; "sequenceDiagram" return 'SD'; "," return ','; ";" return 'NL'; -[^\->:\n,;]+ { yytext = yytext.trim(); return 'ACTOR'; } +[^\+\->:\n,;]+ { yytext = yytext.trim(); return 'ACTOR'; } "->>" return 'SOLID_ARROW'; "-->>" return 'DOTTED_ARROW'; "->" return 'SOLID_OPEN_ARROW'; @@ -52,6 +54,8 @@ \-[x] return 'SOLID_CROSS'; \-\-[x] return 'DOTTED_CROSS'; ":"[^#\n;]+ return 'TXT'; +"+" return '+'; +"-" return '-'; <> return 'NL'; . return 'INVALID'; @@ -84,6 +88,8 @@ statement : 'participant' actor 'AS' restOfLine 'NL' {$2.description=$4; $$=$2;} | 'participant' actor 'NL' {$$=$2;} | signal 'NL' + | 'activate' actor 'NL' {$$={type: 'activeStart', signalType: yy.LINETYPE.ACTIVE_START, actor: $2};} + | 'deactivate' actor 'NL' {$$={type: 'activeEnd', signalType: yy.LINETYPE.ACTIVE_END, actor: $2};} | note_statement 'NL' | 'title' SPACE text 'NL' | 'loop' restOfLine document end @@ -139,8 +145,16 @@ placement ; signal - : actor signaltype actor text2 - {$$ = [$1,$3,{type: 'addMessage', from:$1.actor, to:$3.actor, signalType:$2, msg:$4}]} + : actor signaltype '+' actor text2 + { $$ = [$1,$4,{type: 'addMessage', from:$1.actor, to:$4.actor, signalType:$2, msg:$5}, + {type: 'activeStart', signalType: yy.LINETYPE.ACTIVE_START, actor: $4} + ]} + | actor signaltype '-' actor text2 + { $$ = [$1,$4,{type: 'addMessage', from:$1.actor, to:$4.actor, signalType:$2, msg:$5}, + {type: 'activeEnd', signalType: yy.LINETYPE.ACTIVE_END, actor: $1} + ]} + | actor signaltype actor text2 + { $$ = [$1,$3,{type: 'addMessage', from:$1.actor, to:$3.actor, signalType:$2, msg:$4}]} ; actor diff --git a/src/diagrams/sequenceDiagram/parser/sequenceDiagram.js b/src/diagrams/sequenceDiagram/parser/sequenceDiagram.js index 060f55b897..3c603e7e85 100644 --- a/src/diagrams/sequenceDiagram/parser/sequenceDiagram.js +++ b/src/diagrams/sequenceDiagram/parser/sequenceDiagram.js @@ -1,4 +1,4 @@ -/* parser generated by jison 0.4.15 */ +/* parser generated by jison 0.4.17 */ /* Returns a Parser object of the following structure: @@ -72,51 +72,57 @@ } */ var sequenceDiagram = (function(){ -var o=function(k,v,o,l){for(o=o||{},l=k.length;l--;o[k[l]]=v);return o},$V0=[2,2],$V1=[1,5],$V2=[1,7],$V3=[1,8],$V4=[1,11],$V5=[1,12],$V6=[1,13],$V7=[1,14],$V8=[1,16],$V9=[1,17],$Va=[1,7,9,10,16,18,19,20,21,22,23,33],$Vb=[7,9,10,16,18,19,20,21,23,33],$Vc=[1,53]; +var o=function(k,v,o,l){for(o=o||{},l=k.length;l--;o[k[l]]=v);return o},$V0=[1,2],$V1=[1,3],$V2=[1,4],$V3=[2,4],$V4=[1,9],$V5=[1,11],$V6=[1,12],$V7=[1,14],$V8=[1,15],$V9=[1,17],$Va=[1,18],$Vb=[1,19],$Vc=[1,20],$Vd=[1,22],$Ve=[1,23],$Vf=[1,4,5,10,15,16,18,20,21,22,23,24,25,37],$Vg=[4,5,10,15,16,18,20,21,22,23,25,37],$Vh=[35,36,37],$Vi=[1,67]; var parser = {trace: function trace() { }, yy: {}, -symbols_: {"error":2,"start":3,"SD":4,"document":5,"line":6,"SPACE":7,"statement":8,"NL":9,"participant":10,"actor":11,"AS":12,"restOfLine":13,"signal":14,"note_statement":15,"title":16,"text":17,"loop":18,"end":19,"opt":20,"alt":21,"else":22,"note":23,"placement":24,"text2":25,"over":26,"actor_pair":27,"spaceList":28,",":29,"left_of":30,"right_of":31,"signaltype":32,"ACTOR":33,"SOLID_OPEN_ARROW":34,"DOTTED_OPEN_ARROW":35,"SOLID_ARROW":36,"DOTTED_ARROW":37,"SOLID_CROSS":38,"DOTTED_CROSS":39,"TXT":40,"$accept":0,"$end":1}, -terminals_: {2:"error",4:"SD",7:"SPACE",9:"NL",10:"participant",12:"AS",13:"restOfLine",16:"title",17:"text",18:"loop",19:"end",20:"opt",21:"alt",22:"else",23:"note",26:"over",29:",",30:"left_of",31:"right_of",33:"ACTOR",34:"SOLID_OPEN_ARROW",35:"DOTTED_OPEN_ARROW",36:"SOLID_ARROW",37:"DOTTED_ARROW",38:"SOLID_CROSS",39:"DOTTED_CROSS",40:"TXT"}, -productions_: [0,[3,2],[5,0],[5,2],[6,2],[6,1],[6,1],[8,5],[8,3],[8,2],[8,2],[8,4],[8,4],[8,4],[8,7],[15,4],[15,4],[28,2],[28,1],[27,3],[27,1],[24,1],[24,1],[14,4],[11,1],[32,1],[32,1],[32,1],[32,1],[32,1],[32,1],[25,1]], +symbols_: {"error":2,"start":3,"SPACE":4,"NL":5,"SD":6,"document":7,"line":8,"statement":9,"participant":10,"actor":11,"AS":12,"restOfLine":13,"signal":14,"activate":15,"deactivate":16,"note_statement":17,"title":18,"text":19,"loop":20,"end":21,"opt":22,"alt":23,"else":24,"note":25,"placement":26,"text2":27,"over":28,"actor_pair":29,"spaceList":30,",":31,"left_of":32,"right_of":33,"signaltype":34,"+":35,"-":36,"ACTOR":37,"SOLID_OPEN_ARROW":38,"DOTTED_OPEN_ARROW":39,"SOLID_ARROW":40,"DOTTED_ARROW":41,"SOLID_CROSS":42,"DOTTED_CROSS":43,"TXT":44,"$accept":0,"$end":1}, +terminals_: {2:"error",4:"SPACE",5:"NL",6:"SD",10:"participant",12:"AS",13:"restOfLine",15:"activate",16:"deactivate",18:"title",19:"text",20:"loop",21:"end",22:"opt",23:"alt",24:"else",25:"note",28:"over",31:",",32:"left_of",33:"right_of",35:"+",36:"-",37:"ACTOR",38:"SOLID_OPEN_ARROW",39:"DOTTED_OPEN_ARROW",40:"SOLID_ARROW",41:"DOTTED_ARROW",42:"SOLID_CROSS",43:"DOTTED_CROSS",44:"TXT"}, +productions_: [0,[3,2],[3,2],[3,2],[7,0],[7,2],[8,2],[8,1],[8,1],[9,5],[9,3],[9,2],[9,3],[9,3],[9,2],[9,4],[9,4],[9,4],[9,7],[17,4],[17,4],[30,2],[30,1],[29,3],[29,1],[26,1],[26,1],[14,5],[14,5],[14,4],[11,1],[34,1],[34,1],[34,1],[34,1],[34,1],[34,1],[27,1]], performAction: function anonymous(yytext, yyleng, yylineno, yy, yystate /* action[1] */, $$ /* vstack */, _$ /* lstack */) { /* this == yyval */ var $0 = $$.length - 1; switch (yystate) { -case 1: +case 3: yy.apply($$[$0]);return $$[$0]; break; -case 2: +case 4: this.$ = [] break; -case 3: +case 5: $$[$0-1].push($$[$0]);this.$ = $$[$0-1] break; -case 4: case 5: +case 6: case 7: this.$ = $$[$0] break; -case 6: +case 8: this.$=[]; break; -case 7: +case 9: $$[$0-3].description=$$[$0-1]; this.$=$$[$0-3]; break; -case 8: +case 10: this.$=$$[$0-1]; break; case 12: +this.$={type: 'activeStart', signalType: yy.LINETYPE.ACTIVE_START, actor: $$[$0-1]}; +break; +case 13: +this.$={type: 'activeEnd', signalType: yy.LINETYPE.ACTIVE_END, actor: $$[$0-1]}; +break; +case 16: $$[$0-1].unshift({type: 'loopStart', loopText:$$[$0-2], signalType: yy.LINETYPE.LOOP_START}); $$[$0-1].push({type: 'loopEnd', loopText:$$[$0-2], signalType: yy.LINETYPE.LOOP_END}); this.$=$$[$0-1]; break; -case 13: +case 17: $$[$0-1].unshift({type: 'optStart', optText:$$[$0-2], signalType: yy.LINETYPE.OPT_START}); $$[$0-1].push({type: 'optEnd', optText:$$[$0-2], signalType: yy.LINETYPE.OPT_END}); this.$=$$[$0-1]; break; -case 14: +case 18: // Alt start $$[$0-4].unshift({type: 'altStart', altText:$$[$0-5], signalType: yy.LINETYPE.ALT_START}); @@ -130,11 +136,11 @@ case 14: this.$=$$[$0-4]; break; -case 15: +case 19: this.$ = [$$[$0-1], {type:'addNote', placement:$$[$0-2], actor:$$[$0-1].actor, text:$$[$0]}]; break; -case 16: +case 20: // Coerce actor_pair into a [to, from, ...] array $$[$0-2] = [].concat($$[$0-1], $$[$0-1]).slice(0, 2); @@ -142,49 +148,59 @@ case 16: $$[$0-2][1] = $$[$0-2][1].actor; this.$ = [$$[$0-1], {type:'addNote', placement:yy.PLACEMENT.OVER, actor:$$[$0-2].slice(0, 2), text:$$[$0]}]; break; -case 19: +case 23: this.$ = [$$[$0-2], $$[$0]]; break; -case 20: +case 24: this.$ = $$[$0]; break; -case 21: +case 25: this.$ = yy.PLACEMENT.LEFTOF; break; -case 22: +case 26: this.$ = yy.PLACEMENT.RIGHTOF; break; -case 23: -this.$ = [$$[$0-3],$$[$0-1],{type: 'addMessage', from:$$[$0-3].actor, to:$$[$0-1].actor, signalType:$$[$0-2], msg:$$[$0]}] +case 27: + this.$ = [$$[$0-4],$$[$0-1],{type: 'addMessage', from:$$[$0-4].actor, to:$$[$0-1].actor, signalType:$$[$0-3], msg:$$[$0]}, + {type: 'activeStart', signalType: yy.LINETYPE.ACTIVE_START, actor: $$[$0-1]} + ] break; -case 24: +case 28: + this.$ = [$$[$0-4],$$[$0-1],{type: 'addMessage', from:$$[$0-4].actor, to:$$[$0-1].actor, signalType:$$[$0-3], msg:$$[$0]}, + {type: 'activeEnd', signalType: yy.LINETYPE.ACTIVE_END, actor: $$[$0-4]} + ] +break; +case 29: + this.$ = [$$[$0-3],$$[$0-1],{type: 'addMessage', from:$$[$0-3].actor, to:$$[$0-1].actor, signalType:$$[$0-2], msg:$$[$0]}] +break; +case 30: this.$={type: 'addActor', actor:$$[$0]} break; -case 25: +case 31: this.$ = yy.LINETYPE.SOLID_OPEN; break; -case 26: +case 32: this.$ = yy.LINETYPE.DOTTED_OPEN; break; -case 27: +case 33: this.$ = yy.LINETYPE.SOLID; break; -case 28: +case 34: this.$ = yy.LINETYPE.DOTTED; break; -case 29: +case 35: this.$ = yy.LINETYPE.SOLID_CROSS; break; -case 30: +case 36: this.$ = yy.LINETYPE.DOTTED_CROSS; break; -case 31: +case 37: this.$ = $$[$0].substring(1).trim().replace(/\\n/gm, "\n"); break; } }, -table: [{3:1,4:[1,2]},{1:[3]},o([1,7,9,10,16,18,20,21,23,33],$V0,{5:3}),{1:[2,1],6:4,7:$V1,8:6,9:$V2,10:$V3,11:15,14:9,15:10,16:$V4,18:$V5,20:$V6,21:$V7,23:$V8,33:$V9},o($Va,[2,3]),{8:18,10:$V3,11:15,14:9,15:10,16:$V4,18:$V5,20:$V6,21:$V7,23:$V8,33:$V9},o($Va,[2,5]),o($Va,[2,6]),{11:19,33:$V9},{9:[1,20]},{9:[1,21]},{7:[1,22]},{13:[1,23]},{13:[1,24]},{13:[1,25]},{32:26,34:[1,27],35:[1,28],36:[1,29],37:[1,30],38:[1,31],39:[1,32]},{24:33,26:[1,34],30:[1,35],31:[1,36]},o([9,12,29,34,35,36,37,38,39,40],[2,24]),o($Va,[2,4]),{9:[1,38],12:[1,37]},o($Va,[2,9]),o($Va,[2,10]),{17:[1,39]},o($Vb,$V0,{5:40}),o($Vb,$V0,{5:41}),o([7,9,10,16,18,20,21,22,23,33],$V0,{5:42}),{11:43,33:$V9},{33:[2,25]},{33:[2,26]},{33:[2,27]},{33:[2,28]},{33:[2,29]},{33:[2,30]},{11:44,33:$V9},{11:46,27:45,33:$V9},{33:[2,21]},{33:[2,22]},{13:[1,47]},o($Va,[2,8]),{9:[1,48]},{6:4,7:$V1,8:6,9:$V2,10:$V3,11:15,14:9,15:10,16:$V4,18:$V5,19:[1,49],20:$V6,21:$V7,23:$V8,33:$V9},{6:4,7:$V1,8:6,9:$V2,10:$V3,11:15,14:9,15:10,16:$V4,18:$V5,19:[1,50],20:$V6,21:$V7,23:$V8,33:$V9},{6:4,7:$V1,8:6,9:$V2,10:$V3,11:15,14:9,15:10,16:$V4,18:$V5,20:$V6,21:$V7,22:[1,51],23:$V8,33:$V9},{25:52,40:$Vc},{25:54,40:$Vc},{25:55,40:$Vc},{29:[1,56],40:[2,20]},{9:[1,57]},o($Va,[2,11]),o($Va,[2,12]),o($Va,[2,13]),{13:[1,58]},{9:[2,23]},{9:[2,31]},{9:[2,15]},{9:[2,16]},{11:59,33:$V9},o($Va,[2,7]),o($Vb,$V0,{5:60}),{40:[2,19]},{6:4,7:$V1,8:6,9:$V2,10:$V3,11:15,14:9,15:10,16:$V4,18:$V5,19:[1,61],20:$V6,21:$V7,23:$V8,33:$V9},o($Va,[2,14])], -defaultActions: {27:[2,25],28:[2,26],29:[2,27],30:[2,28],31:[2,29],32:[2,30],35:[2,21],36:[2,22],52:[2,23],53:[2,31],54:[2,15],55:[2,16],59:[2,19]}, +table: [{3:1,4:$V0,5:$V1,6:$V2},{1:[3]},{3:5,4:$V0,5:$V1,6:$V2},{3:6,4:$V0,5:$V1,6:$V2},o([1,4,5,10,15,16,18,20,22,23,25,37],$V3,{7:7}),{1:[2,1]},{1:[2,2]},{1:[2,3],4:$V4,5:$V5,8:8,9:10,10:$V6,11:21,14:13,15:$V7,16:$V8,17:16,18:$V9,20:$Va,22:$Vb,23:$Vc,25:$Vd,37:$Ve},o($Vf,[2,5]),{9:24,10:$V6,11:21,14:13,15:$V7,16:$V8,17:16,18:$V9,20:$Va,22:$Vb,23:$Vc,25:$Vd,37:$Ve},o($Vf,[2,7]),o($Vf,[2,8]),{11:25,37:$Ve},{5:[1,26]},{11:27,37:$Ve},{11:28,37:$Ve},{5:[1,29]},{4:[1,30]},{13:[1,31]},{13:[1,32]},{13:[1,33]},{34:34,38:[1,35],39:[1,36],40:[1,37],41:[1,38],42:[1,39],43:[1,40]},{26:41,28:[1,42],32:[1,43],33:[1,44]},o([5,12,31,38,39,40,41,42,43,44],[2,30]),o($Vf,[2,6]),{5:[1,46],12:[1,45]},o($Vf,[2,11]),{5:[1,47]},{5:[1,48]},o($Vf,[2,14]),{19:[1,49]},o($Vg,$V3,{7:50}),o($Vg,$V3,{7:51}),o([4,5,10,15,16,18,20,22,23,24,25,37],$V3,{7:52}),{11:55,35:[1,53],36:[1,54],37:$Ve},o($Vh,[2,31]),o($Vh,[2,32]),o($Vh,[2,33]),o($Vh,[2,34]),o($Vh,[2,35]),o($Vh,[2,36]),{11:56,37:$Ve},{11:58,29:57,37:$Ve},{37:[2,25]},{37:[2,26]},{13:[1,59]},o($Vf,[2,10]),o($Vf,[2,12]),o($Vf,[2,13]),{5:[1,60]},{4:$V4,5:$V5,8:8,9:10,10:$V6,11:21,14:13,15:$V7,16:$V8,17:16,18:$V9,20:$Va,21:[1,61],22:$Vb,23:$Vc,25:$Vd,37:$Ve},{4:$V4,5:$V5,8:8,9:10,10:$V6,11:21,14:13,15:$V7,16:$V8,17:16,18:$V9,20:$Va,21:[1,62],22:$Vb,23:$Vc,25:$Vd,37:$Ve},{4:$V4,5:$V5,8:8,9:10,10:$V6,11:21,14:13,15:$V7,16:$V8,17:16,18:$V9,20:$Va,22:$Vb,23:$Vc,24:[1,63],25:$Vd,37:$Ve},{11:64,37:$Ve},{11:65,37:$Ve},{27:66,44:$Vi},{27:68,44:$Vi},{27:69,44:$Vi},{31:[1,70],44:[2,24]},{5:[1,71]},o($Vf,[2,15]),o($Vf,[2,16]),o($Vf,[2,17]),{13:[1,72]},{27:73,44:$Vi},{27:74,44:$Vi},{5:[2,29]},{5:[2,37]},{5:[2,19]},{5:[2,20]},{11:75,37:$Ve},o($Vf,[2,9]),o($Vg,$V3,{7:76}),{5:[2,27]},{5:[2,28]},{44:[2,23]},{4:$V4,5:$V5,8:8,9:10,10:$V6,11:21,14:13,15:$V7,16:$V8,17:16,18:$V9,20:$Va,21:[1,77],22:$Vb,23:$Vc,25:$Vd,37:$Ve},o($Vf,[2,18])], +defaultActions: {5:[2,1],6:[2,2],43:[2,25],44:[2,26],66:[2,29],67:[2,37],68:[2,19],69:[2,20],73:[2,27],74:[2,28],75:[2,23]}, parseError: function parseError(str, hash) { if (hash.recoverable) { this.trace(str); @@ -193,7 +209,7 @@ parseError: function parseError(str, hash) { this.message = msg; this.hash = hash; } - _parseError.prototype = new Error(); + _parseError.prototype = Error; throw new _parseError(str, hash); } @@ -664,7 +680,7 @@ options: {"case-insensitive":true}, performAction: function anonymous(yy,yy_,$avoiding_name_collisions,YY_START) { var YYSTATE=YY_START; switch($avoiding_name_collisions) { -case 0:return 9; +case 0:return 5; break; case 1:/* skip all whitespace */ break; @@ -676,64 +692,72 @@ case 4:/* skip comments */ break; case 5: this.begin('ID'); return 10; break; -case 6: this.begin('ALIAS'); return 33; +case 6: this.begin('ALIAS'); return 37; break; case 7: this.popState(); this.popState(); this.begin('LINE'); return 12; break; -case 8: this.popState(); this.popState(); return 9; +case 8: this.popState(); this.popState(); return 5; break; -case 9: this.begin('LINE'); return 18; +case 9: this.begin('LINE'); return 20; break; -case 10: this.begin('LINE'); return 20; +case 10: this.begin('LINE'); return 22; break; -case 11: this.begin('LINE'); return 21; +case 11: this.begin('LINE'); return 23; break; -case 12: this.begin('LINE'); return 22; +case 12: this.begin('LINE'); return 24; break; case 13: this.popState(); return 13; break; -case 14:return 19; +case 14:return 21; break; -case 15:return 30; +case 15:return 32; break; -case 16:return 31; +case 16:return 33; break; -case 17:return 26; +case 17:return 28; break; -case 18:return 23; +case 18:return 25; break; -case 19:return 16; +case 19: this.begin('ID'); return 15; break; -case 20:return 4; +case 20: this.begin('ID'); return 16; break; -case 21:return 29; +case 21:return 18; break; -case 22:return 9; +case 22:return 6; break; -case 23:return 33; +case 23:return 31; break; -case 24:return 36; +case 24:return 5; break; -case 25:return 37; +case 25: yy_.yytext = yy_.yytext.trim(); return 37; break; -case 26:return 34; +case 26:return 40; break; -case 27:return 35; +case 27:return 41; break; case 28:return 38; break; case 29:return 39; break; -case 30:return 40; +case 30:return 42; +break; +case 31:return 43; +break; +case 32:return 44; +break; +case 33:return 35; +break; +case 34:return 36; break; -case 31:return 9; +case 35:return 5; break; -case 32:return 'INVALID'; +case 36:return 'INVALID'; break; } }, -rules: [/^(?:[\n]+)/i,/^(?:\s+)/i,/^(?:((?!\n)\s)+)/i,/^(?:#[^\n]*)/i,/^(?:%[^\n]*)/i,/^(?:participant\b)/i,/^(?:[^\->:\n,;]+?(?=((?!\n)\s)+as(?!\n)\s|[#\n;]|$))/i,/^(?:as\b)/i,/^(?:(?:))/i,/^(?:loop\b)/i,/^(?:opt\b)/i,/^(?:alt\b)/i,/^(?:else\b)/i,/^(?:[^#\n;]*)/i,/^(?:end\b)/i,/^(?:left of\b)/i,/^(?:right of\b)/i,/^(?:over\b)/i,/^(?:note\b)/i,/^(?:title\b)/i,/^(?:sequenceDiagram\b)/i,/^(?:,)/i,/^(?:;)/i,/^(?:[^\->:\n,;]+)/i,/^(?:->>)/i,/^(?:-->>)/i,/^(?:->)/i,/^(?:-->)/i,/^(?:-[x])/i,/^(?:--[x])/i,/^(?::[^#\n;]+)/i,/^(?:$)/i,/^(?:.)/i], -conditions: {"LINE":{"rules":[2,3,13],"inclusive":false},"ALIAS":{"rules":[2,3,7,8],"inclusive":false},"ID":{"rules":[2,3,6],"inclusive":false},"INITIAL":{"rules":[0,1,3,4,5,9,10,11,12,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32],"inclusive":true}} +rules: [/^(?:[\n]+)/i,/^(?:\s+)/i,/^(?:((?!\n)\s)+)/i,/^(?:#[^\n]*)/i,/^(?:%[^\n]*)/i,/^(?:participant\b)/i,/^(?:[^\->:\n,;]+?(?=((?!\n)\s)+as(?!\n)\s|[#\n;]|$))/i,/^(?:as\b)/i,/^(?:(?:))/i,/^(?:loop\b)/i,/^(?:opt\b)/i,/^(?:alt\b)/i,/^(?:else\b)/i,/^(?:[^#\n;]*)/i,/^(?:end\b)/i,/^(?:left of\b)/i,/^(?:right of\b)/i,/^(?:over\b)/i,/^(?:note\b)/i,/^(?:activate\b)/i,/^(?:deactivate\b)/i,/^(?:title\b)/i,/^(?:sequenceDiagram\b)/i,/^(?:,)/i,/^(?:;)/i,/^(?:[^\+\->:\n,;]+)/i,/^(?:->>)/i,/^(?:-->>)/i,/^(?:->)/i,/^(?:-->)/i,/^(?:-[x])/i,/^(?:--[x])/i,/^(?::[^#\n;]+)/i,/^(?:\+)/i,/^(?:-)/i,/^(?:$)/i,/^(?:.)/i], +conditions: {"LINE":{"rules":[2,3,13],"inclusive":false},"ALIAS":{"rules":[2,3,7,8],"inclusive":false},"ID":{"rules":[2,3,6],"inclusive":false},"INITIAL":{"rules":[0,1,3,4,5,9,10,11,12,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36],"inclusive":true}} }); return lexer; })(); diff --git a/src/diagrams/sequenceDiagram/sequenceDb.js b/src/diagrams/sequenceDiagram/sequenceDb.js index e924bd6845..70b9b2b3a8 100644 --- a/src/diagrams/sequenceDiagram/sequenceDb.js +++ b/src/diagrams/sequenceDiagram/sequenceDb.js @@ -65,7 +65,9 @@ exports.LINETYPE = { ALT_ELSE : 13 , ALT_END : 14 , OPT_START : 15 , - OPT_END : 16 + OPT_END : 16 , + ACTIVE_START : 17 , + ACTIVE_END : 18 }; exports.ARROWTYPE = { @@ -100,11 +102,17 @@ exports.apply = function(param){ exports.apply(item); }); } else { - // log.debug(param); + // console.info(param); switch(param.type){ case 'addActor': exports.addActor(param.actor, param.actor, param.description); break; + case 'activeStart': + exports.addSignal(param.actor, undefined, undefined, param.signalType); + break; + case 'activeEnd': + exports.addSignal(param.actor, undefined, undefined, param.signalType); + break; case 'addNote': exports.addNote(param.actor,param.placement, param.text); break; diff --git a/src/diagrams/sequenceDiagram/sequenceDiagram.spec.js b/src/diagrams/sequenceDiagram/sequenceDiagram.spec.js index effabd47e9..b6a429a895 100644 --- a/src/diagrams/sequenceDiagram/sequenceDiagram.spec.js +++ b/src/diagrams/sequenceDiagram/sequenceDiagram.spec.js @@ -130,7 +130,7 @@ describe('when parsing a sequenceDiagram',function() { }); it('it should handle in arrow messages', function () { var str = 'sequenceDiagram\n' + - 'Alice-->>Bob:Hello Bob, how are you?'; + 'Alice-->>Bob:Hello Bob, how are you?'; sq.parse(str); var actors = sq.yy.getActors(); @@ -142,6 +142,84 @@ describe('when parsing a sequenceDiagram',function() { expect(messages.length).toBe(1); expect(messages[0].type).toBe(sq.yy.LINETYPE.DOTTED); }); + it('it should handle actor activation', function () { + var str = 'sequenceDiagram\n' + + 'Alice-->>Bob:Hello Bob, how are you?\n' + + 'activate Bob\n' + + 'Bob-->>Alice:Hello Alice, I\'m fine and you?\n' + + 'deactivate Bob'; + + sq.parse(str); + var actors = sq.yy.getActors(); + expect(actors.Alice.description).toBe('Alice'); + expect(actors.Bob.description).toBe('Bob'); + + var messages = sq.yy.getMessages(); + + expect(messages.length).toBe(4); + expect(messages[0].type).toBe(sq.yy.LINETYPE.DOTTED); + expect(messages[1].type).toBe(sq.yy.LINETYPE.ACTIVE_START); + expect(messages[1].from.actor).toBe('Bob'); + expect(messages[2].type).toBe(sq.yy.LINETYPE.DOTTED); + expect(messages[3].type).toBe(sq.yy.LINETYPE.ACTIVE_END); + expect(messages[3].from.actor).toBe('Bob'); + }); + it('it should handle actor one line notation activation', function () { + var str = 'sequenceDiagram\n' + + 'Alice-->>+Bob:Hello Bob, how are you?\n' + + 'Bob-->>- Alice:Hello Alice, I\'m fine and you?'; + + sq.parse(str); + var actors = sq.yy.getActors(); + expect(actors.Alice.description).toBe('Alice'); + expect(actors.Bob.description).toBe('Bob'); + + var messages = sq.yy.getMessages(); + + expect(messages.length).toBe(4); + console.log('msg', messages[0]); + expect(messages[0].type).toBe(sq.yy.LINETYPE.DOTTED); + console.log('msg', messages[1]); + expect(messages[1].type).toBe(sq.yy.LINETYPE.ACTIVE_START); + expect(messages[1].from.actor).toBe('Bob'); + console.log('msg', messages[2]); + expect(messages[2].type).toBe(sq.yy.LINETYPE.DOTTED); + console.log('msg', messages[3]); + expect(messages[3].type).toBe(sq.yy.LINETYPE.ACTIVE_END); + expect(messages[3].from.actor).toBe('Bob'); + }); + it('it should handle stacked activations', function () { + var str = 'sequenceDiagram\n' + + 'Alice-->>+Bob:Hello Bob, how are you?\n' + + 'Bob-->>+Carol:Carol, let me introduce Alice?\n' + + 'Bob-->>- Alice:Hello Alice, please meet Carol?\n' + + 'Carol->>- Bob:Oh Bob, I\'m so happy to be here!'; + + sq.parse(str); + var actors = sq.yy.getActors(); + expect(actors.Alice.description).toBe('Alice'); + expect(actors.Bob.description).toBe('Bob'); + + var messages = sq.yy.getMessages(); + + expect(messages.length).toBe(8); + console.log('msg', messages[0]); + expect(messages[0].type).toBe(sq.yy.LINETYPE.DOTTED); + console.log('msg', messages[1]); + expect(messages[1].type).toBe(sq.yy.LINETYPE.ACTIVE_START); + expect(messages[1].from.actor).toBe('Bob'); + console.log('msg', messages[2]); + expect(messages[2].type).toBe(sq.yy.LINETYPE.DOTTED); + console.log('msg', messages[3]); + expect(messages[3].type).toBe(sq.yy.LINETYPE.ACTIVE_START); + expect(messages[3].from.actor).toBe('Carol'); + console.log('msg', messages[5]); + expect(messages[5].type).toBe(sq.yy.LINETYPE.ACTIVE_END); + expect(messages[5].from.actor).toBe('Bob'); + console.log('msg', messages[7]); + expect(messages[7].type).toBe(sq.yy.LINETYPE.ACTIVE_END); + expect(messages[7].from.actor).toBe('Carol'); + }); it('it should handle comments in a sequenceDiagram', function () { str = 'sequenceDiagram\n' + 'Alice->Bob: Hello Bob, how are you?\n'+ diff --git a/src/diagrams/sequenceDiagram/sequenceRenderer.js b/src/diagrams/sequenceDiagram/sequenceRenderer.js index 7d51ca3c41..6e9f7cf5cc 100644 --- a/src/diagrams/sequenceDiagram/sequenceRenderer.js +++ b/src/diagrams/sequenceDiagram/sequenceRenderer.js @@ -15,7 +15,7 @@ var conf = { diagramMarginY:10, // Margin between actors actorMargin:50, - // Width of actor moxes + // Width of actor boxes width:150, // Height of actor boxes height:65, @@ -29,7 +29,10 @@ var conf = { mirrorActors:false, // Depending on css styling this might need adjustment // Prolongs the edge of the diagram downwards - bottomMarginAdj:1 + bottomMarginAdj:1, + + // width of activation box + activationWidth:10 }; //var bb = getBBox('path'); @@ -42,9 +45,11 @@ exports.bounds = { }, verticalPos:0, - list: [], + sequenceItems: [], + activations: [], init : function(){ - this.list = []; + this.sequenceItems = []; + this.activations = []; this.data = { startx:undefined, stopx :undefined, @@ -60,24 +65,31 @@ exports.bounds = { obj[key] = fun(val,obj[key]); } }, - updateLoops:function(startx,starty,stopx,stopy){ + updateBounds:function(startx,starty,stopx,stopy){ var _self = this; var cnt = 0; - this.list.forEach(function(loop){ + function updateFn(type) { return function updateItemBounds(item) { cnt++; - // The loop list is a stack so the biggest margins in the beginning of the list - var n = _self.list.length-cnt+1; - - _self.updateVal(loop, 'startx',startx - n*conf.boxMargin, Math.min); - _self.updateVal(loop, 'starty',starty - n*conf.boxMargin, Math.min); - _self.updateVal(loop, 'stopx' ,stopx + n*conf.boxMargin, Math.max); - _self.updateVal(loop, 'stopy' ,stopy + n*conf.boxMargin, Math.max); - - _self.updateVal(exports.bounds.data,'startx',startx - n*conf.boxMargin ,Math.min); - _self.updateVal(exports.bounds.data,'starty',starty - n*conf.boxMargin ,Math.min); - _self.updateVal(exports.bounds.data,'stopx' ,stopx + n*conf.boxMargin ,Math.max); - _self.updateVal(exports.bounds.data,'stopy' ,stopy + n*conf.boxMargin ,Math.max); - }); + // The loop sequenceItems is a stack so the biggest margins in the beginning of the sequenceItems + var n = _self.sequenceItems.length-cnt+1; + + _self.updateVal(item, 'starty',starty - n*conf.boxMargin, Math.min); + _self.updateVal(item, 'stopy' ,stopy + n*conf.boxMargin, Math.max); + + _self.updateVal(exports.bounds.data, 'startx', startx - n * conf.boxMargin, Math.min); + _self.updateVal(exports.bounds.data, 'stopx', stopx + n * conf.boxMargin, Math.max); + + if (!(type == 'activation')) { + _self.updateVal(item, 'startx',startx - n*conf.boxMargin, Math.min); + _self.updateVal(item, 'stopx' ,stopx + n*conf.boxMargin, Math.max); + + _self.updateVal(exports.bounds.data, 'starty', starty - n * conf.boxMargin, Math.min); + _self.updateVal(exports.bounds.data, 'stopy', stopy + n * conf.boxMargin, Math.max); + } + }} + + this.sequenceItems.forEach(updateFn()); + this.activations.forEach(updateFn('activation')); }, insert:function(startx,starty,stopx,stopy){ @@ -93,22 +105,38 @@ exports.bounds = { this.updateVal(exports.bounds.data,'stopx' ,_stopx ,Math.max); this.updateVal(exports.bounds.data,'stopy' ,_stopy ,Math.max); - this.updateLoops(_startx,_starty,_stopx,_stopy); + this.updateBounds(_startx,_starty,_stopx,_stopy); }, + newActivation:function(message, diagram){ + var actorRect = sq.yy.getActors()[message.from.actor]; + var stackedSize = actorActivations(message.from.actor).length; + var x = actorRect.x + conf.width/2 + (stackedSize-1)*conf.activationWidth/2; + this.activations.push({startx:x,starty:this.verticalPos+2,stopx:x+conf.activationWidth,stopy:undefined, + actor: message.from.actor, + anchored: svgDraw.anchorElement(diagram) + }); + }, + endActivation:function(message){ + // find most recent activation for given actor + var lastActorActivationIdx = this.activations + .map(function(activation) { return activation.actor }) + .lastIndexOf(message.from.actor); + var activation = this.activations.splice(lastActorActivationIdx, 1)[0]; + return activation; + }, newLoop:function(title){ - this.list.push({startx:undefined,starty:this.verticalPos,stopx:undefined,stopy:undefined, title:title}); + this.sequenceItems.push({startx:undefined,starty:this.verticalPos,stopx:undefined,stopy:undefined, title:title}); }, endLoop:function(){ - var loop = this.list.pop(); - //loop.stopy = exports.bounds.getVerticalPos(); + var loop = this.sequenceItems.pop(); return loop; }, addElseToLoop:function(message){ - var loop = this.list.pop(); + var loop = this.sequenceItems.pop(); loop.elsey = exports.bounds.getVerticalPos(); loop.elseText = message; - this.list.push(loop); + this.sequenceItems.push(loop); }, bumpVerticalPos:function(bump){ this.verticalPos = this.verticalPos + bump; @@ -254,7 +282,7 @@ module.exports.drawActors = function(diagram, actors, actorKeys,verticalPos){ // Add some rendering data to the object actors[key].x = i*conf.actorMargin +i*conf.width; actors[key].y = verticalPos; - actors[key].width = conf.diagramMarginY; + actors[key].width = conf.diagramMarginX; actors[key].height = conf.diagramMarginY; // Draw the box with the attached line @@ -276,6 +304,23 @@ module.exports.setConf = function(cnf){ conf[key] = cnf[key]; }); }; + +var actorActivations = function(actor) { + return module.exports.bounds.activations.filter(function(activation) { + return activation.actor == actor; + }); +}; + +var actorFlowVerticaBounds = function(actor) { + // handle multiple stacked activations for same actor + var actors = sq.yy.getActors(); + var activations = actorActivations(actor); + + var left = activations.reduce(function(acc,activation) { return Math.min(acc,activation.startx)}, actors[actor].x + conf.width/2); + var right = activations.reduce(function(acc,activation) { return Math.max(acc,activation.stopx)}, actors[actor].x + conf.width/2); + return [left,right]; +}; + /** * Draws a flowchart in the tag with id: id based on the graph definition in text. * @param text @@ -328,6 +373,17 @@ module.exports.draw = function (text, id) { forceWidth); } break; + case sq.yy.LINETYPE.ACTIVE_START: + // exports.bounds.bumpVerticalPos(conf.boxMargin); + exports.bounds.newActivation(msg, diagram); + // exports.bounds.bumpVerticalPos(conf.boxMargin + conf.boxTextMargin); + break; + case sq.yy.LINETYPE.ACTIVE_END: + var activationData = exports.bounds.endActivation(msg); + svgDraw.drawActivation(diagram, activationData, exports.bounds.getVerticalPos(), conf); + + exports.bounds.insert(activationData.startx, exports.bounds.getVerticalPos() -10, activationData.stopx, exports.bounds.getVerticalPos()); + break; case sq.yy.LINETYPE.LOOP_START: exports.bounds.bumpVerticalPos(conf.boxMargin); exports.bounds.newLoop(msg.message); @@ -369,15 +425,22 @@ module.exports.draw = function (text, id) { exports.bounds.bumpVerticalPos(conf.boxMargin); break; default: + try { exports.bounds.bumpVerticalPos(conf.messageMargin); - startx = actors[msg.from].x + conf.width/2; - stopx = actors[msg.to].x + conf.width/2; + var fromBounds = actorFlowVerticaBounds(msg.from); + var toBounds = actorFlowVerticaBounds(msg.to); + var forward = fromBounds[0] < toBounds[0]; + startx = fromBounds[forward?1:0]; + stopx = toBounds[forward?0:1]; drawMessage(diagram, startx, stopx, exports.bounds.getVerticalPos(), msg); - + } catch (e) { + console.error('error while drawing message', e); + } } }); + if(conf.mirrorActors){ // Draw actors below diagram exports.bounds.bumpVerticalPos(conf.boxMargin*2); diff --git a/src/diagrams/sequenceDiagram/svgDraw.js b/src/diagrams/sequenceDiagram/svgDraw.js index 3daf223fe9..abc76cba67 100644 --- a/src/diagrams/sequenceDiagram/svgDraw.js +++ b/src/diagrams/sequenceDiagram/svgDraw.js @@ -121,6 +121,26 @@ exports.drawActor = function(elem, left, verticalPos, description,conf){ ; }; +exports.anchorElement = function(elem) { + return elem.append('g'); +}; +/** + * Draws an actor in the diagram with the attaced line + * @param elem - element to append activation rect + * @param bounds - activation box bounds + * @param verticalPos - precise y cooridnate of bottom activation box edge + */ +exports.drawActivation = function(elem,bounds,verticalPos){ + var rect = exports.getNoteRect(); + var g = bounds.anchored; + rect.x = bounds.startx; + rect.y = bounds.starty; + rect.fill = '#f4f4f4'; + rect.width = bounds.stopx - bounds.startx; + rect.height = verticalPos - bounds.starty; + exports.drawRect(g, rect); +}; + /** * Draws an actor in the diagram with the attaced line * @param center - The center of the the actor