From: Jay Freeman (saurik) Date: Wed, 15 Jan 2014 03:44:49 +0000 (-0800) Subject: Update @import syntax to desugar into require(). X-Git-Tag: v0.9.500~52 X-Git-Url: https://git.saurik.com/cycript.git/commitdiff_plain/417dcc1222fa08913a7b671c988878a3568095c2?hp=49c0d26358a08666c00b885839a1d104a808a953 Update @import syntax to desugar into require(). --- diff --git a/Cycript.yy.in b/Cycript.yy.in index 8593f26..89ed1a9 100644 --- a/Cycript.yy.in +++ b/Cycript.yy.in @@ -72,6 +72,7 @@ typedef struct { CYInfix *infix_; CYLiteral *literal_; CYMember *member_; + CYModule *module_; CYNull *null_; CYNumber *number_; CYProgram *program_; @@ -491,6 +492,7 @@ int cylex(YYSTYPE *, cy::location *, void *); %type MessageParameterListOpt %type MessageScope %type ModifiedType +%type Module %type PrefixedType %type PrimitiveType %type SelectorCall_ @@ -1580,20 +1582,13 @@ PrimaryExpression ; /* }}} */ /* Cycript (Objective-C): @import Directive {{{ */ -PathName - : "/" PathName - | "." PathName - | Word PathName - | - ; - -ImportPath - : "<" PathName ">" - | StringLiteral +Module + : Module "." Word { $$ = CYNew CYModule($3, $1); } + | Word { $$ = CYNew CYModule($1); } ; -StatementListItem - : LexSetStatement LexSetRegExp "@import" ImportPath { $$ = CYNew CYImport(); } +Declaration__ + : "@import" Module { $$ = CYNew CYImport($2); } ; /* }}} */ /* Cycript (Objective-C): Boxed Expressions {{{ */ diff --git a/ObjectiveC/Output.cpp b/ObjectiveC/Output.cpp index fd2a8d8..6fd2229 100644 --- a/ObjectiveC/Output.cpp +++ b/ObjectiveC/Output.cpp @@ -94,6 +94,12 @@ void CYMessage::Output(CYOutput &out, bool replace) const { out << code_; } +void CYModule::Output(CYOutput &out) const { + out << part_; + if (next_ != NULL) + out << '.' << next_; +} + void CYBox::Output(CYOutput &out, CYFlags flags) const { out << '@'; value_->Output(out, Precedence(), CYRight(flags)); diff --git a/ObjectiveC/Replace.cpp b/ObjectiveC/Replace.cpp index e7d687f..ec2411b 100644 --- a/ObjectiveC/Replace.cpp +++ b/ObjectiveC/Replace.cpp @@ -93,7 +93,7 @@ CYStatement *CYField::Replace(CYContext &context) const { $T(NULL) } CYStatement *CYImport::Replace(CYContext &context) { - return this; + return $ CYVar($L1($L(module_->part_->Word(), $C1($V("require"), module_->Replace(context, "/"))))); } CYStatement *CYMessage::Replace(CYContext &context, bool replace) const { $T(NULL) @@ -142,6 +142,12 @@ CYExpression *CYMessageParameter::TypeSignature(CYContext &context) const { return MessageType(context, type_, next_); } +CYString *CYModule::Replace(CYContext &context, const char *separator) const { + if (next_ == NULL) + return $ CYString(part_); + return $ CYString($pool.strcat(next_->Replace(context, separator)->Value(), separator, part_->Word(), NULL)); +} + CYExpression *CYBox::Replace(CYContext &context) { return $C1($M($V("Instance"), $S("box")), value_); } diff --git a/ObjectiveC/Syntax.hpp b/ObjectiveC/Syntax.hpp index 618a25e..8fa10ee 100644 --- a/ObjectiveC/Syntax.hpp +++ b/ObjectiveC/Syntax.hpp @@ -169,9 +169,32 @@ struct CYProtocol : void Output(CYOutput &out) const; }; +struct CYModule : + CYNext, + CYThing +{ + CYWord *part_; + + CYModule(CYWord *part, CYModule *next = NULL) : + CYNext(next), + part_(part) + { + } + + CYString *Replace(CYContext &context, const char *separator) const; + void Output(CYOutput &out) const; +}; + struct CYImport : CYStatement { + CYModule *module_; + + CYImport(CYModule *module) : + module_(module) + { + } + virtual CYStatement *Replace(CYContext &context); virtual void Output(CYOutput &out, CYFlags flags) const; };