mirror of
https://github.com/mermaid-js/mermaid.git
synced 2025-11-14 17:54:13 +01:00
Added full jison for bar and line chart
This commit is contained in:
@@ -21,6 +21,14 @@
|
||||
%x axis_data_band
|
||||
%x axis_data_band_capture
|
||||
%x axis_data_band_str
|
||||
%x line
|
||||
%x line_title
|
||||
%x line_data
|
||||
%x line_data_entries
|
||||
%x bar
|
||||
%x bar_title
|
||||
%x bar_data
|
||||
%x bar_data_entries
|
||||
%%
|
||||
\%\%\{ { this.begin('open_directive'); return 'open_directive'; }
|
||||
<open_directive>((?:(?!\}\%\%)[^:.])*) { this.begin('type_directive'); return 'type_directive'; }
|
||||
@@ -57,7 +65,7 @@ accDescr\s*"{"\s* { this.begin("acc_descr_multiline");}
|
||||
<x_axis,y_axis>[^\s]+" "*(\r?\n) {this.popState(); return 'AXIS_TITLE';}
|
||||
<x_axis,y_axis>[^\s]+" "* {this.begin("axis_data"); return 'AXIS_TITLE'; }
|
||||
|
||||
<axis_data>[+-]?\d+(\.\d+)?" "*"-->"" "*[+-]?\d+(\.\d+)?" "* { return 'AXIS_RANGE_DATA';}
|
||||
<axis_data>[+-]?\d+(?:\.\d+)?" "*"-->"" "*[+-]?\d+(?:\.\d+)?" "* { return 'AXIS_RANGE_DATA';}
|
||||
|
||||
<axis_data>[\[]" "* {this.begin("axis_data_band"); this.begin("axis_data_band_capture")}
|
||||
<axis_data_band>[,]" "* {this.begin("axis_data_band_capture")}
|
||||
@@ -66,11 +74,29 @@ accDescr\s*"{"\s* { this.begin("acc_descr_multiline");}
|
||||
<axis_data_band_str>["]" "* {this.popState(); this.popState();}
|
||||
<axis_data_band_capture>[^\s]+" "* {this.popState(); return "AXIS_BAND_DATA"}
|
||||
<axis_data_band>[\]]" "* {this.popState(); return "AXIS_BAND_DATA_END"}
|
||||
|
||||
|
||||
<axis_data>[\r\n]+ {this.popState(); this.popState();}
|
||||
|
||||
|
||||
"line"" "* {this.begin("line"); return 'LINE';}
|
||||
<line>["] {this.begin("line_title");}
|
||||
<line_title>[^"]+ {return 'LINE_TITLE';}
|
||||
<line_title>["]" "* {this.popState(); this.begin("line_data");}
|
||||
<line_data>"["" "* {this.begin('line_data_entries');}
|
||||
<line_data_entries>(?:[+-]?\d+(?:\.\d+)?)+(?:" "*[,]" "*(?:[+-]?\d+(?:\.\d+)?)+)*" "* {return 'LINE_DATA'}
|
||||
<line_data_entries>"]"" "* {this.popState(); this.popState(); this.popState()}
|
||||
<line>[^\s]+" "* {this.begin("line_data"); return 'LINE_TITLE';}
|
||||
|
||||
"bar"" "* {this.begin("bar"); return 'BAR';}
|
||||
<bar>["] {this.begin("bar_title");}
|
||||
<bar_title>[^"]+ {return 'BAR_TITLE';}
|
||||
<bar_title>["]" "* {this.popState(); this.begin("bar_data");}
|
||||
<bar_data>"["" "* {this.begin('bar_data_entries');}
|
||||
<bar_data_entries>(?:[+-]?\d+(?:\.\d+)?)+(?:" "*[,]" "*(?:[+-]?\d+(?:\.\d+)?)+)*" "* {return 'BAR_DATA'}
|
||||
<bar_data_entries>"]"" "* {this.popState(); this.popState(); this.popState()}
|
||||
<bar>[^\s]+" "* {this.begin("bar_data"); return 'BAR_TITLE';}
|
||||
|
||||
|
||||
|
||||
|
||||
["][`] { this.begin("md_string");}
|
||||
<md_string>[^`"]+ { return "MD_STR";}
|
||||
@@ -130,10 +156,20 @@ statement
|
||||
| directive
|
||||
| X_AXIS parseXAxis
|
||||
| Y_AXIS parseYAxis
|
||||
| parseLine
|
||||
| parseBar
|
||||
;
|
||||
|
||||
parseLine
|
||||
: LINE LINE_TITLE LINE_DATA {yy.addLineData($2.trim(), $3.split(',').map(d => Number(d.trim())));}
|
||||
;
|
||||
|
||||
parseBar
|
||||
: BAR BAR_TITLE BAR_DATA {yy.addBarData($2.trim(), $3.split(',').map(d => Number(d.trim())));}
|
||||
;
|
||||
|
||||
parseXAxis
|
||||
: AXIS_TITLE {yy.setXAxisTitle($1.trim());}
|
||||
: AXIS_TITLE statement {yy.setXAxisTitle($1.trim());}
|
||||
| AXIS_TITLE xAxisBandData {yy.setXAxisTitle($1.trim());}
|
||||
| AXIS_TITLE AXIS_RANGE_DATA {yy.setXAxisTitle($1.trim()); $$ = $2.split("-->"); yy.setXAxisRangeData(Number($$[0]), Number($$[1]));}
|
||||
;
|
||||
@@ -144,7 +180,7 @@ xAxisBandData
|
||||
;
|
||||
|
||||
parseYAxis
|
||||
: AXIS_TITLE {yy.setYAxisTitle($1.trim());}
|
||||
: AXIS_TITLE statement {yy.setYAxisTitle($1.trim());}
|
||||
| AXIS_TITLE AXIS_RANGE_DATA {yy.setYAxisTitle($1.trim()); $$ = $2.split("-->"); yy.setYAxisRangeData(Number($$[0]), Number($$[1]));}
|
||||
;
|
||||
|
||||
|
||||
@@ -17,6 +17,8 @@ const mockDB: Record<string, Mock<any, any>> = {
|
||||
setYAxisTitle: vi.fn(),
|
||||
setYAxisRangeData: vi.fn(),
|
||||
addYAxisBand: vi.fn(),
|
||||
addLineData: vi.fn(),
|
||||
addBarData: vi.fn(),
|
||||
};
|
||||
|
||||
function clearMocks() {
|
||||
@@ -104,8 +106,8 @@ describe('Testing xychart jison file', () => {
|
||||
expect(parserFnConstructor(str)).not.toThrow();
|
||||
expect(mockDB.setXAxisTitle).toHaveBeenCalledWith('xAxisName');
|
||||
expect(mockDB.addXAxisBand).toHaveBeenCalledTimes(2);
|
||||
expect(mockDB.addXAxisBand).toHaveBeenNthCalledWith(1, "cat2");
|
||||
expect(mockDB.addXAxisBand).toHaveBeenNthCalledWith(2, "cat1");
|
||||
expect(mockDB.addXAxisBand).toHaveBeenNthCalledWith(1, 'cat2');
|
||||
expect(mockDB.addXAxisBand).toHaveBeenNthCalledWith(2, 'cat1');
|
||||
});
|
||||
it('parse y-axis', () => {
|
||||
let str = 'xychart-beta \ny-axis yAxisName\n';
|
||||
@@ -135,6 +137,58 @@ describe('Testing xychart jison file', () => {
|
||||
expect(parserFnConstructor(str)).not.toThrow();
|
||||
expect(mockDB.setYAxisTitle).toHaveBeenCalledWith('yAxisName');
|
||||
expect(mockDB.setYAxisRangeData).toHaveBeenCalledWith(45.5, 33);
|
||||
});
|
||||
it('parse line Data', () => {
|
||||
let str = 'xychart-beta\nx-axis xAxisName\ny-axis yAxisName\n line lineTitle [23, 45, 56.6]';
|
||||
expect(parserFnConstructor(str)).not.toThrow();
|
||||
expect(mockDB.setYAxisTitle).toHaveBeenCalledWith('yAxisName');
|
||||
expect(mockDB.setXAxisTitle).toHaveBeenCalledWith('xAxisName');
|
||||
expect(mockDB.addLineData).toHaveBeenCalledWith('lineTitle', [23, 45, 56.6]);
|
||||
|
||||
clearMocks();
|
||||
|
||||
str =
|
||||
'xychart-beta\nx-axis xAxisName\ny-axis yAxisName\n line "lineTitle with space" [ +23 , -45 , 56.6 ] ';
|
||||
expect(parserFnConstructor(str)).not.toThrow();
|
||||
expect(mockDB.setYAxisTitle).toHaveBeenCalledWith('yAxisName');
|
||||
expect(mockDB.setXAxisTitle).toHaveBeenCalledWith('xAxisName');
|
||||
expect(mockDB.addLineData).toHaveBeenCalledWith('lineTitle with space', [23, -45, 56.6]);
|
||||
|
||||
clearMocks();
|
||||
str =
|
||||
'xychart-beta\nx-axis xAxisName\ny-axis yAxisName\n line "lineTitle with space" [ +23 , -4aa5 , 56.6 ] ';
|
||||
expect(parserFnConstructor(str)).toThrow();
|
||||
});
|
||||
it('parse bar Data', () => {
|
||||
let str = 'xychart-beta\nx-axis xAxisName\ny-axis yAxisName\n bar barTitle [23, 45, 56.6]';
|
||||
expect(parserFnConstructor(str)).not.toThrow();
|
||||
expect(mockDB.setYAxisTitle).toHaveBeenCalledWith('yAxisName');
|
||||
expect(mockDB.setXAxisTitle).toHaveBeenCalledWith('xAxisName');
|
||||
expect(mockDB.addBarData).toHaveBeenCalledWith('barTitle', [23, 45, 56.6]);
|
||||
|
||||
clearMocks();
|
||||
|
||||
str =
|
||||
'xychart-beta\nx-axis xAxisName\ny-axis yAxisName\n bar "barTitle with space" [ +23 , -45 , 56.6 ] ';
|
||||
expect(parserFnConstructor(str)).not.toThrow();
|
||||
expect(mockDB.setYAxisTitle).toHaveBeenCalledWith('yAxisName');
|
||||
expect(mockDB.setXAxisTitle).toHaveBeenCalledWith('xAxisName');
|
||||
expect(mockDB.addBarData).toHaveBeenCalledWith('barTitle with space', [23, -45, 56.6]);
|
||||
clearMocks();
|
||||
|
||||
str =
|
||||
'xychart-beta\nx-axis xAxisName\ny-axis yAxisName\n bar "barTitle with space" [ +23 , -4aa5 , 56.6 ] ';
|
||||
expect(parserFnConstructor(str)).toThrow();
|
||||
});
|
||||
it('parse multiple bar and line', () => {
|
||||
let str =
|
||||
'xychart-beta\nx-axis xAxisName\ny-axis yAxisName\n bar barTitle1 [23, 45, 56.6] \n line lineTitle1 [11, 45.5, 67, 23] \n bar barTitle2 [13, 42, 56.89] \n line lineTitle2 [45, 99, 012]';
|
||||
expect(parserFnConstructor(str)).not.toThrow();
|
||||
expect(mockDB.setYAxisTitle).toHaveBeenCalledWith('yAxisName');
|
||||
expect(mockDB.setXAxisTitle).toHaveBeenCalledWith('xAxisName');
|
||||
expect(mockDB.addBarData).toHaveBeenCalledWith('barTitle1', [23, 45, 56.6]);
|
||||
expect(mockDB.addBarData).toHaveBeenCalledWith('barTitle2', [13, 42, 56.89]);
|
||||
expect(mockDB.addLineData).toHaveBeenCalledWith('lineTitle1', [11, 45.5, 67, 23]);
|
||||
expect(mockDB.addLineData).toHaveBeenCalledWith('lineTitle2', [45, 99, 12]);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user