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' axisFormat: '%Y-%m-%d'
}, },
/** /**
* The object containing configurations specific for sequence diagrams * The object containing configurations specific for journey diagrams
*/ */
journey: { journey: {
/** /**

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@@ -4,25 +4,33 @@
* MIT license. * MIT license.
*/ */
%lex %lex
%x string
%options case-insensitive %options case-insensitive
%{ %x string
// Pre-lexer code can go here %x title
%} %x open_directive
%x type_directive
%x arg_directive
%x close_directive
%% %%
\%\%\{ { 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 */ \%\%[^\n]* /* do nothing */
\s+ /* skip whitespace */ [\s]+ /* ignore */
"pie" return 'pie' ; title { console.log('starting title');this.begin("title");return 'title'; }
[\s\n\r]+ return 'NL' ; <title>([^(?:\n#;)]*) { this.popState(); return "title_value"; }
[\s]+ return 'space'; ["] { this.begin("string"); }
"title"\s[^#\n;]+ return 'title'; <string>["] { this.popState(); }
["] {/*console.log('begin str');*/this.begin("string");} <string>[^"]* { return "txt"; }
<string>["] {/*console.log('pop-state');*/this.popState();} "pie" return 'PIE';
<string>[^"]* {/*console.log('ending string')*/return "STR";} ":"[\s]*[\d]+(?:\.[\d]+)? return "value";
":"[\s]*[\d]+(?:\.[\d]+)? return "VALUE";
<<EOF>> return 'EOF'; <<EOF>> return 'EOF';
@@ -33,8 +41,9 @@
%% /* language grammar */ %% /* language grammar */
start start
// %{ : info document 'EOF' { return yy; } } : eol start { console.warn('NEWLINE start'); }
: pie document 'EOF' | directive start { console.warn('directive start'); }
| PIE document EOF { console.warn('PIE document EOF'); }
; ;
document document
@@ -43,15 +52,43 @@ document
; ;
line line
: statement { } : statement { $$ = $1 }
| 'NL' | eol { $$=[]; }
; ;
statement statement
: STR VALUE { :
/*console.log('str:'+$1+' value: '+$2)*/ | txt value { yy.addSection($1,yy.cleanupValue($2)); }
yy.addSection($1,yy.cleanupValue($2)); } | title title_value { $$=$2.trim();yy.setTitle($$); }
| title {yy.setTitle($1.substr(6));$$=$1.substr(6);} | 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(); pie.parser.yy.clear();
}); });
it('should handle simple pie', function() { 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(); const sections = pieDb.getSections();
console.log('sections: ', sections); console.log('sections: ', sections);
const section1 = sections['ash']; const section1 = sections['ash'];
expect(section1).toBe(60); 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() { 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(); const sections = pieDb.getSections();
console.log('sections: ', sections); console.log('sections: ', sections);
const section1 = sections['ash']; const section1 = sections['ash'];
@@ -30,7 +72,10 @@ describe('when parsing pie', function() {
it('should handle simple pie with negative decimal', function() { it('should handle simple pie with negative decimal', function() {
expect(() => { 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(); }).toThrowError();
}); });
}); });

View File

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

View File

@@ -153,7 +153,7 @@ export const draw = (txt, id) => {
}); });
} catch (e) { } catch (e) {
logger.error('Error while rendering info diagram'); 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 mermaidAPI from '../../mermaidAPI';
import configApi from '../../config';
let title = ''; let title = '';
let currentSection = ''; let currentSection = '';
@@ -118,6 +119,7 @@ const getActors = function() {
export default { export default {
parseDirective, parseDirective,
getConfig: () => configApi.getConfig().journey,
clear, clear,
setTitle, setTitle,
getTitle, getTitle,

View File

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