]> git.saurik.com Git - cycript.git/blobdiff - Cycript.yy.in
Optimize for lexer performance: stop backtracking.
[cycript.git] / Cycript.yy.in
index 14f6aadcc5e61c1a3399077c533c2821cf8e57b3..8eba3a40e0d98c7961fb411ab03d055d39ebdc19 100644 (file)
@@ -1,33 +1,30 @@
 /* Cycript - Optimizing JavaScript Compiler/Runtime
- * Copyright (C) 2009-2013  Jay Freeman (saurik)
+ * Copyright (C) 2009-2015  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 <http://www.gnu.org/licenses/>.
+ * 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 <http://www.gnu.org/licenses/>.
 **/
 /* }}} */
 
-@if Bison23 %{
-
-@if Bison24 %code top {
+%code top {
 #define cyscanner driver.scanner_
 #define YYSTACKEXPANDABLE 1
-@if Bison23 #define yyerrok (yyerrstatus_ = 0)
-@if Bison24 }
+}
 
-@if Bison24 %code requires {
+%code requires {
 #include "Driver.hpp"
 #include "Parser.hpp"
 #define CYNew new($pool)
@@ -72,6 +69,7 @@ typedef struct {
         CYInfix *infix_;
         CYLiteral *literal_;
         CYMember *member_;
+        CYModule *module_;
         CYNull *null_;
         CYNumber *number_;
         CYProgram *program_;
@@ -84,16 +82,20 @@ typedef struct {
         CYTrue *true_;
         CYWord *word_;
 
+@begin C
+        CYTypeModifier *modifier_;
+        CYTypeSpecifier *specifier_;
+        CYTypedIdentifier *typedIdentifier_;
+        CYTypedParameter *typedParameter_;
+@end
+
 @begin ObjectiveC
         CYClassName *className_;
-        CYField *field_;
+        CYClassField *classField_;
         CYMessage *message_;
         CYMessageParameter *messageParameter_;
         CYProtocol *protocol_;
         CYSelectorPart *selector_;
-        CYTypeModifier *type_;
-        CYTypedIdentifier *typedIdentifier_;
-        CYTypedParameter *typedParameter_;
 @end
 
 @begin E4X
@@ -105,18 +107,15 @@ typedef struct {
 } YYSTYPE;
 
 #define YYSTYPE YYSTYPE
-@if Bison24 }
-
-@if Bison24 %code provides {
-int cylex(YYSTYPE *, cy::location *, void *);
-@if Bison24 }
+}
 
-@if Bison23 %}
+%code provides {
+int cylex(YYSTYPE *, CYLocation *, void *);
+}
 
-%name-prefix="cy"
+%name-prefix "cy"
 
-@if Bison23 %skeleton "lalr1.cc"
-@if Bison24 %language "C++"
+%language "C++"
 
 %initial-action {
     @$.begin.filename = @$.end.filename = &driver.filename_;
@@ -125,6 +124,8 @@ int cylex(YYSTYPE *, cy::location *, void *);
 %locations
 %defines
 
+%define api.location.type { CYLocation }
+
 //%glr-parser
 //%expect 1
 
@@ -159,6 +160,7 @@ int cylex(YYSTYPE *, cy::location *, void *);
 
 @begin E4X ObjectiveC
 %token At "@"
+%token Pound "#"
 @end
 
 %token Ampersand "&"
@@ -224,17 +226,31 @@ int cylex(YYSTYPE *, cy::location *, void *);
 %token OpenBracket "["
 %token CloseBracket "]"
 
+%token AtError "@error"
+
 @begin Java
 %token AtClass "@class"
 @end
 
+@begin C
+%token <identifier_> Typedef "typedef"
+%token <identifier_> Unsigned "unsigned"
+%token <identifier_> Signed "signed"
+%token <identifier_> 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 <identifier_> Yes "YES"
+%token <identifier_> No "NO"
 @end
 
 %token <false_> False "false"
@@ -338,6 +354,8 @@ int cylex(YYSTYPE *, cy::location *, void *);
 %type <argument_> ArgumentList
 %type <argument_> ArgumentListOpt
 %type <argument_> Arguments
+%type <expression_> ArrayComprehension
+%type <expression_> ArrayInitialiser
 %type <literal_> ArrayLiteral
 %type <expression_> ArrowFunction
 %type <functionParameter_> ArrowParameters
@@ -359,10 +377,10 @@ int cylex(YYSTYPE *, cy::location *, void *);
 %type <clause_> CaseClause
 %type <clause_> CaseClausesOpt
 %type <catch_> CatchOpt
-%type <comprehension_> ComprehensionForList
-%type <comprehension_> ComprehensionForListOpt
-%type <comprehension_> ComprehensionList
-%type <comprehension_> ComprehensionListOpt
+%type <expression_> Comprehension
+%type <comprehension_> ComprehensionFor
+%type <comprehension_> ComprehensionIf
+%type <comprehension_> ComprehensionTail
 %type <expression_> ConditionalExpression
 %type <statement_> ContinueStatement
 %type <statement_> ConciseBody
@@ -383,7 +401,6 @@ int cylex(YYSTYPE *, cy::location *, void *);
 %type <expression_> ExpressionOpt
 %type <statement_> ExpressionStatement
 %type <finally_> FinallyOpt
-%type <comprehension_> ForComprehension
 %type <for_> ForStatementInitialiser
 %type <forin_> ForInStatementInitialiser
 %type <declaration_> FormalParameter
@@ -395,8 +412,8 @@ int cylex(YYSTYPE *, cy::location *, void *);
 %type <expression_> FunctionExpression
 %type <identifier_> Identifier
 %type <identifier_> IdentifierOpt
+%type <identifier_> IdentifierType
 %type <word_> IdentifierName
-%type <comprehension_> IfComprehension
 %type <statement_> IfStatement
 %type <expression_> Initialiser
 %type <expression_> InitialiserOpt
@@ -412,12 +429,12 @@ int cylex(YYSTYPE *, cy::location *, void *);
 %type <member_> MemberAccess
 %type <expression_> MemberExpression_
 %type <expression_> MemberExpression
+%type <module_> Module
 %type <expression_> MultiplicativeExpression
 %type <expression_> NewExpression
 %type <null_> NullLiteral
 %type <literal_> ObjectLiteral
 %type <compound_> Parenthetical
-%type <compound_> ParentheticalOpt
 %type <expression_> PostfixExpression
 %type <expression_> PrimaryExpression
 %type <statement_> Program
@@ -455,15 +472,32 @@ int cylex(YYSTYPE *, cy::location *, void *);
 %type <statement_> VariableStatement
 %type <statement_> WithStatement
 %type <word_> Word
+@begin ObjectiveC
 %type <word_> WordOpt
+@end
 %type <expression_> Variable
 
+@begin C
+%type <specifier_> IntegerType
+%type <specifier_> IntegerTypeOpt
+%type <typedIdentifier_> PrefixedType
+%type <specifier_> PrimitiveType
+%type <typedIdentifier_> SuffixedType
+%type <typedIdentifier_> TypeSignifier
+%type <modifier_> TypeQualifierLeft
+%type <typedIdentifier_> TypeQualifierRight
+%type <typedIdentifier_> TypedIdentifier
+%type <typedParameter_> TypedParameterList_
+%type <typedParameter_> TypedParameterList
+%type <typedParameter_> TypedParameterListOpt
+@end
+
 @begin ObjectiveC
 %type <expression_> BoxableExpression
 %type <statement_> CategoryStatement
 %type <expression_> ClassExpression
-%type <field_> ClassFieldListOpt
-%type <field_> ClassFields
+%type <classField_> ClassFieldListOpt
+%type <classField_> ClassFields
 %type <statement_> ClassStatement
 %type <expression_> ClassSuperOpt
 %type <message_> ClassMessageDeclaration
@@ -473,17 +507,12 @@ int cylex(YYSTYPE *, cy::location *, void *);
 %type <protocol_> ClassProtocolListOpt
 %type <protocol_> ClassProtocols
 %type <protocol_> ClassProtocolsOpt
-%type <expression_> EncodedType
 %type <expression_> MessageExpression
 %type <messageParameter_> MessageParameter
 %type <messageParameter_> MessageParameters
 %type <messageParameter_> MessageParameterList
 %type <messageParameter_> MessageParameterListOpt
 %type <bool_> MessageScope
-%type <type_> ModifiedType
-%type <typedIdentifier_> PrefixedType
-%type <expression_> PrimitiveType
-%type <type_> QualifiedType
 %type <argument_> SelectorCall_
 %type <argument_> SelectorCall
 %type <selector_> SelectorExpression_
@@ -491,12 +520,7 @@ int cylex(YYSTYPE *, cy::location *, void *);
 %type <selector_> SelectorExpressionOpt
 %type <argument_> SelectorList
 %type <word_> SelectorWordOpt
-%type <typedIdentifier_> SuffixedType
-%type <expression_> TypeOpt
-%type <typedIdentifier_> TypedIdentifier
-%type <typedParameter_> TypedParameterList_
-%type <typedParameter_> TypedParameterList
-%type <typedParameter_> TypedParameterListOpt
+%type <typedIdentifier_> TypeOpt
 %type <argument_> VariadicCall
 @end
 
@@ -551,16 +575,16 @@ LexSetRegExp
     ;
 
 LexNoBrace
-    : { if (yychar == yyempty_) driver.no_.OpenBrace = true; else if (yychar == token::OpenBrace || yychar == token::OpenBrace_) yychar = token::OpenBrace__; }
+    : { if (yyla.empty()) driver.no_.OpenBrace = true; else if (yyla.type == yytranslate_(token::OpenBrace) || yyla.type == yytranslate_(token::OpenBrace_)) yyla.type = yytranslate_(token::OpenBrace__); }
     ;
 
 LexNoFunction
-    : { if (yychar == yyempty_) driver.no_.Function = true; else if (yychar == token::Function) yychar = token::Function_; }
+    : { if (yyla.empty()) driver.no_.Function = true; else if (yyla.type == yytranslate_(token::Function)) yyla.type = yytranslate_(token::Function_); }
     ;
 
 LexNoAtImplementation :
 @begin ObjectiveC
-    { if (yychar == yyempty_) driver.no_.AtImplementation = true; else if (yychar == token::AtImplementation) yychar = token::AtImplementation_; }
+    { if (yyla.empty()) driver.no_.AtImplementation = true; else if (yyla.type == yytranslate_(token::AtImplementation)) yyla.type = yytranslate_(token::AtImplementation_); }
 @end
     ;
 
@@ -600,7 +624,7 @@ Word
     | "continue" NewLineOpt { $$ = $1; }
     | "debugger" { $$ = $1; }
     | "default" { $$ = $1; }
-    | "delete" { $$ = $1; }
+    | "delete" LexSetRegExp { $$ = $1; }
     | "do" { $$ = $1; }
     | "else" { $$ = $1; }
     | "enum" { $$ = $1; }
@@ -619,7 +643,7 @@ Word
     // XXX: as it currently is not an Identifier
     | "let" { $$ = $1; }
 
-    | "new" { $$ = $1; }
+    | "new" LexSetRegExp { $$ = $1; }
     | "null" { $$ = $1; }
     | "return" NewLineOpt { $$ = $1; }
     | "super" { $$ = $1; }
@@ -628,19 +652,21 @@ Word
     | "throw" NewLineOpt { $$ = $1; }
     | "true" { $$ = $1; }
     | "try" { $$ = $1; }
-    | "typeof" { $$ = $1; }
+    | "typeof" LexSetRegExp { $$ = $1; }
     | "var" { $$ = $1; }
-    | "void" { $$ = $1; }
+    | "void" LexSetRegExp { $$ = $1; }
     | "while" { $$ = $1; }
     | "with" { $$ = $1; }
     ;
 
+@begin ObjectiveC
 WordOpt
     : Word { $$ = $1; }
     | { $$ = NULL; }
     ;
+@end
 
-Identifier
+IdentifierType
     : Identifier_ { $$ = $1; }
 
     | "implements" { $$ = $1; }
@@ -654,19 +680,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; }
@@ -677,6 +698,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; }
@@ -710,12 +750,12 @@ BooleanLiteral
 
 /* 7.9 Automatic Semicolon Insertion {{{ */
 StrictSemi
-    : { driver.Warning(yylloc, "warning, automatic semi-colon insertion required"); }
+    : { driver.Warning(@$, "warning, automatic semi-colon insertion required"); }
     ;
 
 Terminator
     : ";"
-    | error { if (yychar != yyeof_ && yychar != token::CloseBrace && !yylval.newline_) YYABORT; else { yyerrok; driver.errors_.pop_back(); } } StrictSemi
+    | error { if (yyla.type_get() != yyeof_ && yyla.type != yytranslate_(token::CloseBrace) && !yyla.value.newline_) { error(@1, "required semi-colon"); } else { yyerrok; driver.errors_.pop_back(); } } StrictSemi
     ;
 
 TerminatorOpt
@@ -729,11 +769,6 @@ Parenthetical
     : "(" LexPushInOff Expression ")" LexPopIn { $$ = $3; }
     ;
 
-ParentheticalOpt
-    : Parenthetical { $$ = $1; }
-    | { $$ = NULL; }
-    ;
-
 Variable
     : Identifier { $$ = CYNew CYVariable($1); }
     ;
@@ -742,14 +777,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); }
     ;
