From 00b4cb83ba50bc0dcd209dfde3b99836ae8b7666 Mon Sep 17 00:00:00 2001 From: "Jay Freeman (saurik)" Date: Mon, 2 Nov 2015 05:22:28 -0800 Subject: [PATCH] Fix a few poor syntax assumptions in C-like types. --- Cycript.yy.in | 44 +++++++++++--------------------------------- Parser.hpp | 13 ++++++++++++- Replace.cpp | 13 +++++++++++++ 3 files changed, 36 insertions(+), 34 deletions(-) diff --git a/Cycript.yy.in b/Cycript.yy.in index 9cf3b8e..d963589 100644 --- a/Cycript.yy.in +++ b/Cycript.yy.in @@ -477,15 +477,12 @@ int cylex(YYSTYPE *, CYLocation *, void *); %type Variable @begin C -%type ArrayedType -%type FunctionedType %type IntegerType %type IntegerTypeOpt %type PrefixedType %type PrimitiveType -%type TypeParenthetical -%type TypeSignifier %type SuffixedType +%type TypeSignifier %type TypeQualifierLeft %type TypeQualifierRight %type TypedIdentifier @@ -515,7 +512,6 @@ int cylex(YYSTYPE *, CYLocation *, void *); %type MessageParameterList %type MessageParameterListOpt %type MessageScope -%type ModifiedType %type SelectorCall_ %type SelectorCall %type 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); } ; /* }}} */ diff --git a/Parser.hpp b/Parser.hpp index 63270a1..16a576c 100644 --- a/Parser.hpp +++ b/Parser.hpp @@ -33,6 +33,7 @@ #include #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 { @@ -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, 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 { diff --git a/Replace.cpp b/Replace.cpp index 155aa58..ea21912 100644 --- a/Replace.cpp +++ b/Replace.cpp @@ -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)); } -- 2.47.2