Merge pull request #265 from gibson042/sequenceDiagram-alias

Allow sequenceDiagram participant aliasing
This commit is contained in:
Knut Sveidqvist
2015-12-06 13:42:53 +01:00
3 changed files with 42 additions and 13 deletions

View File

@@ -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'; }
@@ -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}}
;

View File

@@ -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);

View File

@@ -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?';