@@ -774,19 +814,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 {{{ */
@@ -841,6 +888,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); }
     ;
@@ -871,6 +919,7 @@ ArgumentList_
 
 ArgumentList
     : AssignmentExpression ArgumentList_ { $$ = CYNew CYArgument(NULL, $1, $2); }
+    | LexSetRegExp Word ":" AssignmentExpression ArgumentList_ { $$ = CYNew CYArgument($2, $4, $5); }
     ;
 
 ArgumentListOpt
@@ -984,6 +1033,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 {{{ */
@@ -1007,11 +1057,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
@@ -1324,7 +1374,8 @@ ArrowFunction
 
 ArrowParameters
     : BindingIdentifier { $$ = CYNew CYFunctionParameter(CYNew CYDeclaration($1)); }
-    //| ParentheticalOpt { $$ = $1; }
+    | "(" LexPushInOff LexSetRegExp ")" LexPopIn { $$ = NULL; }
+    | Parenthetical { $$ = $1->Parameters(); if ($$ == NULL) error(@1, "invalid parameter list"); }
     ;
 
 ConciseBody
@@ -1347,48 +1398,72 @@ ProgramBodyOpt
     ;
 /* }}} */
 
-@begin ObjectiveC
-/* Cycript (Objective-C): Type Encoding {{{ */
+@begin C
+/* Cycript (C): Type Encoding {{{ */
+TypeSignifier
+    : IdentifierType { $$ = CYNew CYTypedIdentifier(@1, $1); }
+    | "(" LexPushInOff "*" TypeQualifierRight ")" LexPopIn { $$ = $4; }
+    ;
+
 SuffixedType
-    : IdentifierOpt { $$ = CYNew CYTypedIdentifier($1); }
-    | "(" LexPushInOff PrefixedType ")" LexPopIn { $$ = $3; }
-    | SuffixedType "[" NumericLiteral "]" { CYSetLast($1->type_) = CYNew CYTypeArrayOf($3); $$ = $1; }
+    : SuffixedType "[" NumericLiteral "]" { $$ = $1; $$->modifier_ = CYNew CYTypeArrayOf($3, $$->modifier_); }
+    | "(" LexPushInOff "^" TypeQualifierRight ")" LexPopIn "(" LexPushInOff TypedParameterListOpt ")" LexPopIn { $$ = $4; $$->modifier_ = CYNew CYTypeBlockWith($9, $$->modifier_); }
+    | TypeSignifier "(" LexPushInOff TypedParameterListOpt ")" LexPopIn { $$ = $1; $$->modifier_ = CYNew CYTypeFunctionWith($4, $$->modifier_); }
+    | "(" LexPushInOff TypedParameterListOpt ")" LexPopIn { $$ = CYNew CYTypedIdentifier(@1); $$->modifier_ = CYNew CYTypeFunctionWith($3, $$->modifier_); }
+    | TypeSignifier { $$ = $1; }
+    | { $$ = CYNew CYTypedIdentifier(@$); }
     ;
 
 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"); }
+    ;
+
+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")); }
     ;
 
-EncodedType
-    : TypedIdentifier { $$ = CYNew CYEncodedType($1->type_); }
+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? */
@@ -1397,7 +1472,7 @@ ClassSuperOpt
     ;
 
 ClassFieldListOpt
-    : Expression Identifier ";" ClassFieldListOpt { $$ = CYNew CYField($1, $2, $4); }
+    : TypedIdentifier ";" ClassFieldListOpt { $$ = CYNew CYClassField($1, $3); }
     | LexSetRegExp { $$ = NULL; }
     ;
 
@@ -1411,12 +1486,12 @@ MessageScope
     ;
 
 TypeOpt
-    : "(" LexSetRegExp EncodedType ")" { $$ = $3; }
-    | { $$ = NULL; }
+    : "(" LexSetRegExp TypedIdentifier ")" { if ($3->identifier_ != NULL) error($3->location_, "unexpected identifier"); $$ = $3; }
+    | { $$ = CYNew CYTypedIdentifier(CYNew CYTypeVariable("id")); }
     ;
 
 MessageParameter
-    : Word ":" TypeOpt Identifier { $$ = CYNew CYMessageParameter($1, $3, $4); }
+    : Word ":" TypeOpt Identifier { $3->identifier_ = $4; $$ = CYNew CYMessageParameter($1, $3); }
     ;
 
 MessageParameterList
@@ -1430,7 +1505,7 @@ MessageParameterListOpt
 
 MessageParameters
     : MessageParameterList { $$ = $1; }
-    | Word { $$ = CYNew CYMessageParameter($1, NULL, NULL); }
+    | Word { $$ = CYNew CYMessageParameter($1, NULL); }
     ;
 
 ClassMessageDeclaration
@@ -1542,23 +1617,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; }
@@ -1568,6 +1640,8 @@ BoxableExpression
     | ArrayLiteral { $$ = $1; }
     | ObjectLiteral { $$ = $1; }
     | Parenthetical { $$ = $1; }
+    | "YES" { $$ = CYNew CYTrue(); }
+    | "NO" { $$ = CYNew CYFalse(); }
     ;
 
 PrimaryExpression
@@ -1575,22 +1649,13 @@ PrimaryExpression
     ;
 /* }}} */
 /* Cycript (Objective-C): Block Expressions {{{ */
-TypedParameterList_
-    : "," TypedParameterList { $$ = $2; }
-    | { $$ = NULL; }
-    ;
-
-TypedParameterList
-    : TypedIdentifier TypedParameterList_ { $$ = CYNew CYTypedParameter($1, $2); }
-    ;
-
-TypedParameterListOpt
-    : TypedParameterList { $$ = $1; }
-    | { $$ = NULL; }
+PrimaryExpression
+    : "^" TypedIdentifier { if ($2->identifier_ != NULL) error($2->location_, "unexpected identifier"); } BRACE LexPushInOff FunctionBody "}" LexPopIn { if (CYTypeFunctionWith *function = $2->Function()) $$ = CYNew CYObjCBlock($2, function->parameters_, $6); else error($2->location_, "expected parameters"); }
     ;
-
+/* }}} */
+/* Cycript (Objective-C): Instance Literals {{{ */
 PrimaryExpression
-    : "^" ModifiedType "(" LexPushInOff TypedParameterListOpt ")" LexPopIn BRACE LexPushInOff FunctionBody "}" LexPopIn { $$ = CYNew CYObjCBlock($2, $5, $10); }
+    : "#" NumericLiteral { $$ = CYNew CYInstanceLiteral($2); }
     ;
 /* }}} */
 @end
@@ -1616,6 +1681,36 @@ Var_
     : "auto"
     ;
 /* }}} */
+/* Cycript (C): Lambda Expressions {{{ */
+TypedParameterList_
+    : "," TypedParameterList { $$ = $2; }
+    | { $$ = NULL; }
+    ;
+
+TypedParameterList
+    : TypedIdentifier TypedParameterList_ { $$ = CYNew CYTypedParameter($1, $2); }
+    ;
+
+TypedParameterListOpt
+    : TypedParameterList { $$ = $1; }
+    | { $$ = NULL; }
+    ;
+
+PrimaryExpression
+    : "[" LexPushInOff LexSetRegExp "&" LexSetRegExp "]" LexPopIn "(" LexPushInOff TypedParameterListOpt ")" LexPopIn "->" TypedIdentifier BRACE LexPushInOff FunctionBody "}" LexPopIn { $$ = CYNew CYLambda($14, $10, $17); }
+    ;
+/* }}} */
+/* Cycript (C): Type Definitions {{{ */
+Statement__
+    : "typedef" TypedIdentifier { if ($2->identifier_ == NULL) error($2->location_, "expected identifier"); } Terminator { $$ = CYNew CYTypeDefinition($2); }
+    ;
+/* }}} */
+/* Cycript (C): extern "C" {{{ */
+Statement__
+    : "extern" StringLiteral { if (strcmp($2->Value(), "C") != 0) error(@2, "unknown extern binding"); } TypedIdentifier { if ($4->identifier_ == NULL) error($4->location_, "expected identifier"); } Terminator { $$ = CYNew CYExternal($2, $4); }
+    ;
+/* }}} */
+
 @end
 
 /* YUI: Documentation Comments {{{ */
@@ -1799,27 +1894,13 @@ Statement__
 @end
 
 /* JavaScript 1.7: Array Comprehensions {{{ */
-IfComprehension
-    : "if" "(" Expression ")" { $$ = CYNew CYIfComprehension($3); }
+Comprehension
+    : AssignmentExpression ComprehensionFor ComprehensionTail { $$ = CYNew CYArrayComprehension($1, $2->Modify($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; }
-    ;
-
-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 {{{ */