From 69ce29841d496d2e7acda8c2c7308ba722979222 Mon Sep 17 00:00:00 2001 From: Richard Gibson Date: Sun, 6 Dec 2015 05:51:02 -0500 Subject: [PATCH 1/2] Test for sequenceDiagram participant aliasing --- .../sequenceDiagram/sequenceDiagram.spec.js | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/diagrams/sequenceDiagram/sequenceDiagram.spec.js b/src/diagrams/sequenceDiagram/sequenceDiagram.spec.js index a2922858b3..effabd47e9 100644 --- a/src/diagrams/sequenceDiagram/sequenceDiagram.spec.js +++ b/src/diagrams/sequenceDiagram/sequenceDiagram.spec.js @@ -67,6 +67,25 @@ describe('when parsing a sequenceDiagram',function() { expect(messages[0].from).toBe('Alice'); expect(messages[1].from).toBe('Bob'); }); + it('it should alias participants', function () { + str = 'sequenceDiagram\n' + + 'participant A as Alice\n' + + 'participant B as Bob\n' + + 'A->B:Hello Bob, how are you?\n' + + 'B-->A: I am good thanks!'; + + sq.parse(str); + + var actors = sq.yy.getActors(); + expect(Object.keys(actors)).toEqual(['A', 'B']); + expect(actors.A.description).toBe('Alice'); + expect(actors.B.description).toBe('Bob'); + + var messages = sq.yy.getMessages(); + expect(messages.length).toBe(2); + expect(messages[0].from).toBe('A'); + expect(messages[1].from).toBe('B'); + }); it('it should handle in async messages', function () { var str = 'sequenceDiagram\n' + 'Alice-xBob:Hello Bob, how are you?'; From b42939796c959383ccacf931c1dbc4a0a5dfcfde Mon Sep 17 00:00:00 2001 From: Richard Gibson Date: Sun, 6 Dec 2015 05:53:08 -0500 Subject: [PATCH 2/2] Allow sequenceDiagram participant aliasing Fixes gh-263 --- .../parser/sequenceDiagram.jison | 25 +++++++++++-------- src/diagrams/sequenceDiagram/sequenceDb.js | 11 +++++--- 2 files changed, 23 insertions(+), 13 deletions(-) diff --git a/src/diagrams/sequenceDiagram/parser/sequenceDiagram.jison b/src/diagrams/sequenceDiagram/parser/sequenceDiagram.jison index 2af34f4ec3..47cbea8e95 100644 --- a/src/diagrams/sequenceDiagram/parser/sequenceDiagram.jison +++ b/src/diagrams/sequenceDiagram/parser/sequenceDiagram.jison @@ -12,17 +12,24 @@ %options case-insensitive +// Special states for recognizing aliases +%x ID +%x ALIAS + // A special state for grabbing text up to the first comment/newline %x LINE %% -[\n]+ return 'NL'; -\s+ /* skip all whitespace */ -((?!\n)\s)+ /* skip same-line whitespace */ -\#[^\n]* /* skip comments */ -\%%[^\n]* /* skip comments */ -"participant" return 'participant'; +[\n]+ return 'NL'; +\s+ /* skip all whitespace */ +((?!\n)\s)+ /* skip same-line whitespace */ +\#[^\n]* /* skip comments */ +\%%[^\n]* /* skip comments */ +"participant" { this.begin('ID'); return 'participant'; } +[^\->:\n,;]+?(?=((?!\n)\s)+"as"(?!\n)\s|[#\n;]|$) { this.begin('ALIAS'); return 'ACTOR'; } +"as" { this.popState(); this.popState(); this.begin('LINE'); return 'AS'; } +(?:) { this.popState(); this.popState(); return 'NL'; } "loop" { this.begin('LINE'); return 'loop'; } "opt" { this.begin('LINE'); return 'opt'; } "alt" { this.begin('LINE'); return 'alt'; } @@ -72,7 +79,8 @@ line ; statement - : 'participant' actor 'NL' {$$=$2;} + : 'participant' actor 'AS' restOfLine 'NL' {$2.description=$4; $$=$2;} + | 'participant' actor 'NL' {$$=$2;} | signal 'NL' | note_statement 'NL' | 'title' SPACE text 'NL' @@ -133,9 +141,6 @@ signal {$$ = [$1,$3,{type: 'addMessage', from:$1.actor, to:$3.actor, signalType:$2, msg:$4}]} ; -actors: actors actor - | actor - ; actor : ACTOR {$$={type: 'addActor', actor:$1}} ; diff --git a/src/diagrams/sequenceDiagram/sequenceDb.js b/src/diagrams/sequenceDiagram/sequenceDb.js index 284315bd0a..e924bd6845 100644 --- a/src/diagrams/sequenceDiagram/sequenceDb.js +++ b/src/diagrams/sequenceDiagram/sequenceDb.js @@ -2,7 +2,6 @@ * Created by knut on 14-11-19. */ var actors = {}; -var actorKeys = []; var messages = []; var notes = []; var Logger = require('../../logger'); @@ -11,8 +10,14 @@ var log = new Logger.Log(); exports.addActor = function(id,name,description){ + // Don't allow description nulling + var old = actors[id]; + if ( old && name === old.name && description == null ) return; + + // Don't allow null descriptions, either + if ( description == null ) description = name; + actors[id] = {name:name, description:description}; - actorKeys.push(id); }; exports.addMessage = function(idFrom, idTo, message, answer){ @@ -98,7 +103,7 @@ exports.apply = function(param){ // log.debug(param); switch(param.type){ case 'addActor': - exports.addActor(param.actor, param.actor, param.actor); + exports.addActor(param.actor, param.actor, param.description); break; case 'addNote': exports.addNote(param.actor,param.placement, param.text);