|
1 | 1 | package chapi.ast.rustast
|
2 | 2 |
|
3 | 3 | import chapi.ast.antlr.RustParser
|
| 4 | +import chapi.ast.antlr.RustParser.ItemContext |
| 5 | +import chapi.ast.antlr.RustParser.MacroIdentifierLikeTokenContext |
4 | 6 | import chapi.ast.antlr.RustParser.TypePathSegmentContext
|
5 | 7 | import chapi.ast.antlr.RustParser.Type_Context
|
6 | 8 | import chapi.ast.antlr.RustParserBaseListener
|
@@ -72,26 +74,53 @@ open class RustAstBaseListener(private val fileName: String) : RustParserBaseLis
|
72 | 74 | Imports = imports
|
73 | 75 | }
|
74 | 76 |
|
75 |
| - fun buildPosition(ctx: ParserRuleContext): CodePosition { |
76 |
| - val position = CodePosition() |
77 |
| - position.StartLine = ctx.start.line |
78 |
| - position.StartLinePosition = ctx.start.charPositionInLine |
79 |
| - position.StopLine = ctx.stop.line |
80 |
| - position.StopLinePosition = ctx.stop.charPositionInLine |
81 |
| - return position |
| 77 | + override fun enterOuterAttribute(ctx: RustParser.OuterAttributeContext?) { |
| 78 | + |
82 | 79 | }
|
83 | 80 |
|
84 | 81 | override fun enterStructStruct(ctx: RustParser.StructStructContext?) {
|
85 |
| - val structName = ctx!!.identifier().text |
| 82 | + val structName = ctx?.identifier()?.text ?: return |
| 83 | + |
| 84 | + val item = ctx.parent?.parent?.parent |
| 85 | + var annotation: MutableList<CodeAnnotation> = mutableListOf() |
| 86 | + if (item is ItemContext) { |
| 87 | + annotation = buildAttribute(item.outerAttribute()) |
| 88 | + } |
86 | 89 |
|
87 | 90 | val codeStruct = CodeDataStruct(
|
88 | 91 | NodeName = structName,
|
89 |
| - Package = codeContainer.PackageName |
| 92 | + Package = codeContainer.PackageName, |
| 93 | + Annotations = annotation |
90 | 94 | )
|
91 | 95 |
|
92 | 96 | structMap[structName] = codeStruct
|
93 | 97 | }
|
94 | 98 |
|
| 99 | + private fun buildAttribute(outerAttribute: List<RustParser.OuterAttributeContext>): MutableList<CodeAnnotation> { |
| 100 | + return outerAttribute.map { attributeContext -> |
| 101 | + val annotationName = attributeContext.attr()?.simplePath()?.simplePathSegment()?.filter { it.identifier() != null }?.map { |
| 102 | + it.identifier().text |
| 103 | + }?.joinToString(".") ?: "" |
| 104 | + |
| 105 | + val keyValues = attributeContext.attr().attrInput().delimTokenTree().tokenTree() |
| 106 | + .mapNotNull { |
| 107 | + it.tokenTreeToken() |
| 108 | + .mapNotNull { token -> token.macroIdentifierLikeToken() } |
| 109 | + .map { tokenContext -> |
| 110 | + AnnotationKeyValue( |
| 111 | + Key = tokenContext.identifier().text, |
| 112 | + Value = tokenContext.identifier().text |
| 113 | + ) |
| 114 | + } |
| 115 | + }.flatten() |
| 116 | + |
| 117 | + CodeAnnotation( |
| 118 | + Name = annotationName, |
| 119 | + KeyValues = keyValues |
| 120 | + ) |
| 121 | + }.toMutableList() |
| 122 | + } |
| 123 | + |
95 | 124 | override fun exitStructStruct(ctx: RustParser.StructStructContext?) {
|
96 | 125 |
|
97 | 126 | }
|
@@ -189,6 +218,15 @@ open class RustAstBaseListener(private val fileName: String) : RustParserBaseLis
|
189 | 218 | structMap[currentNode.NodeName] = currentNode
|
190 | 219 | }
|
191 | 220 |
|
| 221 | + private fun buildPosition(ctx: ParserRuleContext): CodePosition { |
| 222 | + val position = CodePosition() |
| 223 | + position.StartLine = ctx.start.line |
| 224 | + position.StartLinePosition = ctx.start.charPositionInLine |
| 225 | + position.StopLine = ctx.stop.line |
| 226 | + position.StopLinePosition = ctx.stop.charPositionInLine |
| 227 | + return position |
| 228 | + } |
| 229 | + |
192 | 230 | private fun buildDedicatedStructs(): List<CodeDataStruct> {
|
193 | 231 | if (individualFunctions.isEmpty() && individualFields.isEmpty()) return emptyList()
|
194 | 232 |
|
|
0 commit comments