Skip to content

Commit 0c00846

Browse files
authored
Merge pull request #7570 from PinguinsRule/bug/7091_fix_parsing_bug
fix #7091: Ensure only one word is allowed between 'state' and '{'
2 parents 531e07c + 8810f62 commit 0c00846

3 files changed

Lines changed: 41 additions & 1 deletion

File tree

.changeset/tired-rockets-rule.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'mermaid': patch
3+
---
4+
5+
Fix invalid syntax between state and '{'

packages/mermaid/src/diagrams/state/parser/state-parser.spec.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,40 @@ describe('state parser can parse...', () => {
1414
stateDiagram.parser.yy.clear();
1515
});
1616

17+
describe('invalid name between state and curly bracket', () => {
18+
describe('valid syntax', () => {
19+
it('should only accept 1 word', () => {
20+
const diagramText = `stateDiagram-v2
21+
state valid { X }`;
22+
23+
stateDiagram.parser.parse(diagramText);
24+
25+
const states = stateDiagram.parser.yy.getStates();
26+
expect(states.get('valid')).not.toBeUndefined();
27+
});
28+
});
29+
30+
describe('invalid syntax', () => {
31+
it('should throw error with 2 words', () => {
32+
const diagramText = `stateDiagram-v2
33+
state invalid syntax { Y }`;
34+
35+
expect(() => {
36+
stateDiagram.parser.parse(diagramText);
37+
}).toThrow('Error: State name must be a single word.');
38+
});
39+
40+
it('should also throw with more than 2 words', () => {
41+
const diagramText = `stateDiagram-v2
42+
state invalid syntax with more than 2 words { Z }`;
43+
44+
expect(() => {
45+
stateDiagram.parser.parse(diagramText);
46+
}).toThrow('Error: State name must be a single word.');
47+
});
48+
});
49+
});
50+
1751
describe('states with id displayed as a (name)', () => {
1852
describe('syntax 1: stateID as "name in quotes"', () => {
1953
it('stateID as "some name"', () => {

packages/mermaid/src/diagrams/state/parser/stateDiagram.jison

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ accDescr\s*"{"\s* { this.begin("acc_descr_multili
130130
<STATE_ID>[^\n\{]* { if (!processId()) return; this.popState(); /* console.log('STATE_ID', yytext); */ return "ID"; }
131131
<STATE_STRING>["] { this.popState(); }
132132
<STATE_STRING>[^"]* { /* console.log('Long description:', yytext); */ return "STATE_DESCR"; }
133+
<STATE>\w+\s+\w+.*?\{ { throw new Error('Error: State name must be a single word. Found: "' + yytext.trim() + '"'); }
133134
<STATE>[^\n\s\{]+ { /* console.log('COMPOSIT_STATE', yytext); */ return 'COMPOSIT_STATE'; }
134135
<STATE>\n { this.popState(); }
135136
<INITIAL,STATE>\{ { this.popState(); this.pushState('struct'); /* console.log('begin struct', yytext); */ return 'STRUCT_START'; }
@@ -349,4 +350,4 @@ notePosition
349350
| right_of
350351
;
351352

352-
%%
353+
%%

0 commit comments

Comments
 (0)