]> git.saurik.com Git - cycript.git/commitdiff
C++ Bison conversion.
authorJay Freeman (saurik) <saurik@saurik.com>
Tue, 29 Sep 2009 22:41:09 +0000 (22:41 +0000)
committerJay Freeman (saurik) <saurik@saurik.com>
Tue, 29 Sep 2009 22:41:09 +0000 (22:41 +0000)
Cycript.l
Cycript.y
Library.mm
Parser.hpp
makefile

index 24e72d7c1a87eded81d10264ec670b576f299436..2467b96fad0cdd371af8af1f8bb3769c2a241c90 100644 (file)
--- a/Cycript.l
+++ b/Cycript.l
@@ -1,5 +1,6 @@
 %{
-#include "Cycript.tab.h"
+#include "Cycript.tab.hh"
+typedef cy::parser::token tk;
 %}
 
 %option prefix="cy"
@@ -17,88 +18,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 tk::Ampersand;
+"&&"   return tk::AmpersandAmpersand;
+"&="   return tk::AmpersandEqual;
+"^"    return tk::Carrot;
+"^="   return tk::CarrotEqual;
+"="    return tk::Equal;
+"=="   return tk::EqualEqual;
+"==="  return tk::EqualEqualEqual;
+"!"    return tk::Exclamation;
+"!="   return tk::ExclamationEqual;
+"!=="  return tk::ExclamationEqualEqual;
+"-"    return tk::Hyphen;
+"-="   return tk::HyphenEqual;
+"--"   return tk::HyphenHyphen;
+"->"   return tk::HyphenRight;
+"<"    return tk::Left;
+"<="   return tk::LeftEqual;
+"<<"   return tk::LeftLeft;
+"<<="  return tk::LeftLeftEqual;
+"%"    return tk::Percent;
+"%="   return tk::PercentEqual;
+"."    return tk::Period;
+"|"    return tk::Pipe;
+"|="   return tk::PipeEqual;
+"||"   return tk::PipePipe;
+"+"    return tk::Plus;
+"+="   return tk::PlusEqual;
+"++"   return tk::PlusPlus;
+">"    return tk::Right;
+">="   return tk::RightEqual;
+">>"   return tk::RightRight;
+">>="  return tk::RightRightEqual;
+">>>"  return tk::RightRightRight;
+">>>=" return tk::RightRightRightEqual;
+"/"    return tk::Slash;
+"/="   return tk::SlashEqual;
+"*"    return tk::Star;
+"*="   return tk::StarEqual;
+"~"    return tk::Tilde;
 
-":"    return CYTokenColon;
-","    return CYTokenComma;
-"?"    return CYTokenQuestion;
-";"    return CYTokenSemiColon;
+":"    return tk::Colon;
+","    return tk::Comma;
+"?"    return tk::Question;
+";"    return tk::SemiColon;
 
-"("    return CYTokenOpenParen;
-")"    return CYTokenCloseParen;
-"{"    return CYTokenOpenBrace;
-"}"    return CYTokenCloseBrace;
-"["    return CYTokenOpenBracket;
-"]"    return CYTokenCloseBracket;
+"("    return tk::OpenParen;
+")"    return tk::CloseParen;
+"{"    return tk::OpenBrace;
+"}"    return tk::CloseBrace;
+"["    return tk::OpenBracket;
+"]"    return tk::CloseBracket;
 
-"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;
+"break"      return tk::Break;
+"case"       return tk::Case;
+"catch"      return tk::Catch;
+"continue"   return tk::Continue;
+"default"    return tk::Default;
+"delete"     return tk::Delete;
+"do"         return tk::Do;
+"else"       return tk::Else;
+"false"      return tk::False;
+"finally"    return tk::Finally;
+"for"        return tk::For;
+"function"   return tk::Function;
+"if"         return tk::If;
+"in"         return tk::In;
+"instanceof" return tk::InstanceOf;
+"new"        return tk::New;
+"null"       return tk::Null;
+"return"     return tk::Return;
+"switch"     return tk::Switch;
+"this"       return tk::This;
+"throw"      return tk::Throw;
+"true"       return tk::True;
+"try"        return tk::Try;
+"typeof"     return tk::TypeOf;
+"var"        return tk::Var;
+"void"       return tk::Void;
+"while"      return tk::While;
+"with"       return tk::With;
 
