Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit2e15e07

Browse files
committed
fix(parser): improve member detection after colon in Mermaid#438
Enhance MermaidParser to check for member definitions following a colon, ensuring more accurate parsing of class diagram statements. Added related test coverage and parser method for class diagrams.
1 parent32075c4 commit2e15e07

File tree

3 files changed

+74
-23
lines changed

3 files changed

+74
-23
lines changed

‎exts/ext-diagram/src/241/main/kotlin/cc/unitmesh/diagram/parser/MermaidClassDiagramParser.kt‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,13 @@ class MermaidClassDiagramParser {
4141
}
4242
}
4343

44+
/**
45+
* Parse Mermaid class diagram from AST
46+
*/
47+
funparse(ast:ClassDiagramNode):GraphvizDiagramData {
48+
return convertAstToGraphvizData(ast)
49+
}
50+
4451
/**
4552
* Convert AST to GraphvizDiagramData
4653
*/

‎exts/ext-diagram/src/241/main/kotlin/cc/unitmesh/diagram/parser/mermaid/MermaidParser.kt‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -597,14 +597,18 @@ class MermaidParser(private val tokens: List<MermaidToken>) {
597597
try {
598598
if (check(TokenType.ALPHA,TokenType.BQUOTE_STR)) {
599599
advance()
600-
val hasColon= check(TokenType.COLON)
601-
position= saved
602-
return hasColon
600+
if (check(TokenType.COLON)) {
601+
advance()
602+
// Check if the next token is a member definition
603+
val hasMember= check(TokenType.LABEL,TokenType.MEMBER)
604+
position= saved
605+
return hasMember
606+
}
603607
}
604608
}catch (e:Exception) {
605609
position= saved
606610
}
607-
611+
608612
returnfalse
609613
}
610614

‎exts/ext-diagram/src/241/test/kotlin/cc/unitmesh/diagram/parser/MermaidLexerTest.kt‎

Lines changed: 59 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package cc.unitmesh.diagram.parser
22

33
importcc.unitmesh.diagram.parser.mermaid.MermaidLexer
44
importcc.unitmesh.diagram.parser.mermaid.TokenType
5+
importcc.unitmesh.diagram.parser.mermaid.MermaidToken
56
importkotlin.test.Test
67
importkotlin.test.assertEquals
78
importkotlin.test.assertTrue
@@ -73,28 +74,67 @@ class MermaidLexerTest {
7374
User : Added
7475
""".trimIndent()
7576

76-
val parser= cc.unitmesh.diagram.parser.mermaid.MermaidParser(cc.unitmesh.diagram.parser.mermaid.MermaidLexer(input).tokenize())
77+
val lexer= cc.unitmesh.diagram.parser.mermaid.MermaidLexer(input)
78+
val tokens= lexer.tokenize()
79+
80+
// Verify lexer produces expected tokens
81+
val expectedTokenTypes=listOf(
82+
TokenType.CLASS_DIAGRAM,
83+
TokenType.NEWLINE,
84+
TokenType.CLASS,
85+
TokenType.ALPHA,// User
86+
TokenType.STRUCT_START,
87+
TokenType.MEMBER,// +name
88+
TokenType.MEMBER,// +email
89+
TokenType.STRUCT_STOP,
90+
TokenType.NEWLINE,
91+
TokenType.ALPHA,// User
92+
TokenType.COLON,
93+
TokenType.ALPHA,// Added
94+
TokenType.EOF
95+
)
96+
97+
assertEquals(expectedTokenTypes.size, tokens.size,"Token count mismatch")
98+
for (iin expectedTokenTypes.indices) {
99+
assertEquals(expectedTokenTypes[i], tokens[i].type,"Token type mismatch at position$i")
100+
}
77101

78-
println("Testing parser...")
102+
// Verify specific token values
103+
assertEquals("User", tokens[3].value,"First User token value")
104+
assertEquals("User", tokens[9].value,"Second User token value")
105+
assertEquals("Added", tokens[11].value,"Annotation token value")
106+
107+
val parser= cc.unitmesh.diagram.parser.mermaid.MermaidParser(tokens)
79108
val result= parser.parse()
80109

81-
when (result) {
82-
is cc.unitmesh.diagram.parser.mermaid.ParseResult.Success-> {
83-
println("Parse successful!")
84-
println("AST statements:${result.ast.statements.map { it::class.simpleName }}")
110+
// Verify parsing succeeded
111+
assertTrue(resultis cc.unitmesh.diagram.parser.mermaid.ParseResult.Success,"Parser should succeed")
85112

86-
val classAnnotationStatements= result.ast.statements.filterIsInstance<cc.unitmesh.diagram.parser.mermaid.ClassAnnotationStatementNode>()
87-
println("Found${classAnnotationStatements.size} class annotation statements")
88-
classAnnotationStatements.forEach { stmt->
89-
println(" Class:${stmt.className}, Annotation:${stmt.annotation}")
90-
}
91-
}
92-
is cc.unitmesh.diagram.parser.mermaid.ParseResult.Error-> {
93-
println("Parse failed with errors:")
94-
result.errors.forEach { error->
95-
println("${error.message}")
96-
}
97-
}
98-
}
113+
val successResult= resultas cc.unitmesh.diagram.parser.mermaid.ParseResult.Success
114+
115+
// Verify AST contains expected statements
116+
assertEquals(2, successResult.ast.statements.size,"Should have 2 statements")
117+
assertTrue(successResult.ast.statements[0]is cc.unitmesh.diagram.parser.mermaid.ClassStatementNode,"First statement should be ClassStatementNode")
118+
assertTrue(successResult.ast.statements[1]is cc.unitmesh.diagram.parser.mermaid.ClassAnnotationStatementNode,"Second statement should be ClassAnnotationStatementNode")
119+
120+
// Verify class annotation statement details
121+
val classAnnotationStatements= successResult.ast.statements.filterIsInstance<cc.unitmesh.diagram.parser.mermaid.ClassAnnotationStatementNode>()
122+
assertEquals(1, classAnnotationStatements.size,"Should have exactly 1 class annotation statement")
123+
124+
val annotationStmt= classAnnotationStatements[0]
125+
assertEquals("User", annotationStmt.className,"Class name should be 'User'")
126+
assertEquals("Added", annotationStmt.annotation,"Annotation should be 'Added'")
127+
128+
// Test the class diagram parser
129+
val classDiagramParser= cc.unitmesh.diagram.parser.MermaidClassDiagramParser()
130+
val diagramResult= classDiagramParser.parse(successResult.ast)
131+
132+
// Verify diagram parsing results
133+
assertTrue(diagramResult.graphAttributes.containsKey("User_change"),"Should contain User_change attribute")
134+
assertEquals("Added", diagramResult.graphAttributes["User_change"],"User_change should be 'Added'")
135+
assertEquals("mermaid_class_diagram", diagramResult.graphAttributes["type"],"Type should be mermaid_class_diagram")
136+
137+
assertEquals(1, diagramResult.entities.size,"Should have 1 entity")
138+
assertEquals("User", diagramResult.entities.first().getName(),"Entity name should be 'User'")
99139
}
100140
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp