]> git.saurik.com Git - cycript.git/commitdiff
Fix a few poor syntax assumptions in C-like types.
authorJay Freeman (saurik) <saurik@saurik.com>
Mon, 2 Nov 2015 13:22:28 +0000 (05:22 -0800)
committerJay Freeman (saurik) <saurik@saurik.com>
Mon, 2 Nov 2015 13:22:28 +0000 (05:22 -0800)
Cycript.yy.in
Parser.hpp
Replace.cpp

index 9cf3b8ef358900e6a8538fcbcede1ce5beb95f31..d963589e56257ad6c5a1d3d57d37be5d9bbda64d 100644 (file)
@@ -477,15 +477,12 @@ int cylex(YYSTYPE *, CYLocation *, void *);
 %type <expression_> Variable
 
 @begin C
-%type <typedIdentifier_> ArrayedType
-%type <modifier_> FunctionedType
 %type <specifier_> IntegerType
 %type <specifier_> IntegerTypeOpt
 %type <typedIdentifier_> PrefixedType
 %type <specifier_> PrimitiveType
-%type <typedIdentifier_> TypeParenthetical
-%type <typedIdentifier_> TypeSignifier
 %type <typedIdentifier_> SuffixedType
+%type <typedIdentifier_> TypeSignifier
 %type <modifier_> TypeQualifierLeft
 %type <typedIdentifier_> TypeQualifierRight
 %type <typedIdentifier_> TypedIdentifier
@@ -515,7 +512,6 @@ int cylex(YYSTYPE *, CYLocation *, void *);
 %type <messageParameter_> MessageParameterList
 %type <messageParameter_> MessageParameterListOpt
 %type <bool_> MessageScope
-%type <typedIdentifier_> ModifiedType
 %type <argument_> SelectorCall_
 %type <argument_> SelectorCall
 %type <selector_> SelectorExpression_
@@ -1404,31 +1400,18 @@ ProgramBodyOpt
 
 @begin C
 /* Cycript (C): Type Encoding {{{ */
-TypeParenthetical
-    : "(" LexPushInOff PrefixedType ")" LexPopIn { $$ = $3; }
-    ;
-
 TypeSignifier
-    : IdentifierType { $$ = CYNew CYTypedIdentifier($1); }
-    | TypeParenthetical { $$ = $1; }
-    ;
-
-ArrayedType
-    : ArrayedType "[" NumericLiteral "]" { $$ = $1; $$->modifier_ = CYNew CYTypeArrayOf($3, $$->modifier_); }
-    | TypeSignifier { $$ = $1; }
-    | { $$ = CYNew CYTypedIdentifier(); }
-    ;
-
-FunctionedType
-    : "(" LexPushInOff TypedParameterListOpt ")" LexPopIn { $$ = CYNew CYTypeFunctionWith($3); }
+    : IdentifierType { $$ = CYNew CYTypedIdentifier(@1, $1); }
+    | "(" LexPushInOff "*" TypeQualifierRight ")" LexPopIn { $$ = $4; }
     ;
 
 SuffixedType
-    : ArrayedType { $$ = $1; }
+    : SuffixedType "[" NumericLiteral "]" { $$ = $1; $$->modifier_ = CYNew CYTypeArrayOf($3, $$->modifier_); }
     | "(" LexPushInOff "^" TypeQualifierRight ")" LexPopIn "(" LexPushInOff TypedParameterListOpt ")" LexPopIn { $$ = $4; $$->modifier_ = CYNew CYTypeBlockWith($9, $$->modifier_); }
-    | TypeParenthetical FunctionedType { $$ = $1; CYSetLast($2) = $$->modifier_; $$->modifier_ = $2; }
-    | IdentifierType FunctionedType { $$ = CYNew CYTypedIdentifier($1); CYSetLast($2) = $$->modifier_; $$->modifier_ = $2; }
-    | FunctionedType { $$ = CYNew CYTypedIdentifier(); CYSetLast($1) = $$->modifier_; $$->modifier_ = $1; }
+    | TypeSignifier "(" LexPushInOff TypedParameterListOpt ")" LexPopIn { $$ = $1; $$->modifier_ = CYNew CYTypeFunctionWith($4, $$->modifier_); }
+    | "(" LexPushInOff TypedParameterListOpt ")" LexPopIn { $$ = CYNew CYTypedIdentifier(@1); $$->modifier_ = CYNew CYTypeFunctionWith($3, $$->modifier_); }
+    | TypeSignifier { $$ = $1; }
+    | { $$ = CYNew CYTypedIdentifier(@$); }
     ;
 
 PrefixedType
@@ -1666,13 +1649,8 @@ PrimaryExpression
     ;
 /* }}} */
 /* Cycript (Objective-C): Block Expressions {{{ */
-ModifiedType
-    : TypeQualifierLeft PrimitiveType { $$ = CYNew CYTypedIdentifier(); $$->specifier_ = $2; $$->modifier_ = $1; }
-    | ModifiedType "*" { $$ = $1; $$->modifier_ = CYNew CYTypePointerTo($$->modifier_); }
-    ;
-
 PrimaryExpression
