From 63b4c5a87f72580a85b2555c251490c1eb30021a Mon Sep 17 00:00:00 2001 From: "Jay Freeman (saurik)" Date: Tue, 29 Sep 2009 22:41:09 +0000 Subject: [PATCH] C++ Bison conversion. --- Cycript.l | 161 ++++++++++++++++++++--------------------- Cycript.y | 207 +++++++++++++++++++++++++---------------------------- Library.mm | 11 ++- Parser.hpp | 92 ++++++++++++++++++++++++ makefile | 8 +-- 5 files changed, 283 insertions(+), 196 deletions(-) diff --git a/Cycript.l b/Cycript.l index 24e72d7..2467b96 100644 --- 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] ; diff --git a/Cycript.y b/Cycript.y index f6ac4de..9ccf4e0 100644 --- 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 +%token NumericLiteral +%token 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 - -void cyerror(YYLTYPE *locp, CYParser *context, const char *msg) { - fprintf(stderr, "err:%s\n", msg); -} diff --git a/Library.mm b/Library.mm index 4dd2414..3de8415 100644 --- a/Library.mm +++ b/Library.mm @@ -75,6 +75,7 @@ #include #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 diff --git a/Parser.hpp b/Parser.hpp index 65806af..e0ed1f1 100644 --- a/Parser.hpp +++ b/Parser.hpp @@ -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*/ diff --git a/makefile b/makefile index 5e246e3..45c3e1c 100644 --- 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 $@ -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 -- 2.45.2