Skip to content

Commit

Permalink
conjunction
Browse files Browse the repository at this point in the history
  • Loading branch information
stereobooster committed Nov 15, 2024
1 parent 0a64047 commit 692bb58
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 13 deletions.
28 changes: 18 additions & 10 deletions packages/parser/src/language/flowchart/flowchart.langium
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ entry Flowchart:
NEWLINE*
FlowchartType FlowchartDirection?
DELIMITER
((nodes+=FlowchartNode | edges+=FlowchartEdge | subgraphs+=FlowchartSubgraph | styles+=FlowchartStyle | interactions+=FlowchartInteraction) DELIMITER)*
((nodes+=FlowchartNode | edges+=(FlowchartEdge) | subgraphs+=FlowchartSubgraph | styles+=FlowchartStyle | interactions+=FlowchartInteraction) DELIMITER)*
;

fragment FlowchartDirection:
Expand All @@ -24,7 +24,7 @@ fragment FlowchartType:
FlowchartSubgraph:
"subgraph" id=ID DELIMITER
("direction" FlowchartDirection DELIMITER)?
((nodes+=FlowchartNode | edges+=FlowchartEdge | subgraphs+=FlowchartSubgraph) DELIMITER)*
((nodes+=FlowchartNode | edges+=(FlowchartEdge) | subgraphs+=FlowchartSubgraph) DELIMITER)*
"end";

terminal EllipseLabel: "(-" -> "-)";
Expand Down Expand Up @@ -99,6 +99,11 @@ FlowchartNode:
FlowchartNodeSubroutine |
FlowchartNodeHexagon;

terminal AMP: "&";

FlowchartNodeConjunction:
nodes+=FlowchartNode (AMP nodes+=FlowchartNode)+;

terminal fragment ArrowStart: "<" | "x" | "o";
terminal fragment ArrowEnd: ">" | "x" | "o";
terminal fragment DottedArrowEnd: "."* ".-" ArrowEnd?;
Expand All @@ -119,22 +124,25 @@ terminal InvisibleArrow: "~~" "~"+;
terminal EdgeLabel: "|" -> "|";

FlowchartEdge:
start=FlowchartNode ends+=(FlowchartEdgeRegular | FlowchartEdgeDotted | FlowchartEdgeThick | FlowchartEdgeInvisible)+;
start=(FlowchartNode|FlowchartNodeConjunction) ends+=(FlowchartEdgeRegular | FlowchartEdgeDotted | FlowchartEdgeThick | FlowchartEdgeInvisible)+;

// FlowchartEdgeConjunction:
// edges+=FlowchartEdge (AMP edges+=FlowchartEdge)+;

FlowchartEdgeDotted:
(arrow=DottedArrow (label=EdgeLabel)? end=FlowchartNode) |
(arrow=DottedArrowWithLabel end=FlowchartNode);
(arrow=DottedArrow (label=EdgeLabel)? end=(FlowchartNode|FlowchartNodeConjunction)) |
(arrow=DottedArrowWithLabel end=(FlowchartNode|FlowchartNodeConjunction));

FlowchartEdgeRegular:
(arrow=RegularArrow (label=EdgeLabel)? end=FlowchartNode) |
(arrow=RegularArrowWithLabel end=FlowchartNode);
(arrow=RegularArrow (label=EdgeLabel)? end=(FlowchartNode|FlowchartNodeConjunction)) |
(arrow=RegularArrowWithLabel end=(FlowchartNode|FlowchartNodeConjunction));

FlowchartEdgeThick:
(arrow=ThickArrow (label=EdgeLabel)? end=FlowchartNode) |
(arrow=ThickArrowWithLabel end=FlowchartNode);
(arrow=ThickArrow (label=EdgeLabel)? end=(FlowchartNode|FlowchartNodeConjunction)) |
(arrow=ThickArrowWithLabel end=(FlowchartNode|FlowchartNodeConjunction));

FlowchartEdgeInvisible:
arrow=(InvisibleArrow) (label=EdgeLabel)? end=FlowchartNode;
arrow=(InvisibleArrow) (label=EdgeLabel)? end=(FlowchartNode|FlowchartNodeConjunction);

FlowchartStyle:
"style" id=ID items+=FlowchartStylePair ("," items+=FlowchartStylePair)*;
Expand Down
22 changes: 19 additions & 3 deletions packages/parser/tests/flowchart.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -354,9 +354,25 @@ describe('flowchart', () => {
});
});

// describe('conjunction', () => {
// // conjunction: a --> b & c--> d, A & B--> C & D
// });
describe('conjunction', () => {
it('should handle nodes conjunction', () => {
const result = parse(`flowchart; A & B --> C & D;`);
expectNoErrorsOrAlternatives(result);
expect(result.value.edges).toHaveLength(1);
expect(result.value.edges[0].start.nodes).toHaveLength(2);
expect(result.value.edges[0].start.nodes[0].id).toBe('A');
expect(result.value.edges[0].start.nodes[1].id).toBe('B');
expect(result.value.edges[0].ends[0].end.nodes[0].id).toBe('C');
expect(result.value.edges[0].ends[0].end.nodes[1].id).toBe('D');
});

it.skip('should handle edges conjunction', () => {
const result = parse(`flowchart; A --> B & C --> D;`);
expectNoErrorsOrAlternatives(result);
expect(result.value.edges).toHaveLength(1);
expect(result.value.edges[0].edges).toHaveLength(2);
});
});

// TODO: string (current mermaid doesn't support escape chars), markdown string, html escapes
// TODO: extract label, start, end and length from arrows
Expand Down

0 comments on commit 692bb58

Please sign in to comment.