From 54f2c63db14a2c9559bb088cdcc88c6c1169b20f Mon Sep 17 00:00:00 2001 From: Subhash Halder Date: Fri, 1 Sep 2023 20:57:05 +0530 Subject: [PATCH] Addressed all the new comment on jison --- .../src/diagrams/xychart/parser/xychart.jison | 104 +++++------------- .../xychart/parser/xychart.jison.spec.ts | 15 --- 2 files changed, 26 insertions(+), 93 deletions(-) diff --git a/packages/mermaid/src/diagrams/xychart/parser/xychart.jison b/packages/mermaid/src/diagrams/xychart/parser/xychart.jison index 705532727..6c5cb92a9 100644 --- a/packages/mermaid/src/diagrams/xychart/parser/xychart.jison +++ b/packages/mermaid/src/diagrams/xychart/parser/xychart.jison @@ -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'; } -((?:(?!\}\%\%)[^:.])*) { this.pushState('type_directive'); return 'type_directive'; } -":" { this.popState(); this.pushState('arg_directive'); return ':'; } -\}\%\% { this.popState(); this.popState(); return 'close_directive'; } -((?:(?!\}\%\%).|\n)*) return 'arg_directive'; \%\%(?!\{)[^\n]* /* skip comments */ [^\}]\%\%[^\n]* /* skip comments */ (\r?\n) { this.popState(); return 'NEWLINE'; } @@ -34,38 +26,37 @@ "accDescr"\s*":"\s* { this.pushState("acc_descr");return 'acc_descr'; } (?!\n|;|#)*[^\n]* { this.popState(); return "acc_descr_value"; } "accDescr"\s*"{"\s* { this.pushState("acc_descr_multiline");} -[\}] { this.popState(); } +"{" { this.popState(); } [^\}]* { 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"; } -[\[] { this.popState(); return 'SQUARE_BRACES_START'; } -[+-]?(?:\d+(?:\.\d+)?|\.\d+) { return 'NUMBER_WITH_DECIMAL'; } +"[" { this.pushState("axis_band_data"); return 'SQUARE_BRACES_START'; } "-->" { return 'ARROW_DELIMITER'; } "line" { this.pushState("data"); return 'LINE'; } "bar" { this.pushState("data"); return 'BAR'; } -[\[] { this.pushState("data_inner"); return 'SQUARE_BRACES_START'; } -[+-]?(?:\d+(?:\.\d+)?|\.\d+) { return 'NUMBER_WITH_DECIMAL';} -[\]] { this.popState(); return 'SQUARE_BRACES_END'; } +"[" { this.pushState("data_inner"); return 'SQUARE_BRACES_START'; } +[+-]?(?:\d+(?:\.\d+)?|\.\d+) { return 'NUMBER_WITH_DECIMAL'; } +"]" { this.popState(); return 'SQUARE_BRACES_END'; } -["][`] { this.pushState("md_string");} -[^`"]+ { return "MD_STR";} -[`]["] { this.popState();} +(?:"`) { this.pushState("md_string"); } +(?:(?!`\").)+ { return "MD_STR"; } +(?:`") { this.popState(); } ["] this.pushState("string"); ["] this.popState(); [^"]* 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 ; diff --git a/packages/mermaid/src/diagrams/xychart/parser/xychart.jison.spec.ts b/packages/mermaid/src/diagrams/xychart/parser/xychart.jison.spec.ts index 446d225ec..0515a4e94 100644 --- a/packages/mermaid/src/diagrams/xychart/parser/xychart.jison.spec.ts +++ b/packages/mermaid/src/diagrams/xychart/parser/xychart.jison.spec.ts @@ -9,7 +9,6 @@ const parserFnConstructor = (str: string) => { }; const mockDB: Record> = { - 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 =