Fixed parsers to use the correct type name in close_directive

Added directive support to pie
(Refactored pie parsing to be a bit more clear)
Added more unit tests to pie
This commit is contained in:
chris moran
2020-07-27 13:24:23 -04:00
parent b9fa2f4125
commit 771bf78576
15 changed files with 134 additions and 37 deletions

View File

@@ -593,7 +593,7 @@ const config = {
axisFormat: '%Y-%m-%d'
},
/**
* The object containing configurations specific for sequence diagrams
* The object containing configurations specific for journey diagrams
*/
journey: {
/**

View File

@@ -1,6 +1,6 @@
import { select } from 'd3';
import { logger } from '../../logger';
import { getConfig } from '../../config';
import configApi, { getConfig } from '../../config';
import common from '../common/common';
import utils from '../../utils';
import mermaidAPI from '../../mermaidAPI';
@@ -294,6 +294,7 @@ funs.push(setupToolTips);
export default {
parseDirective,
getConfig: () => configApi.getConfig().class,
addClass,
bindFunctions,
clear,

View File

@@ -256,7 +256,7 @@ argDirective
;
closeDirective
: close_directive { yy.parseDirective('}%%', 'close_directive', 'sequence'); }
: close_directive { yy.parseDirective('}%%', 'close_directive', 'class'); }
;
%%

View File

@@ -3,6 +3,7 @@
*/
import { logger } from '../../logger';
import mermaidAPI from '../../mermaidAPI';
import configApi from '../../config';
let entities = {};
let relationships = [];
@@ -73,6 +74,7 @@ export default {
Cardinality,
Identification,
parseDirective,
getConfig: () => configApi.getConfig().er,
addEntity,
getEntities,
addRelationship,

View File

@@ -111,7 +111,7 @@ argDirective
;
closeDirective
: close_directive { yy.parseDirective('}%%', 'close_directive', 'sequence'); }
: close_directive { yy.parseDirective('}%%', 'close_directive', 'er'); }
;
%%

View File

@@ -1,6 +1,6 @@
import { select } from 'd3';
import utils from '../../utils';
import { getConfig } from '../../config';
import configApi, { getConfig } from '../../config';
import common from '../common/common';
import mermaidAPI from '../../mermaidAPI';
@@ -622,6 +622,7 @@ const destructLink = (_str, _startStr) => {
export default {
parseDirective,
getConfig: () => configApi.getConfig().flowchart,
addVertex,
addLink,
updateLinkInterpolate,

View File

@@ -489,7 +489,7 @@ argDirective
;
closeDirective
: close_directive { yy.parseDirective('}%%', 'close_directive', 'sequence'); }
: close_directive { yy.parseDirective('}%%', 'close_directive', 'flowchart'); }
;
%%

View File

@@ -1,7 +1,7 @@
import moment from 'moment-mini';
import { sanitizeUrl } from '@braintree/sanitize-url';
import { logger } from '../../logger';
import { getConfig } from '../../config';
import configApi, { getConfig } from '../../config';
import utils from '../../utils';
import mermaidAPI from '../../mermaidAPI';
@@ -588,6 +588,7 @@ export const bindFunctions = function(element) {
export default {
parseDirective,
getConfig: () => configApi.getConfig().gantt,
clear,
setDateFormat,
getDateFormat,

View File

@@ -165,7 +165,7 @@ argDirective
;
closeDirective
: close_directive { yy.parseDirective('}%%', 'close_directive', 'sequence'); }
: close_directive { yy.parseDirective('}%%', 'close_directive', 'gantt'); }
;
%%

View File

@@ -4,26 +4,34 @@
* MIT license.
*/
%lex
%x string
%options case-insensitive
%{
// Pre-lexer code can go here
%}
%x string
%x title
%x open_directive
%x type_directive
%x arg_directive
%x close_directive
%%
\%\%[^\n]* /* do nothing */
\s+ /* skip whitespace */
"pie" return 'pie' ;
[\s\n\r]+ return 'NL' ;
[\s]+ return 'space';
"title"\s[^#\n;]+ return 'title';
["] {/*console.log('begin str');*/this.begin("string");}
<string>["] {/*console.log('pop-state');*/this.popState();}
<string>[^"]* {/*console.log('ending string')*/return "STR";}
":"[\s]*[\d]+(?:\.[\d]+)? return "VALUE";
<<EOF>> return 'EOF' ;
\%\%\{ { this.begin('open_directive'); return 'open_directive'; }
<open_directive>((?:(?!\}\%\%)[^:.])*) { this.begin('type_directive'); return 'type_directive'; }
<type_directive>":" { this.popState(); this.begin('arg_directive'); return ':'; }
<type_directive,arg_directive>\}\%\% { this.popState(); this.popState(); return 'close_directive'; }
<arg_directive>((?:(?!\}\%\%).|\n)*) return 'arg_directive';
\%\%(?!\{)[^\n]* /* skip comments */
[^\}]\%\%[^\n]* /* skip comments */{ console.log('Crap after close'); }
[\n\r]+ return 'NEWLINE';
\%\%[^\n]* /* do nothing */
[\s]+ /* ignore */
title { console.log('starting title');this.begin("title");return 'title'; }
<title>([^(?:\n#;)]*) { this.popState(); return "title_value"; }
["] { this.begin("string"); }
<string>["] { this.popState(); }
<string>[^"]* { return "txt"; }
"pie" return 'PIE';
":"[\s]*[\d]+(?:\.[\d]+)? return "value";
<<EOF>> return 'EOF';
/lex
@@ -33,8 +41,9 @@
%% /* language grammar */
start
// %{ : info document 'EOF' { return yy; } }
: pie document 'EOF'
: eol start { console.warn('NEWLINE start'); }
| directive start { console.warn('directive start'); }
| PIE document EOF { console.warn('PIE document EOF'); }
;
document
@@ -43,15 +52,43 @@ document
;
line
: statement { }
| 'NL'
: statement { $$ = $1 }
| eol { $$=[]; }
;
statement
: STR VALUE {
/*console.log('str:'+$1+' value: '+$2)*/
yy.addSection($1,yy.cleanupValue($2)); }
| title {yy.setTitle($1.substr(6));$$=$1.substr(6);}
:
| txt value { yy.addSection($1,yy.cleanupValue($2)); }
| title title_value { $$=$2.trim();yy.setTitle($$); }
| directive
;
directive
: openDirective typeDirective closeDirective
| openDirective typeDirective ':' argDirective closeDirective
;
eol
:
| SPACE eol
| NEWLINE eol
| ';' eol
;
openDirective
: open_directive { yy.parseDirective('%%{', 'open_directive'); }
;
typeDirective
: type_directive { yy.parseDirective($1, 'type_directive'); }
;
argDirective
: arg_directive { $1 = $1.trim().replace(/'/g, '"'); yy.parseDirective($1, 'arg_directive'); }
;
closeDirective
: close_directive { yy.parseDirective('}%%', 'close_directive', 'pie'); }
;
%%

View File

@@ -13,15 +13,57 @@ describe('when parsing pie', function() {
pie.parser.yy.clear();
});
it('should handle simple pie', function() {
const res = pie.parser.parse('pie \n"ash" : 60\n"bat" : 40\n');
const res = pie.parser.parse(`pie
"ash" : 60
"bat" : 40
`);
const sections = pieDb.getSections();
console.log('sections: ', sections);
const section1 = sections['ash'];
expect(section1).toBe(60);
});
it('should handle simple pie with comments', function() {
const res = pie.parser.parse(`pie
%% comments
"ash" : 60
"bat" : 40
`);
const sections = pieDb.getSections();
console.log('sections: ', sections);
const section1 = sections['ash'];
expect(section1).toBe(60);
});
it('should handle simple pie with a directive', function() {
const res = pie.parser.parse(`%%{init: {'logLevel':0}}%%
pie
"ash" : 60
"bat" : 40
`);
const sections = pieDb.getSections();
console.log('sections: ', sections);
const section1 = sections['ash'];
expect(section1).toBe(60);
});
it('should handle simple pie with a title', function() {
const res = pie.parser.parse(`pie title a 60/40 pie
"ash" : 60
"bat" : 40
`);
const sections = pieDb.getSections();
const title = pieDb.getTitle();
console.log('sections: ', sections);
const section1 = sections['ash'];
expect(section1).toBe(60);
expect(title).toBe('a 60/40 pie');
});
it('should handle simple pie with positive decimal', function() {
const res = pie.parser.parse('pie \n"ash" : 60.67\n"bat" : 40\n');
const res = pie.parser.parse(`pie
"ash" : 60.67
"bat" : 40
`);
const sections = pieDb.getSections();
console.log('sections: ', sections);
const section1 = sections['ash'];
@@ -30,7 +72,10 @@ describe('when parsing pie', function() {
it('should handle simple pie with negative decimal', function() {
expect(() => {
pie.parser.parse('pie \n"ash" : 60.67\n"bat" : 40..12\n');
pie.parser.parse(`pie
"ash" : 60.67
"bat" : 40..12
`);
}).toThrowError();
});
});

View File

@@ -2,10 +2,16 @@
*
*/
import { logger } from '../../logger';
import mermaidAPI from '../../mermaidAPI';
import configApi from '../../config';
let sections = {};
let title = '';
export const parseDirective = function(statement, context, type) {
mermaidAPI.parseDirective(this, statement, context, type);
};
const addSection = function(id, value) {
if (typeof sections[id] === 'undefined') {
sections[id] = value;
@@ -39,6 +45,8 @@ const clear = function() {
// }
export default {
parseDirective,
getConfig: () => configApi.getConfig().pie,
addSection,
getSections,
cleanupValue,

View File

@@ -153,7 +153,7 @@ export const draw = (txt, id) => {
});
} catch (e) {
logger.error('Error while rendering info diagram');
logger.error(e.message);
logger.error(e);
}
};

View File

@@ -1,4 +1,5 @@
import mermaidAPI from '../../mermaidAPI';
import configApi from '../../config';
let title = '';
let currentSection = '';
@@ -118,6 +119,7 @@ const getActors = function() {
export default {
parseDirective,
getConfig: () => configApi.getConfig().journey,
clear,
setTitle,
getTitle,

View File

@@ -82,7 +82,7 @@ argDirective
;
closeDirective
: close_directive { yy.parseDirective('}%%', 'close_directive', 'sequence'); }
: close_directive { yy.parseDirective('}%%', 'close_directive', 'journey'); }
;
%%