]> git.saurik.com Git - cycript.git/blobdiff - Parser.ypp.in
Support cast and call of pointer-to-function type.
[cycript.git] / Parser.ypp.in
index f73ccd168b9758b9d83a630351a260d02799c764..88c01bfcd47392c0a31d74543c92ef0a1270ea3b 100644 (file)
@@ -80,6 +80,7 @@
 %union { CYWord *word_; }
 
 @begin C
+%union { CYTypeStructField *structField_; }
 %union { CYTypeModifier *modifier_; }
 %union { CYTypeSpecifier *specifier_; }
 %union { CYTypedIdentifier *typedIdentifier_; }
@@ -330,6 +331,7 @@ type; })
 %token _typedef_ "typedef"
 %token _unsigned_ "unsigned"
 %token _signed_ "signed"
+%token _struct_ "struct"
 %token _extern_ "extern"
 @end
 
@@ -417,6 +419,7 @@ type; })
 %token _package_ "package"
 %token _private_ "private"
 %token _protected_ "protected"
+%token ___proto___ "__proto__"
 %token _prototype_ "prototype"
 %token _public_ "public"
 %token _set_ "set"
@@ -543,6 +546,7 @@ type; })
 %type <identifier_> IdentifierNoOf
 %type <identifier_> IdentifierType
 %type <identifier_> IdentifierTypeNoOf
+%type <identifier_> IdentifierTypeOpt
 %type <word_> IdentifierName
 %type <variable_> IdentifierReference
 %type <statement_> IfStatement
@@ -583,6 +587,8 @@ type; })
 %type <bool_> RegularExpressionSlash
 %type <expression_> RelationalExpression
 %type <statement_> ReturnStatement
+%type <target_> RubyBlockExpression_
+%type <target_> RubyBlockExpression
 %type <rubyProc_> RubyProcExpression
 %type <functionParameter_> RubyProcParameterList_
 %type <functionParameter_> RubyProcParameterList
@@ -626,10 +632,15 @@ type; })
 %type <specifier_> IntegerTypeOpt
 %type <typedIdentifier_> PrefixedType
 %type <specifier_> PrimitiveType
+%type <structField_> StructFieldListOpt
 %type <typedIdentifier_> SuffixedType
+%type <typedIdentifier_> SuffixedTypeOpt
 %type <typedIdentifier_> TypeSignifier
+%type <typedIdentifier_> TypeSignifierOpt
 %type <modifier_> TypeQualifierLeft
+%type <modifier_> TypeQualifierLeftOpt
 %type <typedIdentifier_> TypeQualifierRight
+%type <typedIdentifier_> TypeQualifierRightOpt
 %type <typedIdentifier_> TypedIdentifierMaybe
 %type <typedIdentifier_> TypedIdentifierNo
 %type <typedIdentifier_> TypedIdentifierYes
@@ -712,10 +723,12 @@ LexPopIn: { driver.in_.pop(); };
 
 LexPushReturnOn: { driver.return_.push(true); };
 LexPopReturn: { driver.return_.pop(); };
+Return: "return"[return] { if (!driver.return_.top()) CYERR(@return, "invalid return"); };
 
 LexPushSuperOn: { driver.super_.push(true); };
 LexPushSuperOff: { driver.super_.push(false); };
 LexPopSuper: { driver.super_.pop(); };
+Super: "super"[super] { if (!driver.super_.top()) CYERR(@super, "invalid super"); };
 
 LexPushYieldOn: { driver.yield_.push(true); };
 LexPushYieldOff: { driver.yield_.push(false); };
@@ -909,6 +922,7 @@ IdentifierTypeNoOf
     | "package" { $$ = CYNew CYIdentifier("package"); }
     | "private" { $$ = CYNew CYIdentifier("private"); }
     | "protected" { $$ = CYNew CYIdentifier("protected"); }
+    | "__proto__" { $$ = CYNew CYIdentifier("__proto__"); }
     | "prototype" { $$ = CYNew CYIdentifier("prototype"); }
     | "public" { $$ = CYNew CYIdentifier("public"); }
     | "set" { $$ = CYNew CYIdentifier("set"); }
