From 1dbba6ccaecdd0572932cb02fe92a9154d2abe83 Mon Sep 17 00:00:00 2001
From: "Jay Freeman (saurik)" <saurik@saurik.com>
Date: Tue, 29 Sep 2009 21:39:35 +0000
Subject: [PATCH] Finished JS Bison/Flex parser.

---
 Bridge.def |   1 +
 Cycript.l  | 151 +++++++++-------
 Cycript.y  | 511 ++++++++++++++++++++++++++++++++++++++++++++++++++++-
 Library.mm |  50 ------
 makefile   |   8 +-
 5 files changed, 597 insertions(+), 124 deletions(-)

diff --git a/Bridge.def b/Bridge.def
index 3dd4fb0..26870d7 100644
--- a/Bridge.def
+++ b/Bridge.def
@@ -111,6 +111,7 @@ F UIKeyboardInputModeUsesKBStar B@
 
 F _UIImageWithName @@
 
+F UIFormattedPhoneNumberFromString @@
 F UIUnformattedPhoneNumberFromString @@
 
 V UIApp @
diff --git a/Cycript.l b/Cycript.l
index 10be43f..24e72d7 100644
--- a/Cycript.l
+++ b/Cycript.l
@@ -17,71 +17,88 @@ number        [-]?{digit}*[.]?{digit}+
 
 %%
 
-"&"    { return CYTokenAmpersand; }
-"&&"   { return CYTokenAmpersandAmpersand; }
-"&="   { return CYTokenAmpersandEqual; }
-"^"    { return CYTokenCarrot; }
-"^="   { return CYTokenCarrotEqual; }
-"="    { return CYTokenEqual; }
-"=="   { return CYTokenEqualEqual; }
-"==="  { return CYTokenEqualEqualEqual; }
-"!"    { return CYTokenExclamation; }
-"!="   { return CYTokenExclamationEqual; }
-"!=="  { return CYTokenExclamationEqualEqual; }
-"-"    { return CYTokenHyphen; }
-"-="   { return CYTokenHyphenEqual; }
-"--"   { return CYTokenHyphenHyphen; }
-"->"   { return CYTokenHyphenRight; }
-"<"    { return CYTokenLeft; }
-"<="   { return CYTokenLeftEqual; }
-"<<"   { return CYTokenLeftLeft; }
-"<<="  { return CYTokenLeftLeftEqual; }
-"%"    { return CYTokenPercent; }
-"%="   { return CYTokenPercentEqual; }
-"."    { return CYTokenPeriod; }
-"|"    { return CYTokenPipe; }
-"|="   { return CYTokenPipeEqual; }
-"||"   { return CYTokenPipePipe; }
-"+"    { return CYTokenPlus; }
-"+="   { return CYTokenPlusEqual; }
-"++"   { return CYTokenPlusPlus; }
-">"    { return CYTokenRight; }
-">="   { return CYTokenRightEqual; }
-">>"   { return CYTokenRightRight; }
-">>="  { return CYTokenRightRightEqual; }
-">>>"  { return CYTokenRightRightRight; }
-">>>=" { return CYTokenRightRightRightEqual; }
-"/"    { return CYTokenSlash; }
-"/="   { return CYTokenSlashEqual; }
-"*"    { return CYTokenStar; }
-"*="   { return CYTokenStarEqual; }
-"~"    { return CYTokenTilde; }
+"&"    return CYTokenAmpersand;
+"&&"   return CYTokenAmpersandAmpersand;
+"&="   return CYTokenAmpersandEqual;
+"^"    return CYTokenCarrot;
+"^="   return CYTokenCarrotEqual;
+"="    return CYTokenEqual;
+"=="   return CYTokenEqualEqual;
+"==="  return CYTokenEqualEqualEqual;
+"!"    return CYTokenExclamation;
+"!="   return CYTokenExclamationEqual;
+"!=="  return CYTokenExclamationEqualEqual;
+"-"    return CYTokenHyphen;
+"-="   return CYTokenHyphenEqual;
+"--"   return CYTokenHyphenHyphen;
+"->"   return CYTokenHyphenRight;
+"<"    return CYTokenLeft;
+"<="   return CYTokenLeftEqual;
+"<<"   return CYTokenLeftLeft;
+"<<="  return CYTokenLeftLeftEqual;
+"%"    return CYTokenPercent;
+"%="   return CYTokenPercentEqual;
+"."    return CYTokenPeriod;
+"|"    return CYTokenPipe;
+"|="   return CYTokenPipeEqual;
+"||"   return CYTokenPipePipe;
+"+"    return CYTokenPlus;
+"+="   return CYTokenPlusEqual;
+"++"   return CYTokenPlusPlus;
+">"    return CYTokenRight;
+">="   return CYTokenRightEqual;
+">>"   return CYTokenRightRight;
+">>="  return CYTokenRightRightEqual;
+">>>"  return CYTokenRightRightRight;
+">>>=" return CYTokenRightRightRightEqual;
+"/"    return CYTokenSlash;
+"/="   return CYTokenSlashEqual;
+"*"    return CYTokenStar;
+"*="   return CYTokenStarEqual;
+"~"    return CYTokenTilde;
 
