X-Git-Url: https://git.saurik.com/cycript.git/blobdiff_plain/6009702382b8710a9abf6fd62c1f30e991d6986e..79492f212da2fcaa4ce44d00fc08530e1630d05b:/Cycript.yy.in diff --git a/Cycript.yy.in b/Cycript.yy.in index 8adbbf5..b2f5629 100644 --- a/Cycript.yy.in +++ b/Cycript.yy.in @@ -1,21 +1,21 @@ /* Cycript - Optimizing JavaScript Compiler/Runtime - * Copyright (C) 2009-2013 Jay Freeman (saurik) + * Copyright (C) 2009-2014 Jay Freeman (saurik) */ -/* GNU General Public License, Version 3 {{{ */ +/* GNU Affero General Public License, Version 3 {{{ */ /* - * Cycript is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published - * by the Free Software Foundation, either version 3 of the License, - * or (at your option) any later version. - * - * Cycript is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Cycript. If not, see . + * GNU Affero General Public License for more details. + + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . **/ /* }}} */ @@ -72,6 +72,7 @@ typedef struct { CYInfix *infix_; CYLiteral *literal_; CYMember *member_; + CYModule *module_; CYNull *null_; CYNumber *number_; CYProgram *program_; @@ -84,6 +85,13 @@ typedef struct { CYTrue *true_; CYWord *word_; +@begin C + CYTypeModifier *modifier_; + CYTypeSpecifier *specifier_; + CYTypedIdentifier *typedIdentifier_; + CYTypedParameter *typedParameter_; +@end + @begin ObjectiveC CYClassName *className_; CYField *field_; @@ -91,9 +99,6 @@ typedef struct { CYMessageParameter *messageParameter_; CYProtocol *protocol_; CYSelectorPart *selector_; - CYTypeModifier *type_; - CYTypedIdentifier *typedIdentifier_; - CYTypedParameter *typedParameter_; @end @begin E4X @@ -105,12 +110,17 @@ typedef struct { } YYSTYPE; #define YYSTYPE YYSTYPE -@if Bison24 } @if Bison24 %code provides { int cylex(YYSTYPE *, cy::location *, void *); @if Bison24 } +@if Bison23 %} +@if Bison23 %union { +@if Bison23 } +@if Bison23 %{ + +@if Bison24 } @if Bison23 %} %name-prefix="cy" @@ -159,6 +169,7 @@ int cylex(YYSTYPE *, cy::location *, void *); @begin E4X ObjectiveC %token At "@" +%token Pound "#" @end %token Ampersand "&" @@ -232,15 +243,23 @@ int cylex(YYSTYPE *, cy::location *, void *); @begin C %token Typedef "typedef" +%token Unsigned "unsigned" +%token Signed "signed" +%token Extern "extern" +@end + +@begin C +%token AtEncode "@encode" @end @begin ObjectiveC %token AtImplementation "@implementation" %token AtImplementation_ ";@implementation" %token AtImport "@import" -%token AtEncode "@encode" %token AtEnd "@end" %token AtSelector "@selector" +%token Yes "YES" +%token No "NO" @end %token False "false" @@ -344,6 +363,8 @@ int cylex(YYSTYPE *, cy::location *, void *); %type ArgumentList %type ArgumentListOpt %type Arguments +%type ArrayComprehension +%type ArrayInitialiser %type ArrayLiteral %type ArrowFunction %type ArrowParameters @@ -365,10 +386,10 @@ int cylex(YYSTYPE *, cy::location *, void *); %type CaseClause %type CaseClausesOpt %type CatchOpt -%type ComprehensionForList -%type ComprehensionForListOpt -%type ComprehensionList -%type ComprehensionListOpt +%type Comprehension +%type ComprehensionFor +%type ComprehensionIf +%type ComprehensionTail %type ConditionalExpression %type ContinueStatement %type ConciseBody @@ -389,7 +410,6 @@ int cylex(YYSTYPE *, cy::location *, void *); %type ExpressionOpt %type ExpressionStatement %type FinallyOpt -%type ForComprehension %type ForStatementInitialiser %type ForInStatementInitialiser %type FormalParameter @@ -401,8 +421,8 @@ int cylex(YYSTYPE *, cy::location *, void *); %type FunctionExpression %type Identifier %type IdentifierOpt +%type IdentifierType %type IdentifierName -%type IfComprehension %type IfStatement %type Initialiser %type InitialiserOpt @@ -418,6 +438,7 @@ int cylex(YYSTYPE *, cy::location *, void *); %type MemberAccess %type MemberExpression_ %type MemberExpression +%type Module %type MultiplicativeExpression %type NewExpression %type NullLiteral @@ -464,6 +485,24 @@ int cylex(YYSTYPE *, cy::location *, void *); %type WordOpt %type Variable +@begin C +%type ArrayedType +%type FunctionedType +%type IntegerType +%type IntegerTypeOpt +%type PrefixedType +%type PrimitiveType +%type TypeParenthetical +%type TypeSignifier +%type SuffixedType +%type TypeQualifierLeft +%type TypeQualifierRight +%type TypedIdentifier +%type TypedParameterList_ +%type TypedParameterList +%type TypedParameterListOpt +@end + @begin ObjectiveC %type BoxableExpression %type CategoryStatement @@ -479,17 +518,13 @@ int cylex(YYSTYPE *, cy::location *, void *); %type ClassProtocolListOpt %type ClassProtocols %type ClassProtocolsOpt -%type EncodedType %type MessageExpression %type MessageParameter %type MessageParameters %type MessageParameterList %type MessageParameterListOpt %type MessageScope -%type ModifiedType -%type PrefixedType -%type PrimitiveType -%type QualifiedType +%type ModifiedType %type SelectorCall_ %type SelectorCall %type SelectorExpression_ @@ -497,12 +532,7 @@ int cylex(YYSTYPE *, cy::location *, void *); %type SelectorExpressionOpt %type SelectorList %type SelectorWordOpt -%type SuffixedType -%type TypeOpt -%type TypedIdentifier -%type TypedParameterList_ -%type TypedParameterList -%type TypedParameterListOpt +%type TypeOpt %type VariadicCall @end @@ -646,13 +676,9 @@ WordOpt | { $$ = NULL; } ; -Identifier +IdentifierType : Identifier_ { $$ = $1; } -@begin C - | "typedef" { $$ = $1; } -@end - | "implements" { $$ = $1; } | "interface" { $$ = $1; } | "package" { $$ = $1; } @@ -664,19 +690,14 @@ Identifier | "abstract" { $$ = $1; } | "boolean" { $$ = $1; } | "byte" { $$ = $1; } - | "char" { $$ = $1; } | "double" { $$ = $1; } | "final" { $$ = $1; } | "float" { $$ = $1; } | "goto" { $$ = $1; } - | "int" { $$ = $1; } - | "long" { $$ = $1; } | "native" { $$ = $1; } - | "short" { $$ = $1; } | "synchronized" { $$ = $1; } | "throws" { $$ = $1; } | "transient" { $$ = $1; } - | "volatile" { $$ = $1; } // XXX: currently I only have this as Word // | "let" { $$ = $1; } @@ -687,6 +708,25 @@ Identifier | "of" { $$ = $1; } ; +Identifier + : IdentifierType + | "char" { $$ = $1; } + | "int" { $$ = $1; } + | "long" { $$ = $1; } + | "short" { $$ = $1; } + | "volatile" { $$ = $1; } +@begin C + | "typedef" { $$ = $1; } + | "unsigned" { $$ = $1; } + | "signed" { $$ = $1; } + | "extern" { $$ = $1; } +@end +@begin ObjectiveC + | "YES" { $$ = $1; } + | "NO" { $$ = $1; } +@end + ; + IdentifierOpt : Identifier { $$ = $1; } | { $$ = NULL; } @@ -752,14 +792,19 @@ PrimaryExpression : "this" { $$ = $1; } | Variable { $$ = $1; } | Literal { $$ = $1; } - | ArrayLiteral { $$ = $1; } + | ArrayInitialiser { $$ = $1; } | ObjectLiteral { $$ = $1; } - | FunctionExpression { $$ = $1; } | Parenthetical { $$ = $1; } | AutoComplete { driver.mode_ = CYDriver::AutoPrimary; YYACCEPT; } ; /* }}} */ -/* 11.1.4.1 Array Initialiser {{{ */ +/* 11.1.4 Array Initializer {{{ */ +ArrayInitialiser + : ArrayLiteral { $$ = $1; } + | ArrayComprehension { $$ = $1; } + ; +/* }}} */ +/* 11.1.4.1 Array Literal {{{ */ ArrayLiteral : "[" LexPushInOff ElementListOpt "]" LexPopIn { $$ = CYNew CYArray($3); } ; @@ -784,19 +829,26 @@ ElementListOpt ; /* }}} */ /* 11.1.4.2 Array Comprehension {{{ */ -PrimaryExpression - : "[" LexPushInOff AssignmentExpression ComprehensionForList "]" LexPopIn { $$ = CYNew CYArrayComprehension($3, $4); } +ArrayComprehension + : "[" LexPushInOff Comprehension "]" LexPopIn { $$ = $3; } ; -ComprehensionForList - : "for" Binding "in" Expression ComprehensionForListOpt { $$ = CYNew CYForInComprehension($2, $4, $5); } - | "for" Binding "of" Expression ComprehensionForListOpt { $$ = CYNew CYForOfComprehension($2, $4, $5); } +Comprehension + : LexSetRegExp ComprehensionFor ComprehensionTail AssignmentExpression { $$ = CYNew CYArrayComprehension($4, $2->Modify($3)); } ; -ComprehensionForListOpt - : ComprehensionForList { $$ = $1; } - | "if" Expression { $$ = CYNew CYIfComprehension($2); } - | { $$ = NULL; } +ComprehensionTail + : { $$ = NULL; } + | ComprehensionFor ComprehensionTail { $$ = $1->Modify($2); } + | ComprehensionIf ComprehensionTail { $$ = $1->Modify($2); } + ; + +ComprehensionFor + : "for" "(" Binding "of" Expression ")" { $$ = CYNew CYForOfComprehension($3, $5); } + ; + +ComprehensionIf + : "if" "(" AssignmentExpression ")" { $$ = CYNew CYIfComprehension($3); } ; /* }}} */ /* 11.1.5 Object Initialiser {{{ */ @@ -851,6 +903,7 @@ MemberExpression_ MemberExpression : LexSetRegExp PrimaryExpression { $$ = $2; } + | LexSetRegExp FunctionExpression { $$ = $2; } | MemberExpression_ { driver.context_ = $1; } MemberAccess { $3->SetLeft($1); $$ = $3; } | LexSetRegExp "new" MemberExpression Arguments { $$ = CYNew cy::Syntax::New($3, $4); } ; @@ -994,6 +1047,7 @@ LogicalORExpression ConditionalExpression : LogicalORExpression { $$ = $1; } | LogicalORExpression "?" LexPushInOff AssignmentExpression ":" LexPopIn AssignmentExpression { $$ = CYNew CYCondition($1, $4, $7); } + | LogicalORExpression "?" LexPushInOff LexSetRegExp ":" LexPopIn AssignmentExpression { $$ = CYNew CYCondition($1, $1, $7); } ; /* }}} */ /* 11.13 Assignment Operators {{{ */ @@ -1017,11 +1071,11 @@ AssignmentExpression /* 11.14 Comma Operator {{{ */ Expression_ : "," Expression { $$ = $2; } - | { $$ = CYNew CYCompound(); } + | { $$ = NULL; } ; Expression - : AssignmentExpression Expression_ { $2->AddPrev($1); $$ = $2; } + : AssignmentExpression Expression_ { $$ = CYNew CYCompound($1, $2); } ; ExpressionOpt @@ -1357,48 +1411,85 @@ ProgramBodyOpt ; /* }}} */ -@begin ObjectiveC -/* Cycript (Objective-C): Type Encoding {{{ */ +@begin C +/* Cycript (C): Type Encoding {{{ */ +TypeParenthetical + : "(" LexPushInOff PrefixedType ")" LexPopIn { $$ = $3; } + ; + +TypeSignifier + : IdentifierType { $$ = CYNew CYTypedIdentifier($1); } + | TypeParenthetical { $$ = $1; } + ; + +ArrayedType + : ArrayedType "[" NumericLiteral "]" { $$ = $1; $$->modifier_ = CYNew CYTypeArrayOf($3, $$->modifier_); } + | TypeSignifier { $$ = $1; } + | { $$ = CYNew CYTypedIdentifier(); } + ; + +FunctionedType + : "(" LexPushInOff TypedParameterListOpt ")" LexPopIn { $$ = CYNew CYTypeFunctionWith($3); } + ; + SuffixedType - : IdentifierOpt { $$ = CYNew CYTypedIdentifier($1); } - | "(" LexPushInOff PrefixedType ")" LexPopIn { $$ = $3; } - | SuffixedType "[" NumericLiteral "]" { CYSetLast($1->type_) = CYNew CYTypeArrayOf($3); $$ = $1; } + : ArrayedType { $$ = $1; } + | "(" LexPushInOff "^" TypeQualifierRight ")" LexPopIn "(" LexPushInOff TypedParameterListOpt ")" LexPopIn { $$ = $4; $$->modifier_ = CYNew CYTypeBlockWith($9, $$->modifier_); } + | TypeParenthetical FunctionedType { $$ = $1; CYSetLast($2) = $$->modifier_; $$->modifier_ = $2; } + | IdentifierType FunctionedType { $$ = CYNew CYTypedIdentifier($1); CYSetLast($2) = $$->modifier_; $$->modifier_ = $2; } + | FunctionedType { $$ = CYNew CYTypedIdentifier(); CYSetLast($1) = $$->modifier_; $$->modifier_ = $1; } ; PrefixedType - : SuffixedType { $$ = $1; } - | "const" PrefixedType { CYSetLast($2->type_) = CYNew CYTypeConstant(); $$ = $2; } - | "*" PrefixedType { CYSetLast($2->type_) = CYNew CYTypePointerTo(); $$ = $2; } + : "*" TypeQualifierRight { $$ = $2; $$->modifier_ = CYNew CYTypePointerTo($$->modifier_); } ; -PrimitiveType - : Variable { $$ = $1; } - | "void" { $$ = CYNew cy::Syntax::New(CYNew CYVariable(CYNew CYIdentifier("Type")), CYNew CYArgument(CYNew CYString("v"))); } +TypeQualifierLeft + : { $$ = NULL; } + | "const" TypeQualifierLeft { $$ = $2; CYSetLast($$) = CYNew CYTypeConstant(); } + | "volatile" TypeQualifierLeft { $$ = $2; CYSetLast($$) = CYNew CYTypeVolatile(); } ; -QualifiedType - : PrimitiveType { $$ = CYNew CYTypeVariable($1); } - | "const" QualifiedType { $$ = CYNew CYTypeConstant($2); } +TypeQualifierRight + : PrefixedType { $$ = $1; } + | SuffixedType { $$ = $1; } + | "const" TypeQualifierRight { $$ = $2; $$->modifier_ = CYNew CYTypeConstant($$->modifier_); } + | "volatile" TypeQualifierRight { $$ = $2; $$->modifier_ = CYNew CYTypeVolatile($$->modifier_); } ; -ModifiedType - : QualifiedType { $$ = $1; } - | QualifiedType "*" { $$ = CYNew CYTypePointerTo($1); } - | QualifiedType "const" { $$ = CYNew CYTypeConstant($1); } +IntegerType + : "int" { $$ = CYNew CYTypeVariable("int"); } + | "unsigned" IntegerTypeOpt { $$ = CYNew CYTypeUnsigned($2); } + | "signed" IntegerTypeOpt { $$ = CYNew CYTypeSigned($2); } + | "long" IntegerTypeOpt { $$ = CYNew CYTypeLong($2); } + | "short" IntegerTypeOpt { $$ = CYNew CYTypeShort($2); } ; -TypedIdentifier - : QualifiedType PrefixedType { CYSetLast($2->type_) = $1; $$ = $2;} +IntegerTypeOpt + : IntegerType { $$ = $1; } + | { $$ = CYNew CYTypeVariable("int"); } ; -EncodedType - : TypedIdentifier { $$ = CYNew CYEncodedType($1->type_); } +PrimitiveType + : IdentifierType { $$ = CYNew CYTypeVariable($1); } + | IntegerType { $$ = $1; } + | "void" { $$ = CYNew CYTypeVoid(); } + | "char" { $$ = CYNew CYTypeVariable("char"); } + | "signed" "char" { $$ = CYNew CYTypeSigned(CYNew CYTypeVariable("char")); } + | "unsigned" "char" { $$ = CYNew CYTypeUnsigned(CYNew CYTypeVariable("char")); } + ; + +TypedIdentifier + : TypeQualifierLeft PrimitiveType TypeQualifierRight { $$ = $3; $$->specifier_ = $2; CYSetLast($1) = $$->modifier_; $$->modifier_ = $1; } ; PrimaryExpression - : AtEncode "(" EncodedType ")" { $$ = $3; } + : "@encode" "(" TypedIdentifier ")" { $$ = CYNew CYEncodedType($3); } ; /* }}} */ +@end + +@begin ObjectiveC /* Cycript (Objective-C): @class Declaration {{{ */ ClassSuperOpt /* XXX: why the hell did I choose MemberExpression? */ @@ -1407,7 +1498,7 @@ ClassSuperOpt ; ClassFieldListOpt - : Expression Identifier ";" ClassFieldListOpt { $$ = CYNew CYField($1, $2, $4); } + : TypedIdentifier ";" ClassFieldListOpt { $$ = CYNew CYField($1, $3); } | LexSetRegExp { $$ = NULL; } ; @@ -1421,7 +1512,7 @@ MessageScope ; TypeOpt - : "(" LexSetRegExp EncodedType ")" { $$ = $3; } + : "(" LexSetRegExp TypedIdentifier ")" { $$ = $3; } | { $$ = NULL; } ; @@ -1552,23 +1643,20 @@ PrimaryExpression | "@selector" "(" LexPushInOff SelectorExpression ")" LexPopIn { $$ = CYNew CYSelector($4); } ; /* }}} */ -/* Cycript (Objective-C): @import Directive {{{ */ -PathName - : "/" PathName - | "." PathName - | Word PathName - | - ; +@end -ImportPath - : "<" PathName ">" - | StringLiteral +/* Cycript: @import Directive {{{ */ +Module + : Module "." Word { $$ = CYNew CYModule($3, $1); } + | Word { $$ = CYNew CYModule($1); } ; -StatementListItem - : LexSetStatement LexSetRegExp "@import" ImportPath { $$ = CYNew CYImport(); } +Declaration__ + : "@import" Module { $$ = CYNew CYImport($2); } ; /* }}} */ + +@begin ObjectiveC /* Cycript (Objective-C): Boxed Expressions {{{ */ BoxableExpression : NullLiteral { $$ = $1; } @@ -1578,6 +1666,8 @@ BoxableExpression | ArrayLiteral { $$ = $1; } | ObjectLiteral { $$ = $1; } | Parenthetical { $$ = $1; } + | "YES" { $$ = CYNew CYTrue(); } + | "NO" { $$ = CYNew CYFalse(); } ; PrimaryExpression @@ -1585,10 +1675,20 @@ PrimaryExpression ; /* }}} */ /* Cycript (Objective-C): Block Expressions {{{ */ +ModifiedType + : TypeQualifierLeft PrimitiveType { $$ = CYNew CYTypedIdentifier(); $$->specifier_ = $2; $$->modifier_ = $1; } + | ModifiedType "*" { $$ = $1; $$->modifier_ = CYNew CYTypePointerTo($$->modifier_); } + ; + PrimaryExpression : "^" ModifiedType "(" LexPushInOff TypedParameterListOpt ")" LexPopIn BRACE LexPushInOff FunctionBody "}" LexPopIn { $$ = CYNew CYObjCBlock($2, $5, $10); } ; /* }}} */ +/* Cycript (Objective-C): Instance Literals {{{ */ +PrimaryExpression + : "#" NumericLiteral { $$ = CYNew CYInstanceLiteral($2); } + ; +/* }}} */ @end @begin C @@ -1628,14 +1728,20 @@ TypedParameterListOpt ; PrimaryExpression - : "[" LexPushInOff LexSetRegExp "=" "]" LexPopIn "(" LexPushInOff TypedParameterListOpt ")" LexPopIn "->" ModifiedType BRACE LexPushInOff FunctionBody "}" LexPopIn { $$ = CYNew CYLambda($13, $9, $16); } + : "[" LexPushInOff LexSetRegExp "&" LexSetRegExp "]" LexPopIn "(" LexPushInOff TypedParameterListOpt ")" LexPopIn "->" TypedIdentifier BRACE LexPushInOff FunctionBody "}" LexPopIn { $$ = CYNew CYLambda($14, $10, $17); } ; /* }}} */ /* Cycript (C): Type Definitions {{{ */ Statement__ - : "typedef" TypedIdentifier Terminator { $$ = CYNew CYTypeDefinition($2); } + : "typedef" TypedIdentifier { if ($2->identifier_ == NULL) YYABORT; } Terminator { $$ = CYNew CYTypeDefinition($2); } ; /* }}} */ +/* Cycript (C): extern "C" {{{ */ +Statement__ + : "extern" StringLiteral { if (strcmp($2->Value(), "C") != 0) YYABORT; } TypedIdentifier Terminator { $$ = CYNew CYExternal($2, $4); } + ; +/* }}} */ + @end /* YUI: Documentation Comments {{{ */ @@ -1819,27 +1925,13 @@ Statement__ @end /* JavaScript 1.7: Array Comprehensions {{{ */ -IfComprehension - : "if" "(" Expression ")" { $$ = CYNew CYIfComprehension($3); } - ; - -ForComprehension - : "for" "(" Identifier "in" Expression ")" { $$ = CYNew CYForInComprehension($3, $5); } - | "for" "each" "(" Identifier "in" Expression ")" { $$ = CYNew CYForOfComprehension($4, $6); } - ; - -ComprehensionList - : ForComprehension ComprehensionListOpt { $1->SetNext($2); $$ = $1; } +Comprehension + : AssignmentExpression ComprehensionFor ComprehensionTail { $$ = CYNew CYArrayComprehension($1, $2->Modify($3)); } ; -ComprehensionListOpt - : ComprehensionList { $$ = $1; } - | IfComprehension { $$ = $1; } - | { $$ = NULL; } - ; - -PrimaryExpression - : "[" LexPushInOff AssignmentExpression ComprehensionList "]" LexPopIn { $$ = CYNew CYArrayComprehension($3, $4); } +ComprehensionFor + : "for" "(" Binding "in" Expression ")" { $$ = CYNew CYForInComprehension($3, $5); } + | "for" "each" "(" Binding "in" Expression ")" { $$ = CYNew CYForOfComprehension($4, $6); } ; /* }}} */ /* JavaScript 1.7: for each {{{ */