X-Git-Url: https://git.saurik.com/cycript.git/blobdiff_plain/aea76473fa673adddb206e34d675e59c298e3581..43ef65b6e8d957c81dfee4598d748457cdd9ba84:/Cycript.yy.in diff --git a/Cycript.yy.in b/Cycript.yy.in index 9b9a05c..7399651 100644 --- a/Cycript.yy.in +++ b/Cycript.yy.in @@ -1,5 +1,5 @@ /* Cycript - Optimizing JavaScript Compiler/Runtime - * Copyright (C) 2009-2010 Jay Freeman (saurik) + * Copyright (C) 2009-2012 Jay Freeman (saurik) */ /* GNU Lesser General Public License, Version 3 {{{ */ @@ -39,8 +39,11 @@ #include "E4X/Syntax.hpp" @end +#include "Highlight.hpp" + typedef struct { bool newline_; + hi::Value highlight_; union { bool bool_; @@ -78,6 +81,9 @@ typedef struct { CYString *string_; CYThis *this_; CYTrue *true_; + CYTypeModifier *type_; + CYTypedIdentifier *typedIdentifier_; + CYTypedParameter *typedParameter_; CYWord *word_; @begin ObjectiveC @@ -126,6 +132,7 @@ int cylex(YYSTYPE *, cy::location *, void *); %parse-param { CYDriver &driver } %lex-param { void *cyscanner } +/* Token Declarations {{{ */ @begin E4X %token XMLCDATA %token XMLComment @@ -224,6 +231,7 @@ int cylex(YYSTYPE *, cy::location *, void *); %token AtImplementation "@implementation" %token AtImplementation_ ";@implementation" %token AtImport "@import" +%token AtEncode "@encode" %token AtEnd "@end" %token AtSelector "@selector" @end @@ -233,6 +241,7 @@ int cylex(YYSTYPE *, cy::location *, void *); %token True "true" // ES3/ES5/WIE/JSC Reserved +%token Auto "auto" %token Break "break" %token Case "case" %token Catch "catch" @@ -332,6 +341,7 @@ int cylex(YYSTYPE *, cy::location *, void *); %type ArrowFunction %type ArrowParameters %type AssignmentExpression +%type Binding %type BindingIdentifier %type BitwiseANDExpression %type Block_ @@ -348,6 +358,8 @@ int cylex(YYSTYPE *, cy::location *, void *); %type CaseClause %type CaseClausesOpt %type CatchOpt +%type ComprehensionForList +%type ComprehensionForListOpt %type ComprehensionList %type ComprehensionListOpt %type ConditionalExpression @@ -393,7 +405,7 @@ int cylex(YYSTYPE *, cy::location *, void *); %type LetStatement %type LexicalDeclaration %type Literal -%type LiteralNoRE +%type ValueLiteral %type LogicalANDExpression %type LogicalORExpression %type MemberAccess @@ -443,13 +455,16 @@ int cylex(YYSTYPE *, cy::location *, void *); %type WithStatement %type Word %type WordOpt +%type Variable @begin ObjectiveC +%type BoxableExpression %type CategoryStatement %type ClassExpression +%type ClassFieldListOpt +%type ClassFields %type ClassStatement %type ClassSuperOpt -%type ClassFieldList %type ClassMessageDeclaration %type ClassMessageDeclarationListOpt %type ClassName @@ -457,12 +472,17 @@ 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 SelectorCall_ %type SelectorCall %type SelectorExpression_ @@ -470,7 +490,12 @@ 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 VariadicCall @end @@ -494,12 +519,14 @@ int cylex(YYSTYPE *, cy::location *, void *); %type XMLListInitialiser %type XMLInitialiser @end - +/* }}} */ +/* Token Priorities {{{ */ %nonassoc "" %left "++" "--" %nonassoc "if" %nonassoc "else" +/* }}} */ %start Program @@ -523,53 +550,45 @@ LexSetRegExp ; LexNoBrace - : { if (yychar == yyempty_) driver.nobrace_ = true; else if (yychar == token::OpenBrace || yychar == token::OpenBrace_) yychar = token::OpenBrace__; } + : { if (yychar == yyempty_) driver.no_.OpenBrace = true; else if (yychar == token::OpenBrace || yychar == token::OpenBrace_) yychar = token::OpenBrace__; } ; LexNoFunction - : { if (yychar == token::Function) yychar = token::Function_; } + : { if (yychar == yyempty_) driver.no_.Function = true; else if (yychar == token::Function) yychar = token::Function_; } + ; + +LexNoAtImplementation + : { if (yychar == yyempty_) driver.no_.AtImplementation = true; else if (yychar == token::AtImplementation) yychar = token::AtImplementation_; } ; LexSetStatement - : LexNoBrace LexNoFunction + : LexNoBrace LexNoFunction LexNoAtImplementation ; /* }}} */ - +/* Virtual Tokens {{{ */ BRACE : "{" | "\n{" ; -StrictSemi - : { driver.Warning(yylloc, "warning, automatic semi-colon insertion required"); } - ; - -Terminator - : ";" - | error { if (yychar != yyeof_ && yychar != token::CloseBrace && !yylval.newline_) YYABORT; else { yyerrok; driver.errors_.pop_back(); } } StrictSemi +Var_ + : "var" ; +/* }}} */ -TerminatorOpt - : ";" - | error { yyerrok; driver.errors_.pop_back(); } StrictSemi +/* 7.6 Identifier Names and Identifiers {{{ */ +IdentifierName + : Word { $$ = $1; } ; -/*CommaOpt - : "," - | - ;*/ - NewLineOpt : "\n" | ; -IdentifierName - : Word { $$ = $1; } - ; - Word : Identifier { $$ = $1; } + | "auto" { $$ = $1; } | "break" NewLineOpt { $$ = $1; } | "case" { $$ = $1; } | "catch" { $$ = $1; } @@ -618,15 +637,6 @@ WordOpt | { $$ = NULL; } ; -@begin ObjectiveC -PrimaryExpression - : "@" LiteralNoRE { $$ = CYNew CYBox($2); } - | "@" ArrayLiteral { $$ = CYNew CYBox($2); } - | "@" ObjectLiteral { $$ = CYNew CYBox($2); } - | "@" Parenthetical { $$ = CYNew CYBox($2); } - ; -@end - Identifier : Identifier_ { $$ = $1; } @@ -668,27 +678,48 @@ IdentifierOpt : Identifier { $$ = $1; } | { $$ = NULL; } ; +/* }}} */ -LiteralNoRE +/* 7.8 Literals {{{ */ +Literal : NullLiteral { $$ = $1; } - | BooleanLiteral { $$ = $1; } - | NumericLiteral { $$ = $1; } - | StringLiteral { $$ = $1; } + | ValueLiteral { $$ = $1; } ; -Literal - : LiteralNoRE { $$ = $1; } +ValueLiteral + : BooleanLiteral { $$ = $1; } + | NumericLiteral { $$ = $1; } + | StringLiteral { $$ = $1; } | RegularExpressionLiteral { $$ = $1; } ; - +/* }}} */ +/* 7.8.1 Null Literals {{{ */ NullLiteral : "null" { $$ = $1; } ; - +/* }}} */ +/* 7.8.2 Boolean Literals {{{ */ BooleanLiteral : "true" { $$ = $1; } | "false" { $$ = $1; } ; +/* }}} */ + +/* 7.9 Automatic Semicolon Insertion {{{ */ +StrictSemi + : { driver.Warning(yylloc, "warning, automatic semi-colon insertion required"); } + ; + +Terminator + : ";" + | error { if (yychar != yyeof_ && yychar != token::CloseBrace && !yylval.newline_) YYABORT; else { yyerrok; driver.errors_.pop_back(); } } StrictSemi + ; + +TerminatorOpt + : ";" + | error { yyerrok; driver.errors_.pop_back(); } StrictSemi + ; +/* }}} */ /* 11.1 Primary Expressions {{{ */ Parenthetical @@ -700,9 +731,13 @@ ParentheticalOpt | { $$ = NULL; } ; +Variable + : Identifier { $$ = CYNew CYVariable($1); } + ; + PrimaryExpression : "this" { $$ = $1; } - | Identifier { $$ = CYNew CYVariable($1); } + | Variable { $$ = $1; } | Literal { $$ = $1; } | ArrayLiteral { $$ = $1; } | ObjectLiteral { $$ = $1; } @@ -735,6 +770,22 @@ ElementListOpt | LexSetRegExp { $$ = NULL; } ; /* }}} */ +/* 11.1.4.2 Array Comprehension {{{ */ +PrimaryExpression + : "[" LexPushInOff AssignmentExpression ComprehensionForList LexPopIn "]" { $$ = CYNew CYArrayComprehension($3, $4); } + ; + +ComprehensionForList + : "for" Binding "in" Expression ComprehensionForListOpt { $$ = CYNew CYForInComprehension($2, $4, $5); } + | "for" Binding "of" Expression ComprehensionForListOpt { $$ = CYNew CYForOfComprehension($2, $4, $5); } + ; + +ComprehensionForListOpt + : ComprehensionForList { $$ = $1; } + | "if" Expression { $$ = CYNew CYIfComprehension($2); } + | { $$ = NULL; } + ; +/* }}} */ /* 11.1.5 Object Initialiser {{{ */ ObjectLiteral : BRACE LexPushInOff PropertyDefinitionListOpt LexPopIn "}" { $$ = CYNew CYObject($3); } @@ -742,7 +793,7 @@ ObjectLiteral PropertyDefinitionList_ : "," PropertyDefinitionList { $$ = $2; } - | "," { $$ = NULL; } + | "," LexSetRegExp { $$ = NULL; } | { $$ = NULL; } ; @@ -752,12 +803,13 @@ PropertyDefinitionList PropertyDefinitionListOpt : PropertyDefinitionList { $$ = $1; } - | { $$ = NULL; } + | LexSetRegExp { $$ = NULL; } ; PropertyDefinition - //: IdentifierName { $$ = CYNew CYProperty($1, CYNew CYVariable($1)); } - : PropertyName ":" AssignmentExpression { $$ = CYNew CYProperty($1, $3); } + // XXX: this should be IdentifierName + : LexSetRegExp Identifier { $$ = CYNew CYProperty($2, CYNew CYVariable($2)); } + | PropertyName ":" AssignmentExpression { $$ = CYNew CYProperty($1, $3); } //| MethodDefinition ; @@ -1024,7 +1076,7 @@ StatementList StatementListOpt : StatementList { $$ = $1; } - | LexSetRegExp { $$ = NULL; } + | LexSetStatement LexSetRegExp { $$ = NULL; } ; StatementListItem @@ -1032,11 +1084,16 @@ StatementListItem | Declaration { $$ = $1; } ; /* }}} */ + /* 12.2 Declarations {{{ */ BindingIdentifier : Identifier { $$ = $1; } ; +Binding + : BindingIdentifier + ; + // XXX: BindingPattern /* }}} */ /* 12.2.1 Let and Const Declarations {{{ */ @@ -1051,7 +1108,7 @@ LetOrConst /* }}} */ /* 12.2.2 Variable Statement {{{ */ VariableStatement - : "var" VariableDeclarationList Terminator { $$ = CYNew CYVar($2); } + : Var_ VariableDeclarationList Terminator { $$ = CYNew CYVar($2); } ; VariableDeclarationList_ @@ -1088,6 +1145,7 @@ SingleNameBinding : BindingIdentifier InitialiserOpt { $$ = CYNew CYDeclaration($1, $2); } ; /* }}} */ + /* 12.3 Empty Statement {{{ */ EmptyStatement : ";" { $$ = CYNew CYEmpty(); } @@ -1126,7 +1184,7 @@ IterationStatement ForStatementInitialiser : ExpressionOpt { $$ = $1; } - | LexSetRegExp "var" VariableDeclarationList { $$ = CYNew CYForDeclarations($3); } + | LexSetRegExp Var_ VariableDeclarationList { $$ = CYNew CYForDeclarations($3); } ; /* }}} */ /* 12.6.4 The for-in and for-of Statements {{{ */ @@ -1137,7 +1195,7 @@ IterationStatement ForInStatementInitialiser : LeftHandSideExpression { $$ = $1; } - | LexSetRegExp "var" VariableDeclaration { $$ = $3; } + | LexSetRegExp Var_ VariableDeclaration { $$ = $3; } ; /* }}} */ @@ -1155,7 +1213,7 @@ BreakStatement /* }}} */ /* 12.9 The return Statement {{{ */ ReturnStatement - : "return" "\n" StrictSemi { $$ = CYNew CYReturn(NULL); } + : "return" LexSetRegExp "\n" StrictSemi { $$ = CYNew CYReturn(NULL); } | "return" ExpressionOpt Terminator { $$ = CYNew CYReturn($2); } ; /* }}} */ @@ -1195,7 +1253,7 @@ LabelledStatement /* }}} */ /* 12.13 The throw Statement {{{ */ ThrowStatement - : "throw" "\n" StrictSemi { YYABORT; } + : "throw" LexSetRegExp "\n" StrictSemi { YYABORT; } | "throw" Expression Terminator { $$ = CYNew cy::Syntax::Throw($2); } ; /* }}} */ @@ -1258,7 +1316,7 @@ FunctionBody /* }}} */ /* 13.2 Arrow Function Definitions {{{ */ ArrowFunction - : LexSetRegExp ArrowParameters "=>" LexNoBrace ConciseBody { $$ = CYNew CYFunctionExpression(NULL, $2, $5); } + : LexSetRegExp ArrowParameters "=>" LexNoBrace ConciseBody { $$ = CYNew CYFatArrow($2, $5); } ; ArrowParameters @@ -1268,7 +1326,7 @@ ArrowParameters ConciseBody : AssignmentExpression { $$ = CYNew CYReturn($1); } - | ";{" LexPushInOff FunctionBody LexPopIn "}" { $$ = $3; } + | LexSetRegExp ";{" LexPushInOff FunctionBody LexPopIn "}" { $$ = $4; } ; /* }}} */ /* 14 Program {{{ */ @@ -1282,11 +1340,52 @@ ProgramBody ProgramBodyOpt : ProgramBody { $$ = $1; } - | { $$ = NULL; } + | LexSetStatement LexSetRegExp { $$ = NULL; } ; /* }}} */ @begin ObjectiveC +/* Cycript (Objective-C): Type Encoding {{{ */ +SuffixedType + : IdentifierOpt { $$ = CYNew CYTypedIdentifier($1); } + | "(" LexPushInOff PrefixedType LexPopIn ")" { $$ = $3; } + | SuffixedType "[" NumericLiteral "]" { CYSetLast($1->type_) = CYNew CYTypeArrayOf($3->Value()); $$ = $1; } + ; + +PrefixedType + : SuffixedType { $$ = $1; } + | "const" PrefixedType { CYSetLast($2->type_) = CYNew CYTypeConstant(); $$ = $2; } + | "*" PrefixedType { CYSetLast($2->type_) = CYNew CYTypePointerTo(); $$ = $2; } + ; + +PrimitiveType + : Variable { $$ = $1; } + | "void" { $$ = CYNew cy::Syntax::New(CYNew CYVariable(CYNew CYIdentifier("Type")), CYNew CYArgument(CYNew CYString("v"))); } + ; + +QualifiedType + : PrimitiveType { $$ = CYNew CYTypeVariable($1); } + | "const" QualifiedType { $$ = CYNew CYTypeConstant($2); } + ; + +ModifiedType + : QualifiedType { $$ = $1; } + | QualifiedType "*" { $$ = CYNew CYTypePointerTo($1); } + | QualifiedType "const" { $$ = CYNew CYTypeConstant($1); } + ; + +TypedIdentifier + : QualifiedType PrefixedType { CYSetLast($2->type_) = $1; $$ = $2;} + ; + +EncodedType + : TypedIdentifier { $$ = CYNew CYEncodedType($1->type_); } + ; + +PrimaryExpression + : AtEncode "(" EncodedType ")" { $$ = $3; } + ; +/* }}} */ /* Cycript (Objective-C): @class Declaration {{{ */ ClassSuperOpt /* XXX: why the hell did I choose MemberExpression? */ @@ -1294,17 +1393,13 @@ ClassSuperOpt | { $$ = NULL; } ; -ClassField - : Expression Identifier ";" - ; - -ClassFieldList - : BRACE ClassFieldListOpt "}" { $$ = NULL; } +ClassFieldListOpt + : Expression Identifier ";" ClassFieldListOpt { $$ = CYNew CYField($1, $2, $4); } + | LexSetRegExp { $$ = NULL; } ; -ClassFieldListOpt - : ClassFieldListOpt ClassField - | +ClassFields + : BRACE ClassFieldListOpt "}" { $$ = $2; } ; MessageScope @@ -1313,8 +1408,7 @@ MessageScope ; TypeOpt - : "(" Expression ")" { $$ = $2; } - | "(" LexSetRegExp "void" ")" { $$ = NULL; } + : "(" LexSetRegExp EncodedType ")" { $$ = $3; } | { $$ = NULL; } ; @@ -1372,11 +1466,11 @@ ClassProtocolListOpt ; ClassExpression - : "@implementation" LexPushInOff ClassNameOpt ClassSuperOpt ClassProtocolListOpt ClassFieldList ClassMessageDeclarationListOpt LexPopIn "@end" { $$ = CYNew CYClassExpression($3, $4, $5, $6, $7); } + : "@implementation" LexPushInOff ClassNameOpt ClassSuperOpt ClassProtocolListOpt ClassFields LexPopIn ClassMessageDeclarationListOpt "@end" { $$ = CYNew CYClassExpression($3, $4, $5, $6, $8); } ; ClassStatement - : ";@implementation" ClassName ClassSuperOpt ClassProtocolListOpt ClassFieldList ClassMessageDeclarationListOpt "@end" { $$ = CYNew CYClassStatement($2, $3, $4, $5, $6); } + : ";@implementation" ClassName ClassSuperOpt ClassProtocolListOpt ClassFields ClassMessageDeclarationListOpt "@end" { $$ = CYNew CYClassStatement($2, $3, $4, $5, $6); } ; CategoryName @@ -1459,7 +1553,41 @@ ImportPath ; StatementListItem - : "@import" ImportPath { $$ = CYNew CYImport(); } + : LexSetStatement LexSetRegExp "@import" ImportPath { $$ = CYNew CYImport(); } + ; +/* }}} */ +/* Cycript (Objective-C): Boxed Expressions {{{ */ +BoxableExpression + : NullLiteral { $$ = $1; } + | BooleanLiteral { $$ = $1; } + | NumericLiteral { $$ = $1; } + | StringLiteral { $$ = $1; } + | ArrayLiteral { $$ = $1; } + | ObjectLiteral { $$ = $1; } + | Parenthetical { $$ = $1; } + ; + +PrimaryExpression + : "@" BoxableExpression { $$ = CYNew CYBox($2); } + ; +/* }}} */ +/* Cycript (Objective-C): Block Expressions {{{ */ +TypedParameterList_ + : "," TypedParameterList { $$ = $2; } + | { $$ = NULL; } + ; + +TypedParameterList + : TypedIdentifier TypedParameterList_ { $$ = CYNew CYTypedParameter($1, $2); } + ; + +TypedParameterListOpt + : TypedParameterList { $$ = $1; } + | { $$ = NULL; } + ; + +PrimaryExpression + : "^" ModifiedType "(" LexPushInOff TypedParameterListOpt LexPopIn ")" BRACE LexPushInOff FunctionBody LexPopIn "}" { $$ = CYNew CYObjCBlock($2, $5, $10); } ; /* }}} */ @end @@ -1480,6 +1608,11 @@ MemberAccess | "->" AutoComplete { driver.mode_ = CYDriver::AutoIndirect; YYACCEPT; } ; /* }}} */ +/* Cycript (C): auto Compatibility {{{ */ +Var_ + : "auto" + ; +/* }}} */ @end /* YUI: Documentation Comments {{{ */ @@ -1514,11 +1647,12 @@ LexSetXMLTag : { driver.SetCondition(CYDriver::XMLTagCondition); } ; /* }}} */ - +/* Virtual Tokens {{{ */ XMLWhitespaceOpt : XMLWhitespace | ; +/* }}} */ /* 8.1 Context Keywords {{{ */ Identifier @@ -1533,6 +1667,7 @@ XMLMarkup | XMLPI { $$ = $1; } ; /* }}} */ + /* 11.1 Primary Expressions {{{ */ PrimaryExpression : PropertyIdentifier { $$ = CYNew CYPropertyVariable($1); } @@ -1636,6 +1771,7 @@ XMLListInitialiser : "<>" LexPushInOff LexPushXMLContent XMLElementContent LexPop LexPopIn "" { $$ = CYNew CYXMLList($4); } ; /* }}} */ + /* 11.2 Left-Hand-Side Expressions {{{ */ PropertyIdentifier_ : Identifier { $$ = $1; } @@ -1665,8 +1801,8 @@ IfComprehension ; ForComprehension - : "for" "(" LexPushInOn Identifier LexPopIn "!in" Expression ")" { $$ = CYNew CYForInComprehension($4, $7); } - | "for" "each" "(" LexPushInOn Identifier LexPopIn "!in" Expression ")" { $$ = CYNew CYForOfComprehension($5, $8); } + : "for" "(" Identifier "in" Expression ")" { $$ = CYNew CYForInComprehension($3, $5); } + | "for" "each" "(" Identifier "in" Expression ")" { $$ = CYNew CYForOfComprehension($4, $6); } ; ComprehensionList