-"break" { return CYTokenBreak; }
-"case" { return CYTokenCase; }
-"catch" { return CYTokenCatch; }
-"continue" { return CYTokenContinue; }
-"default" { return CYTokenDefault; }
-"delete" { return CYTokenDelete; }
-"do" { return CYTokenDo; }
-"else" { return CYTokenElse; }
-"false" { return CYTokenFalse; }
-"finally" { return CYTokenFinally; }
-"for" { return CYTokenFor; }
-"function" { return CYTokenFunction; }
-"if" { return CYTokenIf; }
-"in" { return CYTokenIn; }
-"instanceof" { return CYTokenInstanceOf; }
-"new" { return CYTokenNew; }
-"null" { return CYTokenNull; }
-"return" { return CYTokenReturn; }
-"switch" { return CYTokenSwitch; }
-"this" { return CYTokenThis; }
-"throw" { return CYTokenThrow; }
-"true" { return CYTokenTrue; }
-"try" { return CYTokenTry; }
-"typeof" { return CYTokenTypeOf; }
-"var" { return CYTokenVar; }
-"void" { return CYTokenVoid; }
-"while" { return CYTokenWhile; }
-"with" { return CYTokenWith; }
+":"    return CYTokenColon;
+","    return CYTokenComma;
+"?"    return CYTokenQuestion;
+";"    return CYTokenSemiColon;
+
+"("    return CYTokenOpenParen;
+")"    return CYTokenCloseParen;
+"{"    return CYTokenOpenBrace;
+"}"    return CYTokenCloseBrace;
+"["    return CYTokenOpenBracket;
+"]"    return CYTokenCloseBracket;
+
+"break"      return CYTokenBreak;
+"case"       return CYTokenCase;
+"catch"      return CYTokenCatch;
+"continue"   return CYTokenContinue;
+"default"    return CYTokenDefault;
+"delete"     return CYTokenDelete;
+"do"         return CYTokenDo;
+"else"       return CYTokenElse;
+"false"      return CYTokenFalse;
+"finally"    return CYTokenFinally;
+"for"        return CYTokenFor;
+"function"   return CYTokenFunction;
+"if"         return CYTokenIf;
+"in"         return CYTokenIn;
+"instanceof" return CYTokenInstanceOf;
+"new"        return CYTokenNew;
+"null"       return CYTokenNull;
+"return"     return CYTokenReturn;
+"switch"     return CYTokenSwitch;
+"this"       return CYTokenThis;
+"throw"      return CYTokenThrow;
+"true"       return CYTokenTrue;
+"try"        return CYTokenTry;
+"typeof"     return CYTokenTypeOf;
+"var"        return CYTokenVar;
+"void"       return CYTokenVoid;
+"while"      return CYTokenWhile;
+"with"       return CYTokenWith;
+
+[a-zA-Z$_][a-zA-Z$_0-9]* return CYTokenIdentifier;
+[0-9]+                   return CYTokenNumber;
+
+[ \t\n]                  ;
diff --git a/Cycript.y b/Cycript.y
index 71da89e..f6ac4de 100644
--- a/Cycript.y
+++ b/Cycript.y
@@ -1,14 +1,19 @@
-%{
+%code top {
 #include "Parser.hpp"
 #include "Cycript.tab.h"
 void cyerror(YYLTYPE *locp, CYParser *context, const char *msg);
 int cylex(YYSTYPE *lvalp, YYLTYPE *llocp);
-%}
+}
+
+%name-prefix "cy"
 
