diff --git a/chapi-ast-c/src/main/antlr/C.g4 b/chapi-ast-c/src/main/antlr/C.g4 index d1532d26..b039547a 100644 --- a/chapi-ast-c/src/main/antlr/C.g4 +++ b/chapi-ast-c/src/main/antlr/C.g4 @@ -27,6 +27,10 @@ */ /** C 2011 grammar built from the C11 Spec */ + +// $antlr-format alignTrailingComments true, columnLimit 150, minEmptyLines 1, maxEmptyLinesToKeep 1, reflowComments false, useTab false +// $antlr-format allowShortRulesOnASingleLine false, allowShortBlocksOnASingleLine true, alignSemicolons hanging, alignColons hanging + grammar C; compilationUnit @@ -34,7 +38,7 @@ compilationUnit ; includeDeclaration - : '#' Whitespace? 'include' Whitespace? (('"' includeIdentifier '"') | ('<' includeIdentifier '>' )) + : '#' Whitespace? 'include' Whitespace? (('"' includeIdentifier '"') | ('<' includeIdentifier '>' )) ; includeIdentifier @@ -42,864 +46,1065 @@ includeIdentifier ; primaryExpression - : Identifier - | Constant - | StringLiteral+ - | '(' expression ')' - | genericSelection - | '__extension__'? '(' compoundStatement ')' // Blocks (GCC extension) - | '__builtin_va_arg' '(' unaryExpression ',' typeName ')' - | '__builtin_offsetof' '(' typeName ',' unaryExpression ')' + : Identifier + | Constant + | StringLiteral+ + | '(' expression ')' + | genericSelection + | '__extension__'? '(' compoundStatement ')' // Blocks (GCC extension) + | '__builtin_va_arg' '(' unaryExpression ',' typeName ')' + | '__builtin_offsetof' '(' typeName ',' unaryExpression ')' ; genericSelection - : '_Generic' '(' assignmentExpression ',' genericAssocList ')' + : '_Generic' '(' assignmentExpression ',' genericAssocList ')' ; genericAssocList - : genericAssociation - | genericAssocList ',' genericAssociation + : genericAssociation (',' genericAssociation)* ; genericAssociation - : typeName ':' assignmentExpression - | 'default' ':' assignmentExpression + : (typeName | 'default') ':' assignmentExpression ; postfixExpression - : primaryExpression - | postfixExpression '[' expression ']' - | postfixExpression '(' argumentExpressionList? ')' - | postfixExpression '.' Identifier - | postfixExpression '->' Identifier - | postfixExpression '++' - | postfixExpression '--' - | '(' typeName ')' '{' initializerList '}' - | '(' typeName ')' '{' initializerList ',' '}' - | '__extension__' '(' typeName ')' '{' initializerList '}' - | '__extension__' '(' typeName ')' '{' initializerList ',' '}' + : (primaryExpression | '__extension__'? '(' typeName ')' '{' initializerList ','? '}') ( + '[' expression ']' + | '(' argumentExpressionList? ')' + | ('.' | '->') Identifier + | '++' + | '--' + )* ; argumentExpressionList - : assignmentExpression - | argumentExpressionList ',' assignmentExpression + : assignmentExpression (',' assignmentExpression)* ; unaryExpression - : postfixExpression - | '++' unaryExpression - | '--' unaryExpression - | unaryOperator castExpression - | 'sizeof' unaryExpression - | 'sizeof' '(' typeName ')' - | '_Alignof' '(' typeName ')' - | '&&' Identifier // GCC extension address of label + : ('++' | '--' | 'sizeof')* ( + postfixExpression + | unaryOperator castExpression + | ('sizeof' | '_Alignof') '(' typeName ')' + | '&&' Identifier // GCC extension address of label + ) ; unaryOperator - : '&' | '*' | '+' | '-' | '~' | '!' + : '&' + | '*' + | '+' + | '-' + | '~' + | '!' ; castExpression - : '(' typeName ')' castExpression - | '__extension__' '(' typeName ')' castExpression - | unaryExpression - | DigitSequence // for + : '__extension__'? '(' typeName ')' castExpression + | unaryExpression + | DigitSequence // for ; multiplicativeExpression - : castExpression - | multiplicativeExpression '*' castExpression - | multiplicativeExpression '/' castExpression - | multiplicativeExpression '%' castExpression + : castExpression (('*' | '/' | '%') castExpression)* ; additiveExpression - : multiplicativeExpression - | additiveExpression '+' multiplicativeExpression - | additiveExpression '-' multiplicativeExpression + : multiplicativeExpression (('+' | '-') multiplicativeExpression)* ; shiftExpression - : additiveExpression - | shiftExpression '<<' additiveExpression - | shiftExpression '>>' additiveExpression + : additiveExpression (('<<' | '>>') additiveExpression)* ; relationalExpression - : shiftExpression - | relationalExpression '<' shiftExpression - | relationalExpression '>' shiftExpression - | relationalExpression '<=' shiftExpression - | relationalExpression '>=' shiftExpression + : shiftExpression (('<' | '>' | '<=' | '>=') shiftExpression)* ; equalityExpression - : relationalExpression - | equalityExpression '==' relationalExpression - | equalityExpression '!=' relationalExpression + : relationalExpression (('==' | '!=') relationalExpression)* ; andExpression - : equalityExpression - | andExpression '&' equalityExpression + : equalityExpression ('&' equalityExpression)* ; exclusiveOrExpression - : andExpression - | exclusiveOrExpression '^' andExpression + : andExpression ('^' andExpression)* ; inclusiveOrExpression - : exclusiveOrExpression - | inclusiveOrExpression '|' exclusiveOrExpression + : exclusiveOrExpression ('|' exclusiveOrExpression)* ; logicalAndExpression - : inclusiveOrExpression - | logicalAndExpression '&&' inclusiveOrExpression + : inclusiveOrExpression ('&&' inclusiveOrExpression)* ; logicalOrExpression - : logicalAndExpression - | logicalOrExpression '||' logicalAndExpression + : logicalAndExpression ('||' logicalAndExpression)* ; conditionalExpression - : logicalOrExpression ('?' expression ':' conditionalExpression)? + : logicalOrExpression ('?' expression ':' conditionalExpression)? ; assignmentExpression - : conditionalExpression - | unaryExpression assignmentOperator assignmentExpression - | DigitSequence // for + : conditionalExpression + | unaryExpression assignmentOperator assignmentExpression + | DigitSequence // for ; assignmentOperator - : '=' | '*=' | '/=' | '%=' | '+=' | '-=' | '<<=' | '>>=' | '&=' | '^=' | '|=' + : '=' + | '*=' + | '/=' + | '%=' + | '+=' + | '-=' + | '<<=' + | '>>=' + | '&=' + | '^=' + | '|=' ; expression - : assignmentExpression - | expression ',' assignmentExpression + : assignmentExpression (',' assignmentExpression)* ; constantExpression - : conditionalExpression + : conditionalExpression ; declaration - : declarationSpecifiers initDeclaratorList ';' - | declarationSpecifiers ';' - | staticAssertDeclaration + : declarationSpecifiers initDeclaratorList? ';' + | staticAssertDeclaration ; declarationSpecifiers - : declarationSpecifier+ - ; - -declarationSpecifiers2 - : declarationSpecifier+ + : declarationSpecifier+ ; declarationSpecifier - : storageClassSpecifier - | typeSpecifier - | typeQualifier - | functionSpecifier - | alignmentSpecifier + : storageClassSpecifier + | typeSpecifier + | typeQualifier + | functionSpecifier + | alignmentSpecifier ; initDeclaratorList - : initDeclarator - | initDeclaratorList ',' initDeclarator + : initDeclarator (',' initDeclarator)* ; initDeclarator - : declarator - | declarator '=' initializer + : declarator ('=' initializer)? ; storageClassSpecifier - : 'typedef' - | 'extern' - | 'static' - | '_Thread_local' - | 'auto' - | 'register' + : 'typedef' + | 'extern' + | 'static' + | '_Thread_local' + | 'auto' + | 'register' ; typeSpecifier - : ('void' - | 'char' - | 'short' - | 'int' - | 'long' - | 'float' - | 'double' - | 'signed' - | 'unsigned' - | '_Bool' - | '_Complex' - | '__m128' - | '__m128d' - | '__m128i') - | '__extension__' '(' ('__m128' | '__m128d' | '__m128i') ')' - | atomicTypeSpecifier - | structOrUnionSpecifier - | enumSpecifier - | typedefName - | '__typeof__' '(' constantExpression ')' // GCC extension - | typeSpecifier pointer + : 'void' + | 'char' + | 'short' + | 'int' + | 'long' + | 'float' + | 'double' + | 'signed' + | 'unsigned' + | '_Bool' + | '_Complex' + | '__m128' + | '__m128d' + | '__m128i' + | '__extension__' '(' ('__m128' | '__m128d' | '__m128i') ')' + | atomicTypeSpecifier + | structOrUnionSpecifier + | enumSpecifier + | typedefName + | '__typeof__' '(' constantExpression ')' // GCC extension ; structOrUnionSpecifier - : structOrUnion Identifier? '{' structDeclarationList '}' - | structOrUnion Identifier + : structOrUnion Identifier? '{' structDeclarationList '}' + | structOrUnion Identifier ; structOrUnion - : 'struct' - | 'union' + : 'struct' + | 'union' ; structDeclarationList - : structDeclaration - | structDeclarationList structDeclaration + : structDeclaration+ ; -structDeclaration - : specifierQualifierList structDeclaratorList? ';' - | staticAssertDeclaration +structDeclaration // The first two rules have priority order and cannot be simplified to one expression. + : specifierQualifierList structDeclaratorList? ';' + | staticAssertDeclaration ; specifierQualifierList - : typeSpecifier specifierQualifierList? - | typeQualifier specifierQualifierList? + : (typeSpecifier | typeQualifier) specifierQualifierList? ; structDeclaratorList - : structDeclarator - | structDeclaratorList ',' structDeclarator + : structDeclarator (',' structDeclarator)* ; structDeclarator - : declarator - | declarator? ':' constantExpression + : declarator + | declarator? ':' constantExpression ; enumSpecifier - : 'enum' Identifier? '{' enumeratorList '}' - | 'enum' Identifier? '{' enumeratorList ',' '}' - | 'enum' Identifier + : 'enum' Identifier? '{' enumeratorList ','? '}' + | 'enum' Identifier ; enumeratorList - : enumerator - | enumeratorList ',' enumerator + : enumerator (',' enumerator)* ; enumerator - : enumerationConstant - | enumerationConstant '=' constantExpression + : enumerationConstant ('=' constantExpression)? ; enumerationConstant - : Identifier + : Identifier ; atomicTypeSpecifier - : '_Atomic' '(' typeName ')' + : '_Atomic' '(' typeName ')' ; typeQualifier - : 'const' - | 'restrict' - | 'volatile' - | '_Atomic' + : 'const' + | 'restrict' + | 'volatile' + | '_Atomic' ; functionSpecifier - : ('inline' - | '_Noreturn' - | '__inline__' // GCC extension - | '__stdcall') - | gccAttributeSpecifier - | '__declspec' '(' Identifier ')' + : 'inline' + | '_Noreturn' + | '__inline__' // GCC extension + | '__stdcall' + | gccAttributeSpecifier + | '__declspec' '(' Identifier ')' ; alignmentSpecifier - : '_Alignas' '(' typeName ')' - | '_Alignas' '(' constantExpression ')' + : '_Alignas' '(' (typeName | constantExpression) ')' ; declarator - : pointer? directDeclarator gccDeclaratorExtension* + : pointer? directDeclarator gccDeclaratorExtension* ; directDeclarator - : Identifier #identifierDirectDeclarator - | '(' declarator ')' #declaratorDirectDeclarator - | directDeclarator '[' typeQualifierList? assignmentExpression? ']' #assignmentExpressionDirectDeclarator +// : Identifier +// | '(' declarator ')' +// | directDeclarator '[' typeQualifierList? assignmentExpression? ']' +// | directDeclarator '[' 'static' typeQualifierList? assignmentExpression ']' +// | directDeclarator '[' typeQualifierList 'static' assignmentExpression ']' +// | directDeclarator '[' typeQualifierList? '*' ']' +// | directDeclarator '(' parameterTypeList ')' +// | directDeclarator '(' identifierList? ')' +// | Identifier ':' DigitSequence // bit field +// | vcSpecificModifer Identifier // Visual C Extension +// | '(' vcSpecificModifer declarator ')' // Visual C Extension +// ; + : Identifier #identifierDirectDeclarator + | '(' declarator ')' #declaratorDirectDeclarator + | directDeclarator '[' typeQualifierList? assignmentExpression? ']' #assignmentExpressionDirectDeclarator | directDeclarator '[' 'static' typeQualifierList? assignmentExpression ']' #preStaticAssignmentExpressionDirectDeclarator | directDeclarator '[' typeQualifierList 'static' assignmentExpression ']' #postStaticAssignmentExpressionDirectDeclarator - | directDeclarator '[' typeQualifierList? '*' ']' #typeQualifierListPointerDirectDeclarator - | directDeclarator '(' parameterTypeList ')' #parameterDirectDeclarator - | directDeclarator '(' identifierList? ')' #identifierListDirectDeclarator - | Identifier ':' DigitSequence #bitFieldDirectDeclarator // bit field - | '(' typeSpecifier? pointer directDeclarator ')' #functionPointerDirectDeclarator // function pointer like: (__cdecl *f) + | directDeclarator '[' typeQualifierList? '*' ']' #typeQualifierListPointerDirectDeclarator + | directDeclarator '(' parameterTypeList ')' #parameterDirectDeclarator + | directDeclarator '(' identifierList? ')' #identifierListDirectDeclarator + | Identifier ':' DigitSequence #bitFieldDirectDeclarator // bit field + | vcSpecificModifer Identifier #vcSpecificModiferDirectDeclarator + | '(' typeSpecifier? pointer directDeclarator ')' #functionPointerDirectDeclarator // function pointer like: (__cdecl *f) + ; + +vcSpecificModifer + : '__cdecl' + | '__clrcall' + | '__stdcall' + | '__fastcall' + | '__thiscall' + | '__vectorcall' ; gccDeclaratorExtension - : '__asm' '(' StringLiteral+ ')' - | gccAttributeSpecifier + : '__asm' '(' StringLiteral+ ')' + | gccAttributeSpecifier ; gccAttributeSpecifier - : '__attribute__' '(' '(' gccAttributeList ')' ')' + : '__attribute__' '(' '(' gccAttributeList ')' ')' ; gccAttributeList - : gccAttribute (',' gccAttribute)* - | // empty + : gccAttribute? (',' gccAttribute?)* ; gccAttribute - : ~(',' | '(' | ')') // relaxed def for "identifier or reserved word" - ('(' argumentExpressionList? ')')? - | // empty + : ~(',' | '(' | ')') // relaxed def for "identifier or reserved word" + ('(' argumentExpressionList? ')')? ; nestedParenthesesBlock - : ( ~('(' | ')') - | '(' nestedParenthesesBlock ')' - )* + : (~('(' | ')') | '(' nestedParenthesesBlock ')')* ; pointer - : '*' typeQualifierList? - | '*' typeQualifierList? pointer - | '^' typeQualifierList? // Blocks language extension - | '^' typeQualifierList? pointer // Blocks language extension + : (('*' | '^') typeQualifierList?)+ // ^ - Blocks language extension ; typeQualifierList - : typeQualifier - | typeQualifierList typeQualifier + : typeQualifier+ ; parameterTypeList - : parameterList (',' '...')? + : parameterList (',' '...')? ; parameterList - : parameterDeclaration (',' parameterDeclaration)* + : parameterDeclaration (',' parameterDeclaration)* ; parameterDeclaration - : declarationSpecifiers declarator - | declarationSpecifiers2 abstractDeclarator? + : declarationSpecifiers declarator + | declarationSpecifiers abstractDeclarator? ; identifierList - : Identifier - | identifierList ',' Identifier + : Identifier (',' Identifier)* ; typeName - : specifierQualifierList abstractDeclarator? + : specifierQualifierList abstractDeclarator? ; abstractDeclarator - : pointer - | pointer? directAbstractDeclarator gccDeclaratorExtension* + : pointer + | pointer? directAbstractDeclarator gccDeclaratorExtension* ; directAbstractDeclarator - : '(' abstractDeclarator ')' gccDeclaratorExtension* - | '[' typeQualifierList? assignmentExpression? ']' - | '[' 'static' typeQualifierList? assignmentExpression ']' - | '[' typeQualifierList 'static' assignmentExpression ']' - | '[' '*' ']' - | '(' parameterTypeList? ')' gccDeclaratorExtension* - | directAbstractDeclarator '[' typeQualifierList? assignmentExpression? ']' - | directAbstractDeclarator '[' 'static' typeQualifierList? assignmentExpression ']' - | directAbstractDeclarator '[' typeQualifierList 'static' assignmentExpression ']' - | directAbstractDeclarator '[' '*' ']' - | directAbstractDeclarator '(' parameterTypeList? ')' gccDeclaratorExtension* + : '(' abstractDeclarator ')' gccDeclaratorExtension* + | '[' typeQualifierList? assignmentExpression? ']' + | '[' 'static' typeQualifierList? assignmentExpression ']' + | '[' typeQualifierList 'static' assignmentExpression ']' + | '[' '*' ']' + | '(' parameterTypeList? ')' gccDeclaratorExtension* + | directAbstractDeclarator '[' typeQualifierList? assignmentExpression? ']' + | directAbstractDeclarator '[' 'static' typeQualifierList? assignmentExpression ']' + | directAbstractDeclarator '[' typeQualifierList 'static' assignmentExpression ']' + | directAbstractDeclarator '[' '*' ']' + | directAbstractDeclarator '(' parameterTypeList? ')' gccDeclaratorExtension* ; typedefName - : Identifier + : Identifier ; initializer - : assignmentExpression - | '{' initializerList '}' - | '{' initializerList ',' '}' + : assignmentExpression + | '{' initializerList ','? '}' ; initializerList - : designation? initializer - | initializerList ',' designation? initializer + : designation? initializer (',' designation? initializer)* ; designation - : designatorList '=' + : designatorList '=' ; designatorList - : designator - | designatorList designator + : designator+ ; designator - : '[' constantExpression ']' - | '.' Identifier + : '[' constantExpression ']' + | '.' Identifier ; staticAssertDeclaration - : '_Static_assert' '(' constantExpression ',' StringLiteral+ ')' ';' + : '_Static_assert' '(' constantExpression ',' StringLiteral+ ')' ';' ; statement - : labeledStatement - | compoundStatement - | expressionStatement - | selectionStatement - | iterationStatement - | jumpStatement - | ('__asm' | '__asm__') ('volatile' | '__volatile__') '(' (logicalOrExpression (',' logicalOrExpression)*)? (':' (logicalOrExpression (',' logicalOrExpression)*)?)* ')' ';' + : labeledStatement + | compoundStatement + | expressionStatement + | selectionStatement + | iterationStatement + | jumpStatement + | ('__asm' | '__asm__') ('volatile' | '__volatile__') '(' ( + logicalOrExpression (',' logicalOrExpression)* + )? (':' (logicalOrExpression (',' logicalOrExpression)*)?)* ')' ';' ; labeledStatement - : Identifier ':' statement - | 'case' constantExpression ':' statement - | 'default' ':' statement + : Identifier ':' statement? + | 'case' constantExpression ':' statement + | 'default' ':' statement ; compoundStatement - : '{' blockItemList? '}' + : '{' blockItemList? '}' ; blockItemList - : blockItem - | blockItemList blockItem + : blockItem+ ; blockItem - : statement - | declaration + : statement + | declaration ; expressionStatement - : expression? ';' + : expression? ';' ; selectionStatement - : 'if' '(' expression ')' statement ('else' statement)? - | 'switch' '(' expression ')' statement + : 'if' '(' expression ')' statement ('else' statement)? + | 'switch' '(' expression ')' statement ; iterationStatement - : While '(' expression ')' statement - | Do statement While '(' expression ')' ';' - | For '(' forCondition ')' statement + : While '(' expression ')' statement + | Do statement While '(' expression ')' ';' + | For '(' forCondition ')' statement ; // | 'for' '(' expression? ';' expression? ';' forUpdate? ')' statement // | For '(' declaration expression? ';' expression? ')' statement forCondition - : forDeclaration ';' forExpression? ';' forExpression? - | expression? ';' forExpression? ';' forExpression? - ; + : (forDeclaration | expression?) ';' forExpression? ';' forExpression? + ; forDeclaration - : declarationSpecifiers initDeclaratorList - | declarationSpecifiers + : declarationSpecifiers initDeclaratorList? ; forExpression - : assignmentExpression - | forExpression ',' assignmentExpression + : assignmentExpression (',' assignmentExpression)* ; jumpStatement - : 'goto' Identifier ';' - | 'continue' ';' - | 'break' ';' - | 'return' expression? ';' - | 'goto' unaryExpression ';' // GCC extension + : ( + 'goto' Identifier + | 'continue' + | 'break' + | 'return' expression? + | 'goto' unaryExpression // GCC extension + ) ';' ; translationUnit - : externalDeclaration - | translationUnit externalDeclaration + : externalDeclaration+ ; externalDeclaration - : functionDefinition - | declaration - | ';' // stray ; + : functionDefinition + | declaration + | ';' // stray ; ; functionDefinition - : declarationSpecifiers? declarator declarationList? compoundStatement + : declarationSpecifiers? declarator declarationList? compoundStatement ; declarationList - : declaration - | declarationList declaration - ; - -Auto : 'auto'; -Break : 'break'; -Case : 'case'; -Char : 'char'; -Const : 'const'; -Continue : 'continue'; -Default : 'default'; -Do : 'do'; -Double : 'double'; -Else : 'else'; -Enum : 'enum'; -Extern : 'extern'; -Float : 'float'; -For : 'for'; -Goto : 'goto'; -If : 'if'; -Inline : 'inline'; -Int : 'int'; -Long : 'long'; -Register : 'register'; -Restrict : 'restrict'; -Return : 'return'; -Short : 'short'; -Signed : 'signed'; -Sizeof : 'sizeof'; -Static : 'static'; -Struct : 'struct'; -Switch : 'switch'; -Typedef : 'typedef'; -Union : 'union'; -Unsigned : 'unsigned'; -Void : 'void'; -Volatile : 'volatile'; -While : 'while'; - -Alignas : '_Alignas'; -Alignof : '_Alignof'; -Atomic : '_Atomic'; -Bool : '_Bool'; -Complex : '_Complex'; -Generic : '_Generic'; -Imaginary : '_Imaginary'; -Noreturn : '_Noreturn'; -StaticAssert : '_Static_assert'; -ThreadLocal : '_Thread_local'; - -LeftParen : '('; -RightParen : ')'; -LeftBracket : '['; -RightBracket : ']'; -LeftBrace : '{'; -RightBrace : '}'; - -Less : '<'; -LessEqual : '<='; -Greater : '>'; -GreaterEqual : '>='; -LeftShift : '<<'; -RightShift : '>>'; - -Plus : '+'; -PlusPlus : '++'; -Minus : '-'; -MinusMinus : '--'; -Star : '*'; -Div : '/'; -Mod : '%'; - -And : '&'; -Or : '|'; -AndAnd : '&&'; -OrOr : '||'; -Caret : '^'; -Not : '!'; -Tilde : '~'; - -Question : '?'; -Colon : ':'; -Semi : ';'; -Comma : ','; - -Assign : '='; + : declaration+ + ; + +Auto + : 'auto' + ; + +Break + : 'break' + ; + +Case + : 'case' + ; + +Char + : 'char' + ; + +Const + : 'const' + ; + +Continue + : 'continue' + ; + +Default + : 'default' + ; + +Do + : 'do' + ; + +Double + : 'double' + ; + +Else + : 'else' + ; + +Enum + : 'enum' + ; + +Extern + : 'extern' + ; + +Float + : 'float' + ; + +For + : 'for' + ; + +Goto + : 'goto' + ; + +If + : 'if' + ; + +Inline + : 'inline' + ; + +Int + : 'int' + ; + +Long + : 'long' + ; + +Register + : 'register' + ; + +Restrict + : 'restrict' + ; + +Return + : 'return' + ; + +Short + : 'short' + ; + +Signed + : 'signed' + ; + +Sizeof + : 'sizeof' + ; + +Static + : 'static' + ; + +Struct + : 'struct' + ; + +Switch + : 'switch' + ; + +Typedef + : 'typedef' + ; + +Union + : 'union' + ; + +Unsigned + : 'unsigned' + ; + +Void + : 'void' + ; + +Volatile + : 'volatile' + ; + +While + : 'while' + ; + +Alignas + : '_Alignas' + ; + +Alignof + : '_Alignof' + ; + +Atomic + : '_Atomic' + ; + +Bool + : '_Bool' + ; + +Complex + : '_Complex' + ; + +Generic + : '_Generic' + ; + +Imaginary + : '_Imaginary' + ; + +Noreturn + : '_Noreturn' + ; + +StaticAssert + : '_Static_assert' + ; + +ThreadLocal + : '_Thread_local' + ; + +LeftParen + : '(' + ; + +RightParen + : ')' + ; + +LeftBracket + : '[' + ; + +RightBracket + : ']' + ; + +LeftBrace + : '{' + ; + +RightBrace + : '}' + ; + +Less + : '<' + ; + +LessEqual + : '<=' + ; + +Greater + : '>' + ; + +GreaterEqual + : '>=' + ; + +LeftShift + : '<<' + ; + +RightShift + : '>>' + ; + +Plus + : '+' + ; + +PlusPlus + : '++' + ; + +Minus + : '-' + ; + +MinusMinus + : '--' + ; + +Star + : '*' + ; + +Div + : '/' + ; + +Mod + : '%' + ; + +And + : '&' + ; + +Or + : '|' + ; + +AndAnd + : '&&' + ; + +OrOr + : '||' + ; + +Caret + : '^' + ; + +Not + : '!' + ; + +Tilde + : '~' + ; + +Question + : '?' + ; + +Colon + : ':' + ; + +Semi + : ';' + ; + +Comma + : ',' + ; + +Assign + : '=' + ; + // '*=' | '/=' | '%=' | '+=' | '-=' | '<<=' | '>>=' | '&=' | '^=' | '|=' -StarAssign : '*='; -DivAssign : '/='; -ModAssign : '%='; -PlusAssign : '+='; -MinusAssign : '-='; -LeftShiftAssign : '<<='; -RightShiftAssign : '>>='; -AndAssign : '&='; -XorAssign : '^='; -OrAssign : '|='; - -Equal : '=='; -NotEqual : '!='; - -Arrow : '->'; -Dot : '.'; -Ellipsis : '...'; +StarAssign + : '*=' + ; + +DivAssign + : '/=' + ; + +ModAssign + : '%=' + ; + +PlusAssign + : '+=' + ; + +MinusAssign + : '-=' + ; + +LeftShiftAssign + : '<<=' + ; + +RightShiftAssign + : '>>=' + ; + +AndAssign + : '&=' + ; + +XorAssign + : '^=' + ; + +OrAssign + : '|=' + ; + +Equal + : '==' + ; + +NotEqual + : '!=' + ; + +Arrow + : '->' + ; + +Dot + : '.' + ; + +Ellipsis + : '...' + ; Identifier - : IdentifierNondigit - ( IdentifierNondigit - | Digit - )* + : IdentifierNondigit (IdentifierNondigit | Digit)* ; -fragment -IdentifierNondigit - : Nondigit - | UniversalCharacterName +fragment IdentifierNondigit + : Nondigit + | UniversalCharacterName //| // other implementation-defined characters... ; -fragment -Nondigit - : [a-zA-Z_] +fragment Nondigit + : [a-zA-Z_] ; -fragment -Digit - : [0-9] +fragment Digit + : [0-9] ; -fragment -UniversalCharacterName - : '\\u' HexQuad - | '\\U' HexQuad HexQuad +fragment UniversalCharacterName + : '\\u' HexQuad + | '\\U' HexQuad HexQuad ; -fragment -HexQuad - : HexadecimalDigit HexadecimalDigit HexadecimalDigit HexadecimalDigit +fragment HexQuad + : HexadecimalDigit HexadecimalDigit HexadecimalDigit HexadecimalDigit ; Constant - : IntegerConstant - | FloatingConstant + : IntegerConstant + | FloatingConstant //| EnumerationConstant - | CharacterConstant + | CharacterConstant ; -fragment -IntegerConstant - : DecimalConstant IntegerSuffix? - | OctalConstant IntegerSuffix? - | HexadecimalConstant IntegerSuffix? - | BinaryConstant +fragment IntegerConstant + : DecimalConstant IntegerSuffix? + | OctalConstant IntegerSuffix? + | HexadecimalConstant IntegerSuffix? + | BinaryConstant ; -fragment -BinaryConstant - : '0' [bB] [0-1]+ - ; +fragment BinaryConstant + : '0' [bB] [0-1]+ + ; -fragment -DecimalConstant - : NonzeroDigit Digit* +fragment DecimalConstant + : NonzeroDigit Digit* ; -fragment -OctalConstant - : '0' OctalDigit* +fragment OctalConstant + : '0' OctalDigit* ; -fragment -HexadecimalConstant - : HexadecimalPrefix HexadecimalDigit+ +fragment HexadecimalConstant + : HexadecimalPrefix HexadecimalDigit+ ; -fragment -HexadecimalPrefix - : '0' [xX] +fragment HexadecimalPrefix + : '0' [xX] ; -fragment -NonzeroDigit - : [1-9] +fragment NonzeroDigit + : [1-9] ; -fragment -OctalDigit - : [0-7] +fragment OctalDigit + : [0-7] ; -fragment -HexadecimalDigit - : [0-9a-fA-F] +fragment HexadecimalDigit + : [0-9a-fA-F] ; -fragment -IntegerSuffix - : UnsignedSuffix LongSuffix? - | UnsignedSuffix LongLongSuffix - | LongSuffix UnsignedSuffix? - | LongLongSuffix UnsignedSuffix? +fragment IntegerSuffix + : UnsignedSuffix LongSuffix? + | UnsignedSuffix LongLongSuffix + | LongSuffix UnsignedSuffix? + | LongLongSuffix UnsignedSuffix? ; -fragment -UnsignedSuffix - : [uU] +fragment UnsignedSuffix + : [uU] ; -fragment -LongSuffix - : [lL] +fragment LongSuffix + : [lL] ; -fragment -LongLongSuffix - : 'll' | 'LL' +fragment LongLongSuffix + : 'll' + | 'LL' ; -fragment -FloatingConstant - : DecimalFloatingConstant - | HexadecimalFloatingConstant +fragment FloatingConstant + : DecimalFloatingConstant + | HexadecimalFloatingConstant ; -fragment -DecimalFloatingConstant - : FractionalConstant ExponentPart? FloatingSuffix? - | DigitSequence ExponentPart FloatingSuffix? +fragment DecimalFloatingConstant + : FractionalConstant ExponentPart? FloatingSuffix? + | DigitSequence ExponentPart FloatingSuffix? ; -fragment -HexadecimalFloatingConstant - : HexadecimalPrefix HexadecimalFractionalConstant BinaryExponentPart FloatingSuffix? - | HexadecimalPrefix HexadecimalDigitSequence BinaryExponentPart FloatingSuffix? +fragment HexadecimalFloatingConstant + : HexadecimalPrefix (HexadecimalFractionalConstant | HexadecimalDigitSequence) BinaryExponentPart FloatingSuffix? ; -fragment -FractionalConstant - : DigitSequence? '.' DigitSequence - | DigitSequence '.' +fragment FractionalConstant + : DigitSequence? '.' DigitSequence + | DigitSequence '.' ; -fragment -ExponentPart - : 'e' Sign? DigitSequence - | 'E' Sign? DigitSequence +fragment ExponentPart + : [eE] Sign? DigitSequence ; -fragment -Sign - : '+' | '-' +fragment Sign + : [+-] ; DigitSequence - : Digit+ + : Digit+ ; -fragment -HexadecimalFractionalConstant - : HexadecimalDigitSequence? '.' HexadecimalDigitSequence - | HexadecimalDigitSequence '.' +fragment HexadecimalFractionalConstant + : HexadecimalDigitSequence? '.' HexadecimalDigitSequence + | HexadecimalDigitSequence '.' ; -fragment -BinaryExponentPart - : 'p' Sign? DigitSequence - | 'P' Sign? DigitSequence +fragment BinaryExponentPart + : [pP] Sign? DigitSequence ; -fragment -HexadecimalDigitSequence - : HexadecimalDigit+ +fragment HexadecimalDigitSequence + : HexadecimalDigit+ ; -fragment -FloatingSuffix - : 'f' | 'l' | 'F' | 'L' +fragment FloatingSuffix + : [flFL] ; -fragment -CharacterConstant - : '\'' CCharSequence '\'' - | 'L\'' CCharSequence '\'' - | 'u\'' CCharSequence '\'' - | 'U\'' CCharSequence '\'' +fragment CharacterConstant + : '\'' CCharSequence '\'' + | 'L\'' CCharSequence '\'' + | 'u\'' CCharSequence '\'' + | 'U\'' CCharSequence '\'' ; -fragment -CCharSequence - : CChar+ +fragment CCharSequence + : CChar+ ; -fragment -CChar - : ~['\\\r\n] - | EscapeSequence +fragment CChar + : ~['\\\r\n] + | EscapeSequence ; -fragment -EscapeSequence - : SimpleEscapeSequence - | OctalEscapeSequence - | HexadecimalEscapeSequence - | UniversalCharacterName +fragment EscapeSequence + : SimpleEscapeSequence + | OctalEscapeSequence + | HexadecimalEscapeSequence + | UniversalCharacterName ; -fragment -SimpleEscapeSequence - : '\\' ['"?abfnrtv\\] +fragment SimpleEscapeSequence + : '\\' ['"?abfnrtv\\] ; -fragment -OctalEscapeSequence - : '\\' OctalDigit - | '\\' OctalDigit OctalDigit - | '\\' OctalDigit OctalDigit OctalDigit +fragment OctalEscapeSequence + : '\\' OctalDigit OctalDigit? OctalDigit? ; -fragment -HexadecimalEscapeSequence - : '\\x' HexadecimalDigit+ +fragment HexadecimalEscapeSequence + : '\\x' HexadecimalDigit+ ; StringLiteral - : EncodingPrefix? '"' SCharSequence? '"' + : EncodingPrefix? '"' SCharSequence? '"' ; -fragment -EncodingPrefix - : 'u8' - | 'u' - | 'U' - | 'L' +fragment EncodingPrefix + : 'u8' + | 'u' + | 'U' + | 'L' ; -fragment -SCharSequence - : SChar+ +fragment SCharSequence + : SChar+ ; -fragment -SChar - : ~["\\\r\n] - | EscapeSequence - | '\\\n' // Added line - | '\\\r\n' // Added line +fragment SChar + : ~["\\\r\n] + | EscapeSequence + | '\\\n' // Added line + | '\\\r\n' // Added line ; +//MultiLineMacro +// : '#' (~[\n]*? '\\' '\r'? '\n')+ ~ [\n]+ -> channel (HIDDEN) +// ; +// +//Directive +// : '#' ~ [\n]* -> channel (HIDDEN) +// ; + ComplexDefine - : '#' Whitespace? 'define' ~[#]* - -> skip + : '#' Whitespace? 'define' ~[#]* -> skip ; // ignore the following asm blocks: @@ -910,25 +1115,7 @@ ComplexDefine } */ AsmBlock - : 'asm' ~'{'* '{' ~'}'* '}' - -> skip - ; - -// 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]* - -> skip - ; - -PragmaDirective - : '#' Whitespace? 'pragma' Whitespace ~[\r\n]* - -> skip + : 'asm' ~'{'* '{' ~'}'* '}' -> channel(HIDDEN) ; Whitespace 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 e1be3868..313ba7d6 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 @@ -29,7 +29,7 @@ open class CFullIdentListener(fileName: String) : CAstBaseListener() { currentDataStruct = it } - ctx?.structDeclarationList()?.structDeclaration()?.let { + ctx?.structDeclarationList()?.structDeclaration()?.map { it.specifierQualifierList()?.let { qualifierList -> val field = CodeField( TypeType = qualifierList.typeSpecifier().text, @@ -82,8 +82,10 @@ open class CFullIdentListener(fileName: String) : CAstBaseListener() { } } + val pointer = it.declarator().pointer()?.text ?: "" + if (type != null && name != null) { - CodeProperty(TypeValue = name, TypeType = type) + CodeProperty(TypeValue = name, TypeType = type + pointer) } else { 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 55508b3e..8348f0ad 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 @@ -198,4 +198,3 @@ typedef struct { // assertEquals(secondDs.Fields[1].TypeType, "struct element*") } } - diff --git a/chapi-domain/src/main/kotlin/chapi/domain/core/CodeProperty.kt b/chapi-domain/src/main/kotlin/chapi/domain/core/CodeProperty.kt index c22f8c94..5a1167cf 100644 --- a/chapi-domain/src/main/kotlin/chapi/domain/core/CodeProperty.kt +++ b/chapi-domain/src/main/kotlin/chapi/domain/core/CodeProperty.kt @@ -9,7 +9,7 @@ data class CodeProperty( var TypeValue: String, var TypeType: String, var Annotations: List = listOf(), - // for TypeScript and Parameter + /// for TypeScript and Parameter var ObjectValue: List = listOf(), var ReturnTypes: List = listOf(), var Parameters: List = listOf(),