X-Git-Url: https://git.saurik.com/cycript.git/blobdiff_plain/693d501bb3c190a047d02b897f84917ccb623db6..4cf49641600bf5204fce42b168b489ea19fc865a:/Cycript.y?ds=sidebyside diff --git a/Cycript.y b/Cycript.y index 9d663b2..e51a0ec 100644 --- a/Cycript.y +++ b/Cycript.y @@ -11,24 +11,31 @@ typedef struct { bool newline_; union { + bool bool_; + CYArgument *argument_; CYBoolean *boolean_; CYClause *clause_; CYCatch *catch_; + CYCompound *compound_; CYDeclaration *declaration_; CYDeclarations *declarations_; CYElement *element_; CYExpression *expression_; CYFalse *false_; + CYField *field_; CYForInitialiser *for_; CYForInInitialiser *forin_; + CYFunctionParameter *functionParameter_; CYIdentifier *identifier_; CYLiteral *literal_; + CYMessage *message_; + CYMessageParameter *messageParameter_; CYName *name_; CYNull *null_; CYNumber *number_; - CYParameter *parameter_; CYProperty *property_; + CYSelectorPart *selector_; CYSource *source_; CYStatement *statement_; CYString *string_; @@ -118,6 +125,10 @@ int cylex(YYSTYPE *lvalp, cy::location *llocp, void *scanner); %token OpenBracket "[" %token CloseBracket "]" +%token AtClass "@class" +%token AtSelector "@selector" +%token AtEnd "@end" + %token Break "break" %token Case "case" %token Catch "catch" @@ -147,6 +158,38 @@ int cylex(YYSTYPE *lvalp, cy::location *llocp, void *scanner); %token While "while" %token With "with" +%token Abstract "abstract" +%token Boolean "boolean" +%token Byte "byte" +%token Char "char" +%token Class "class" +%token Const "const" +%token Debugger "debugger" +%token Double "double" +%token Enum "enum" +%token Export "export" +%token Extends "extends" +%token Final "final" +%token Float "float" +%token Goto "goto" +%token Implements "implements" +%token Import "import" +%token Int "int" +%token Interface "interface" +%token Long "long" +%token Native "native" +%token Package "package" +%token Private "private" +%token Protected "protected" +%token Public "public" +%token Short "short" +%token Static "static" +%token Super "super" +%token Synchronized "synchronized" +%token Throws "throws" +%token Transient "transient" +%token Volatile "volatile" + %token Identifier %token NumericLiteral %token StringLiteral @@ -179,6 +222,11 @@ int cylex(YYSTYPE *lvalp, cy::location *llocp, void *scanner); %type CaseClause %type CaseClausesOpt %type CatchOpt +%type ClassDeclaration +%type ClassMessageDeclaration +%type ClassMessageDeclarationListOpt +%type ClassSuperOpt +%type ClassFieldList %type ConditionalExpression %type ConditionalExpressionNoBF %type ConditionalExpressionNoIn @@ -186,8 +234,9 @@ int cylex(YYSTYPE *lvalp, cy::location *llocp, void *scanner); %type DefaultClause %type DoWhileStatement %type Element +%type ElementOpt %type ElementList -%type ElementList_ +%type ElementListOpt %type ElseStatementOpt %type EmptyStatement %type EqualityExpression @@ -195,10 +244,10 @@ int cylex(YYSTYPE *lvalp, cy::location *llocp, void *scanner); %type EqualityExpressionNoIn %type Expression %type ExpressionOpt -%type Expression_ +%type Expression_ %type ExpressionNoBF %type ExpressionNoIn -%type ExpressionNoIn_ +%type ExpressionNoIn_ %type ExpressionNoInOpt %type ExpressionStatement %type FinallyOpt @@ -206,8 +255,8 @@ int cylex(YYSTYPE *lvalp, cy::location *llocp, void *scanner); %type ForStatementInitialiser %type ForInStatement %type ForInStatementInitialiser -%type FormalParameterList -%type FormalParameterList_ +%type FormalParameterList +%type FormalParameterList_ %type FunctionBody %type FunctionDeclaration %type FunctionExpression @@ -232,6 +281,11 @@ int cylex(YYSTYPE *lvalp, cy::location *llocp, void *scanner); %type MemberExpression %type MemberExpression_ %type MemberExpressionNoBF +%type MessageParameter +%type MessageParameters +%type MessageParameterList +%type MessageParameterListOpt +%type MessageScope %type MultiplicativeExpression %type MultiplicativeExpressionNoBF %type NewExpression @@ -253,6 +307,9 @@ int cylex(YYSTYPE *lvalp, cy::location *llocp, void *scanner); %type RelationalExpressionNoBF %type RelationalExpressionNoIn %type ReturnStatement +%type SelectorExpression +%type SelectorExpression_ +%type SelectorExpressionOpt %type ShiftExpression %type ShiftExpressionNoBF %type SourceElement @@ -263,6 +320,7 @@ int cylex(YYSTYPE *lvalp, cy::location *llocp, void *scanner); %type SwitchStatement %type ThrowStatement %type TryStatement +%type TypeOpt %type UnaryExpression %type UnaryExpression_ %type UnaryExpressionNoBF @@ -284,6 +342,19 @@ int cylex(YYSTYPE *lvalp, cy::location *llocp, void *scanner); %type SelectorList %type VariadicCall +%left "*" "/" "%" +%left "+" "-" +%left "<<" ">>" ">>>" +%left "<" ">" "<=" ">=" "instanceof" "in" +%left "==" "!=" "===" "!==" +%left "&" +%left "^" +%left "|" +%left "&&" +%left "||" + +%right "=" "*=" "/=" "%=" "+=" "-=" "<<=" ">>=" ">>>=" "&=" "^=" "|=" + %nonassoc "if" %nonassoc "else" @@ -294,13 +365,13 @@ int cylex(YYSTYPE *lvalp, cy::location *llocp, void *scanner); TerminatorOpt : ";" | "\n" - | error { yyerrok; } + | error { yyerrok; driver.errors_.pop_back(); } ; Terminator : ";" | "\n" - | error { if (yychar != 0 && yychar != cy::parser::token::CloseBrace && !yylval.newline_) YYABORT; else yyerrok; } + | error { if (yychar != 0 && yychar != cy::parser::token::CloseBrace && !yylval.newline_) YYABORT; else { yyerrok; driver.errors_.pop_back(); } } ; CommaOpt @@ -320,32 +391,63 @@ WordOpt Word : Identifier { $$ = $1; } + | "abstract" { $$ = $1; } + | "boolean" { $$ = $1; } | "break" NewLineOpt { $$ = $1; } + | "byte" { $$ = $1; } | "case" { $$ = $1; } | "catch" { $$ = $1; } + | "char" { $$ = $1; } + | "class" { $$ = $1; } + | "const" { $$ = $1; } | "continue" NewLineOpt { $$ = $1; } + | "debugger" { $$ = $1; } | "default" { $$ = $1; } | "delete" { $$ = $1; } | "do" { $$ = $1; } + | "double" { $$ = $1; } | "else" { $$ = $1; } + | "enum" { $$ = $1; } + | "export" { $$ = $1; } + | "extends" { $$ = $1; } | "false" { $$ = $1; } + | "final" { $$ = $1; } | "finally" { $$ = $1; } + | "float" { $$ = $1; } | "for" { $$ = $1; } | "function" { $$ = $1; } + | "goto" { $$ = $1; } | "if" { $$ = $1; } + | "implements" { $$ = $1; } + | "import" { $$ = $1; } /* XXX: | "in" { $$ = $1; } */ /* XXX: | "instanceof" { $$ = $1; } */ + | "int" { $$ = $1; } + | "interface" { $$ = $1; } + | "long" { $$ = $1; } + | "native" { $$ = $1; } | "new" { $$ = $1; } | "null" { $$ = $1; } + | "package" { $$ = $1; } + | "private" { $$ = $1; } + | "protected" { $$ = $1; } + | "public" { $$ = $1; } | "return" NewLineOpt { $$ = $1; } + | "short" { $$ = $1; } + | "static" { $$ = $1; } + | "super" { $$ = $1; } | "switch" { $$ = $1; } + | "synchronized" { $$ = $1; } | "this" { $$ = $1; } | "throw" NewLineOpt { $$ = $1; } + | "throws" { $$ = $1; } + | "transient" { $$ = $1; } | "true" { $$ = $1; } | "try" { $$ = $1; } | "typeof" { $$ = $1; } | "var" { $$ = $1; } | "void" { $$ = $1; } + | "volatile" { $$ = $1; } | "while" { $$ = $1; } | "with" { $$ = $1; } ; @@ -391,26 +493,31 @@ PrimaryExpressionNoBF /* }}} */ /* 11.1.4 Array Initialiser {{{ */ ArrayLiteral - : "[" ElementList "]" { $$ = $2; } + : "[" ElementListOpt "]" { $$ = new(driver.pool_) CYArray($2); } ; Element : AssignmentExpression { $$ = $1; } + ; + +ElementOpt + : Element { $$ = $1; } | { $$ = NULL; } ; -ElementList_ - : "," ElementList { $$ = $2; } +ElementListOpt + : ElementList { $$ = $1; } | { $$ = NULL; } ; ElementList - : Element ElementList_ { $$ = new(driver.pool_) CYElement($1, $2); } + : ElementOpt "," ElementListOpt { $$ = new(driver.pool_) CYElement($1, $3); } + | Element { $$ = new(driver.pool_) CYElement($1, NULL); } ; /* }}} */ /* 11.1.5 Object Initialiser {{{ */ ObjectLiteral - : "{" PropertyNameAndValueListOpt "}" { $$ = new CYObject($2); } + : "{" PropertyNameAndValueListOpt "}" { $$ = new(driver.pool_) CYObject($2); } ; PropertyNameAndValueList_ @@ -779,12 +886,12 @@ AssignmentExpressionNoBF ; Expression_ - : "," Expression { $$ = $2; } + : "," Expression { $$ = new(driver.pool_) CYCompound($2); } | { $$ = NULL; } ; ExpressionNoIn_ - : "," ExpressionNoIn { $$ = $2; } + : "," ExpressionNoIn { $$ = new(driver.pool_) CYCompound($2); } | { $$ = NULL; } ; @@ -799,15 +906,15 @@ ExpressionNoInOpt ; Expression - : AssignmentExpression Expression_ { if ($1) { $1->SetNext($2); $$ = $1; } else $$ = $2; } + : AssignmentExpression Expression_ { if ($2) { $2->AddPrev($1); $$ = $2; } else $$ = $1; } ; ExpressionNoIn - : AssignmentExpressionNoIn ExpressionNoIn_ { if ($1) { $1->SetNext($2); $$ = $1; } else $$ = $2; } + : AssignmentExpressionNoIn ExpressionNoIn_ { if ($2) { $2->AddPrev($1); $$ = $2; } else $$ = $1; } ; ExpressionNoBF - : AssignmentExpressionNoBF Expression_ { if ($1) { $1->SetNext($2); $$ = $1; } else $$ = $2; } + : AssignmentExpressionNoBF Expression_ { if ($2) { $2->AddPrev($1); $$ = $2; } else $$ = $1; } ; Statement @@ -828,8 +935,7 @@ Statement ; Block - : "{" "}" { $$ = new(driver.pool_) CYEmpty(); } - | "{" StatementList "}" { $$ = $2; } + : "{" StatementListOpt "}" { $$ = $2 ?: new(driver.pool_) CYEmpty(); } ; StatementList @@ -1013,7 +1119,7 @@ FormalParameterList_ ; FormalParameterList - : Identifier FormalParameterList_ { $$ = new(driver.pool_) CYParameter($1, $2); } + : Identifier FormalParameterList_ { $$ = new(driver.pool_) CYFunctionParameter($1, $2); } | { $$ = NULL; } ; @@ -1022,7 +1128,7 @@ FunctionBody ; Program - : SourceElements { driver.source_.push_back($1); $$ = $1; } + : SourceElements { driver.source_ = $1; } ; SourceElements @@ -1030,16 +1136,66 @@ SourceElements | { $$ = NULL; } ; -/*Command - : SourceElement { driver.source_.push_back($2); if (driver.filename_.empty() && false) YYACCEPT; $2->Show(std::cout); } - ;*/ - SourceElement : Statement { $$ = $1; } | FunctionDeclaration { $$ = $1; } ; /* Objective-C Extensions {{{ */ +ClassSuperOpt + : ":" MemberExpressionNoBF { $$ = $2; } + | { $$ = NULL; } + ; + +ClassFieldList + : "{" "}" { $$ = NULL; } + ; + +MessageScope + : "+" { $$ = false; } + | "-" { $$ = true; } + ; + +TypeOpt + : "(" Expression ")" { $$ = $2; } + | { $$ = NULL; } + ; + +MessageParameter + : Word ":" TypeOpt Identifier { $$ = new CYMessageParameter($1, $3, $4); } + ; + +MessageParameterListOpt + : MessageParameterList { $$ = $1; } + | { $$ = NULL; } + ; + +MessageParameterList + : MessageParameter MessageParameterListOpt { $1->SetNext($2); $$ = $1; } + ; + +MessageParameters + : MessageParameterList { $$ = $1; } + | Word { $$ = new CYMessageParameter($1, NULL, NULL); } + ; + +ClassMessageDeclaration + : MessageScope TypeOpt MessageParameters "{" FunctionBody "}" { $$ = new CYMessage($1, $2, $3, $5); } + ; + +ClassMessageDeclarationListOpt + : ClassMessageDeclarationListOpt ClassMessageDeclaration { if ($1) { $1->SetNext($2); $$ = $1; } else $$ = $2; } + | { $$ = NULL; } + ; + +ClassDeclaration + : "@class" Identifier ClassSuperOpt ClassFieldList ClassMessageDeclarationListOpt "@end" { $$ = new CYClass($2, $3, $4, $5); } + ; + +SourceElement + : ClassDeclaration { $$ = $1; } + ; + VariadicCall : "," AssignmentExpression VariadicCall { $$ = new(driver.pool_) CYArgument(NULL, $2, $3); } | { $$ = NULL; } @@ -1060,11 +1216,26 @@ SelectorList ; MessageExpression - : "[" AssignmentExpression SelectorList "]" { $$ = new(driver.pool_) CYMessage($2, $3); } + : "[" AssignmentExpression SelectorList "]" { $$ = new(driver.pool_) CYSend($2, $3); } ; -PrimaryExpression +SelectorExpressionOpt + : SelectorExpression_ { $$ = $1; } + | { $$ = NULL; } + ; + +SelectorExpression_ + : WordOpt ":" SelectorExpressionOpt { $$ = new(driver.pool_) CYSelectorPart($1, true, $3); } + ; + +SelectorExpression + : SelectorExpression_ { $$ = $1; } + | Word { $$ = new(driver.pool_) CYSelectorPart($1, false, NULL); } + ; + +PrimaryExpression_ : MessageExpression { $$ = $1; } + | "@selector" "(" SelectorExpression ")" { $$ = new CYSelector($3); } ; /* }}} */