From 46f4f308790f8c412abb5f81f278822aef198e20 Mon Sep 17 00:00:00 2001 From: "Jay Freeman (saurik)" Date: Sat, 15 Sep 2012 01:28:25 -0700 Subject: [PATCH] Add @encode() support and use its grammar for types. --- Cycript.l.in | 1 + Cycript.yy.in | 41 +++++++++++++++++++++++++++++++++++++++-- ObjectiveC/Output.mm | 4 ++++ ObjectiveC/Replace.cpp | 8 ++++++++ ObjectiveC/Syntax.hpp | 34 ++++++++++++++++++++++++++++++++++ 5 files changed, 86 insertions(+), 2 deletions(-) diff --git a/Cycript.l.in b/Cycript.l.in index 58ed9fd..c946c89 100644 --- a/Cycript.l.in +++ b/Cycript.l.in @@ -254,6 +254,7 @@ XMLName {XMLNameStart}{XMLNamePart}* @end @begin ObjectiveC +"@encode" L C F(tk::AtEncode, hi::Meta); "@end" L C F(tk::AtEnd, hi::Meta); "@implementation" L C F(yyextra->no_.AtImplementation ? tk::AtImplementation_ : tk::AtImplementation, hi::Meta); "@import" L C F(tk::AtImport, hi::Meta); diff --git a/Cycript.yy.in b/Cycript.yy.in index 3f240e4..92d57f6 100644 --- a/Cycript.yy.in +++ b/Cycript.yy.in @@ -73,6 +73,7 @@ typedef struct { CYMember *member_; CYNull *null_; CYNumber *number_; + CYEncodedPart *part_; CYProgram *program_; CYProperty *property_; CYPropertyName *propertyName_; @@ -81,6 +82,7 @@ typedef struct { CYString *string_; CYThis *this_; CYTrue *true_; + CYEncodedType *type_; CYWord *word_; @begin ObjectiveC @@ -228,6 +230,7 @@ int cylex(YYSTYPE *, cy::location *, void *); %token AtImplementation "@implementation" %token AtImplementation_ ";@implementation" %token AtImport "@import" +%token AtEncode "@encode" %token AtEnd "@end" %token AtSelector "@selector" @end @@ -451,6 +454,7 @@ int cylex(YYSTYPE *, cy::location *, void *); %type WithStatement %type Word %type WordOpt +%type Variable @begin ObjectiveC %type BoxableExpression @@ -473,6 +477,8 @@ int cylex(YYSTYPE *, cy::location *, void *); %type MessageParameterList %type MessageParameterListOpt %type MessageScope +%type ModifiedType +%type PrefixedType %type SelectorCall_ %type SelectorCall %type SelectorExpression_ @@ -480,7 +486,9 @@ int cylex(YYSTYPE *, cy::location *, void *); %type SelectorExpressionOpt %type SelectorList %type SelectorWordOpt +%type SuffixedType %type TypeOpt +%type TypedIdentifier %type VariadicCall @end @@ -716,9 +724,13 @@ ParentheticalOpt | { $$ = NULL; } ; +Variable + : Identifier { $$ = CYNew CYVariable($1); } + ; + PrimaryExpression : "this" { $$ = $1; } - | Identifier { $$ = CYNew CYVariable($1); } + | Variable { $$ = $1; } | Literal { $$ = $1; } | ArrayLiteral { $$ = $1; } | ObjectLiteral { $$ = $1; } @@ -1326,6 +1338,31 @@ ProgramBodyOpt /* }}} */ @begin ObjectiveC +/* Cycript (Objective-C): Type Encoding {{{ */ +SuffixedType + : { $$ = NULL; } + | "(" PrefixedType ")" { $$ = $2; } + | SuffixedType "[" NumericLiteral "]" { $$ = CYNew CYEncodedPart($1, "arrayOf", CYNew CYArgument($3)); } + ; + +PrefixedType + : SuffixedType { $$ = $1; } + | "*" PrefixedType { $$ = CYNew CYEncodedPart($2, "pointerTo"); } + ; + +ModifiedType + : Variable { $$ = CYNew CYEncodedType($1); } + | "const" ModifiedType { $2->base_ = CYNew CYCall(CYNew CYDirectMember($2->base_, CYNew CYString("constant"))); $$ = $2; } + ; + +TypedIdentifier + : ModifiedType PrefixedType { $1->parts_ = $2; $$ = $1;} + ; + +PrimaryExpression + : AtEncode "(" TypedIdentifier ")" { $$ = $3; } + ; +/* }}} */ /* Cycript (Objective-C): @class Declaration {{{ */ ClassSuperOpt /* XXX: why the hell did I choose MemberExpression? */ @@ -1348,7 +1385,7 @@ MessageScope ; TypeOpt - : "(" Expression ")" { $$ = $2; } + : "(" TypedIdentifier ")" { $$ = $2; } | "(" LexSetRegExp "void" ")" { $$ = CYNew CYString("v"); } | { $$ = NULL; } ; diff --git a/ObjectiveC/Output.mm b/ObjectiveC/Output.mm index 43172e2..c90418d 100644 --- a/ObjectiveC/Output.mm +++ b/ObjectiveC/Output.mm @@ -75,6 +75,10 @@ void CYClassStatement::Output(CYOutput &out, CYFlags flags) const { CYClass::Output(out, flags); } +void CYEncodedType::Output(CYOutput &out, CYFlags flags) const { + // XXX: this is seriously wrong +} + void CYField::Output(CYOutput &out) const { } diff --git a/ObjectiveC/Replace.cpp b/ObjectiveC/Replace.cpp index 7f4e206..f6e98f1 100644 --- a/ObjectiveC/Replace.cpp +++ b/ObjectiveC/Replace.cpp @@ -78,6 +78,14 @@ CYStatement *CYClassStatement::Replace(CYContext &context) { return $E(Replace_(context)); } +CYExpression *CYEncodedPart::Replace(CYContext &context, CYExpression *base) { $T(base) + return next_->Replace(context, $ CYCall($ CYDirectMember(base, $ CYString(name_)), arguments_)); +} + +CYExpression *CYEncodedType::Replace(CYContext &context) { + return parts_->Replace(context, base_); +} + CYStatement *CYField::Replace(CYContext &context) const { $T(NULL) CYVariable *cyn($V("$cyn")); CYVariable *cyt($V("$cyt")); diff --git a/ObjectiveC/Syntax.hpp b/ObjectiveC/Syntax.hpp index 580ea3a..0caaad4 100644 --- a/ObjectiveC/Syntax.hpp +++ b/ObjectiveC/Syntax.hpp @@ -24,6 +24,40 @@ #include "Parser.hpp" +struct CYEncodedPart : + CYNext +{ + const char *name_; + CYArgument *arguments_; + + CYEncodedPart(CYEncodedPart *next, const char *name, CYArgument *arguments = NULL) : + CYNext(next), + name_(name), + arguments_(arguments) + { + } + + CYExpression *Replace(CYContext &context, CYExpression *base); +}; + +struct CYEncodedType : + CYExpression +{ + CYExpression *base_; + CYEncodedPart *parts_; + + CYEncodedType(CYExpression *base, CYEncodedPart *parts = NULL) : + base_(base), + parts_(parts) + { + } + + CYPrecedence(1) + + virtual CYExpression *Replace(CYContext &context); + virtual void Output(CYOutput &out, CYFlags flags) const; +}; + struct CYBox : CYExpression { -- 2.47.2