X-Git-Url: https://git.saurik.com/cycript.git/blobdiff_plain/cacd1a887afacbdd77cf3d9ce3f0c580af9b3614..2892653821db515977146a160552209d7cd3cb6c:/Cycript.y.in diff --git a/Cycript.y.in b/Cycript.y.in index a0ce2cc..37e7611 100644 --- a/Cycript.y.in +++ b/Cycript.y.in @@ -1,4 +1,4 @@ -/* Cycript - Remote Execution Server and Disassembler +/* Cycript - Inlining/Optimizing JavaScript Compiler * Copyright (C) 2009 Jay Freeman (saurik) */ @@ -47,7 +47,11 @@ #include "Parser.hpp" @begin ObjectiveC -#include "ObjectiveC.hpp" +#include "ObjectiveC/Syntax.hpp" +@end + +@begin E4X +#include "E4X.hpp" @end typedef struct { @@ -62,7 +66,7 @@ typedef struct { CYAssignment *assignment_; CYBoolean *boolean_; CYClause *clause_; - CYCatch *catch_; + cy::Syntax::Catch *catch_; CYComprehension *comprehension_; CYCompound *compound_; CYDeclaration *declaration_; @@ -96,6 +100,10 @@ typedef struct { CYMessageParameter *messageParameter_; CYSelectorPart *selector_; @end + +@begin E4X + CYAttribute *attribute_; +@end }; } YYSTYPE; @@ -124,7 +132,29 @@ int cylex(YYSTYPE *lvalp, cy::location *llocp, void *scanner); %parse-param { CYDriver &driver } %lex-param { void *scanner } +@begin E4X +%token XMLCDATA +%token XMLComment +%token XMLPI + +%token XMLAttributeValue +%token XMLName +%token XMLTagCharacters +%token XMLText +%token XMLWhitespace +@end + +@begin E4X +%token LeftRight "<>" +%token LeftSlashRight "" + +%token SlashRight "/>" +%token LeftSlash " Each "each" +@begin E4X +// E4X Conditional +%token Namespace "namespace" +%token XML "xml" +@end + %token Identifier_ %token NumericLiteral %token StringLiteral @@ -279,7 +315,6 @@ int cylex(YYSTYPE *lvalp, cy::location *llocp, void *scanner); %type Arguments %type ArrayLiteral %type AssigneeExpression -%type AssigneeExpression_ %type AssigneeExpressionNoBF %type AssignmentExpression %type AssignmentExpression_ @@ -376,13 +411,12 @@ int cylex(YYSTYPE *lvalp, cy::location *llocp, void *scanner); %type PrimaryExpression %type PrimaryExpression_ %type PrimaryExpressionNoBF +%type PrimaryExpressionNoBF_ %type Program %type PropertyName %type PropertyNameAndValueList %type PropertyNameAndValueList_ %type PropertyNameAndValueListOpt -%type RegularExpressionLiteral_ -%type RegularExpressionToken %type RelationalExpression %type RelationalExpression_ %type RelationalExpressionNoBF @@ -392,6 +426,7 @@ int cylex(YYSTYPE *lvalp, cy::location *llocp, void *scanner); %type ShiftExpression %type ShiftExpressionNoBF %type SourceElement +%type SourceElement_ %type SourceElements %type Statement %type Statement_ @@ -400,6 +435,7 @@ int cylex(YYSTYPE *lvalp, cy::location *llocp, void *scanner); %type SwitchStatement %type ThrowStatement %type TryStatement +%type UnaryAssigneeExpression %type UnaryExpression %type UnaryExpression_ %type UnaryExpressionNoBF @@ -441,6 +477,28 @@ int cylex(YYSTYPE *lvalp, cy::location *llocp, void *scanner); %type WordOpt @end +@begin E4X +%type PropertyIdentifier_ +%type PropertySelector +%type PropertySelector_ +%type QualifiedIdentifier +%type QualifiedIdentifier_ +%type WildcardIdentifier +%type XMLComment +%type XMLCDATA +%type XMLElement +%type XMLElementContent +%type XMLMarkup +%type XMLPI + +%type AttributeIdentifier +%type DefaultXMLNamespaceStatement +%type PropertyIdentifier +%type XMLListInitialiser +%type XMLInitialiser +@end + +/* %left "*" "/" "%" %left "+" "-" %left "<<" ">>" ">>>" @@ -453,6 +511,7 @@ int cylex(YYSTYPE *lvalp, cy::location *llocp, void *scanner); %left "||" %right "=" "*=" "/=" "%=" "+=" "-=" "<<=" ">>=" ">>>=" "&=" "^=" "|=" +*/ %nonassoc "if" %nonassoc "else" @@ -461,6 +520,12 @@ int cylex(YYSTYPE *lvalp, cy::location *llocp, void *scanner); %% +/* Lexer State {{{ */ +LexSetRegExp + : { driver.SetCondition(CYDriver::RegExpCondition); } + ; +/* }}} */ + StrictSemi : { driver.Warning(yylloc, "warning, automatic semi-colon insertion required"); } ; @@ -576,21 +641,12 @@ IdentifierOpt | { $$ = NULL; } ; -RegularExpressionToken - : "/" { $$ = CYDriver::RegExStart; } - | "/=" { $$ = CYDriver::RegExRest; } - ; - -RegularExpressionLiteral_ - : RegularExpressionToken { driver.SetCondition($1); } RegularExpressionLiteral { $$ = $3; } - ; - Literal : NullLiteral { $$ = $1; } | BooleanLiteral { $$ = $1; } | NumericLiteral { $$ = $1; } | StringLiteral { $$ = $1; } - | RegularExpressionLiteral_ { $$ = $1; } + | RegularExpressionLiteral { $$ = $1; } ; NullLiteral @@ -604,6 +660,11 @@ BooleanLiteral /* 11.1 Primary Expressions {{{ */ PrimaryExpression_ + : ObjectLiteral { $$ = $1; } + | PrimaryExpressionNoBF_ { $$ = $1; } + ; + +PrimaryExpressionNoBF_ : "this" { $$ = $1; } | Identifier { $$ = new(driver.pool_) CYVariable($1); } | Literal { $$ = $1; } @@ -612,12 +673,11 @@ PrimaryExpression_ ; PrimaryExpression - : ObjectLiteral { $$ = $1; } - | PrimaryExpression_ { $$ = $1; } + : LexSetRegExp PrimaryExpression_ { $$ = $2; } ; PrimaryExpressionNoBF - : PrimaryExpression_ { $$ = $1; } + : PrimaryExpressionNoBF_ { $$ = $1; } ; /* }}} */ /* 11.1.4 Array Initialiser {{{ */ @@ -631,12 +691,12 @@ Element ElementOpt : Element { $$ = $1; } - | { $$ = NULL; } + | LexSetRegExp { $$ = NULL; } ; ElementListOpt : ElementList { $$ = $1; } - | { $$ = NULL; } + | LexSetRegExp { $$ = NULL; } ; ElementList @@ -684,7 +744,7 @@ MemberExpression : PrimaryExpression { $$ = $1; } | FunctionExpression { $$ = $1; } | MemberExpression MemberAccess { $2->SetLeft($1); $$ = $2; } - | MemberExpression_ { $$ = $1; } + | LexSetRegExp MemberExpression_ { $$ = $2; } ; MemberExpressionNoBF @@ -699,7 +759,7 @@ NewExpression_ NewExpression : MemberExpression { $$ = $1; } - | NewExpression_ { $$ = $1; } + | LexSetRegExp NewExpression_ { $$ = $2; } ; NewExpressionNoBF @@ -726,7 +786,7 @@ ArgumentList_ ArgumentListOpt : ArgumentList { $$ = $1; } - | { $$ = NULL; } + | LexSetRegExp { $$ = NULL; } ; ArgumentList @@ -777,7 +837,7 @@ UnaryExpression_ UnaryExpression : PostfixExpression { $$ = $1; } - | UnaryExpression_ { $$ = $1; } + | LexSetRegExp UnaryExpression_ { $$ = $2; } ; UnaryExpressionNoBF @@ -993,12 +1053,12 @@ AssignmentExpression_ AssigneeExpression : LeftHandSideExpression { $$ = $1; } - | AssigneeExpression_ { $$ = $1; } + | LexSetRegExp UnaryAssigneeExpression { $$ = $2; } ; AssigneeExpressionNoBF : LeftHandSideExpressionNoBF { $$ = $1; } - | AssigneeExpression_ { $$ = $1; } + | UnaryAssigneeExpression { $$ = $1; } ; AssignmentExpression @@ -1040,12 +1100,12 @@ ExpressionNoIn_ ExpressionOpt : Expression { $$ = $1; } - | { $$ = NULL; } + | LexSetRegExp { $$ = NULL; } ; ExpressionNoInOpt : ExpressionNoIn { $$ = $1; } - | { $$ = NULL; } + | LexSetRegExp { $$ = NULL; } ; Expression @@ -1080,7 +1140,7 @@ Statement_ ; Statement - : Statement_ { $$ = $1; } + : LexSetRegExp Statement_ { $$ = $2; } ; /* }}} */ /* 12.1 Block {{{ */ @@ -1098,7 +1158,7 @@ StatementList StatementListOpt : StatementList { $$ = $1; } - | { $$ = NULL; } + | LexSetRegExp { $$ = NULL; } ; /* }}} */ /* 12.2 Variable Statement {{{ */ @@ -1196,7 +1256,7 @@ ForStatement ForStatementInitialiser : ExpressionNoInOpt { $$ = $1; } - | "var" VariableDeclarationListNoIn { $$ = $2; } + | LexSetRegExp "var" VariableDeclarationListNoIn { $$ = $3; } ; /* }}} */ /* 12.6.4 The for-in Statement {{{ */ @@ -1206,7 +1266,7 @@ ForInStatement ForInStatementInitialiser : LeftHandSideExpression { $$ = $1; } - | "var" VariableDeclarationNoIn { $$ = $2; } + | LexSetRegExp "var" VariableDeclarationNoIn { $$ = $3; } ; /* }}} */ @@ -1261,16 +1321,16 @@ LabelledStatement /* }}} */ /* 12.13 The throw Statement {{{ */ ThrowStatement - : "throw" Expression Terminator { $$ = new(driver.pool_) CYThrow($2); } + : "throw" Expression Terminator { $$ = new(driver.pool_) cy::Syntax::Throw($2); } ; /* }}} */ /* 12.14 The try Statement {{{ */ TryStatement - : "try" Block_ CatchOpt FinallyOpt { $$ = new(driver.pool_) CYTry($2, $3, $4); } + : "try" Block_ CatchOpt FinallyOpt { $$ = new(driver.pool_) cy::Syntax::Try($2, $3, $4); } ; CatchOpt - : "catch" "(" Identifier ")" Block_ { $$ = new(driver.pool_) CYCatch($3, $5); } + : "catch" "(" Identifier ")" Block_ { $$ = new(driver.pool_) cy::Syntax::Catch($3, $5); } | { $$ = NULL; } ; @@ -1286,7 +1346,7 @@ FunctionDeclaration ; FunctionExpression - : "function" IdentifierOpt "(" FormalParameterList ")" "{" FunctionBody "}" { $$ = new(driver.pool_) CYFunctionExpression($2, $4, $7); } + : LexSetRegExp "function" IdentifierOpt "(" FormalParameterList ")" "{" FunctionBody "}" { $$ = new(driver.pool_) CYFunctionExpression($3, $5, $8); } ; FormalParameterList_ @@ -1310,19 +1370,24 @@ Program SourceElements : SourceElement SourceElements { $1->SetNext($2); $$ = $1; } - | { $$ = NULL; } + | LexSetRegExp { $$ = NULL; } ; -SourceElement +SourceElement_ : Statement_ { $$ = $1; } | FunctionDeclaration { $$ = $1; } ; + +SourceElement + : LexSetRegExp SourceElement_ { $$ = $2; } + ; /* }}} */ @begin ObjectiveC /* Cycript (Objective-C): @class Declaration {{{ */ ClassSuperOpt - : ":" MemberExpressionNoBF { $$ = $2; } + /* XXX: why the hell did I choose MemberExpressionNoBF? */ + : ":" LexSetRegExp MemberExpressionNoBF { $$ = $3; } | { $$ = NULL; } ; @@ -1389,7 +1454,7 @@ CategoryStatement : "@class" ClassName ClassMessageDeclarationListOpt "@end" { $$ = new(driver.pool_) CYCategory($2, $3); } ; -PrimaryExpression +PrimaryExpression_ : ClassExpression { $$ = $1; } ; @@ -1420,7 +1485,7 @@ SelectorList MessageExpression : "[" AssignmentExpression SelectorList "]" { $$ = new(driver.pool_) CYSendDirect($2, $3); } - | "[" "super" SelectorList "]" { $$ = new(driver.pool_) CYSendSuper($3); } + | "[" LexSetRegExp "super" SelectorList "]" { $$ = new(driver.pool_) CYSendSuper($4); } ; SelectorExpressionOpt @@ -1437,7 +1502,7 @@ SelectorExpression | Word { $$ = new(driver.pool_) CYSelectorPart($1, false, NULL); } ; -PrimaryExpression_ +PrimaryExpressionNoBF_ : MessageExpression { $$ = $1; } | "@selector" "(" SelectorExpression ")" { $$ = new(driver.pool_) CYSelector($3); } ; @@ -1446,8 +1511,8 @@ PrimaryExpression_ @begin C /* Cycript (C): Pointer Indirection/Addressing {{{ */ -AssigneeExpression_ - : "*" UnaryExpression { $$ = new(driver.pool_) CYIndirect($2); } +UnaryAssigneeExpression + : "^" UnaryExpression { $$ = new(driver.pool_) CYIndirect($2); } ; UnaryExpression_ @@ -1461,6 +1526,186 @@ MemberAccess /* }}} */ @end +@begin E4X +/* Lexer State {{{ */ +LexPushRegExp + : { driver.PushCondition(CYDriver::RegExpCondition); } + ; + +LexPushXMLContent + : { driver.PushCondition(CYDriver::XMLContentCondition); } + ; + +LexPushXMLTag + : { driver.PushCondition(CYDriver::XMLTagCondition); } + ; + +LexPop + : { driver.PopCondition(); } + ; + +LexSetXMLContent + : { driver.SetCondition(CYDriver::XMLContentCondition); } + ; + +LexSetXMLTag + : { driver.SetCondition(CYDriver::XMLTagCondition); } + ; +/* }}} */ + +XMLWhitespaceOpt + : XMLWhitespace + | + ; + +/* 8.1 Context Keywords {{{ */ +Identifier + : "namespace" { $$ = $1; } + | "xml" { $$ = $1; } + ; +/* }}} */ +/* 8.3 XML Initialiser Input Elements {{{ */ +XMLMarkup + : XMLComment { $$ = $1; } + | XMLCDATA { $$ = $1; } + | XMLPI { $$ = $1; } + ; +/* }}} */ +/* 11.1 Primary Expressions {{{ */ +PrimaryExpressionNoBF_ + : PropertyIdentifier { $$ = $1; } + | XMLInitialiser { $$ = $1; } + | XMLListInitialiser { $$ = $1; } + ; + +PropertyIdentifier + : AttributeIdentifier { $$ = $1; } + | QualifiedIdentifier { $$ = $1; } + | WildcardIdentifier { $$ = $1; } + ; +/* }}} */ +/* 11.1.1 Attribute Identifiers {{{ */ +AttributeIdentifier + : "@" QualifiedIdentifier_ { $$ = new(driver.pool_) CYAttribute($2); } + ; + +PropertySelector_ + : PropertySelector + | "[" Expression "]" + ; + +PropertySelector + : Word { $$ = $1; } + | WildcardIdentifier { $$ = $1; } + ; +/* }}} */ +/* 11.1.2 Qualified Identifiers {{{ */ +QualifiedIdentifier_ + : PropertySelector_ { $$ = $1; } + | QualifiedIdentifier { $$ = $1; } + ; + +QualifiedIdentifier + : PropertySelector "::" PropertySelector_ { $$ = new(driver.pool_) CYQName($1, $3); } + ; +/* }}} */ +/* 11.1.3 Wildcard Identifiers {{{ */ +WildcardIdentifier + : "*" { $$ = new(driver.pool_) CYWildcard(); } + ; +/* }}} */ +/* 11.1.4 XML Initialiser {{{ */ +XMLInitialiser + : XMLMarkup { $$ = $1; } + | XMLElement { $$ = $1; } + ; + +XMLElement + : "<" XMLTagContent_ "/>" LexPop + | "<" XMLTagContent_ ">" LexSetXMLContent XMLElementContentOpt "" LexPop + ; + +XMLTagContent_ + : LexPushXMLTag XMLTagContent XMLWhitespaceOpt + ; + +XMLTagContent + : XMLTagName XMLAttributesOpt + ; + +XMLExpression + : "{" LexPushRegExp Expression "}" LexPop + ; + +XMLTagName + : XMLExpression + | XMLName + ; + +XMLAttributes + : XMLWhitespace XMLExpression + | XMLAttributeOpt XMLAttributes + ; + +XMLAttributesOpt + : XMLAttributes + | + ; + +XMLAttributeValue_ + : XMLExpression + | XMLAttributeValue + ; + +XMLAttribute + : XMLWhitespace XMLName XMLWhitespaceOpt "=" XMLWhitespaceOpt XMLAttributeValue_ + ; + +XMLAttributeOpt + : XMLAttribute + | + ; + +XMLElementContent + : XMLExpression XMLElementContentOpt + | XMLMarkup XMLElementContentOpt + | XMLText XMLElementContentOpt + | XMLElement XMLElementContentOpt + ; + +XMLElementContentOpt + : XMLElementContent + | + ; +/* }}} */ +/* 11.1.5 XMLList Initialiser {{{ */ +XMLListInitialiser + : "<>" LexPushXMLContent XMLElementContent "" LexPop { $$ = new(driver.pool_) CYXMLList($3); } + ; +/* }}} */ +/* 11.2 Left-Hand-Side Expressions {{{ */ +PropertyIdentifier_ + : Word { $$ = $1; } + | PropertyIdentifier { $$ = $1; } + ; + +MemberAccess + : "." PropertyIdentifier { $$ = new(driver.pool_) CYPropertyMember(NULL, $2); } + | ".." PropertyIdentifier_ { $$ = new(driver.pool_) CYDescendantMember(NULL, $2); } + | "." "(" Expression ")" { $$ = new(driver.pool_) CYFilteringPredicate(NULL, $3); } + ; +/* }}} */ +/* 12.1 The default xml namespace Statement {{{ */ +DefaultXMLNamespaceStatement + : "default" "xml" "namespace" "=" Expression Terminator { $$ = new(driver.pool_) CYDefaultXMLNamespace($5); } + ; + +Statement_ + : DefaultXMLNamespaceStatement { $$ = $1; } + ; +/* }}} */ +@end + /* ECMAScript5: Object Literal Trailing Comma {{{ */ PropertyNameAndValueList_ : "," { $$ = NULL; } @@ -1486,7 +1731,7 @@ ComprehensionList : ForComprehension ComprehensionListOpt { $1->SetNext($2); $$ = $1; } ; -PrimaryExpression_ +PrimaryExpressionNoBF_ : "[" AssignmentExpression ComprehensionList "]" { $$ = new(driver.pool_) CYArrayComprehension($2, $3); } ; /* }}} */ @@ -1500,13 +1745,13 @@ LetStatement : "let" "(" VariableDeclarationList ")" Block_ { $$ = new(driver.pool_) CYLet($3, $5); } ; -Statement +Statement_ : LetStatement ; *//* }}} */ /* JavaScript FTW: Function Statements {{{ */ Statement - : FunctionDeclaration { driver.Warning(yylloc, "warning, FunctionDeclaration is a SourceElement, not a Statement"); } { $$ = $1; } + : LexSetRegExp FunctionDeclaration { driver.Warning(yylloc, "warning, FunctionDeclaration is a SourceElement, not a Statement"); } { $$ = $2; } ; /* }}} */