@@ -930,6 +944,11 @@ IdentifierType
     | "of" { $$ = CYNew CYIdentifier("of"); }
     ;
 
+IdentifierTypeOpt
+    : IdentifierType[pass] { $$ = $pass; }
+    | { $$ = NULL; }
+    ;
+
 IdentifierNoOf
     : IdentifierTypeNoOf
     | "char" { $$ = CYNew CYIdentifier("char"); }
@@ -940,6 +959,7 @@ IdentifierNoOf
     | "volatile" { $$ = CYNew CYIdentifier("volatile"); }
 @begin C
     | "signed" { $$ = CYNew CYIdentifier("signed"); }
+    | "struct" { $$ = CYNew CYIdentifier("struct"); }
     | "unsigned" { $$ = CYNew CYIdentifier("unsigned"); }
 @end
 @begin ObjectiveC
@@ -1086,8 +1106,8 @@ MemberExpression
     ;
 
 SuperProperty
-    : "super"[super] { if (!driver.super_.top()) CYERR(@super, "invalid super"); } "[" Expression[property] "]" { $$ = CYNew CYSuperAccess($property); }
-    | "super"[super] { if (!driver.super_.top()) CYERR(@super, "invalid super"); } "." IdentifierName[property] { $$ = CYNew CYSuperAccess(CYNew CYString($property)); }
+    : Super "[" Expression[property] "]" { $$ = CYNew CYSuperAccess($property); }
+    | Super "." IdentifierName[property] { $$ = CYNew CYSuperAccess(CYNew CYString($property)); }
     ;
 
 MetaProperty
@@ -1115,7 +1135,7 @@ CallExpression
     ;
 
 SuperCall
-    : "super"[super] { if (!driver.super_.top()) CYERR(@super, "invalid super"); } Arguments[arguments] { $$ = CYNew CYSuperCall($arguments); }
+    : Super Arguments[arguments] { $$ = CYNew CYSuperCall($arguments); }
     ;
 
 Arguments
@@ -1143,13 +1163,13 @@ AccessExpression
     ;
 
 LeftHandSideExpression
-    : AccessExpression[pass] LexNewLineOrOpt { $$ = $pass; }
+    : RubyBlockExpression[pass] { $$ = $pass; }
     | IndirectExpression[pass] { $$ = $pass; }
     ;
 /* }}} */
 /* 12.4 Postfix Expressions {{{ */
 PostfixExpression
-    : AccessExpression[lhs] LexNewLineOrOpt { $$ = $lhs; }
+    : RubyBlockExpression[pass] { $$ = $pass; }
     | AccessExpression[lhs] LexNewLineOrOpt "++" { $$ = CYNew CYPostIncrement($lhs); }
     | AccessExpression[lhs] LexNewLineOrOpt "--" { $$ = CYNew CYPostDecrement($lhs); }
     ;
@@ -1169,7 +1189,6 @@ UnaryExpression_
 
 UnaryExpression
     : PostfixExpression[expression] { $$ = $expression; }
-    | PostfixExpression[expression] "\n" { $$ = $expression; }
     | UnaryExpression_[pass] { $$ = $pass; }
     ;
 /* }}} */
@@ -1538,7 +1557,7 @@ ForStatementInitializer
     ;
 
 ForInStatementInitializer
-    : LexLet LexOf AccessExpression[pass] LexNewLineOrOpt { $$ = $pass; }
+    : LexLet LexOf RubyBlockExpression[pass] { $$ = $pass; }
     | LexLet LexOf IndirectExpression[pass] { $$ = $pass; }
     | LexLet LexOf Var_ LexBind ForBinding[binding] { $$ = CYNew CYForVariable($binding); }
     | ForDeclaration[pass] { $$ = $pass; }
@@ -1566,10 +1585,6 @@ BreakStatement
     ;
 /* }}} */
 /* 13.10 The return Statement {{{ */
