Fixed a regression with classDiagram supporting comments

This commit is contained in:
chris moran 2020-07-27 07:38:12 -04:00
parent c4ad95760a
commit 1fc142a59f
No known key found for this signature in database
GPG Key ID: 7E303019E6BB02D7
2 changed files with 56 additions and 47 deletions

View File

@ -242,69 +242,74 @@ describe('class diagram, ', function () {
it('should handle comments at the start', function () { it('should handle comments at the start', function () {
const str = const str =
'%% Comment\n' + `%% Comment
'classDiagram\n' + classDiagram
'class Class1 {\n' + class Class1 {
'int : test\n' + int : test
'string : foo\n' + string : foo
'test()\n' + test()
'foo()\n' + foo()
'}'; }`;
parser.parse(str); parser.parse(str);
}); });
it('should handle comments at the end', function () { it('should handle comments at the end', function () {
const str = const str =
'classDiagram\n' + `classDiagram
'class Class1 {\n' + class Class1 {
'int : test\n' + int : test
'string : foo\n' + string : foo
'test()\n' + test()
'foo()\n' + foo()
'\n}' +
'%% Comment\n'; }
%% Comment
`;
parser.parse(str); parser.parse(str);
}); });
it('should handle comments at the end no trailing newline', function () { it('should handle comments at the end no trailing newline', function () {
const str = const str =
'classDiagram\n' + `classDiagram
'class Class1 {\n' + class Class1 {
'int : test\n' + int : test
'string : foo\n' + string : foo
'test()\n' + test()
'foo()\n' + foo()
'}\n' + }
'%% Comment'; %% Comment`;
parser.parse(str); parser.parse(str);
}); });
it('should handle a comment with multiple line feeds', function () { it('should handle a comment with multiple line feeds', function () {
const str = const str =
'classDiagram\n\n\n' + `classDiagram
'%% Comment\n\n' +
'class Class1 {\n' +
'int : test\n' + %% Comment
'string : foo\n' +
'test()\n' + class Class1 {
'foo()\n' + int : test
'}'; string : foo
test()
foo()
}`;
parser.parse(str); parser.parse(str);
}); });
it('should handle a comment with mermaid class diagram code in them', function () { it('should handle a comment with mermaid class diagram code in them', function () {
const str = const str =
'classDiagram\n' + `classDiagram
'%% Comment Class01 <|-- Class02\n' + %% Comment Class01 <|-- Class02
'class Class1 {\n' + class Class1 {
'int : test\n' + int : test
'string : foo\n' + string : foo
'test()\n' + test()
'foo()\n' + foo()
'}'; }`;
parser.parse(str); parser.parse(str);
}); });

View File

@ -17,11 +17,11 @@
<type_directive>":" { this.popState(); this.begin('arg_directive'); return ':'; } <type_directive>":" { this.popState(); this.begin('arg_directive'); return ':'; }
<type_directive,arg_directive>\}\%\% { this.popState(); this.popState(); return 'close_directive'; } <type_directive,arg_directive>\}\%\% { this.popState(); this.popState(); return 'close_directive'; }
<arg_directive>((?:(?!\}\%\%).|\n)*) return 'arg_directive'; <arg_directive>((?:(?!\}\%\%).|\n)*) return 'arg_directive';
\%%(?!\{)[^\n]* /* skip comments */ \%\%(?!\{)*[^\n]* /* skip comments */
[^\}]\%\%[^\n]* /* skip comments */ [^\}]\%\%*[^\n]* /* skip comments */
\%\%[^\n]*\n* /* do nothing */ \%\%*[^\n]*[\n]* /* do nothing */
\n+ return 'NEWLINE'; [\n]+ return 'NEWLINE';
\s+ /* skip whitespace */ [\s]+ /* skip whitespace */
"classDiagram-v2" return 'CLASS_DIAGRAM'; "classDiagram-v2" return 'CLASS_DIAGRAM';
"classDiagram" return 'CLASS_DIAGRAM'; "classDiagram" return 'CLASS_DIAGRAM';
[\{] { this.begin("struct"); /*console.log('Starting struct');*/return 'STRUCT_START';} [\{] { this.begin("struct"); /*console.log('Starting struct');*/return 'STRUCT_START';}
@ -146,11 +146,15 @@ mermaidDoc
; ;
graphConfig graphConfig
: CLASS_DIAGRAM NEWLINE statements EOF : NEWLINE
| NEWLINE graphConfig
| graphConfig NEWLINE
| CLASS_DIAGRAM NEWLINE statements EOF
; ;
statements statements
: statement : statement
| NEWLINE statement
| statement NEWLINE | statement NEWLINE
| statement NEWLINE statements | statement NEWLINE statements
; ;