-[a-zA-Z$_][a-zA-Z$_0-9]* return CYTokenIdentifier;
-[0-9]+                   return CYTokenNumber;
+[a-zA-Z$_][a-zA-Z$_0-9]* return tk::Identifier;
+[0-9]+                   return tk::NumericLiteral;
 
 [ \t\n]                  ;
index f6ac4de9a71c868c00292f80711425051c0cda94..9ccf4e09402700eacad8fd85c77167b62bb80e05 100644 (file)
--- a/Cycript.y
+++ b/Cycript.y
@@ -1,14 +1,23 @@
 %code top {
-#include "Parser.hpp"
-#include "Cycript.tab.h"
-void cyerror(YYLTYPE *locp, CYParser *context, const char *msg);
+#include "Cycript.tab.hh"
 int cylex(YYSTYPE *lvalp, YYLTYPE *llocp);
 }
 
+%code requires {
+#include "Parser.hpp"
+}
+
+%union {
+    CYExpression *expression_;
+    CYTokenIdentifier *identifier_;
+    CYTokenNumber *number_;
+    CYTokenString *string_;
+}
+
 %name-prefix "cy"
 
+%language "C++"
 %locations
-%define api.pure
 %glr-parser
 
 %defines
@@ -18,106 +27,100 @@ int cylex(YYSTYPE *lvalp, YYLTYPE *llocp);
 
 %parse-param { CYParser *context }
 
-%token CYTokenAmpersand "&"
-%token CYTokenAmpersandAmpersand "&&"
-%token CYTokenAmpersandEqual "&="
-%token CYTokenCarrot "^"
-%token CYTokenCarrotEqual "^="
-%token CYTokenEqual "="
-%token CYTokenEqualEqual "=="
-%token CYTokenEqualEqualEqual "==="
-%token CYTokenExclamation "!"
-%token CYTokenExclamationEqual "!="
-%token CYTokenExclamationEqualEqual "!=="
-%token CYTokenHyphen "-"
-%token CYTokenHyphenEqual "-="
-%token CYTokenHyphenHyphen "--"
-%token CYTokenHyphenRight "->"
-%token CYTokenLeft "<"
-%token CYTokenLeftEqual "<="
-%token CYTokenLeftLeft "<<"
-%token CYTokenLeftLeftEqual "<<="
-%token CYTokenPercent "%"
-%token CYTokenPercentEqual "%="
-%token CYTokenPeriod "."
-%token CYTokenPipe "|"
-%token CYTokenPipeEqual "|="
-%token CYTokenPipePipe "||"
-%token CYTokenPlus "+"
-%token CYTokenPlusEqual "+="
-%token CYTokenPlusPlus "++"
-%token CYTokenRight ">"
-%token CYTokenRightEqual ">="
-%token CYTokenRightRight ">>"
-%token CYTokenRightRightEqual ">>="
-%token CYTokenRightRightRight ">>>"
-%token CYTokenRightRightRightEqual ">>>="
-%token CYTokenSlash "/"
-%token CYTokenSlashEqual "/="
-%token CYTokenStar "*"
-%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"
-%token CYTokenContinue "continue"
-%token CYTokenDefault "default"
-%token CYTokenDelete "delete"
-%token CYTokenDo "do"
-%token CYTokenElse "else"
-%token CYTokenFalse "false"
-%token CYTokenFinally "finally"
-%token CYTokenFor "for"
-%token CYTokenFunction "function"
-%token CYTokenIf "if"
-%token CYTokenIn "in"
-%token CYTokenInstanceOf "instanceof"
-%token CYTokenNew "new"
-%token CYTokenNull "null"
-%token CYTokenReturn "return"
-%token CYTokenSwitch "switch"
-%token CYTokenThis "this"
-%token CYTokenThrow "throw"
-%token CYTokenTrue "true"
-%token CYTokenTry "try"
-%token CYTokenTypeOf "typeof"
-%token CYTokenVar "var"
-%token CYTokenVoid "void"
-%token CYTokenWhile "while"
-%token CYTokenWith "with"
-
-%token CYTokenIdentifier
-%token CYTokenNumber
-%token CYTokenString
+%token Ampersand "&"
+%token AmpersandAmpersand "&&"
+%token AmpersandEqual "&="
+%token Carrot "^"
+%token CarrotEqual "^="
+%token Equal "="
+%token EqualEqual "=="
+%token EqualEqualEqual "==="
+%token Exclamation "!"
+%token ExclamationEqual "!="
+%token ExclamationEqualEqual "!=="
+%token Hyphen "-"
+%token HyphenEqual "-="
+%token HyphenHyphen "--"
+%token HyphenRight "->"
+%token Left "<"
+%token LeftEqual "<="
+%token LeftLeft "<<"
+%token LeftLeftEqual "<<="
+%token Percent "%"
+%token PercentEqual "%="
+%token Period "."
+%token Pipe "|"
+%token PipeEqual "|="
+%token PipePipe "||"
+%token Plus "+"
+%token PlusEqual "+="
+%token PlusPlus "++"
+%token Right ">"
+%token RightEqual ">="
+%token RightRight ">>"
+%token RightRightEqual ">>="
+%token RightRightRight ">>>"
+%token RightRightRightEqual ">>>="
+%token Slash "/"
+%token SlashEqual "/="
+%token Star "*"
+%token StarEqual "*="
+%token Tilde "~"
+
+%token Colon ":"
+%token Comma ","
+%token Question "?"
+%token SemiColon ";"
+
+%token OpenParen "("
+%token CloseParen ")"
+%token OpenBrace "{"
+%token CloseBrace "}"
+%token OpenBracket "["
+%token CloseBracket "]"
+
+%token Break "break"
+%token Case "case"
+%token Catch "catch"
+%token Continue "continue"
+%token Default "default"
+%token Delete "delete"
+%token Do "do"
+%token Else "else"
+%token False "false"
+%token Finally "finally"
+%token For "for"
+%token Function "function"
+%token If "if"
+%token In "in"
+%token InstanceOf "instanceof"
+%token New "new"
+%token Null "null"
+%token Return "return"
+%token Switch "switch"
+%token This "this"
+%token Throw "throw"
+%token True "true"
+%token Try "try"
+%token TypeOf "typeof"
+%token Var "var"
+%token Void "void"
+%token While "while"
+%token With "with"
+
+%token <identifier_> Identifier
+%token <number_> NumericLiteral
+%token <string_> StringLiteral
 
 %%
 
