Skip to content

Commit

Permalink
chore: update c to latest #24
Browse files Browse the repository at this point in the history
  • Loading branch information
phodal committed Jan 6, 2024
1 parent 0490377 commit 2d2dff5
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 38 deletions.
45 changes: 19 additions & 26 deletions chapi-ast-c/src/main/antlr/C.g4
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -524,10 +535,6 @@ jumpStatement
| 'goto' unaryExpression ';' // GCC extension
;

compilationUnit
: includeDeclaration? translationUnit? EOF
;

translationUnit
: externalDeclaration
| translationUnit externalDeclaration
Expand Down Expand Up @@ -896,14 +903,6 @@ ComplexDefine
: '#' Whitespace? 'define' ~[#]*
-> skip
;

includeDeclaration
: '#' Whitespace? 'include' Whitespace? (('"' includeIdentifier '"') | ('<' includeIdentifier '>' ))
;

includeIdentifier
: Identifier Dot? 'h'?
;

// ignore the following asm blocks:
/*
Expand All @@ -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]*
Expand All @@ -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)
;
13 changes: 5 additions & 8 deletions chapi-ast-c/src/main/kotlin/chapi/ast/cast/CAnalyser.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
11 changes: 7 additions & 4 deletions chapi-ast-c/src/main/kotlin/chapi/ast/cast/CFullIdentListener.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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" -> {
Expand Down Expand Up @@ -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<CParser.ParameterDeclarationContext> = ArrayList()
val parameters: MutableList<CParser.ParameterDeclarationContext> = ArrayList()
this.buildParameters(parameterTypeList, parameters)
for (parameter in parameters) {
var type: String? = null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ struct context{
};
"""
val codeFile = CAnalyser().analysis(code, "helloworld.c")
// assertEquals(codeFile.DataStructures.size, 2)
}

@Test
Expand All @@ -183,5 +184,6 @@ typedef struct {
} element; // Complete definition
"""
val codeFile = CAnalyser().analysis(code, "helloworld.c")
assertEquals(codeFile.DataStructures.size, 3)
}
}

0 comments on commit 2d2dff5

Please sign in to comment.