Skip to content

Commit

Permalink
Improve objc grammar: better expr syntax; fix some issues (#4333)
Browse files Browse the repository at this point in the history
Co-authored-by: Sihao Lu <[email protected]>
  • Loading branch information
DJBen and Sihao Lu authored Nov 29, 2024
1 parent 97bd288 commit 765425a
Show file tree
Hide file tree
Showing 5 changed files with 174 additions and 75 deletions.
11 changes: 8 additions & 3 deletions objc/ObjectiveCLexer.g4
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,12 @@ SELF : 'self';
SUPER : 'super';
YES : 'YES';
AUTORELEASEPOOL : '@autoreleasepool';
CATCH : '@catch';
CATCH : 'catch';
CLASS : '@class';
DYNAMIC : '@dynamic';
ENCODE : '@encode';
END : '@end';
FINALLY : '@finally';
FINALLY : 'finally';
IMPLEMENTATION : '@implementation';
INTERFACE : '@interface';
IMPORT : '@import';
Expand All @@ -122,14 +122,19 @@ SELECTOR : '@selector';
SYNCHRONIZED : '@synchronized';
SYNTHESIZE : '@synthesize';
THROW : '@throw';
TRY : '@try';
TRY : 'try';
ATOMIC : 'atomic';
NONATOMIC : 'nonatomic';
RETAIN : 'retain';
DIRECT : 'direct';

// Attributes with `__` prefix

INLINE_ATTR : '__inline__';
ATTRIBUTE : '__attribute__';
EXTENSION : '__extension__';
STATIC_ASSERT : '_Static_assert';
ALIGN_OF : '_Alignof';
AUTORELEASING_QUALIFIER : '__autoreleasing';
BLOCK : '__block';
BRIDGE : '__bridge';
Expand Down
192 changes: 129 additions & 63 deletions objc/ObjectiveCParser.g4
Original file line number Diff line number Diff line change
Expand Up @@ -130,15 +130,15 @@ propertyAttribute
| COPY
| READONLY
| READWRITE
| DIRECT
| GETTER '=' identifier
| SETTER '=' identifier ':'
| nullabilitySpecifier
| identifier
;

protocolName
: LT protocolList GT
| ('__covariant' | '__contravariant')? identifier
: ('__covariant' | '__contravariant')? identifier
;

instanceVariables
Expand Down Expand Up @@ -187,6 +187,7 @@ implementationDefinitionList
| classMethodDefinition
| instanceMethodDefinition
| propertyImplementation
| ';'
)+
;

Expand Down Expand Up @@ -237,26 +238,30 @@ blockType
: NS_NOESCAPE? nullabilitySpecifier? typeSpecifier nullabilitySpecifier? LP NS_NOESCAPE? '^'
nullabilitySpecifier? RP blockParameters?
;

genericsSpecifierList
: LT (genericsSpecifier (',' genericsSpecifier)*)? GT
;

genericsSpecifier
: genericsType = typeSpecifier (':' genericsConformanceType = typeSpecifier)?
;

dictionaryExpression
dictionaryLiteralExpression
: '@' '{' (dictionaryPair (',' dictionaryPair)* ','?)? '}'
;

dictionaryPair
: castExpression ':' expression
;

arrayExpression
: '@' '[' (expressions ','?)? ']'
arrayLiteralExpression
: '@' '[' assignmentExpression? (',' assignmentExpression)* ','? ']'
;

boxExpression
: '@' LP expression RP
| '@' (constant | identifier)
boxedExpression
: '@' LP constantExpression RP
| '@' constant
;

blockParameters
Expand Down Expand Up @@ -291,7 +296,7 @@ keywordArgument
;

keywordArgumentType
: expressions nullabilitySpecifier? ('{' initializerList '}')?
: expression nullabilitySpecifier? ('{' initializerList '}')?
;

selectorExpression
Expand Down Expand Up @@ -322,17 +327,17 @@ throwStatement
;

tryBlock
: '@try' tryStatement = compoundStatement catchStatement* (
'@finally' finallyStatement = compoundStatement
: '@' TRY tryStatement = compoundStatement catchStatement* (
'@' FINALLY finallyStatement = compoundStatement
)?
;

catchStatement
: '@catch' LP typeVariableDeclarator RP compoundStatement
: '@' CATCH LP (typeVariableDeclarator | '...') RP compoundStatement
;

synchronizedStatement
: '@synchronized' LP expression RP compoundStatement
: '@synchronized' LP identifier RP compoundStatement
;

autoreleaseStatement
Expand Down Expand Up @@ -418,7 +423,9 @@ declarationSpecifiers
| NS_NOESCAPE
| typePrefix
| typeQualifier
)* typeSpecifier attributeSpecifier*
)* typeSpecifier (
attributeSpecifier
)*
;

attributeSpecifier
Expand Down Expand Up @@ -474,6 +481,7 @@ typePrefix
| BLOCK
| INLINE
| NS_INLINE
| INLINE_ATTR
| KINDOF
;

Expand All @@ -493,17 +501,21 @@ protocolQualifier
| 'oneway'
;

typeSpecifierModifier
: 'short'
| 'long'
| 'signed'

numericSignModifier
: 'signed'
| 'unsigned'
;

typeSpecifier
: 'void' typeQualifier*
| typeSpecifierModifier* ('char' | 'short' | 'int' | 'long' | 'float' | 'double') typeQualifier*
| typeofExpression
| numericSignModifier? 'char' typeQualifier*
| numericSignModifier? 'short' typeQualifier*
| numericSignModifier? ('short' | 'long' | 'long' 'long')? 'int' typeQualifier*
| numericSignModifier? 'long'? 'long' typeQualifier*
| 'float' typeQualifier*
| 'long'? 'double' typeQualifier*
| typeofExpression (arcBehaviourSpecifier | nullabilitySpecifier | typeQualifier)*
| structOrUnionSpecifier
| enumSpecifier
| nsEnumOrOptionSpecifier
Expand Down Expand Up @@ -542,7 +554,7 @@ enumeratorList
;

