X-Git-Url: https://git.saurik.com/cycript.git/blobdiff_plain/b09da87b6557610325d46e3294ea0b6aba69ebf0..2f8f4d8d0207f83fbdc1985ddc3eff1df496a4f5:/Cycript.y diff --git a/Cycript.y b/Cycript.y index e51a0ec..dd0ea51 100644 --- a/Cycript.y +++ b/Cycript.y @@ -1,3 +1,42 @@ +/* Cycript - Remove Execution Server and Disassembler + * Copyright (C) 2009 Jay Freeman (saurik) +*/ + +/* Modified BSD License {{{ */ +/* + * Redistribution and use in source and binary + * forms, with or without modification, are permitted + * provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the + * above copyright notice, this list of conditions + * and the following disclaimer. + * 2. Redistributions in binary form must reproduce the + * above copyright notice, this list of conditions + * and the following disclaimer in the documentation + * and/or other materials provided with the + * distribution. + * 3. The name of the author may not be used to endorse + * or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, + * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR + * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ +/* }}} */ + %code top { #include "Cycript.tab.hh" #define scanner driver.scanner_ @@ -14,9 +53,11 @@ typedef struct { bool bool_; CYArgument *argument_; + CYAssignment *assignment_; CYBoolean *boolean_; CYClause *clause_; CYCatch *catch_; + CYClassName *className_; CYCompound *compound_; CYDeclaration *declaration_; CYDeclarations *declarations_; @@ -28,13 +69,15 @@ typedef struct { CYForInInitialiser *forin_; CYFunctionParameter *functionParameter_; CYIdentifier *identifier_; + CYInfix *infix_; CYLiteral *literal_; + CYMember *member_; CYMessage *message_; CYMessageParameter *messageParameter_; - CYName *name_; CYNull *null_; CYNumber *number_; CYProperty *property_; + CYPropertyName *propertyName_; CYSelectorPart *selector_; CYSource *source_; CYStatement *statement_; @@ -201,7 +244,11 @@ int cylex(YYSTYPE *lvalp, cy::location *llocp, void *scanner); %type ArgumentListOpt %type Arguments %type ArrayLiteral +%type AssigneeExpression +%type AssigneeExpression_ +%type AssigneeExpressionNoBF %type AssignmentExpression +%type AssignmentExpression_ %type AssignmentExpressionNoBF %type AssignmentExpressionNoIn %type BitwiseANDExpression @@ -222,9 +269,12 @@ int cylex(YYSTYPE *lvalp, cy::location *llocp, void *scanner); %type CaseClause %type CaseClausesOpt %type CatchOpt -%type ClassDeclaration +%type CategoryStatement +%type ClassExpression %type ClassMessageDeclaration %type ClassMessageDeclarationListOpt +%type ClassName +%type ClassNameOpt %type ClassSuperOpt %type ClassFieldList %type ConditionalExpression @@ -269,7 +319,6 @@ int cylex(YYSTYPE *lvalp, cy::location *llocp, void *scanner); %type IterationStatement %type LabelledStatement %type LeftHandSideExpression -%type LeftHandSideExpression_ %type LeftHandSideExpressionNoBF %type Literal %type LogicalANDExpression @@ -278,6 +327,7 @@ int cylex(YYSTYPE *lvalp, cy::location *llocp, void *scanner); %type LogicalORExpression %type LogicalORExpressionNoBF %type LogicalORExpressionNoIn +%type MemberAccess %type MemberExpression %type MemberExpression_ %type MemberExpressionNoBF @@ -299,13 +349,15 @@ int cylex(YYSTYPE *lvalp, cy::location *llocp, void *scanner); %type PrimaryExpression_ %type PrimaryExpressionNoBF %type Program -%type PropertyName +%type PropertyName %type PropertyNameAndValueList %type PropertyNameAndValueList_ %type PropertyNameAndValueListOpt %type RelationalExpression +%type RelationalExpression_ %type RelationalExpressionNoBF %type RelationalExpressionNoIn +%type RelationalExpressionNoIn_ %type ReturnStatement %type SelectorExpression %type SelectorExpression_ @@ -545,18 +597,21 @@ MemberExpression_ : "new" MemberExpression Arguments { $$ = new(driver.pool_) CYNew($2, $3); } ; +MemberAccess + : "[" Expression "]" { $$ = new(driver.pool_) CYDirectMember(NULL, $2); } + | "." Identifier { $$ = new(driver.pool_) CYDirectMember(NULL, new(driver.pool_) CYString($2)); } + ; + MemberExpression : PrimaryExpression { $$ = $1; } | FunctionExpression { $$ = $1; } - | MemberExpression "[" Expression "]" { $$ = new(driver.pool_) CYMember($1, $3); } - | MemberExpression "." Identifier { $$ = new(driver.pool_) CYMember($1, new(driver.pool_) CYString($3)); } + | MemberExpression MemberAccess { $2->SetLeft($1); $$ = $2; } | MemberExpression_ { $$ = $1; } ; MemberExpressionNoBF : PrimaryExpressionNoBF { $$ = $1; } - | MemberExpressionNoBF "[" Expression "]" { $$ = new(driver.pool_) CYMember($1, $3); } - | MemberExpressionNoBF "." Identifier { $$ = new(driver.pool_) CYMember($1, new(driver.pool_) CYString($3)); } + | MemberExpressionNoBF MemberAccess { $2->SetLeft($1); $$ = $2; } | MemberExpression_ { $$ = $1; } ; @@ -577,15 +632,13 @@ NewExpressionNoBF CallExpression : MemberExpression Arguments { $$ = new(driver.pool_) CYCall($1, $2); } | CallExpression Arguments { $$ = new(driver.pool_) CYCall($1, $2); } - | CallExpression "[" Expression "]" { $$ = new(driver.pool_) CYMember($1, $3); } - | CallExpression "." Identifier { $$ = new(driver.pool_) CYMember($1, new(driver.pool_) CYString($3)); } + | CallExpression MemberAccess { $2->SetLeft($1); $$ = $2; } ; CallExpressionNoBF : MemberExpressionNoBF Arguments { $$ = new(driver.pool_) CYCall($1, $2); } | CallExpressionNoBF Arguments { $$ = new(driver.pool_) CYCall($1, $2); } - | CallExpressionNoBF "[" Expression "]" { $$ = new(driver.pool_) CYMember($1, $3); } - | CallExpressionNoBF "." Identifier { $$ = new(driver.pool_) CYMember($1, new(driver.pool_) CYString($3)); } + | CallExpressionNoBF MemberAccess { $2->SetLeft($1); $$ = $2; } ; ArgumentList_ @@ -609,23 +662,21 @@ Arguments LeftHandSideExpression : NewExpression { $$ = $1; } | CallExpression { $$ = $1; } - | LeftHandSideExpression_ { $$ = $1; } ; LeftHandSideExpressionNoBF : NewExpressionNoBF { $$ = $1; } | CallExpressionNoBF { $$ = $1; } - | LeftHandSideExpression_ { $$ = $1; } ; PostfixExpression - : LeftHandSideExpression { $$ = $1; } + : AssigneeExpression { $$ = $1; } | LeftHandSideExpression "++" { $$ = new(driver.pool_) CYPostIncrement($1); } | LeftHandSideExpression "--" { $$ = new(driver.pool_) CYPostDecrement($1); } ; PostfixExpressionNoBF - : LeftHandSideExpressionNoBF { $$ = $1; } + : AssigneeExpressionNoBF { $$ = $1; } | LeftHandSideExpressionNoBF "++" { $$ = new(driver.pool_) CYPostIncrement($1); } | LeftHandSideExpressionNoBF "--" { $$ = new(driver.pool_) CYPostDecrement($1); } ; @@ -694,33 +745,32 @@ ShiftExpressionNoBF | ShiftExpressionNoBF ">>>" AdditiveExpression { $$ = new(driver.pool_) CYShiftRightUnsigned($1, $3); } ; +RelationalExpressionNoIn_ + : "<" ShiftExpression { $$ = new(driver.pool_) CYLess(NULL, $2); } + | ">" ShiftExpression { $$ = new(driver.pool_) CYGreater(NULL, $2); } + | "<=" ShiftExpression { $$ = new(driver.pool_) CYLessOrEqual(NULL, $2); } + | ">=" ShiftExpression { $$ = new(driver.pool_) CYGreaterOrEqual(NULL, $2); } + | "instanceof" ShiftExpression { $$ = new(driver.pool_) CYInstanceOf(NULL, $2); } + ; + +RelationalExpression_ + : RelationalExpressionNoIn_ { $$ = $1; } + | "in" ShiftExpression { $$ = new(driver.pool_) CYIn(NULL, $2); } + ; + RelationalExpression : ShiftExpression { $$ = $1; } - | RelationalExpression "<" ShiftExpression { $$ = new(driver.pool_) CYLess($1, $3); } - | RelationalExpression ">" ShiftExpression { $$ = new(driver.pool_) CYGreater($1, $3); } - | RelationalExpression "<=" ShiftExpression { $$ = new(driver.pool_) CYLessOrEqual($1, $3); } - | RelationalExpression ">=" ShiftExpression { $$ = new(driver.pool_) CYGreaterOrEqual($1, $3); } - | RelationalExpression "instanceof" ShiftExpression { $$ = new(driver.pool_) CYInstanceOf($1, $3); } - | RelationalExpression "in" ShiftExpression { $$ = new(driver.pool_) CYIn($1, $3); } + | RelationalExpression RelationalExpression_ { $2->SetLeft($1); $$ = $2; } ; RelationalExpressionNoIn : ShiftExpression { $$ = $1; } - | RelationalExpressionNoIn "<" ShiftExpression { $$ = new(driver.pool_) CYLess($1, $3); } - | RelationalExpressionNoIn ">" ShiftExpression { $$ = new(driver.pool_) CYGreater($1, $3); } - | RelationalExpressionNoIn "<=" ShiftExpression { $$ = new(driver.pool_) CYLessOrEqual($1, $3); } - | RelationalExpressionNoIn ">=" ShiftExpression { $$ = new(driver.pool_) CYGreaterOrEqual($1, $3); } - | RelationalExpressionNoIn "instanceof" ShiftExpression { $$ = new(driver.pool_) CYInstanceOf($1, $3); } + | RelationalExpressionNoIn RelationalExpressionNoIn_ { $2->SetLeft($1); $$ = $2; } ; RelationalExpressionNoBF : ShiftExpressionNoBF { $$ = $1; } - | RelationalExpressionNoBF "<" ShiftExpression { $$ = new(driver.pool_) CYLess($1, $3); } - | RelationalExpressionNoBF ">" ShiftExpression { $$ = new(driver.pool_) CYGreater($1, $3); } - | RelationalExpressionNoBF "<=" ShiftExpression { $$ = new(driver.pool_) CYLessOrEqual($1, $3); } - | RelationalExpressionNoBF ">=" ShiftExpression { $$ = new(driver.pool_) CYGreaterOrEqual($1, $3); } - | RelationalExpressionNoBF "instanceof" ShiftExpression { $$ = new(driver.pool_) CYInstanceOf($1, $3); } - | RelationalExpressionNoBF "in" ShiftExpression { $$ = new(driver.pool_) CYIn($1, $3); } + | RelationalExpressionNoBF RelationalExpression_ { $2->SetLeft($1); $$ = $2; } ; EqualityExpression @@ -837,52 +887,55 @@ ConditionalExpressionNoBF | LogicalORExpressionNoBF "?" AssignmentExpression ":" AssignmentExpression { $$ = new(driver.pool_) CYCondition($1, $3, $5); } ; +AssignmentExpression_ + : "=" AssignmentExpression { $$ = new(driver.pool_) CYAssign(NULL, $2); } + | "*=" AssignmentExpression { $$ = new(driver.pool_) CYMultiplyAssign(NULL, $2); } + | "/=" AssignmentExpression { $$ = new(driver.pool_) CYDivideAssign(NULL, $2); } + | "%=" AssignmentExpression { $$ = new(driver.pool_) CYModulusAssign(NULL, $2); } + | "+=" AssignmentExpression { $$ = new(driver.pool_) CYAddAssign(NULL, $2); } + | "-=" AssignmentExpression { $$ = new(driver.pool_) CYSubtractAssign(NULL, $2); } + | "<<=" AssignmentExpression { $$ = new(driver.pool_) CYShiftLeftAssign(NULL, $2); } + | ">>=" AssignmentExpression { $$ = new(driver.pool_) CYShiftRightSignedAssign(NULL, $2); } + | ">>>=" AssignmentExpression { $$ = new(driver.pool_) CYShiftRightUnsignedAssign(NULL, $2); } + | "&=" AssignmentExpression { $$ = new(driver.pool_) CYBitwiseAndAssign(NULL, $2); } + | "^=" AssignmentExpression { $$ = new(driver.pool_) CYBitwiseXOrAssign(NULL, $2); } + | "|=" AssignmentExpression { $$ = new(driver.pool_) CYBitwiseOrAssign(NULL, $2); } + ; + +AssigneeExpression + : LeftHandSideExpression { $$ = $1; } + | AssigneeExpression_ { $$ = $1; } + ; + +AssigneeExpressionNoBF + : LeftHandSideExpressionNoBF { $$ = $1; } + | AssigneeExpression_ { $$ = $1; } + ; + AssignmentExpression : ConditionalExpression { $$ = $1; } - | LeftHandSideExpression "=" AssignmentExpression { $$ = new(driver.pool_) CYAssign($1, $3); } - | LeftHandSideExpression "*=" AssignmentExpression { $$ = new(driver.pool_) CYMultiplyAssign($1, $3); } - | LeftHandSideExpression "/=" AssignmentExpression { $$ = new(driver.pool_) CYDivideAssign($1, $3); } - | LeftHandSideExpression "%=" AssignmentExpression { $$ = new(driver.pool_) CYModulusAssign($1, $3); } - | LeftHandSideExpression "+=" AssignmentExpression { $$ = new(driver.pool_) CYAddAssign($1, $3); } - | LeftHandSideExpression "-=" AssignmentExpression { $$ = new(driver.pool_) CYSubtractAssign($1, $3); } - | LeftHandSideExpression "<<=" AssignmentExpression { $$ = new(driver.pool_) CYShiftLeftAssign($1, $3); } - | LeftHandSideExpression ">>=" AssignmentExpression { $$ = new(driver.pool_) CYShiftRightSignedAssign($1, $3); } - | LeftHandSideExpression ">>>=" AssignmentExpression { $$ = new(driver.pool_) CYShiftRightUnsignedAssign($1, $3); } - | LeftHandSideExpression "&=" AssignmentExpression { $$ = new(driver.pool_) CYBitwiseAndAssign($1, $3); } - | LeftHandSideExpression "^=" AssignmentExpression { $$ = new(driver.pool_) CYBitwiseXOrAssign($1, $3); } - | LeftHandSideExpression "|=" AssignmentExpression { $$ = new(driver.pool_) CYBitwiseOrAssign($1, $3); } + | AssigneeExpression AssignmentExpression_ { $2->SetLeft($1); $$ = $2; } ; AssignmentExpressionNoIn : ConditionalExpressionNoIn { $$ = $1; } - | LeftHandSideExpression "=" AssignmentExpressionNoIn { $$ = new(driver.pool_) CYAssign($1, $3); } - | LeftHandSideExpression "*=" AssignmentExpressionNoIn { $$ = new(driver.pool_) CYMultiplyAssign($1, $3); } - | LeftHandSideExpression "/=" AssignmentExpressionNoIn { $$ = new(driver.pool_) CYDivideAssign($1, $3); } - | LeftHandSideExpression "%=" AssignmentExpressionNoIn { $$ = new(driver.pool_) CYModulusAssign($1, $3); } - | LeftHandSideExpression "+=" AssignmentExpressionNoIn { $$ = new(driver.pool_) CYAddAssign($1, $3); } - | LeftHandSideExpression "-=" AssignmentExpressionNoIn { $$ = new(driver.pool_) CYSubtractAssign($1, $3); } - | LeftHandSideExpression "<<=" AssignmentExpressionNoIn { $$ = new(driver.pool_) CYShiftLeftAssign($1, $3); } - | LeftHandSideExpression ">>=" AssignmentExpressionNoIn { $$ = new(driver.pool_) CYShiftRightSignedAssign($1, $3); } - | LeftHandSideExpression ">>>=" AssignmentExpressionNoIn { $$ = new(driver.pool_) CYShiftRightUnsignedAssign($1, $3); } - | LeftHandSideExpression "&=" AssignmentExpressionNoIn { $$ = new(driver.pool_) CYBitwiseAndAssign($1, $3); } - | LeftHandSideExpression "^=" AssignmentExpressionNoIn { $$ = new(driver.pool_) CYBitwiseXOrAssign($1, $3); } - | LeftHandSideExpression "|=" AssignmentExpressionNoIn { $$ = new(driver.pool_) CYBitwiseOrAssign($1, $3); } + | AssigneeExpression "=" AssignmentExpressionNoIn { $$ = new(driver.pool_) CYAssign($1, $3); } + | AssigneeExpression "*=" AssignmentExpressionNoIn { $$ = new(driver.pool_) CYMultiplyAssign($1, $3); } + | AssigneeExpression "/=" AssignmentExpressionNoIn { $$ = new(driver.pool_) CYDivideAssign($1, $3); } + | AssigneeExpression "%=" AssignmentExpressionNoIn { $$ = new(driver.pool_) CYModulusAssign($1, $3); } + | AssigneeExpression "+=" AssignmentExpressionNoIn { $$ = new(driver.pool_) CYAddAssign($1, $3); } + | AssigneeExpression "-=" AssignmentExpressionNoIn { $$ = new(driver.pool_) CYSubtractAssign($1, $3); } + | AssigneeExpression "<<=" AssignmentExpressionNoIn { $$ = new(driver.pool_) CYShiftLeftAssign($1, $3); } + | AssigneeExpression ">>=" AssignmentExpressionNoIn { $$ = new(driver.pool_) CYShiftRightSignedAssign($1, $3); } + | AssigneeExpression ">>>=" AssignmentExpressionNoIn { $$ = new(driver.pool_) CYShiftRightUnsignedAssign($1, $3); } + | AssigneeExpression "&=" AssignmentExpressionNoIn { $$ = new(driver.pool_) CYBitwiseAndAssign($1, $3); } + | AssigneeExpression "^=" AssignmentExpressionNoIn { $$ = new(driver.pool_) CYBitwiseXOrAssign($1, $3); } + | AssigneeExpression "|=" AssignmentExpressionNoIn { $$ = new(driver.pool_) CYBitwiseOrAssign($1, $3); } ; AssignmentExpressionNoBF : ConditionalExpressionNoBF { $$ = $1; } - | LeftHandSideExpressionNoBF "=" AssignmentExpression { $$ = new(driver.pool_) CYAssign($1, $3); } - | LeftHandSideExpressionNoBF "*=" AssignmentExpression { $$ = new(driver.pool_) CYMultiplyAssign($1, $3); } - | LeftHandSideExpressionNoBF "/=" AssignmentExpression { $$ = new(driver.pool_) CYDivideAssign($1, $3); } - | LeftHandSideExpressionNoBF "%=" AssignmentExpression { $$ = new(driver.pool_) CYModulusAssign($1, $3); } - | LeftHandSideExpressionNoBF "+=" AssignmentExpression { $$ = new(driver.pool_) CYAddAssign($1, $3); } - | LeftHandSideExpressionNoBF "-=" AssignmentExpression { $$ = new(driver.pool_) CYSubtractAssign($1, $3); } - | LeftHandSideExpressionNoBF "<<=" AssignmentExpression { $$ = new(driver.pool_) CYShiftLeftAssign($1, $3); } - | LeftHandSideExpressionNoBF ">>=" AssignmentExpression { $$ = new(driver.pool_) CYShiftRightSignedAssign($1, $3); } - | LeftHandSideExpressionNoBF ">>>=" AssignmentExpression { $$ = new(driver.pool_) CYShiftRightUnsignedAssign($1, $3); } - | LeftHandSideExpressionNoBF "&=" AssignmentExpression { $$ = new(driver.pool_) CYBitwiseAndAssign($1, $3); } - | LeftHandSideExpressionNoBF "^=" AssignmentExpression { $$ = new(driver.pool_) CYBitwiseXOrAssign($1, $3); } - | LeftHandSideExpressionNoBF "|=" AssignmentExpression { $$ = new(driver.pool_) CYBitwiseOrAssign($1, $3); } + | AssigneeExpressionNoBF AssignmentExpression_ { $2->SetLeft($1); $$ = $2; } ; Expression_ @@ -935,7 +988,7 @@ Statement ; Block - : "{" StatementListOpt "}" { $$ = $2 ?: new(driver.pool_) CYEmpty(); } + : "{" StatementListOpt "}" { if ($2) $$ = new(driver.pool_) CYBlock($2); else $$ = new(driver.pool_) CYEmpty(); } ; StatementList @@ -1184,16 +1237,34 @@ ClassMessageDeclaration ; ClassMessageDeclarationListOpt - : ClassMessageDeclarationListOpt ClassMessageDeclaration { if ($1) { $1->SetNext($2); $$ = $1; } else $$ = $2; } + : ClassMessageDeclarationListOpt ClassMessageDeclaration { $2->SetNext($1); $$ = $2; } | { $$ = NULL; } ; -ClassDeclaration - : "@class" Identifier ClassSuperOpt ClassFieldList ClassMessageDeclarationListOpt "@end" { $$ = new CYClass($2, $3, $4, $5); } +ClassName + : Identifier { $$ = $1; } + | "(" AssignmentExpression ")" { $$ = $2; } ; -SourceElement - : ClassDeclaration { $$ = $1; } +ClassNameOpt + : ClassName { $$ = $1; } + | { $$ = NULL; } + ; + +ClassExpression + : "@class" ClassNameOpt ClassSuperOpt ClassFieldList ClassMessageDeclarationListOpt "@end" { $$ = new CYClass($2, $3, $4, $5); } + ; + +CategoryStatement + : "@class" ClassName ClassMessageDeclarationListOpt "@end" { $$ = new CYCategory($2, $3); } + ; + +PrimaryExpression_ + : ClassExpression { $$ = $1; } + ; + +Statement + : CategoryStatement ; VariadicCall @@ -1239,12 +1310,42 @@ PrimaryExpression_ ; /* }}} */ -LeftHandSideExpression_ - : "*" LeftHandSideExpression { $$ = new(driver.pool_) CYIndirect($2); } +AssigneeExpression_ + : "*" UnaryExpression { $$ = new(driver.pool_) CYIndirect($2); } ; UnaryExpression_ : "&" UnaryExpression { $$ = new(driver.pool_) CYAddressOf($2); } ; +MemberAccess + : "->" Identifier { $$ = new(driver.pool_) CYIndirectMember(NULL, new(driver.pool_) CYString($2)); } + ; + +/* + +IfComprehension + : "if" "(" Expression ")" + ; + +ForComprehension + : "for" "(" ForInStatementInitialiser "in" Expression ")" + ; + +ComprehensionListOpt + : ComprehensionList + | IfComprehension + | + ; + +ComprehensionList + : ForComprehension ComprehensionListOpt + ; + +PrimaryExpression_ + : "[" AssignmentExpression ComprehensionList "]" + ; + +*/ + %%