mirror of
https://github.com/mermaid-js/mermaid.git
synced 2025-09-21 16:29:40 +02:00
Addressed all the new comment on jison
This commit is contained in:
@@ -4,22 +4,14 @@
|
||||
%x string
|
||||
%x md_string
|
||||
%x title
|
||||
%x open_directive
|
||||
%x type_directive
|
||||
%x arg_directive
|
||||
%x close_directive
|
||||
%x acc_title
|
||||
%x acc_descr
|
||||
%x acc_descr_multiline
|
||||
%s axis_data
|
||||
%s axis_band_data
|
||||
%s data
|
||||
%s data_inner
|
||||
%%
|
||||
\%\%\{ { this.pushState('open_directive'); return 'open_directive'; }
|
||||
<open_directive>((?:(?!\}\%\%)[^:.])*) { this.pushState('type_directive'); return 'type_directive'; }
|
||||
<type_directive>":" { this.popState(); this.pushState('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 */
|
||||
<axis_data>(\r?\n) { this.popState(); return 'NEWLINE'; }
|
||||
@@ -34,38 +26,37 @@
|
||||
"accDescr"\s*":"\s* { this.pushState("acc_descr");return 'acc_descr'; }
|
||||
<acc_descr>(?!\n|;|#)*[^\n]* { this.popState(); return "acc_descr_value"; }
|
||||
"accDescr"\s*"{"\s* { this.pushState("acc_descr_multiline");}
|
||||
<acc_descr_multiline>[\}] { this.popState(); }
|
||||
<acc_descr_multiline>"{" { this.popState(); }
|
||||
<acc_descr_multiline>[^\}]* { return "acc_descr_multiline_value"; }
|
||||
|
||||
"xychart-beta" {return 'XYCHART';}
|
||||
("vertical"|"horizontal") {return 'CHART_ORIENTATION'}
|
||||
(?:"vertical"|"horizontal") {return 'CHART_ORIENTATION'}
|
||||
|
||||
"x-axis" { this.pushState("axis_data"); return "X_AXIS"; }
|
||||
"y-axis" { this.pushState("axis_data"); return "Y_AXIS"; }
|
||||
<axis_data>[\[] { this.popState(); return 'SQUARE_BRACES_START'; }
|
||||
<axis_data>[+-]?(?:\d+(?:\.\d+)?|\.\d+) { return 'NUMBER_WITH_DECIMAL'; }
|
||||
<axis_data>"[" { this.pushState("axis_band_data"); return 'SQUARE_BRACES_START'; }
|
||||
<axis_data>"-->" { return 'ARROW_DELIMITER'; }
|
||||
|
||||
|
||||
"line" { this.pushState("data"); return 'LINE'; }
|
||||
"bar" { this.pushState("data"); return 'BAR'; }
|
||||
<data>[\[] { this.pushState("data_inner"); return 'SQUARE_BRACES_START'; }
|
||||
<data_inner>[+-]?(?:\d+(?:\.\d+)?|\.\d+) { return 'NUMBER_WITH_DECIMAL';}
|
||||
<data_inner>[\]] { this.popState(); return 'SQUARE_BRACES_END'; }
|
||||
<data>"[" { this.pushState("data_inner"); return 'SQUARE_BRACES_START'; }
|
||||
<axis_data,data_inner>[+-]?(?:\d+(?:\.\d+)?|\.\d+) { return 'NUMBER_WITH_DECIMAL'; }
|
||||
<data_inner,axis_band_data>"]" { this.popState(); return 'SQUARE_BRACES_END'; }
|
||||
|
||||
|
||||
|
||||
|
||||
["][`] { this.pushState("md_string");}
|
||||
<md_string>[^`"]+ { return "MD_STR";}
|
||||
<md_string>[`]["] { this.popState();}
|
||||
(?:"`) { this.pushState("md_string"); }
|
||||
<md_string>(?:(?!`\").)+ { return "MD_STR"; }
|
||||
<md_string>(?:`") { this.popState(); }
|
||||
["] this.pushState("string");
|
||||
<string>["] this.popState();
|
||||
<string>[^"]* return "STR";
|
||||
|
||||
|
||||
[\[] return 'SQUARE_BRACES_START'
|
||||
[\]] return 'SQUARE_BRACES_END'
|
||||
"[" return 'SQUARE_BRACES_START'
|
||||
"]" return 'SQUARE_BRACES_END'
|
||||
[A-Za-z]+ return 'ALPHA';
|
||||
":" return 'COLON';
|
||||
\+ return 'PLUS';
|
||||
@@ -90,7 +81,6 @@
|
||||
|
||||
start
|
||||
: eol start
|
||||
| directive start
|
||||
| XYCHART chartConfig start
|
||||
| XYCHART start
|
||||
| document
|
||||
@@ -117,25 +107,14 @@ statement
|
||||
;
|
||||
|
||||
plotData
|
||||
: SQUARE_BRACES_START commaSeperateNumber SQUARE_BRACES_END { $$ = $commaSeperateNumber }
|
||||
: SQUARE_BRACES_START commaSeparatedNumbers SQUARE_BRACES_END { $$ = $commaSeparatedNumbers }
|
||||
;
|
||||
|
||||
commaSeperateNumber
|
||||
: NUMBER_WITH_DECIMAL commaSeperateNumberValues {
|
||||
$commaSeperateNumberValues = $commaSeperateNumberValues || [];
|
||||
$commaSeperateNumberValues.unshift(Number($NUMBER_WITH_DECIMAL));
|
||||
$$ = $commaSeperateNumberValues
|
||||
}
|
||||
commaSeparatedNumbers
|
||||
: NUMBER_WITH_DECIMAL COMMA commaSeparatedNumbers { $$ = [Number($NUMBER_WITH_DECIMAL), ...$commaSeparatedNumbers] }
|
||||
| NUMBER_WITH_DECIMAL { $$ = [Number($NUMBER_WITH_DECIMAL)] }
|
||||
;
|
||||
|
||||
commaSeperateNumberValues
|
||||
: COMMA NUMBER_WITH_DECIMAL commaSeperateNumberValues {
|
||||
$commaSeperateNumberValues = $commaSeperateNumberValues || [];
|
||||
$commaSeperateNumberValues.unshift(Number($NUMBER_WITH_DECIMAL));
|
||||
$$ = $commaSeperateNumberValues
|
||||
}
|
||||
|;
|
||||
|
||||
parseXAxis
|
||||
: text {yy.setXAxisTitle($text);}
|
||||
| text bandData {yy.setXAxisTitle($text); yy.setXAxisBand($bandData);}
|
||||
@@ -143,66 +122,35 @@ parseXAxis
|
||||
;
|
||||
|
||||
bandData
|
||||
: SQUARE_BRACES_START commaSeperateText SQUARE_BRACES_END {$$ = $commaSeperateText}
|
||||
: SQUARE_BRACES_START commaSeparatedTexts SQUARE_BRACES_END {$$ = $commaSeparatedTexts}
|
||||
;
|
||||
|
||||
commaSeperateText
|
||||
: text commaSeperateTextValues {
|
||||
$commaSeperateTextValues = $commaSeperateTextValues || [];
|
||||
$commaSeperateTextValues.unshift($text);
|
||||
$$ = $commaSeperateTextValues
|
||||
}
|
||||
commaSeparatedTexts
|
||||
: text COMMA commaSeparatedTexts { $$ = [$text, ...$commaSeparatedTexts] }
|
||||
| text { $$ = [$text] }
|
||||
;
|
||||
|
||||
commaSeperateTextValues
|
||||
: COMMA text commaSeperateTextValues {
|
||||
$commaSeperateTextValues = $commaSeperateTextValues || [];
|
||||
$commaSeperateTextValues.unshift($text);
|
||||
$$ = $commaSeperateTextValues
|
||||
}
|
||||
|;
|
||||
|
||||
parseYAxis
|
||||
: text {yy.setYAxisTitle($text);}
|
||||
| text NUMBER_WITH_DECIMAL ARROW_DELIMITER NUMBER_WITH_DECIMAL {yy.setYAxisTitle($text); yy.setYAxisRangeData(Number($NUMBER_WITH_DECIMAL1), Number($NUMBER_WITH_DECIMAL2));}
|
||||
;
|
||||
|
||||
directive
|
||||
: openDirective typeDirective closeDirective
|
||||
| openDirective typeDirective ':' argDirective closeDirective
|
||||
;
|
||||
|
||||
eol
|
||||
: NEWLINE
|
||||
| SEMI
|
||||
| EOF
|
||||
;
|
||||
|
||||
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', 'xychart'); }
|
||||
;
|
||||
|
||||
text: alphaNum { $$={text:$alphaNum, type: 'text'};}
|
||||
| STR { $$={text: $STR, type: 'text'};}
|
||||
| MD_STR { $$={text: $MD_STR, type: 'markdown'};}
|
||||
;
|
||||
| STR { $$={text: $STR, type: 'text'};}
|
||||
| MD_STR { $$={text: $MD_STR, type: 'markdown'};}
|
||||
;
|
||||
|
||||
alphaNum
|
||||
: alphaNumToken {$$=$alphaNumToken;}
|
||||
| alphaNum alphaNumToken {$$=$alphaNum+''+$alphaNumToken;}
|
||||
;
|
||||
: alphaNumToken {$$=$alphaNumToken;}
|
||||
| alphaNum alphaNumToken {$$=$alphaNum+''+$alphaNumToken;}
|
||||
;
|
||||
|
||||
|
||||
alphaNumToken : AMP | NUM | ALPHA | PLUS | EQUALS | MULT | DOT | BRKT| MINUS | UNDERSCORE ;
|
||||
|
@@ -9,7 +9,6 @@ const parserFnConstructor = (str: string) => {
|
||||
};
|
||||
|
||||
const mockDB: Record<string, Mock<any, any>> = {
|
||||
parseDirective: vi.fn(),
|
||||
setOrientation: vi.fn(),
|
||||
setDiagramTitle: vi.fn(),
|
||||
setXAxisTitle: vi.fn(),
|
||||
@@ -54,19 +53,6 @@ describe('Testing xychart jison file', () => {
|
||||
expect(mockDB.setDiagramTitle).toHaveBeenCalledWith('oneLinertitle');
|
||||
});
|
||||
|
||||
it('should be able to parse directive', () => {
|
||||
const str =
|
||||
'%%{init: {"xychart": {"chartWidth": 600, "chartHeight": 600} } }%% \n xychart-beta';
|
||||
expect(parserFnConstructor(str)).not.toThrow();
|
||||
expect(mockDB.parseDirective.mock.calls[0]).toEqual(['%%{', 'open_directive']);
|
||||
expect(mockDB.parseDirective.mock.calls[1]).toEqual(['init', 'type_directive']);
|
||||
expect(mockDB.parseDirective.mock.calls[2]).toEqual([
|
||||
'{"xychart": {"chartWidth": 600, "chartHeight": 600} }',
|
||||
'arg_directive',
|
||||
]);
|
||||
expect(mockDB.parseDirective.mock.calls[3]).toEqual(['}%%', 'close_directive', 'xychart']);
|
||||
});
|
||||
|
||||
it('parse chart orientation', () => {
|
||||
const str = 'xychart-beta vertical';
|
||||
expect(parserFnConstructor(str)).not.toThrow();
|
||||
@@ -339,7 +325,6 @@ describe('Testing xychart jison file', () => {
|
||||
expect(mockDB.setYAxisTitle).toHaveBeenCalledWith({ text: 'yAxisName', type: 'text' });
|
||||
expect(mockDB.setXAxisTitle).toHaveBeenCalledWith({ text: 'xAxisName', type: 'text' });
|
||||
expect(mockDB.setBarData).toHaveBeenCalledWith({ text: '', type: 'text' }, [23, -45, 56.6]);
|
||||
clearMocks();
|
||||
});
|
||||
it('parse bar should throw for unbalanced brackets', () => {
|
||||
let str =
|
||||
|
Reference in New Issue
Block a user