-Return
-    : "return"[return] { if (!driver.return_.top()) CYERR(@return, "invalid return"); }
-    ;
-
 ReturnStatement
     : Return TerminatorSoft { $$ = CYNew CYReturn(NULL); }
     | Return NewLineNot Expression[value] Terminator { $$ = CYNew CYReturn($value); }
@@ -1932,33 +1947,50 @@ ExportSpecifier
 /* Cycript (C): Type Encoding {{{ */
 TypeSignifier
     : IdentifierType[identifier] { $$ = CYNew CYTypedIdentifier(@identifier, $identifier); }
-    | "(" "*" TypeQualifierRight[typed] ")" { $$ = $typed; }
+    | "(" "*" TypeQualifierRightOpt[typed] ")" { $$ = $typed; $$->modifier_ = CYNew CYTypePointerTo($$->modifier_); }
+    ;
+
+TypeSignifierOpt
+    : TypeSignifier[pass] { $$ = $pass; }
+    | { $$ = CYNew CYTypedIdentifier(@$); }
     ;
 
 SuffixedType
-    : SuffixedType[typed] "[" NumericLiteral[size] "]" { $$ = $typed; $$->modifier_ = CYNew CYTypeArrayOf($size, $$->modifier_); }
-    | "(" "^" TypeQualifierRight[typed] ")" "(" TypedParameterListOpt[parameters] ")" { $$ = $typed; $$->modifier_ = CYNew CYTypeBlockWith($parameters, $$->modifier_); }
+    : SuffixedTypeOpt[typed] "[" NumericLiteral[size] "]" { $$ = $typed; $$->modifier_ = CYNew CYTypeArrayOf($size, $$->modifier_); }
+    | "(" "^" TypeQualifierRightOpt[typed] ")" "(" TypedParameterListOpt[parameters] ")" { $$ = $typed; $$->modifier_ = CYNew CYTypeBlockWith($parameters, $$->modifier_); }
     | TypeSignifier[typed] "(" TypedParameterListOpt[parameters] ")" { $$ = $typed; $$->modifier_ = CYNew CYTypeFunctionWith($parameters, $$->modifier_); }
     | "("[parenthesis] TypedParameterListOpt[parameters] ")" { $$ = CYNew CYTypedIdentifier(@parenthesis); $$->modifier_ = CYNew CYTypeFunctionWith($parameters, $$->modifier_); }
-    | TypeSignifier[pass] { $$ = $pass; }
-    | { $$ = CYNew CYTypedIdentifier(@$); }
+    ;
+
+SuffixedTypeOpt
+    : SuffixedType[pass] { $$ = $pass; }
+    | TypeSignifierOpt[pass] { $$ = $pass; }
     ;
 
 PrefixedType
-    : "*" TypeQualifierRight[typed] { $$ = $typed; $$->modifier_ = CYNew CYTypePointerTo($$->modifier_); }
+    : "*" TypeQualifierRightOpt[typed] { $$ = $typed; $$->modifier_ = CYNew CYTypePointerTo($$->modifier_); }
     ;
 
 TypeQualifierLeft
-    : { $$ = NULL; }
-    | "const" TypeQualifierLeft[modifier] { $$ = $modifier; CYSetLast($$) = CYNew CYTypeConstant(); }
-    | "volatile" TypeQualifierLeft[modifier] { $$ = $modifier; CYSetLast($$) = CYNew CYTypeVolatile(); }
+    : "const" TypeQualifierLeftOpt[modifier] { $$ = $modifier; CYSetLast($$) = CYNew CYTypeConstant(); }
+    | "volatile" TypeQualifierLeftOpt[modifier] { $$ = $modifier; CYSetLast($$) = CYNew CYTypeVolatile(); }
+    ;
+
+TypeQualifierLeftOpt
+    : TypeQualifierLeft[pass] { $$ = $pass; }
+    | { $$ = NULL; }
     ;
 
 TypeQualifierRight
