From 2fad14e52c8cde8c45003a2ebb6907a57ca380e4 Mon Sep 17 00:00:00 2001 From: "Jay Freeman (saurik)" Date: Wed, 30 Dec 2015 09:51:01 -0800 Subject: [PATCH] Parse scope and symbol colon operators, from Ruby. --- Complete.cpp | 4 ++++ Driver.hpp | 3 ++- Output.cpp | 27 +++++++++++++++++++++++---- Parser.ypp.in | 41 ++++++++++++++++++++++++++++------------- Replace.cpp | 8 ++++++++ Scanner.lpp.in | 2 +- Syntax.hpp | 31 +++++++++++++++++++++++++++++++ 7 files changed, 97 insertions(+), 19 deletions(-) diff --git a/Complete.cpp b/Complete.cpp index 276ccaf..0cde80c 100644 --- a/Complete.cpp +++ b/Complete.cpp @@ -89,6 +89,10 @@ _visible char **CYComplete(const char *word, const std::string &line, CYUTF8Stri prefix << (*part)->word_ << ':'; } break; + case CYDriver::AutoResolve: + expression = $M(driver.context_, $S("$cyr")); + break; + default: _assert(false); } diff --git a/Driver.hpp b/Driver.hpp index adb03f2..9625ed8 100644 --- a/Driver.hpp +++ b/Driver.hpp @@ -112,7 +112,8 @@ class _visible CYDriver { AutoPrimary, AutoDirect, AutoIndirect, - AutoMessage + AutoMessage, + AutoResolve, } mode_; private: diff --git a/Output.cpp b/Output.cpp index a617408..c436b3c 100644 --- a/Output.cpp +++ b/Output.cpp @@ -326,7 +326,7 @@ void CYCondition::Output(CYOutput &out, CYFlags flags) const { test_->Output(out, Precedence() - 1, CYLeft(flags)); out << ' ' << '?' << ' '; if (true_ != NULL) - true_->Output(out, CYAssign::Precedence_, CYNoFlags); + true_->Output(out, CYAssign::Precedence_, CYNoColon); out << ' ' << ':' << ' '; false_->Output(out, CYAssign::Precedence_, CYRight(flags)); } @@ -340,10 +340,12 @@ void CYContinue::Output(CYOutput &out, CYFlags flags) const { void CYClause::Output(CYOutput &out) const { out << '\t'; - if (value_ != NULL) - out << "case" << ' ' << *value_; - else + if (value_ == NULL) out << "default"; + else { + out << "case" << ' '; + value_->Output(out, CYNoColon); + } out << ':' << '\n'; ++out.indent_; out << code_; @@ -854,6 +856,14 @@ void CYRegEx::Output(CYOutput &out, CYFlags flags) const { out << Value(); } +void CYResolveMember::Output(CYOutput &out, CYFlags flags) const { + object_->Output(out, Precedence(), CYLeft(flags)); + if (const char *word = property_->Word()) + out << "::" << word; + else + out << "::" << '[' << *property_ << ']'; +} + void CYReturn::Output(CYOutput &out, CYFlags flags) const { out << "return"; if (value_ != NULL) @@ -993,6 +1003,15 @@ void CYSwitch::Output(CYOutput &out, CYFlags flags) const { out << '\t' << '}'; } +void CYSymbol::Output(CYOutput &out, CYFlags flags) const { + bool protect((flags & CYNoColon) != 0); + if (protect) + out << '('; + out << ':' << name_; + if (protect) + out << ')'; +} + void CYThis::Output(CYOutput &out, CYFlags flags) const { out << "this"; } diff --git a/Parser.ypp.in b/Parser.ypp.in index ec17e71..5efee05 100644 --- a/Parser.ypp.in +++ b/Parser.ypp.in @@ -253,7 +253,6 @@ type; }) %token SlashRight "/>" %token LeftSlash " VariableStatement %type WithStatement %type Word +%type WordNoUnary @begin ObjectiveC %type WordOpt @end @@ -742,6 +743,9 @@ type; }) /* Token Priorities {{{ */ %nonassoc "if" %nonassoc "else" + +%nonassoc ":" +%nonassoc "!yield" /* }}} */ %start Program @@ -815,7 +819,7 @@ IdentifierName | "instanceof" { $$ = CYNew CYWord("instanceof"); } ; -Word +WordNoUnary : IdentifierNoOf[pass] { $$ = $pass; } | "break" { $$ = CYNew CYWord("break"); } | "case" { $$ = CYNew CYWord("case"); } @@ -826,7 +830,6 @@ Word | "continue" { $$ = CYNew CYWord("continue"); } | "debugger" { $$ = CYNew CYWord("debugger"); } | "default" { $$ = CYNew CYWord("default"); } - | "delete" { $$ = CYNew CYWord("delete"); } | "do" { $$ = CYNew CYWord("do"); } | "else" { $$ = CYNew CYWord("else"); } | "enum" { $$ = CYNew CYWord("enum"); } @@ -839,7 +842,6 @@ Word | "import" { $$ = CYNew CYWord("import"); } | "!in" { $$ = CYNew CYWord("in"); } | "!of" { $$ = CYNew CYWord("of"); } - | "new" { $$ = CYNew CYWord("new"); } | "null" { $$ = CYNew CYWord("null"); } | "return" { $$ = CYNew CYWord("return"); } | "super" { $$ = CYNew CYWord("super"); } @@ -848,11 +850,16 @@ Word | "throw" { $$ = CYNew CYWord("throw"); } | "true" { $$ = CYNew CYWord("true"); } | "try" { $$ = CYNew CYWord("try"); } - | "typeof" { $$ = CYNew CYWord("typeof"); } | "var" { $$ = CYNew CYWord("var"); } - | "void" { $$ = CYNew CYWord("void"); } | "while" { $$ = CYNew CYWord("while"); } | "with" { $$ = CYNew CYWord("with"); } + ; + +Word + : WordNoUnary[pass] { $$ = $pass; } + | "delete" { $$ = CYNew CYWord("delete"); } + | "typeof" { $$ = CYNew CYWord("typeof"); } + | "void" { $$ = CYNew CYWord("void"); } | "yield" { $$ = CYNew CYIdentifier("yield"); } ; @@ -1802,7 +1809,7 @@ GeneratorBody YieldExpression : "!yield" LexNewLineOrNot "\n" LexOf { CYNOT(@$); /* $$ = CYNew CYYieldValue(NULL); */ } - | "!yield" LexNewLineOrNot "" LexNoStar LexOf { CYNOT(@$); /* $$ = CYNew CYYieldValue(NULL); */ } + | "!yield" LexNewLineOrNot "" LexNoStar LexOf { CYNOT(@$); /* $$ = CYNew CYYieldValue(NULL); */ } %prec "!yield" | "!yield" LexNewLineOrNot "" LexNoStar AssignmentExpression[value] { CYNOT(@$); /* $$ = CYNew CYYieldValue($value); */ } | "!yield" LexNewLineOrNot "" LexNoStar LexOf "yield *" AssignmentExpression[generator] { CYNOT(@$); /* $$ = CYNew CYYieldGenerator($generator); */ } ; @@ -2645,16 +2652,12 @@ ComprehensionIf : "if" "(" AssignmentExpression[test] ")" { $$ = CYNew CYIfComprehension($test); } ; /* }}} */ -/* JavaScript FTW: Coalesce Operator {{{ */ -ConditionalExpression - : LogicalORExpression[test] "?" LexPushInOff LexOf ":" LexPopIn AssignmentExpression[false] { $$ = CYNew CYCondition($test, $test, $false); } - ; -/* }}} */ /* JavaScript FTW: Named Arguments {{{ */ ArgumentList - : LexOf Word[tag] ":" AssignmentExpression[value] ArgumentList_[next] { $$ = CYNew CYArgument($tag, $value, $next); } + : LexOf WordNoUnary[tag] ":" AssignmentExpression[value] ArgumentList_[next] { $$ = CYNew CYArgument($tag, $value, $next); } ; /* }}} */ + /* JavaScript FTW: Ruby Blocks {{{ */ RubyProcParameterList_ : "," RubyProcParameterList[parameters] { $$ = $parameters; } @@ -2694,6 +2697,18 @@ RubyBlockExpression | RubyBlockExpression_[pass] { $$ = $pass; } ; /* }}} */ +/* JavaScript FTW: Ruby Scopes {{{ */ +MemberAccess + : "::" "[" Expression[property] "]" { $$ = CYNew CYResolveMember(NULL, $property); } + | "::" IdentifierName[property] { $$ = CYNew CYResolveMember(NULL, CYNew CYString($property)); } + | "::" AutoComplete { driver.mode_ = CYDriver::AutoResolve; YYACCEPT; } + ; +/* }}} */ +/* JavaScript FTW: Ruby Symbols {{{ */ +PrimaryExpression + : ":" Word[name] { $$ = CYNew CYSymbol($name->Word()); } + ; +/* }}} */ %% diff --git a/Replace.cpp b/Replace.cpp index 719b3a5..ed486f7 100644 --- a/Replace.cpp +++ b/Replace.cpp @@ -878,6 +878,10 @@ void CYScript::Replace(CYContext &context) { } } +CYTarget *CYResolveMember::Replace(CYContext &context) { + return $M($M(object_, $S("$cyr")), property_); +} + CYStatement *CYReturn::Replace(CYContext &context) { if (context.nonlocal_ != NULL) { CYProperty *value(value_ == NULL ? NULL : $ CYPropertyValue($S("$cyv"), value_)); @@ -1122,6 +1126,10 @@ CYTarget *CYSuperCall::Replace(CYContext &context) { return $C($C1($M($V(context.super_), $S("bind")), $ CYThis()), arguments_); } +CYTarget *CYSymbol::Replace(CYContext &context) { + return $C1($M($V("Symbol"), $S("for")), $S(name_)); +} + CYStatement *CYSwitch::Replace(CYContext &context) { context.Replace(value_); clauses_->Replace(context); diff --git a/Scanner.lpp.in b/Scanner.lpp.in index edadb8f..b0def8d 100644 --- a/Scanner.lpp.in +++ b/Scanner.lpp.in @@ -340,7 +340,6 @@ XMLName {XMLNameStart}{XMLNamePart}* ".." L E("invalid operator") @begin E4X -"::" L F(tk::ColonColon, hi::Operator); ".." L F(tk::PeriodPeriod, hi::Operator); @end @@ -392,6 +391,7 @@ XMLName {XMLNameStart}{XMLNamePart}* "/=" L F(tk::SlashEqual, hi::Operator); ":" L F(tk::Colon, hi::Structure); +"::" L F(tk::ColonColon, hi::Structure); "," L F(tk::Comma, hi::Structure); "?" L F(tk::Question, hi::Structure); ";" L F(tk::SemiColon, hi::Structure); diff --git a/Syntax.hpp b/Syntax.hpp index abf05bc..a3f03fa 100644 --- a/Syntax.hpp +++ b/Syntax.hpp @@ -150,6 +150,7 @@ enum CYFlags { CYNoRightHand = (1 << 5), CYNoDangle = (1 << 6), CYNoInteger = (1 << 7), + CYNoColon = (1 << 8), CYNoBFC = (CYNoBrace | CYNoFunction | CYNoClass), }; @@ -962,6 +963,22 @@ struct CYVariable : virtual CYFunctionParameter *Parameter() const; }; +struct CYSymbol : + CYTarget +{ + const char *name_; + + CYSymbol(const char *name) : + name_(name) + { + } + + CYPrecedence(0) + + virtual CYTarget *Replace(CYContext &context); + virtual void Output(CYOutput &out, CYFlags flags) const; +}; + struct CYPrefix : CYExpression { @@ -1448,6 +1465,20 @@ struct CYIndirectMember : virtual void Output(CYOutput &out, CYFlags flags) const; }; +struct CYResolveMember : + CYMember +{ + CYResolveMember(CYExpression *object, CYExpression *property) : + CYMember(object, property) + { + } + + CYPrecedence(1) + + virtual CYTarget *Replace(CYContext &context); + virtual void Output(CYOutput &out, CYFlags flags) const; +}; + namespace cy { namespace Syntax { -- 2.45.2