-%pure-parser
-%name-prefix="cy"
 %locations
+%define api.pure
+%glr-parser
+
 %defines
+
+%debug
 %error-verbose
 
 %parse-param { CYParser *context }
@@ -53,6 +58,18 @@ int cylex(YYSTYPE *lvalp, YYLTYPE *llocp);
 %token CYTokenStarEqual "*="
 %token CYTokenTilde "~"
 
+%token CYTokenColon ":"
+%token CYTokenComma ","
+%token CYTokenQuestion "?"
+%token CYTokenSemiColon ";"
+
+%token CYTokenOpenParen "("
+%token CYTokenCloseParen ")"
+%token CYTokenOpenBrace "{"
+%token CYTokenCloseBrace "}"
+%token CYTokenOpenBracket "["
+%token CYTokenCloseBracket "]"
+
 %token CYTokenBreak "break"
 %token CYTokenCase "case"
 %token CYTokenCatch "catch"
@@ -82,9 +99,493 @@ int cylex(YYSTYPE *lvalp, YYLTYPE *llocp);
 %token CYTokenWhile "while"
 %token CYTokenWith "with"
 
+%token CYTokenIdentifier
+%token CYTokenNumber
+%token CYTokenString
+
 %%
 
-hello: ;
+Start
+    : Program
+    ;
+
+IdentifierOpt
+    : Identifier
+    |
+    ;
+
+Identifier
+    : CYTokenIdentifier
+    ;
+
+Literal
+    : NullLiteral
+    | BooleanLiteral
+    | NumericLiteral
+    | StringLiteral
+    ;
+
+NullLiteral
+    : "null"
+    ;
+
+BooleanLiteral
+    : "true"
+    | "false"
+    ;
+
+NumericLiteral
+    : CYTokenNumber
+    ;
+
+StringLiteral
+    : CYTokenString
+    ;
+
+/* Objective-C Extensions {{{ */
+VariadicCall
+    : "," AssignmentExpression VariadicCall
+    |
+    ;
+
+SelectorCall_
+    : SelectorCall
+    | VariadicCall
+    ;
+
+SelectorCall
+    : IdentifierOpt ":" AssignmentExpression SelectorCall_
+    ;
+
+SelectorList
+    : SelectorCall
+    | Identifier
+    ;
+
+ObjectiveCall
+    : "[" AssignmentExpression SelectorList "]"
+    ;
+/* }}} */
+
+/* 11.1 Primary Expressions {{{ */
+PrimaryExpression
+    : "this"
+    | Identifier
+    | Literal
+    | ArrayLiteral
+    | ObjectLiteral
+    | "(" Expression ")"
+    | ObjectiveCall
+    ;
+/* }}} */
+/* 11.1.4 Array Initialiser {{{ */
+ArrayLiteral
+    : "[" ElementList "]"
+    ;
+
+Element
+    : AssignmentExpression
+    |
+    ;
+
+ElementList_
+    : "," ElementList
+    |
+    ;
+
+ElementList
+    : Element ElementList_
+    ;
+/* }}} */
+/* 11.1.5 Object Initialiser {{{ */
+ObjectLiteral
+    : "{" PropertyNameAndValueListOpt "}"
+    ;
+
+PropertyNameAndValueList_
+    : "," PropertyNameAndValueList
+    |
+    ;
+
+PropertyNameAndValueListOpt
+    : PropertyNameAndValueList
+    |
+    ;
+
+PropertyNameAndValueList
+    : PropertyName ":" AssignmentExpression PropertyNameAndValueList_
+    ;
+
+PropertyName
+    : Identifier
+    | StringLiteral
+    | NumericLiteral
+    ;
+/* }}} */
+
+MemberExpression
+    : PrimaryExpression
+    | FunctionExpression
+    | MemberExpression "[" Expression "]"
+    | MemberExpression "." Identifier
+    | "new" MemberExpression Arguments
+    ;
+
+NewExpression
+    : MemberExpression
+    | "new" NewExpression
+    ;
+
+CallExpression
+    : MemberExpression Arguments
+    | CallExpression Arguments
+    | CallExpression "[" Expression "]"
+    | CallExpression "." Identifier
+    ;
+
+ArgumentList_
+    : "," ArgumentList
+    |
+    ;
+
+ArgumentListOpt
+    : ArgumentList
+    |
+    ;
+
+ArgumentList
+    : AssignmentExpression ArgumentList_
+    ;
+
+Arguments
+    : "(" ArgumentListOpt ")"
+    ;
+
+LeftHandSideExpression
+    : NewExpression
+    | CallExpression
+    ;
+
+PostfixExpression
+    : LeftHandSideExpression
+    | LeftHandSideExpression "++"
+    | LeftHandSideExpression "--"
+    ;
+
+UnaryExpression
+    : PostfixExpression
+    | "delete" UnaryExpression
+    | "void" UnaryExpression
+    | "typeof" UnaryExpression
+    | "++" UnaryExpression
+    | "--" UnaryExpression
+    | "+" UnaryExpression
+    | "-" UnaryExpression
+    | "~" UnaryExpression
+    | "!" UnaryExpression
+    | "*" UnaryExpression
+    | "&" UnaryExpression
+    ;
+
+MultiplicativeExpression
+    : UnaryExpression
+    | MultiplicativeExpression "*" UnaryExpression
+    | MultiplicativeExpression "/" UnaryExpression
+    | MultiplicativeExpression "%" UnaryExpression
+    ;
+
+AdditiveExpression
+    : MultiplicativeExpression
+    | AdditiveExpression "+" MultiplicativeExpression
+    | AdditiveExpression "-" MultiplicativeExpression
+    ;
+
+ShiftExpression
+    : AdditiveExpression
+    | ShiftExpression "<<" AdditiveExpression
+    | ShiftExpression ">>" AdditiveExpression
+    | ShiftExpression ">>>" AdditiveExpression
+    ;
+
+RelationalExpression
+    : ShiftExpression
+    | RelationalExpression "<" ShiftExpression
+    | RelationalExpression ">" ShiftExpression
+    | RelationalExpression "<=" ShiftExpression
+    | RelationalExpression ">=" ShiftExpression
+    | RelationalExpression "instanceof" ShiftExpression
+    | RelationalExpression "in" ShiftExpression
+    ;
+
+EqualityExpression
+    : RelationalExpression
+    | EqualityExpression "==" RelationalExpression
+    | EqualityExpression "!=" RelationalExpression
+    | EqualityExpression "===" RelationalExpression
+    | EqualityExpression "!==" RelationalExpression
+    ;
+
+BitwiseANDExpression
+    : EqualityExpression
+    | BitwiseANDExpression "&" EqualityExpression
+    ;
+
+BitwiseXORExpression
+    : BitwiseANDExpression
+    | BitwiseXORExpression "^" BitwiseANDExpression
+    ;
+
+BitwiseORExpression
+    : BitwiseXORExpression
+    | BitwiseORExpression "|" BitwiseXORExpression
+    ;
+
+LogicalANDExpression
+    : BitwiseORExpression
+    | LogicalANDExpression "&&" BitwiseORExpression
+    ;
+
+LogicalORExpression
+    : LogicalANDExpression
+    | LogicalORExpression "||" LogicalANDExpression
+    ;
+
+ConditionalExpression
+    : LogicalORExpression
+    | LogicalORExpression "?" AssignmentExpression ":" AssignmentExpression
+    ;
+
+AssignmentExpression
+    : ConditionalExpression
+    | LeftHandSideExpression AssignmentOperator AssignmentExpression
+    ;
+
+AssignmentOperator
+    : "="
+    | "*="
+    | "/="
+    | "%="
+    | "+="
+    | "-="
+    | "<<="
+    | ">>="
+    | ">>>="
+    | "&="
+    | "^="
+    | "|="
+    ;
+
+Expression_
+    : "," Expression
+    |
+    ;
+
+ExpressionOpt
+    : Expression
+    |
+    ;
+
+Expression
+    : AssignmentExpression Expression_
+    ;
+
+Statement
+    : Block
+    | VariableStatement
+    | EmptyStatement
+    | ExpressionStatement
+    | IfStatement
+    | IterationStatement
+    | ContinueStatement
+    | BreakStatement
+    | ReturnStatement
+    | WithStatement
+    | LabelledStatement
+    | SwitchStatement
+    | ThrowStatement
+    | TryStatement
+    ;
+
+Block
+    : "{" StatementListOpt "}"
+    ;
+
+StatementListOpt
+    : Statement StatementListOpt
+    |
+    ;
+
+VariableStatement
+    : "var" VariableDeclarationList ";"
+    ;
+
+VariableDeclarationList_
+    : "," VariableDeclarationList
+    |
+    ;
+
+VariableDeclarationList
+    : VariableDeclaration VariableDeclarationList_
+    ;
+
+VariableDeclaration
+    : Identifier InitialiserOpt
+    ;
+
+InitialiserOpt
+    : Initialiser
+    |
+    ;
+
+Initialiser
+    : "=" AssignmentExpression
+    ;
+
+EmptyStatement
+    : ";"
+    ;
+
+ExpressionStatement
+    : Expression ";"
+    ;
+
+ElseStatementOpt
+    : "else" Statement
+    |
+    ;
+
+IfStatement
+    : "if" "(" Expression ")" Statement ElseStatementOpt
+    ;
+
+IterationStatement
+    : DoWhileStatement
+    | WhileStatement
+    | ForStatement
+    | ForInStatement
+    ;
+
+DoWhileStatement
+    : "do" Statement "while" "(" Expression ")" ";"
+    ;
+
+WhileStatement
+    : "while" "(" Expression ")" Statement
+    ;
+
+ForStatement
+    : "for" "(" ForStatementInitialiser ";" ExpressionOpt ";" ExpressionOpt ")" Statement
+    ;
+
+ForStatementInitialiser
+    : ExpressionOpt
+    | "var" VariableDeclarationList
+    ;
+
+ForInStatement
+    : "for" "(" ForInStatementInitialiser "in" Expression ")" Statement
+    ;
+
+ForInStatementInitialiser
+    : LeftHandSideExpression
+    | "var" VariableDeclaration
+    ;
+
+ContinueStatement
+    : "continue" IdentifierOpt ";"
+    ;
+
+BreakStatement
+    : "break" IdentifierOpt ";"
+    ;
+
+ReturnStatement
+    : "return" ExpressionOpt ";"
+    ;
+
+WithStatement
+    : "with" "(" Expression ")" Statement
+    ;
+
+SwitchStatement
+    : "switch" "(" Expression ")" CaseBlock
+    ;
+
+CaseBlock
+    : "{" CaseClausesOpt "}"
+    ;
+
+CaseClausesOpt
+    : CaseClause CaseClausesOpt
+    | DefaultClause CaseClausesOpt
+    |
+    ;
+
+CaseClause
+    : "case" Expression ":" StatementListOpt
+    ;
+
+DefaultClause
+    : "default" ":" StatementListOpt
+    ;
+
+LabelledStatement
+    : Identifier ":" Statement
+    ;
+
+ThrowStatement
+    : "throw" Expression ";"
+    ;
+
+TryStatement
+    : "try" Block CatchOpt FinallyOpt
+    ;
+
+CatchOpt
+    : "catch" "(" Identifier ")" Block
+    |
+    ;
+
+FinallyOpt
+    : "finally" Block
+    |
+    ;
+
+FunctionDeclaration
+    : "function" Identifier "(" FormalParameterList ")" "{" FunctionBody "}"
+    ;
+
+FunctionExpression
+    : "function" IdentifierOpt "(" FormalParameterList ")" "{" FunctionBody "}"
+    ;
+
+FormalParameterList_
+    : "," FormalParameterList
+    |
+    ;
+
+FormalParameterList
+    : Identifier FormalParameterList_
+    |
+    ;
+
+FunctionBody
+    : SourceElements
+    ;
+
+Program
+    : SourceElements
+    ;
+
+SourceElements
+    : SourceElement SourceElements
+    |
+    ;
+
+SourceElement
+    : Statement
+    | FunctionDeclaration
+    ;
 
 %%
 
