From 8f56307d1b60c56cc3525ec3bb51716bd503ca2f Mon Sep 17 00:00:00 2001 From: "Jay Freeman (saurik)" Date: Sat, 28 Nov 2015 02:54:35 -0800 Subject: [PATCH] Cleanly separate words and keywords using grammar. --- Cycript.l.in | 82 ++++++++++++------------- Cycript.yy.in | 162 +++++++++++++++++++++++++------------------------- Output.cpp | 4 +- Parser.hpp | 24 -------- 4 files changed, 124 insertions(+), 148 deletions(-) diff --git a/Cycript.l.in b/Cycript.l.in index 064d43a..935189c 100644 --- a/Cycript.l.in +++ b/Cycript.l.in @@ -392,47 +392,47 @@ XMLName {XMLNameStart}{XMLNamePart}* "undefined" L C I(identifier, Identifier("undefined"), tk::Identifier_, hi::Operator); -"false" L C I(false, False(), tk::False, hi::Constant); -"null" L C I(null, Null(), tk::Null, hi::Constant); -"true" L C I(true, True(), tk::True, hi::Constant); - -"auto" L C I(word, Word("auto"), tk::Auto, hi::Meta); -"break" L R I(word, Word("break"), tk::Break, hi::Control); -"case" L C I(word, Word("case"), tk::Case, hi::Control); -"catch" L C I(word, Word("catch"), tk::Catch, hi::Control); -"continue" L R I(word, Word("continue"), tk::Continue, hi::Control); -"default" L C I(word, Word("default"), tk::Default, hi::Control); -"delete" L C I(word, Word("delete"), tk::Delete, hi::Operator); -"do" L C I(word, Word("do"), tk::Do, hi::Control); -"else" L C I(word, Word("else"), tk::Else, hi::Control); -"finally" L C I(word, Word("finally"), tk::Finally, hi::Control); -"for" L C I(word, Word("for"), tk::For, hi::Control); -"function" L C I(word, Word("function"), yyextra->no_.Function ? tk::Function_ : tk::Function, hi::Meta); -"if" L C I(word, Word("if"), tk::If, hi::Control); -"in" L C I(word, Word("in"), yyextra->in_.top() ? tk::In_ : tk::In, hi::Operator); -"instanceof" L C I(word, Word("instanceof"), tk::InstanceOf, hi::Operator); -"new" L C I(word, Word("new"), tk::New, hi::Operator); -"return" L R I(word, Word("return"), tk::Return, hi::Control); -"switch" L C I(word, Word("switch"), tk::Switch, hi::Control); -"this" L C I(this, This(), tk::This, hi::Constant); -"throw" L R I(word, Word("throw"), tk::Throw, hi::Control); -"try" L C I(word, Word("try"), tk::Try, hi::Control); -"typeof" L C I(word, Word("typeof"), tk::TypeOf, hi::Operator); -"var" L C I(word, Word("var"), tk::Var, hi::Meta); -"void" L C I(word, Word("void"), tk::Void, hi::Operator); -"while" L C I(word, Word("while"), tk::While, hi::Control); -"with" L C I(word, Word("with"), tk::With, hi::Control); - -"debugger" L C I(word, Word("debugger"), tk::Debugger, hi::Meta); - -"const" L C I(word, Word("const"), tk::Const, hi::Meta); - -"class" L C I(word, Word("class"), tk::Class, hi::Meta); -"enum" L C I(word, Word("enum"), tk::Enum, hi::Meta); -"export" L C I(word, Word("export"), tk::Export, hi::Meta); -"extends" L C I(word, Word("extends"), tk::Extends, hi::Meta); -"import" L C I(word, Word("import"), tk::Import, hi::Meta); -"super" L C I(word, Word("super"), tk::Super, hi::Constant); +"false" L C F(tk::False, hi::Constant); +"null" L C F(tk::Null, hi::Constant); +"true" L C F(tk::True, hi::Constant); + +"auto" L C F(tk::Auto, hi::Meta); +"break" L R F(tk::Break, hi::Control); +"case" L C F(tk::Case, hi::Control); +"catch" L C F(tk::Catch, hi::Control); +"continue" L R F(tk::Continue, hi::Control); +"default" L C F(tk::Default, hi::Control); +"delete" L C F(tk::Delete, hi::Operator); +"do" L C F(tk::Do, hi::Control); +"else" L C F(tk::Else, hi::Control); +"finally" L C F(tk::Finally, hi::Control); +"for" L C F(tk::For, hi::Control); +"function" L C F(yyextra->no_.Function ? tk::Function_ : tk::Function, hi::Meta); +"if" L C F(tk::If, hi::Control); +"in" L C F(yyextra->in_.top() ? tk::In_ : tk::In, hi::Operator); +"instanceof" L C F(tk::InstanceOf, hi::Operator); +"new" L C F(tk::New, hi::Operator); +"return" L R F(tk::Return, hi::Control); +"switch" L C F(tk::Switch, hi::Control); +"this" L C F(tk::This, hi::Constant); +"throw" L R F(tk::Throw, hi::Control); +"try" L C F(tk::Try, hi::Control); +"typeof" L C F(tk::TypeOf, hi::Operator); +"var" L C F(tk::Var, hi::Meta); +"void" L C F(tk::Void, hi::Operator); +"while" L C F(tk::While, hi::Control); +"with" L C F(tk::With, hi::Control); + +"debugger" L C F(tk::Debugger, hi::Meta); + +"const" L C F(tk::Const, hi::Meta); + +"class" L C F(tk::Class, hi::Meta); +"enum" L C F(tk::Enum, hi::Meta); +"export" L C F(tk::Export, hi::Meta); +"extends" L C F(tk::Extends, hi::Meta); +"import" L C F(tk::Import, hi::Meta); +"super" L C F(tk::Super, hi::Constant); "implements" L C I(identifier, Identifier("implements"), tk::Implements, hi::Meta); "interface" L C I(identifier, Identifier("interface"), tk::Interface, hi::Meta); diff --git a/Cycript.yy.in b/Cycript.yy.in index 29e9438..bc47e7e 100644 --- a/Cycript.yy.in +++ b/Cycript.yy.in @@ -260,53 +260,53 @@ int cylex(YYSTYPE *, CYLocation *, void *); %token No "NO" @end -%token False "false" -%token Null "null" -%token True "true" +%token False "false" +%token Null "null" +%token True "true" // ES3/ES5/WIE/JSC Reserved -%token Auto "auto" -%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 Finally "finally" -%token For "for" -%token Function "function" -%token Function_ ";function" -%token If "if" -%token In "in" -%token In_ "!in" -%token InstanceOf "instanceof" -%token New "new" -%token Return "return" -%token Switch "switch" -%token This "this" -%token Throw "throw" -%token Try "try" -%token TypeOf "typeof" -%token Var "var" -%token Void "void" -%token While "while" -%token With "with" +%token Auto "auto" +%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 Finally "finally" +%token For "for" +%token Function "function" +%token Function_ ";function" +%token If "if" +%token In "in" +%token In_ "!in" +%token InstanceOf "instanceof" +%token New "new" +%token Return "return" +%token Switch "switch" +%token This "this" +%token Throw "throw" +%token Try "try" +%token TypeOf "typeof" +%token Var "var" +%token Void "void" +%token While "while" +%token With "with" // ES3/IE6 Future, ES5/JSC Reserved -%token Debugger "debugger" +%token Debugger "debugger" // ES3/ES5/IE6 Future, JSC Reserved -%token Const "const" +%token Const "const" // ES3/ES5/IE6/JSC Future -%token Class "class" -%token Enum "enum" -%token Export "export" -%token Extends "extends" -%token Import "import" -%token Super "super" +%token Class "class" +%token Enum "enum" +%token Export "export" +%token Extends "extends" +%token Import "import" +%token Super "super" // ES3 Future, ES5 Strict Future %token Implements "implements" @@ -621,48 +621,48 @@ NewLineOpt Word : Identifier { $$ = $1; } - | "auto" { $$ = $1; } - | "break" NewLineOpt { $$ = $1; } - | "case" { $$ = $1; } - | "catch" { $$ = $1; } - | "class" { $$ = $1; } - | "const" { $$ = $1; } - | "continue" NewLineOpt { $$ = $1; } - | "debugger" { $$ = $1; } - | "default" { $$ = $1; } - | "delete" LexSetRegExp { $$ = $1; } - | "do" { $$ = $1; } - | "else" { $$ = $1; } - | "enum" { $$ = $1; } - | "export" { $$ = $1; } - | "extends" { $$ = $1; } - | "false" { $$ = $1; } - | "finally" { $$ = $1; } - /* XXX: | "for" { $$ = $1; } */ - | "function" { $$ = $1; } - | "if" { $$ = $1; } - | "import" { $$ = $1; } - /* XXX: | "in" { $$ = $1; } */ - | "!in" { $$ = $1; } - /* XXX: | "instanceof" { $$ = $1; } */ + | "auto" { $$ = CYNew CYWord("auto"); } + | "break" NewLineOpt { $$ = CYNew CYWord("break"); } + | "case" { $$ = CYNew CYWord("case"); } + | "catch" { $$ = CYNew CYWord("catch"); } + | "class" { $$ = CYNew CYWord("class"); } + | "const" { $$ = CYNew CYWord("const"); } + | "continue" NewLineOpt { $$ = CYNew CYWord("continue"); } + | "debugger" { $$ = CYNew CYWord("debugger"); } + | "default" { $$ = CYNew CYWord("default"); } + | "delete" LexSetRegExp { $$ = CYNew CYWord("delete"); } + | "do" { $$ = CYNew CYWord("do"); } + | "else" { $$ = CYNew CYWord("else"); } + | "enum" { $$ = CYNew CYWord("enum"); } + | "export" { $$ = CYNew CYWord("export"); } + | "extends" { $$ = CYNew CYWord("extends"); } + | "false" { $$ = CYNew CYWord("false"); } + | "finally" { $$ = CYNew CYWord("finally"); } + /* XXX: | "for" { $$ = CYNew CYWord("for"); } */ + | "function" { $$ = CYNew CYWord("function"); } + | "if" { $$ = CYNew CYWord("if"); } + | "import" { $$ = CYNew CYWord("import"); } + /* XXX: | "in" { $$ = CYNew CYWord("in"); } */ + | "!in" { $$ = CYNew CYWord("in"); } + /* XXX: | "instanceof" { $$ = CYNew CYWord("instanceof"); } */ // XXX: as it currently is not an Identifier | "let" { $$ = $1; } - | "new" LexSetRegExp { $$ = $1; } - | "null" { $$ = $1; } - | "return" NewLineOpt { $$ = $1; } - | "super" { $$ = $1; } - | "switch" { $$ = $1; } - | "this" { $$ = $1; } - | "throw" NewLineOpt { $$ = $1; } - | "true" { $$ = $1; } - | "try" { $$ = $1; } - | "typeof" LexSetRegExp { $$ = $1; } - | "var" { $$ = $1; } - | "void" LexSetRegExp { $$ = $1; } - | "while" { $$ = $1; } - | "with" { $$ = $1; } + | "new" LexSetRegExp { $$ = CYNew CYWord("new"); } + | "null" { $$ = CYNew CYWord("null"); } + | "return" NewLineOpt { $$ = CYNew CYWord("return"); } + | "super" { $$ = CYNew CYWord("super"); } + | "switch" { $$ = CYNew CYWord("switch"); } + | "this" { $$ = CYNew CYWord("this"); } + | "throw" NewLineOpt { $$ = CYNew CYWord("throw"); } + | "true" { $$ = CYNew CYWord("true"); } + | "try" { $$ = CYNew CYWord("try"); } + | "typeof" LexSetRegExp { $$ = CYNew CYWord("typeof"); } + | "var" { $$ = CYNew CYWord("var"); } + | "void" LexSetRegExp { $$ = CYNew CYWord("void"); } + | "while" { $$ = CYNew CYWord("while"); } + | "with" { $$ = CYNew CYWord("with"); } ; @begin ObjectiveC @@ -744,13 +744,13 @@ ValueLiteral /* }}} */ /* 7.8.1 Null Literals {{{ */ NullLiteral - : "null" { $$ = $1; } + : "null" { $$ = CYNew CYNull(); } ; /* }}} */ /* 7.8.2 Boolean Literals {{{ */ BooleanLiteral - : "true" { $$ = $1; } - | "false" { $$ = $1; } + : "true" { $$ = CYNew CYTrue(); } + | "false" { $$ = CYNew CYFalse(); } ; /* }}} */ @@ -780,7 +780,7 @@ Variable ; PrimaryExpression - : "this" { $$ = $1; } + : "this" { $$ = CYNew CYThis(); } | Variable { $$ = $1; } | Literal { $$ = $1; } | ArrayInitialiser { $$ = $1; } diff --git a/Output.cpp b/Output.cpp index a26e3e7..c06f757 100644 --- a/Output.cpp +++ b/Output.cpp @@ -610,7 +610,7 @@ void New::Output(CYOutput &out, CYFlags flags) const { } } void CYNull::Output(CYOutput &out, CYFlags flags) const { - CYWord::Output(out); + out << "null"; } void CYNumber::Output(CYOutput &out, CYFlags flags) const { @@ -784,7 +784,7 @@ void CYSwitch::Output(CYOutput &out, CYFlags flags) const { } void CYThis::Output(CYOutput &out, CYFlags flags) const { - CYWord::Output(out); + out << "this"; } namespace cy { diff --git a/Parser.hpp b/Parser.hpp index ac335b5..73a69c6 100644 --- a/Parser.hpp +++ b/Parser.hpp @@ -853,14 +853,8 @@ struct CYRegEx : }; struct CYNull : - CYWord, CYTrivial { - CYNull() : - CYWord("null") - { - } - virtual CYNumber *Number(CYContext &context); virtual CYString *String(CYContext &context); @@ -868,14 +862,8 @@ struct CYNull : }; struct CYThis : - CYWord, CYMagic { - CYThis() : - CYWord("this") - { - } - virtual CYExpression *Replace(CYContext &context); virtual void Output(CYOutput &out, CYFlags flags) const; }; @@ -888,14 +876,8 @@ struct CYBoolean : }; struct CYFalse : - CYWord, CYBoolean { - CYFalse() : - CYWord("false") - { - } - virtual bool Value() const { return false; } @@ -905,14 +887,8 @@ struct CYFalse : }; struct CYTrue : - CYWord, CYBoolean { - CYTrue() : - CYWord("true") - { - } - virtual bool Value() const { return true; } -- 2.45.2