Skip to content

Commit

Permalink
Merge pull request #265 from gibson042/sequenceDiagram-alias
Browse files Browse the repository at this point in the history
Allow sequenceDiagram participant aliasing
  • Loading branch information
knsv committed Dec 6, 2015
2 parents 425a83c + b429397 commit 9fb980a
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 13 deletions.
25 changes: 15 additions & 10 deletions src/diagrams/sequenceDiagram/parser/sequenceDiagram.jison
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
<LINE>((?!\n)\s)+ /* skip same-line whitespace */
<INITIAL,LINE>\#[^\n]* /* skip comments */
\%%[^\n]* /* skip comments */
"participant" return 'participant';
[\n]+ return 'NL';
\s+ /* skip all whitespace */
<ID,ALIAS,LINE>((?!\n)\s)+ /* skip same-line whitespace */
<INITIAL,ID,ALIAS,LINE>\#[^\n]* /* skip comments */
\%%[^\n]* /* skip comments */
"participant" { this.begin('ID'); return 'participant'; }
<ID>[^\->:\n,;]+?(?=((?!\n)\s)+"as"(?!\n)\s|[#\n;]|$) { this.begin('ALIAS'); return 'ACTOR'; }
<ALIAS>"as" { this.popState(); this.popState(); this.begin('LINE'); return 'AS'; }
<ALIAS>(?:) { this.popState(); this.popState(); return 'NL'; }
"loop" { this.begin('LINE'); return 'loop'; }
"opt" { this.begin('LINE'); return 'opt'; }
"alt" { this.begin('LINE'); return 'alt'; }
Expand Down Expand Up @@ -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'
Expand Down Expand Up @@ -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}}
;
Expand Down
11 changes: 8 additions & 3 deletions src/diagrams/sequenceDiagram/sequenceDb.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
* Created by knut on 14-11-19.
*/
var actors = {};
var actorKeys = [];
var messages = [];
var notes = [];
var Logger = require('../../logger');
Expand All @@ -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){
Expand Down Expand Up @@ -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);
Expand Down
19 changes: 19 additions & 0 deletions src/diagrams/sequenceDiagram/sequenceDiagram.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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?';
Expand Down

0 comments on commit 9fb980a

Please sign in to comment.