From 924f67b2b1aabb11cdcbbfc00fd638c9b6bfb868 Mon Sep 17 00:00:00 2001 From: "Jay Freeman (saurik)" Date: Tue, 29 Sep 2009 23:08:23 +0000 Subject: [PATCH] The glue code required for Bison/Flex parsers is insane. --- Cycript.l | 15 +++++++++++++++ Cycript.y | 8 ++++++-- Library.mm | 12 ++++++++++-- Parser.hpp | 10 ++++++++++ 4 files changed, 41 insertions(+), 4 deletions(-) diff --git a/Cycript.l b/Cycript.l index 2467b96..b1aa8a6 100644 --- a/Cycript.l +++ b/Cycript.l @@ -1,6 +1,7 @@ %{ #include "Cycript.tab.hh" typedef cy::parser::token tk; +#define YY_EXTRA_TYPE CYParser * %} %option prefix="cy" @@ -10,6 +11,7 @@ typedef cy::parser::token tk; %option yylineno %option nounput %option interactive +%option reentrant delim [ \t] whitesp {delim}+ @@ -65,8 +67,10 @@ number [-]?{digit}*[.]?{digit}+ "(" return tk::OpenParen; ")" return tk::CloseParen; + "{" return tk::OpenBrace; "}" return tk::CloseBrace; + "[" return tk::OpenBracket; "]" return tk::CloseBracket; @@ -103,3 +107,14 @@ number [-]?{digit}*[.]?{digit}+ [0-9]+ return tk::NumericLiteral; [ \t\n] ; + +%% + +void CYParser::ScannerInit() { + cylex_init(&scanner_); + cyset_extra(this, scanner_); +} + +void CYParser::ScannerDestroy() { + cylex_destroy(scanner_); +} diff --git a/Cycript.y b/Cycript.y index 9ccf4e0..0ecb777 100644 --- a/Cycript.y +++ b/Cycript.y @@ -1,6 +1,7 @@ %code top { #include "Cycript.tab.hh" -int cylex(YYSTYPE *lvalp, YYLTYPE *llocp); +int cylex(YYSTYPE *lvalp, YYLTYPE *llocp, void *scanner); +#define scanner driver->scanner_ } %code requires { @@ -25,7 +26,8 @@ int cylex(YYSTYPE *lvalp, YYLTYPE *llocp); %debug %error-verbose -%parse-param { CYParser *context } +%parse-param { CYParser *driver } +%lex-param { void *scanner } %token Ampersand "&" %token AmpersandAmpersand "&&" @@ -74,8 +76,10 @@ int cylex(YYSTYPE *lvalp, YYLTYPE *llocp); %token OpenParen "(" %token CloseParen ")" + %token OpenBrace "{" %token CloseBrace "}" + %token OpenBracket "[" %token CloseBracket "]" diff --git a/Library.mm b/Library.mm index 3de8415..140d167 100644 --- a/Library.mm +++ b/Library.mm @@ -952,6 +952,14 @@ static JSStaticValue Pointer_staticValues[2] = { {NULL, NULL, NULL, 0} }; +CYParser::CYParser() { + ScannerInit(); +} + +CYParser::~CYParser() { + ScannerDestroy(); +} + extern int cydebug; void cy::parser::error(const cy::parser::location_type &loc, const std::string &msg) { @@ -960,8 +968,8 @@ void cy::parser::error(const cy::parser::location_type &loc, const std::string & void CYConsole(FILE *fin, FILE *fout, FILE *ferr) { cydebug = 1; - CYParser context; - cy::parser parser(&context); + CYParser driver; + cy::parser parser(&driver); parser.parse(); } diff --git a/Parser.hpp b/Parser.hpp index e0ed1f1..7878b0f 100644 --- a/Parser.hpp +++ b/Parser.hpp @@ -2,6 +2,16 @@ #define CYPARSER_HPP class CYParser { + public: + void *scanner_; + + private: + void ScannerInit(); + void ScannerDestroy(); + + public: + CYParser(); + ~CYParser(); }; struct CYExpression { -- 2.45.2