-Start
-    : Program
-    ;
+%start Program;
 
 IdentifierOpt
     : Identifier
     |
     ;
 
-Identifier
-    : CYTokenIdentifier
-    ;
-
 Literal
     : NullLiteral
     | BooleanLiteral
@@ -134,14 +137,6 @@ BooleanLiteral
     | "false"
     ;
 
-NumericLiteral
-    : CYTokenNumber
-    ;
-
-StringLiteral
-    : CYTokenString
-    ;
-
 /* Objective-C Extensions {{{ */
 VariadicCall
     : "," AssignmentExpression VariadicCall
@@ -588,9 +583,3 @@ SourceElement
     ;
 
 %%
-
-#include <stdio.h>
-
-void cyerror(YYLTYPE *locp, CYParser *context, const char *msg) {
-    fprintf(stderr, "err:%s\n", msg);
-}
index 4dd24143afdea9eb195a4e4c067740ddda0276e9..3de8415c408995bfd5ef29c7962017f8e5062d28 100644 (file)
@@ -75,6 +75,7 @@
 #include <map>
 
 #include "Parser.hpp"
+#include "Cycript.tab.hh"
 
 #undef _assert
 #undef _trace
@@ -951,13 +952,17 @@ static JSStaticValue Pointer_staticValues[2] = {
     {NULL, NULL, NULL, 0}
 };
 
-void cyparse(CYParser *parser);
 extern int cydebug;
 
