From 697d6fd2b2bf66a1f0bf1f28de0ced96fc14cbfc Mon Sep 17 00:00:00 2001 From: "Jay Freeman (saurik)" Date: Sun, 25 Oct 2009 21:30:17 +0000 Subject: [PATCH 1/1] Reorganized grammar to improve InputElement{RegExp,Div} factoring. --- Cycript.l.in | 42 ++++++++---------------- Cycript.y.in | 91 ++++++++++++++++++++++++++-------------------------- Parser.hpp | 6 ++-- 3 files changed, 62 insertions(+), 77 deletions(-) diff --git a/Cycript.l.in b/Cycript.l.in index f3201bb..ec2befc 100644 --- a/Cycript.l.in +++ b/Cycript.l.in @@ -7,11 +7,11 @@ typedef cy::parser::token tk; #define YY_EXTRA_TYPE CYDriver * -#define T yylval->newline_ = yyextra->state_ == CYNewLine; +#define T yylval->newline_ = yyextra->state_ == CYNewLine; BEGIN(Div); #define C T yyextra->state_ = CYClear; #define R T yyextra->state_ = CYRestricted; -#define E(prefix) L C BEGIN(INITIAL); { \ +#define E(prefix) L C { \ char *value(reinterpret_cast(apr_palloc(yyextra->pool_, yyleng + sizeof(prefix)))); \ memcpy(value, prefix, sizeof(prefix) - 1); \ memcpy(value + sizeof(prefix) - 1, yytext, yyleng); \ @@ -99,23 +99,14 @@ RegularExpressionFirstChar [^\n*\\/]|{BackslashSequence} RegularExpressionChar [^\n\\/]|{BackslashSequence} RegularExpressionFlags {IdentifierPart}* RegularExpressionChars {RegularExpressionChar}* -RegularExpressionBody_ {RegularExpressionChars} -RegularExpressionBody {RegularExpressionFirstChar}{RegularExpressionBody_} +RegularExpressionBody {RegularExpressionFirstChar}{RegularExpressionChars} -RegularExpressionEnd_ \/{RegularExpressionFlags} -RegularExpressionRest_ {RegularExpressionBody_}{RegularExpressionEnd_} -RegularExpressionStart_ {RegularExpressionBody}{RegularExpressionEnd_} - -%x RegExp -%x RegExpSlash -%x RegExpSlashEqual -%x RegExpSlashRight +%s Div +%s RegExp %% -{RegularExpressionStart_} E("/") -{RegularExpressionRest_} E("/=") -{RegularExpressionRest_} E("/>") +\/{RegularExpressionBody}\/{RegularExpressionFlags} E("") \/\/[^\n]* L \/\*(\n|[^\*]|\*[^/])*\*\/ M @@ -167,12 +158,13 @@ RegularExpressionStart_ {RegularExpressionBody}{RegularExpressionEnd_} ">>=" L C return tk::RightRightEqual; ">>>" L C return tk::RightRightRight; ">>>=" L C return tk::RightRightRightEqual; -"/" L C return tk::Slash; -"/=" L C return tk::SlashEqual; "*" L C return tk::Star; "*=" L C return tk::StarEqual; "~" L C return tk::Tilde; +
"/" L C return tk::Slash; +
"/=" L C return tk::SlashEqual; + ":" L C return tk::Colon; "," L C return tk::Comma; "?" L C return tk::Question; @@ -264,9 +256,9 @@ RegularExpressionStart_ {RegularExpressionBody}{RegularExpressionEnd_} "each" L C yylval->identifier_ = new(yyextra->pool_) CYIdentifier("each"); return tk::Each; -{IdentifierStart}{IdentifierPart}* yylval->identifier_ = new(yyextra->pool_) CYIdentifier(apr_pstrmemdup(yyextra->pool_, yytext, yyleng)); L C return tk::Identifier_; +{IdentifierStart}{IdentifierPart}* L C yylval->identifier_ = new(yyextra->pool_) CYIdentifier(apr_pstrmemdup(yyextra->pool_, yytext, yyleng)); return tk::Identifier_; -(\.[0-9]+|(0|[1-9][0-9]*)(\.[0-9]*)?){Exponent}? yylval->number_ = new(yyextra->pool_) CYNumber(strtod(yytext, NULL)); L C return tk::NumericLiteral; +(\.[0-9]+|(0|[1-9][0-9]*)(\.[0-9]*)?){Exponent}? L C yylval->number_ = new(yyextra->pool_) CYNumber(strtod(yytext, NULL)); return tk::NumericLiteral; 0[xX][0-9a-fA-F]+ L C yylval->number_ = new(yyextra->pool_) CYNumber(strtoull(yytext + 2, NULL, 16)); return tk::NumericLiteral; @@ -331,18 +323,12 @@ void CYDriver::ScannerDestroy() { cylex_destroy(scanner_); } -void CYDriver::SetCondition(Condition condition) { +void CYDriver::BeginCondition(Condition condition) { struct yyguts_t *yyg(reinterpret_cast(scanner_)); switch (condition) { - case RegExpSlash: - BEGIN(RegExpSlash); - break; - case RegExpSlashEqual: - BEGIN(RegExpSlashEqual); - break; - case RegExpSlashRight: - BEGIN(RegExpSlashRight); + case RegExpCondition: + BEGIN(RegExp); break; default: _assert(false); diff --git a/Cycript.y.in b/Cycript.y.in index 7918f27..e770815 100644 --- a/Cycript.y.in +++ b/Cycript.y.in @@ -289,7 +289,6 @@ int cylex(YYSTYPE *lvalp, cy::location *llocp, void *scanner); %type Arguments %type ArrayLiteral %type AssigneeExpression -%type AssigneeExpression_ %type AssigneeExpressionNoBF %type AssignmentExpression %type AssignmentExpression_ @@ -386,13 +385,12 @@ int cylex(YYSTYPE *lvalp, cy::location *llocp, void *scanner); %type PrimaryExpression %type PrimaryExpression_ %type PrimaryExpressionNoBF +%type PrimaryExpressionNoBF_ %type Program %type PropertyName %type PropertyNameAndValueList %type PropertyNameAndValueList_ %type PropertyNameAndValueListOpt -%type RegularExpressionLiteral_ -%type RegularExpressionToken %type RelationalExpression %type RelationalExpression_ %type RelationalExpressionNoBF @@ -402,6 +400,7 @@ int cylex(YYSTYPE *lvalp, cy::location *llocp, void *scanner); %type ShiftExpression %type ShiftExpressionNoBF %type SourceElement +%type SourceElement_ %type SourceElements %type Statement %type Statement_ @@ -410,6 +409,7 @@ int cylex(YYSTYPE *lvalp, cy::location *llocp, void *scanner); %type SwitchStatement %type ThrowStatement %type TryStatement +%type UnaryAssigneeExpression %type UnaryExpression %type UnaryExpression_ %type UnaryExpressionNoBF @@ -586,24 +586,12 @@ IdentifierOpt | { $$ = NULL; } ; -RegularExpressionToken - : "/" { $$ = CYDriver::RegExpSlash; } - | "/=" { $$ = CYDriver::RegExpSlashEqual; } -@begin E4X - | "/>" { $$ = CYDriver::RegExpSlashRight; } -@end - ; - -RegularExpressionLiteral_ - : RegularExpressionToken { driver.SetCondition($1); } RegularExpressionLiteral { $$ = $3; } - ; - Literal : NullLiteral { $$ = $1; } | BooleanLiteral { $$ = $1; } | NumericLiteral { $$ = $1; } | StringLiteral { $$ = $1; } - | RegularExpressionLiteral_ { $$ = $1; } + | RegularExpressionLiteral { $$ = $1; } ; NullLiteral @@ -617,6 +605,11 @@ BooleanLiteral /* 11.1 Primary Expressions {{{ */ PrimaryExpression_ + : ObjectLiteral { $$ = $1; } + | PrimaryExpressionNoBF_ { $$ = $1; } + ; + +PrimaryExpressionNoBF_ : "this" { $$ = $1; } | Identifier { $$ = new(driver.pool_) CYVariable($1); } | Literal { $$ = $1; } @@ -624,13 +617,16 @@ PrimaryExpression_ | "(" Expression ")" { $$ = $2; } ; +LexBeginRegExp + : { driver.BeginCondition(CYDriver::RegExpCondition); } + ; + PrimaryExpression - : ObjectLiteral { $$ = $1; } - | PrimaryExpression_ { $$ = $1; } + : LexBeginRegExp PrimaryExpression_ { $$ = $2; } ; PrimaryExpressionNoBF - : PrimaryExpression_ { $$ = $1; } + : PrimaryExpressionNoBF_ { $$ = $1; } ; /* }}} */ /* 11.1.4 Array Initialiser {{{ */ @@ -644,12 +640,12 @@ Element ElementOpt : Element { $$ = $1; } - | { $$ = NULL; } + | LexBeginRegExp { $$ = NULL; } ; ElementListOpt : ElementList { $$ = $1; } - | { $$ = NULL; } + | LexBeginRegExp { $$ = NULL; } ; ElementList @@ -697,7 +693,7 @@ MemberExpression : PrimaryExpression { $$ = $1; } | FunctionExpression { $$ = $1; } | MemberExpression MemberAccess { $2->SetLeft($1); $$ = $2; } - | MemberExpression_ { $$ = $1; } + | LexBeginRegExp MemberExpression_ { $$ = $2; } ; MemberExpressionNoBF @@ -712,7 +708,7 @@ NewExpression_ NewExpression : MemberExpression { $$ = $1; } - | NewExpression_ { $$ = $1; } + | LexBeginRegExp NewExpression_ { $$ = $2; } ; NewExpressionNoBF @@ -739,7 +735,7 @@ ArgumentList_ ArgumentListOpt : ArgumentList { $$ = $1; } - | { $$ = NULL; } + | LexBeginRegExp { $$ = NULL; } ; ArgumentList @@ -790,7 +786,7 @@ UnaryExpression_ UnaryExpression : PostfixExpression { $$ = $1; } - | UnaryExpression_ { $$ = $1; } + | LexBeginRegExp UnaryExpression_ { $$ = $2; } ; UnaryExpressionNoBF @@ -1006,12 +1002,12 @@ AssignmentExpression_ AssigneeExpression : LeftHandSideExpression { $$ = $1; } - | AssigneeExpression_ { $$ = $1; } + | LexBeginRegExp UnaryAssigneeExpression { $$ = $2; } ; AssigneeExpressionNoBF : LeftHandSideExpressionNoBF { $$ = $1; } - | AssigneeExpression_ { $$ = $1; } + | UnaryAssigneeExpression { $$ = $1; } ; AssignmentExpression @@ -1053,12 +1049,12 @@ ExpressionNoIn_ ExpressionOpt : Expression { $$ = $1; } - | { $$ = NULL; } + | LexBeginRegExp { $$ = NULL; } ; ExpressionNoInOpt : ExpressionNoIn { $$ = $1; } - | { $$ = NULL; } + | LexBeginRegExp { $$ = NULL; } ; Expression @@ -1093,7 +1089,7 @@ Statement_ ; Statement - : Statement_ { $$ = $1; } + : LexBeginRegExp Statement_ { $$ = $2; } ; /* }}} */ /* 12.1 Block {{{ */ @@ -1111,7 +1107,7 @@ StatementList StatementListOpt : StatementList { $$ = $1; } - | { $$ = NULL; } + | LexBeginRegExp { $$ = NULL; } ; /* }}} */ /* 12.2 Variable Statement {{{ */ @@ -1209,7 +1205,7 @@ ForStatement ForStatementInitialiser : ExpressionNoInOpt { $$ = $1; } - | "var" VariableDeclarationListNoIn { $$ = $2; } + | LexBeginRegExp "var" VariableDeclarationListNoIn { $$ = $3; } ; /* }}} */ /* 12.6.4 The for-in Statement {{{ */ @@ -1219,7 +1215,7 @@ ForInStatement ForInStatementInitialiser : LeftHandSideExpression { $$ = $1; } - | "var" VariableDeclarationNoIn { $$ = $2; } + | LexBeginRegExp "var" VariableDeclarationNoIn { $$ = $3; } ; /* }}} */ @@ -1299,7 +1295,7 @@ FunctionDeclaration ; FunctionExpression - : "function" IdentifierOpt "(" FormalParameterList ")" "{" FunctionBody "}" { $$ = new(driver.pool_) CYFunctionExpression($2, $4, $7); } + : LexBeginRegExp "function" IdentifierOpt "(" FormalParameterList ")" "{" FunctionBody "}" { $$ = new(driver.pool_) CYFunctionExpression($3, $5, $8); } ; FormalParameterList_ @@ -1323,19 +1319,24 @@ Program SourceElements : SourceElement SourceElements { $1->SetNext($2); $$ = $1; } - | { $$ = NULL; } + | LexBeginRegExp { $$ = NULL; } ; -SourceElement +SourceElement_ : Statement_ { $$ = $1; } | FunctionDeclaration { $$ = $1; } ; + +SourceElement + : LexBeginRegExp SourceElement_ { $$ = $2; } + ; /* }}} */ @begin ObjectiveC /* Cycript (Objective-C): @class Declaration {{{ */ ClassSuperOpt - : ":" MemberExpressionNoBF { $$ = $2; } + /* XXX: why the hell did I choose MemberExpressionNoBF? */ + : ":" LexBeginRegExp MemberExpressionNoBF { $$ = $3; } | { $$ = NULL; } ; @@ -1402,7 +1403,7 @@ CategoryStatement : "@class" ClassName ClassMessageDeclarationListOpt "@end" { $$ = new(driver.pool_) CYCategory($2, $3); } ; -PrimaryExpression +PrimaryExpression_ : ClassExpression { $$ = $1; } ; @@ -1433,7 +1434,7 @@ SelectorList MessageExpression : "[" AssignmentExpression SelectorList "]" { $$ = new(driver.pool_) CYSendDirect($2, $3); } - | "[" "super" SelectorList "]" { $$ = new(driver.pool_) CYSendSuper($3); } + | "[" LexBeginRegExp "super" SelectorList "]" { $$ = new(driver.pool_) CYSendSuper($4); } ; SelectorExpressionOpt @@ -1450,7 +1451,7 @@ SelectorExpression | Word { $$ = new(driver.pool_) CYSelectorPart($1, false, NULL); } ; -PrimaryExpression_ +PrimaryExpressionNoBF_ : MessageExpression { $$ = $1; } | "@selector" "(" SelectorExpression ")" { $$ = new(driver.pool_) CYSelector($3); } ; @@ -1459,7 +1460,7 @@ PrimaryExpression_ @begin C /* Cycript (C): Pointer Indirection/Addressing {{{ */ -AssigneeExpression_ +UnaryAssigneeExpression : "*" UnaryExpression { $$ = new(driver.pool_) CYIndirect($2); } ; @@ -1483,7 +1484,7 @@ XMLMarkup ; /* }}} */ /* 11.1 Primary Expressions {{{ */ -PrimaryExpression_ +PrimaryExpressionNoBF_ : PropertyIdentifier | XMLInitialiser | XMLListInitialiser @@ -1602,7 +1603,7 @@ ComprehensionList : ForComprehension ComprehensionListOpt { $1->SetNext($2); $$ = $1; } ; -PrimaryExpression_ +PrimaryExpressionNoBF_ : "[" AssignmentExpression ComprehensionList "]" { $$ = new(driver.pool_) CYArrayComprehension($2, $3); } ; /* }}} */ @@ -1616,13 +1617,13 @@ LetStatement : "let" "(" VariableDeclarationList ")" Block_ { $$ = new(driver.pool_) CYLet($3, $5); } ; -Statement +Statement_ : LetStatement ; *//* }}} */ /* JavaScript FTW: Function Statements {{{ */ Statement - : FunctionDeclaration { driver.Warning(yylloc, "warning, FunctionDeclaration is a SourceElement, not a Statement"); } { $$ = $1; } + : LexBeginRegExp FunctionDeclaration { driver.Warning(yylloc, "warning, FunctionDeclaration is a SourceElement, not a Statement"); } { $$ = $2; } ; /* }}} */ diff --git a/Parser.hpp b/Parser.hpp index 578b515..63aa17a 100644 --- a/Parser.hpp +++ b/Parser.hpp @@ -310,9 +310,7 @@ class CYDriver { bool strict_; enum Condition { - RegExpSlash, - RegExpSlashEqual, - RegExpSlashRight, + RegExpCondition, }; std::string filename_; @@ -336,7 +334,7 @@ class CYDriver { CYDriver(const std::string &filename); ~CYDriver(); - void SetCondition(Condition condition); + void BeginCondition(Condition condition); void Warning(const cy::location &location, const char *message); }; -- 2.45.2