diff --git a/Library.mm b/Library.mm
index ee77879..4dd2414 100644
--- a/Library.mm
+++ b/Library.mm
@@ -88,56 +88,6 @@
     CFLog(kCFLogLevelNotice, CFSTR("_trace():%u"), __LINE__); \
 } while (false)
 
-/* Objective-C Handle<> {{{ */
-template <typename Type_>
-class _H {
-    typedef _H<Type_> This_;
-
-  private:
-    Type_ *value_;
-
-    _finline void Retain_() {
-        if (value_ != nil)
-            [value_ retain];
-    }
-
-    _finline void Clear_() {
-        if (value_ != nil)
-            [value_ release];
-    }
-
-  public:
-    _finline _H(const This_ &rhs) :
-        value_(rhs.value_ == nil ? nil : [rhs.value_ retain])
-    {
-    }
-
-    _finline _H(Type_ *value = NULL, bool mended = false) :
-        value_(value)
-    {
-        if (!mended)
-            Retain_();
-    }
-
-    _finline ~_H() {
-        Clear_();
-    }
-
-    _finline operator Type_ *() const {
-        return value_;
-    }
-
-    _finline This_ &operator =(Type_ *value) {
-        if (value_ != value) {
-            Type_ *old(value_);
-            value_ = value;
-            Retain_();
-            if (old != nil)
-                [old release];
-        } return *this;
-    }
-};
-/* }}} */
 /* APR Pool Helpers {{{ */
 void *operator new(size_t size, apr_pool_t *pool) {
     return apr_palloc(pool, size);
diff --git a/makefile b/makefile
index b7c8770..5e246e3 100644
--- a/makefile
+++ b/makefile
@@ -28,8 +28,8 @@ libcycript.plist: Bridge.def makefile
 	    echo "$$2 = ($$1, \"$$3\");";  \
 	done >$@
 
-Cycript.tab.c Cycript.tab.h: Cycript.y
-	bison $<
+Cycript.tab.c Cycript.tab.h: Cycript.y makefile
+	bison -v $<
 
 lex.cy.c: Cycript.l
 	flex $<
@@ -67,4 +67,8 @@ package: all
 	cp -a libcycript.plist package/usr/lib
 	dpkg-deb -b package $(shell grep ^Package: control | cut -d ' ' -f 2-)_$(shell grep ^Version: control | cut -d ' ' -f 2)_iphoneos-arm.deb
 
+test: package
+	dpkg -i $(shell grep ^Package: control | cut -d ' ' -f 2-)_$(shell grep ^Version: control | cut -d ' ' -f 2)_iphoneos-arm.deb
+	cycript
+
 .PHONY: all clean extra package
-- 
2.50.0

