From 2d2dff5c649a78c9e0acc95d68720a0874e6351d Mon Sep 17 00:00:00 2001 From: Phodal Huang Date: Sat, 6 Jan 2024 10:23:57 +0800 Subject: [PATCH] chore: update c to latest #24 --- chapi-ast-c/src/main/antlr/C.g4 | 45 ++++++++----------- .../main/kotlin/chapi/ast/cast/CAnalyser.kt | 13 +++--- .../chapi/ast/cast/CFullIdentListener.kt | 11 +++-- .../chapi/ast/cast/CFullIdentListenerTest.kt | 2 + 4 files changed, 33 insertions(+), 38 deletions(-) diff --git a/chapi-ast-c/src/main/antlr/C.g4 b/chapi-ast-c/src/main/antlr/C.g4 index d06fe0c3..c7fdb889 100644 --- a/chapi-ast-c/src/main/antlr/C.g4 +++ b/chapi-ast-c/src/main/antlr/C.g4 @@ -29,6 +29,17 @@ /** C 2011 grammar built from the C11 Spec */ grammar C; +compilationUnit + : includeDeclaration? translationUnit? EOF + ; + +includeDeclaration + : '#' Whitespace? 'include' Whitespace? (('"' includeIdentifier '"') | ('<' includeIdentifier '>' )) + ; + +includeIdentifier + : Identifier Dot? 'h'? + ; primaryExpression : Identifier @@ -524,10 +535,6 @@ jumpStatement | 'goto' unaryExpression ';' // GCC extension ; -compilationUnit - : includeDeclaration? translationUnit? EOF - ; - translationUnit : externalDeclaration | translationUnit externalDeclaration @@ -896,14 +903,6 @@ ComplexDefine : '#' Whitespace? 'define' ~[#]* -> skip ; - -includeDeclaration - : '#' Whitespace? 'include' Whitespace? (('"' includeIdentifier '"') | ('<' includeIdentifier '>' )) - ; - -includeIdentifier - : Identifier Dot? 'h'? - ; // ignore the following asm blocks: /* @@ -916,13 +915,13 @@ AsmBlock : 'asm' ~'{'* '{' ~'}'* '}' -> skip ; - -// ignore the lines generated by c preprocessor -// sample line : '#line 1 "/home/dm/files/dk1.h" 1' + +// ignore the lines generated by c preprocessor +// sample line : '#line 1 "/home/dm/files/dk1.h" 1' LineAfterPreprocessing : '#line' Whitespace* ~[\r\n]* -> skip - ; + ; LineDirective : '#' Whitespace? DecimalConstant Whitespace? StringLiteral ~[\r\n]* @@ -935,23 +934,17 @@ PragmaDirective ; Whitespace - : [ \t]+ - -> skip + : [ \t]+ -> channel(HIDDEN) ; Newline - : ( '\r' '\n'? - | '\n' - ) - -> skip + : ('\r' '\n'? | '\n') -> channel(HIDDEN) ; BlockComment - : '/*' .*? '*/' - -> skip + : '/*' .*? '*/' -> channel(HIDDEN) ; LineComment - : '//' ~[\r\n]* - -> skip + : '//' ~[\r\n]* -> channel(HIDDEN) ; diff --git a/chapi-ast-c/src/main/kotlin/chapi/ast/cast/CAnalyser.kt b/chapi-ast-c/src/main/kotlin/chapi/ast/cast/CAnalyser.kt index 1af80335..71a00815 100644 --- a/chapi-ast-c/src/main/kotlin/chapi/ast/cast/CAnalyser.kt +++ b/chapi-ast-c/src/main/kotlin/chapi/ast/cast/CAnalyser.kt @@ -18,12 +18,9 @@ open class CAnalyser: Analyser { return listener.getNodeInfo() } - open fun parse(str: String): CParser { - val fromString = CharStreams.fromString(str) - val tokenSource = CLexer(fromString) - val commonTokenStream = CommonTokenStream(tokenSource) - val parser = CParser(commonTokenStream) - return parser - } - + open fun parse(str: String): CParser = + CharStreams.fromString(str) + .let(::CLexer) + .let(::CommonTokenStream) + .let(::CParser) } diff --git a/chapi-ast-c/src/main/kotlin/chapi/ast/cast/CFullIdentListener.kt b/chapi-ast-c/src/main/kotlin/chapi/ast/cast/CFullIdentListener.kt index 1e74357f..25a3d1e2 100644 --- a/chapi-ast-c/src/main/kotlin/chapi/ast/cast/CFullIdentListener.kt +++ b/chapi-ast-c/src/main/kotlin/chapi/ast/cast/CFullIdentListener.kt @@ -14,10 +14,13 @@ open class CFullIdentListener(fileName: String) : CAstBaseListener() { override fun enterIncludeDeclaration(ctx: CParser.IncludeDeclarationContext?) { - val importName = ctx!!.includeIdentifier().text + val includeIdentifier = ctx?.includeIdentifier() + val importName = includeIdentifier?.text ?: "" val imp = CodeImport( - Source = importName + Source = importName, + AsName = includeIdentifier?.Identifier()?.text ?: "" ) + codeContainer.Imports += imp } @@ -52,7 +55,7 @@ open class CFullIdentListener(fileName: String) : CAstBaseListener() { codeContainer.DataStructures += codeDataStruct } - fun parseDirectDeclarator(ctx: CParser.DirectDeclaratorContext) { + private fun parseDirectDeclarator(ctx: CParser.DirectDeclaratorContext) { val directDeclaratorType = ctx::class.java.simpleName when(directDeclaratorType) { "ParameterDirectDeclaratorContext" -> { @@ -82,7 +85,7 @@ open class CFullIdentListener(fileName: String) : CAstBaseListener() { val directDeclarator = ctx as CParser.ParameterDirectDeclaratorContext parseDirectDeclarator(ctx.directDeclarator()) val parameterTypeList = directDeclarator.parameterTypeList().parameterList() - var parameters: MutableList = ArrayList() + val parameters: MutableList = ArrayList() this.buildParameters(parameterTypeList, parameters) for (parameter in parameters) { var type: String? = null diff --git a/chapi-ast-c/src/test/kotlin/chapi/ast/cast/CFullIdentListenerTest.kt b/chapi-ast-c/src/test/kotlin/chapi/ast/cast/CFullIdentListenerTest.kt index c060c16c..9d1a85f2 100644 --- a/chapi-ast-c/src/test/kotlin/chapi/ast/cast/CFullIdentListenerTest.kt +++ b/chapi-ast-c/src/test/kotlin/chapi/ast/cast/CFullIdentListenerTest.kt @@ -170,6 +170,7 @@ struct context{ }; """ val codeFile = CAnalyser().analysis(code, "helloworld.c") +// assertEquals(codeFile.DataStructures.size, 2) } @Test @@ -183,5 +184,6 @@ typedef struct { } element; // Complete definition """ val codeFile = CAnalyser().analysis(code, "helloworld.c") + assertEquals(codeFile.DataStructures.size, 3) } }