+void cy::parser::error(const cy::parser::location_type &loc, const std::string &msg) {
+    std::cerr << loc << ": " << msg << std::endl;
+}
+
 void CYConsole(FILE *fin, FILE *fout, FILE *ferr) {
     cydebug = 1;
-    CYParser parser;
-    cyparse(&parser);
+    CYParser context;
+    cy::parser parser(&context);
+    parser.parse();
 }
 
 MSInitialize { _pooled
index 65806af7e197e858175be391d3a0e4f17b81d3c1..e0ed1f18d525222320ae194f2e3e058d7484fc1b 100644 (file)
@@ -1,2 +1,94 @@
+#ifndef CYPARSER_HPP
+#define CYPARSER_HPP
+
 class CYParser {
 };
+
+struct CYExpression {
+};
+
+struct CYToken {
+    virtual const char *Text() const = 0;
+};
+
+struct CYTokenLiteral :
+    CYExpression,
+    virtual CYToken
+{
+};
+
+struct CYTokenString :
+    CYTokenLiteral
+{
+};
+
+struct CYTokenNumber :
+    CYTokenLiteral
+{
+};
+
+struct CYTokenWord :
+    virtual CYToken
+{
+};
+
+struct CYTokenIdentifier :
+    CYExpression,
+    CYTokenWord
+{
+    const char *word_;
+
+    virtual const char *Text() const {
+        return word_;
+    }
+};
+
+struct CYExpressionPrefix :
+    CYExpression
+{
+    CYExpression *rhs_;
+
+    CYExpressionPrefix(CYExpression *rhs) :
+        rhs_(rhs)
+    {
+    }
+};
+
+struct CYExpressionInfix :
+    CYExpression
+{
+    CYExpression *lhs_;
+    CYExpression *rhs_;
+
+    CYExpressionInfix(CYExpression *lhs, CYExpression *rhs) :
+        lhs_(lhs),
+        rhs_(rhs)
+    {
+    }
+};
+
+struct CYExpressionPostfix :
+    CYExpression
+{
+    CYExpression *lhs_;
+
+    CYExpressionPostfix(CYExpression *lhs) :
+        lhs_(lhs)
+    {
+    }
+};
+
+struct CYExpressionAssignment :
+    CYExpression
+{
+    CYExpression *lhs_;
+    CYExpression *rhs_;
+
+    CYExpressionAssignment(CYExpression *lhs, CYExpression *rhs) :
+        lhs_(lhs),
+        rhs_(rhs)
+    {
+    }
+};
+
+#endif/*CYPARSER_HPP*/
index 5e246e395798cde5c36ae5415d50cf0d1ee5b402..45c3e1c27c9214f84844a5768ba0a82853818b89 100644 (file)
--- a/makefile
+++ b/makefile
@@ -14,7 +14,7 @@ link := -framework CoreFoundation -framework Foundation -F${PKG_ROOT}/System/Lib
 all: cycript libcycript.dylib libcycript.plist
 
 clean:
-       rm -f libcycript.dylib cycript libcycript.plist Struct.hpp lex.cy.c Cycript.tab.c Cycript.tab.h
+       rm -f libcycript.dylib cycript libcycript.plist Struct.hpp lex.cy.c Cycript.tab.cc Cycript.tab.hh location.hh position.hh
 
 libcycript.plist: Bridge.def makefile
        sed -e 's/^C/0/;s/^F/1/;s/^V/2/' Bridge.def | while read -r line; do \
@@ -28,7 +28,7 @@ libcycript.plist: Bridge.def makefile
            echo "$$2 = ($$1, \"$$3\");";  \
        done >$@
 
-Cycript.tab.c Cycript.tab.h: Cycript.y makefile
+Cycript.tab.cc Cycript.tab.hh: Cycript.y makefile
        bison -v $<
 
 lex.cy.c: Cycript.l
@@ -40,8 +40,8 @@ Struct.hpp:
 #Parser.hpp: Parser.py Parser.dat
 #      ./Parser.py <Parser.dat >$@
 
-libcycript.dylib: Library.mm makefile $(menes)/mobilesubstrate/substrate.h sig/*.[ch]pp Struct.hpp Parser.hpp lex.cy.c Cycript.tab.c Cycript.tab.h
-       $(target)g++ -dynamiclib -mthumb -g0 -O2 -Wall -Werror -o $@ $(filter %.cpp,$^) $(filter %.c,$^) $(filter %.mm,$^) -lobjc -I$(menes)/mobilesubstrate $(link) $(flags) -DYYDEBUG=1
+libcycript.dylib: Library.mm makefile $(menes)/mobilesubstrate/substrate.h sig/*.[ch]pp Struct.hpp Parser.hpp lex.cy.c Cycript.tab.cc Cycript.tab.hh
+       $(target)g++ -dynamiclib -mthumb -g0 -O2 -Wall -Werror -o $@ $(filter %.cpp,$^) $(filter %.cc,$^) $(filter %.c,$^) $(filter %.mm,$^) -lobjc -I$(menes)/mobilesubstrate $(link) $(flags) #-DYYDEBUG=1
        ldid -S $@
 
 cycript: Application.mm libcycript.dylib