-    : "^" ModifiedType "(" LexPushInOff TypedParameterListOpt ")" LexPopIn BRACE LexPushInOff FunctionBody "}" LexPopIn { $$ = CYNew CYObjCBlock($2, $5, $10); }
+    : "^" TypedIdentifier { if ($2->identifier_ != NULL) error($2->location_, "unexpected identifier"); } BRACE LexPushInOff FunctionBody "}" LexPopIn { if (CYTypeFunctionWith *function = $2->Function()) $$ = CYNew CYObjCBlock($2, function->parameters_, $6); else error($2->location_, "expected parameters"); }
     ;
 /* }}} */
 /* Cycript (Objective-C): Instance Literals {{{ */
@@ -1724,12 +1702,12 @@ PrimaryExpression
 /* }}} */
 /* Cycript (C): Type Definitions {{{ */
 Statement__
-    : "typedef" TypedIdentifier { if ($2->identifier_ == NULL) YYABORT; } Terminator { $$ = CYNew CYTypeDefinition($2); }
+    : "typedef" TypedIdentifier { if ($2->identifier_ == NULL) error($2->location_, "expected identifier"); } Terminator { $$ = CYNew CYTypeDefinition($2); }
     ;
 /* }}} */
 /* Cycript (C): extern "C" {{{ */
 Statement__
-    : "extern" StringLiteral { if (strcmp($2->Value(), "C") != 0) YYABORT; } TypedIdentifier Terminator { $$ = CYNew CYExternal($2, $4); }
+    : "extern" StringLiteral { if (strcmp($2->Value(), "C") != 0) YYABORT; } TypedIdentifier { if ($4->identifier_ == NULL) error($4->location_, "expected identifier"); } Terminator { $$ = CYNew CYExternal($2, $4); }
     ;
 /* }}} */
 
index 63270a18a331fe897d44a4d6bd13409e63939120..16a576c5e99bc1d786d0c0ecab7259bdcf8bdbb2 100644 (file)
@@ -33,6 +33,7 @@
 #include <cstdlib>
 
 #include "List.hpp"
+#include "Location.hpp"
 #include "Pooling.hpp"
 #include "Options.hpp"
 
@@ -1689,6 +1690,8 @@ struct CYTypeShort :
     virtual void Output(CYOutput &out) const;
 };
 
+struct CYTypeFunctionWith;
+
 struct CYTypeModifier :
     CYNext<CYTypeModifier>
 {
@@ -1704,6 +1707,8 @@ struct CYTypeModifier :
 
     virtual void Output(CYOutput &out, CYIdentifier *identifier) const = 0;
     void Output(CYOutput &out, int precedence, CYIdentifier *identifier) const;
+
+    virtual CYTypeFunctionWith *Function() { return NULL; }
 };
 
 struct CYTypeArrayOf :
@@ -1769,11 +1774,13 @@ struct CYTypedIdentifier :
     CYNext<CYTypedIdentifier>,
     CYThing
 {
+    CYLocation location_;
     CYIdentifier *identifier_;
     CYTypeSpecifier *specifier_;
     CYTypeModifier *modifier_;
 
-    CYTypedIdentifier(CYIdentifier *identifier = NULL) :
+    CYTypedIdentifier(const CYLocation &location, CYIdentifier *identifier = NULL) :
+        location_(location),
         identifier_(identifier),
         specifier_(NULL),
         modifier_(NULL)
@@ -1794,6 +1801,8 @@ struct CYTypedIdentifier :
 
     virtual CYExpression *Replace(CYContext &context);
     virtual void Output(CYOutput &out) const;
+
+    CYTypeFunctionWith *Function();
 };
 
 struct CYEncodedType :
@@ -1943,6 +1952,8 @@ struct CYTypeFunctionWith :
 
     virtual CYExpression *Replace_(CYContext &context, CYExpression *type);
     virtual void Output(CYOutput &out, CYIdentifier *identifier) const;
+
+    virtual CYTypeFunctionWith *Function() { return this; }
 };
 
 namespace cy {
index 155aa58f2d464cfcd83432c0d03e8a6b700809d6..ea2191270ab4fb32f294dd73914526a49624aadf 100644 (file)
@@ -959,6 +959,19 @@ CYExpression *CYTypedIdentifier::Replace(CYContext &context) {
     return modifier_->Replace(context, specifier_->Replace(context));
 }
 
+CYTypeFunctionWith *CYTypedIdentifier::Function() {
+    CYTypeModifier **modifier(&modifier_);
+    if (*modifier == NULL)
+        return NULL;
+    while ((*modifier)->next_ != NULL)
+        modifier = &(*modifier)->next_;
+    CYTypeFunctionWith *function((*modifier)->Function());
+    if (function == NULL)
+        return NULL;
+    *modifier = NULL;
+    return function;
+}
+
 CYArgument *CYTypedParameter::Argument(CYContext &context) { $T(NULL)
     return $ CYArgument(typed_->Replace(context), next_->Argument(context));
 }