-    : PrefixedType[pass] { $$ = $pass; }
-    | SuffixedType[pass] { $$ = $pass; }
-    | "const" TypeQualifierRight[typed] { $$ = $typed; $$->modifier_ = CYNew CYTypeConstant($$->modifier_); }
-    | "volatile" TypeQualifierRight[typed] { $$ = $typed; $$->modifier_ = CYNew CYTypeVolatile($$->modifier_); }
+    : SuffixedType[pass] { $$ = $pass; }
+    | PrefixedType[pass] { $$ = $pass; }
+    | "const" TypeQualifierRightOpt[typed] { $$ = $typed; $$->modifier_ = CYNew CYTypeConstant($$->modifier_); }
+    | "volatile" TypeQualifierRightOpt[typed] { $$ = $typed; $$->modifier_ = CYNew CYTypeVolatile($$->modifier_); }
+    ;
+
+TypeQualifierRightOpt
+    : TypeQualifierRight[pass] { $$ = $pass; }
+    | TypeSignifierOpt[pass] { $$ = $pass; }
     ;
 
 IntegerType
@@ -1974,17 +2006,24 @@ IntegerTypeOpt
     | { $$ = CYNew CYTypeVariable("int"); }
     ;
 
+StructFieldListOpt
+    : TypedIdentifierMaybe[typed] ";" StructFieldListOpt[next] { $$ = CYNew CYTypeStructField($typed, $next); }
+    | { $$ = NULL; }
+    ;
+
 PrimitiveType
     : IdentifierType[name] { $$ = CYNew CYTypeVariable($name); }
     | IntegerType[pass] { $$ = $pass; }
-    | "void" { $$ = CYNew CYTypeVoid(); }
     | "char" { $$ = CYNew CYTypeVariable("char"); }
     | "signed" "char" { $$ = CYNew CYTypeSigned(CYNew CYTypeVariable("char")); }
     | "unsigned" "char" { $$ = CYNew CYTypeUnsigned(CYNew CYTypeVariable("char")); }
+    | "struct" IdentifierTypeOpt[name] "{" StructFieldListOpt[fields] "}" { $$ = CYNew CYTypeStruct($name, $fields); }
     ;
 
 TypedIdentifierMaybe
-    : TypeQualifierLeft[modifier] PrimitiveType[specifier] TypeQualifierRight[typed] { $$ = $typed; $$->specifier_ = $specifier; CYSetLast($modifier) = $$->modifier_; $$->modifier_ = $modifier; }
+    : TypeQualifierLeftOpt[modifier] PrimitiveType[specifier] TypeQualifierRightOpt[typed] { $$ = $typed; $$->specifier_ = $specifier; CYSetLast($modifier) = $$->modifier_; $$->modifier_ = $modifier; }
+    | TypeQualifierLeft[modifier] "void" TypeQualifierRight[typed] { $$ = $typed; $$->specifier_ = CYNew CYTypeVoid(); CYSetLast($modifier) = $$->modifier_; $$->modifier_ = $modifier; }
+    | "void" TypeQualifierRight[typed] { $$ = $typed; $$->specifier_ = CYNew CYTypeVoid(); }
     ;
 
 TypedIdentifierYes
@@ -2212,6 +2251,7 @@ TypedParameterList
 
 TypedParameterListOpt
     : TypedParameterList[pass] { $$ = $pass; }
+    | "void" { $$ = NULL; }
     | { $$ = NULL; }
     ;
 
@@ -2500,8 +2540,14 @@ PrimaryExpression
     : "{" RubyProcParameters[parameters] StatementListOpt[code] "}" { $$ = CYNew CYRubyProc($parameters, $code); }
     ;
 
-PostfixExpression
-    : PostfixExpression[lhs] RubyProcExpression[rhs] LexNewLineOrOpt { $$ = CYNew CYRubyBlock($lhs, $rhs); }
+RubyBlockExpression_
+    : AccessExpression[pass] LexNewLineOrOpt { $$ = $pass; }
+    | RubyBlockExpression_[lhs] RubyProcExpression[rhs] LexNewLineOrOpt { $$ = CYNew CYRubyBlock($lhs, $rhs); }
+    ;
+
+RubyBlockExpression
+    : RubyBlockExpression_[pass] "\n" { $$ = $pass; }
+    | RubyBlockExpression_[pass] { $$ = $pass; }
     ;
 /* }}} */