enumerator
: enumeratorIdentifier ('=' expression)?
: enumeratorIdentifier ('=' assignmentExpression)?
;

enumeratorIdentifier
Expand Down Expand Up @@ -612,13 +624,19 @@ version
;

arrayInitializer
: '{' (expressions ','?)? '}'
: '{' (expression ','?)? '}'
;

// Designated struct initializer
// e.g. struct point_t a = { .y = 2, .x = 1 };
structInitializer
: '{' ('.' expression (',' '.' expression)* ','?)? '}'
: '{' ('.' structAssignmentExpression (',' '.' structAssignmentExpression)* ','?)? '}'
;


structAssignmentExpression
: identifier '=' assignmentExpression
;

initializerList
: initializer (',' initializer)* ','?
;
Expand Down Expand Up @@ -657,7 +675,7 @@ statement
| autoreleaseStatement ';'?
| throwStatement ';'?
| tryBlock ';'?
| expressions ';'?
| expression ';'?
| ';'
;

Expand Down Expand Up @@ -711,12 +729,12 @@ doStatement
;

forStatement
: 'for' LP forLoopInitializer? ';' expression? ';' expressions? RP statement
: 'for' LP forLoopInitializer? ';' expression? ';' expression? RP statement
;

forLoopInitializer
: declarationSpecifiers initDeclaratorList
| expressions
| expression
;

forInStatement
Expand All @@ -730,27 +748,68 @@ jumpStatement
| RETURN expression?
;

expressions
: expression (',' expression)*
castExpression
: (LP typeName RP) castExpression
| unaryExpression
;

expression
: castExpression
| expression op = (MUL | DIV | MOD) expression
| expression op = (ADD | SUB) expression
| expression (LT LT | GT GT) expression
| expression op = (LE | GE | LT | GT) expression
| expression op = (NOTEQUAL | EQUAL) expression
| expression op = BITAND expression
| expression op = BITXOR expression
| expression op = BITOR expression
| expression op = AND expression
| expression op = OR expression
| expression QUESTION trueExpression = expression? COLON falseExpression = expression
| LP compoundStatement RP
| unaryExpression assignmentOperator assignmentExpression = expression
multiplicativeExpression
: castExpression (('*' | '/' | '%') castExpression)*
;

additiveExpression
: multiplicativeExpression (('+' | '-') multiplicativeExpression)*
;

shiftExpression
: additiveExpression ((leftShiftOperator | rightShiftOperator) additiveExpression)*
;

leftShiftOperator
: LT LT
;

rightShiftOperator
: GT GT
;

relationalExpression
: shiftExpression ((LT | GT | LE | GE) shiftExpression)*
;

equalityExpression
: relationalExpression ((EQUAL | NOTEQUAL) relationalExpression)*
;

andExpression
: equalityExpression (BITAND equalityExpression)*
;

exclusiveOrExpression
: andExpression (BITXOR andExpression)*
;

inclusiveOrExpression
: exclusiveOrExpression (BITOR exclusiveOrExpression)*
;

logicalAndExpression
: inclusiveOrExpression (AND inclusiveOrExpression)*
;

logicalOrExpression
: logicalAndExpression (OR logicalAndExpression)*
;

conditionalExpression
: logicalOrExpression ('?' ifExpr = conditionalExpression? ':' elseExpr = conditionalExpression)?
;

assignmentExpression
: conditionalExpression
| unaryExpression assignmentOperator assignmentExpression
;

assignmentOperator
: '='
| '*='
Expand All @@ -764,28 +823,29 @@ assignmentOperator
| '^='
| '|='
;

castExpression
: unaryExpression
| (LP typeName RP) (castExpression | initializer)

expression
: assignmentExpression (',' assignmentExpression)*
;

constantExpression
: conditionalExpression
;

initializer
: expression
: assignmentExpression
| arrayInitializer
| structInitializer
;

constantExpression
: identifier
| constant
;

// The expression that is allowed on the left-hand-side of the assignment operator
unaryExpression
: postfixExpression
| SIZEOF (unaryExpression | LP typeSpecifier RP)
| op = (INC | DEC) unaryExpression
| unaryOperator castExpression
: ('++' | '--')* (
postfixExpression
| unaryOperator castExpression
| ('sizeof' | '_Alignof') LP typeName RP
| AND identifier // GCC extension address of label
)
;

unaryOperator
Expand All @@ -798,8 +858,13 @@ unaryOperator
;

postfixExpression
: primaryExpression postfix*
| postfixExpression (DOT | STRUCTACCESS) identifier postfix* // TODO: get rid of property and postfix expression.
: (primaryExpression | '__extension__'? LP typeName RP '{' initializerList ','? '}') (
'[' expression ']'
| LP argumentExpressionList? RP
| ('.' | '->') identifier
| '++'
| '--'
)*
;

postfix
Expand Down Expand Up @@ -827,10 +892,11 @@ primaryExpression
| selectorExpression
| protocolExpression
| encodeExpression
| dictionaryExpression
| arrayExpression
| boxExpression
| dictionaryLiteralExpression
| arrayLiteralExpression
| boxedExpression
| blockExpression
| '__extension__'? LP compoundStatement RP
;

constant
Expand Down
Loading

0 comments on commit 765425a

Please sign in to comment.