]> git.saurik.com Git - cycript.git/blobdiff - Parser.hpp
Regular expressions cannot have alphabetic follow.
[cycript.git] / Parser.hpp
index 63270a18a331fe897d44a4d6bd13409e63939120..2c534cce50f150d70e6941625a4f8162b2275f29 100644 (file)
@@ -1,5 +1,5 @@
 /* Cycript - Optimizing JavaScript Compiler/Runtime
- * Copyright (C) 2009-2014  Jay Freeman (saurik)
+ * Copyright (C) 2009-2015  Jay Freeman (saurik)
 */
 
 /* GNU Affero General Public License, Version 3 {{{ */
@@ -33,6 +33,7 @@
 #include <cstdlib>
 
 #include "List.hpp"
+#include "Location.hpp"
 #include "Pooling.hpp"
 #include "Options.hpp"
 
@@ -145,16 +146,20 @@ _finline CYFlags CYCenter(CYFlags flags) {
 }
 
 struct CYStatement :
-    CYNext<CYStatement>
+    CYNext<CYStatement>,
+    CYThing
 {
     virtual ~CYStatement() {
     }
 
     void Single(CYOutput &out, CYFlags flags) const;
     void Multiple(CYOutput &out, CYFlags flags = CYNoFlags) const;
+    virtual void Output(CYOutput &out) const;
 
     virtual CYStatement *Replace(CYContext &context) = 0;
 
+    virtual CYStatement *Return();
+
   private:
     virtual void Output(CYOutput &out, CYFlags flags) const = 0;
 };
@@ -312,33 +317,26 @@ typedef std::vector<CYIdentifierUsage> CYIdentifierUsageVector;
 
 struct CYScope {
     bool transparent_;
-
-    CYContext &context_;
-    CYStatement *&statements_;
-
     CYScope *parent_;
 
     CYIdentifierAddressFlagsMap internal_;
     CYIdentifierValueSet identifiers_;
 
-    CYScope(bool transparent, CYContext &context, CYStatement *&statements);
-    virtual ~CYScope();
-
-    void Close();
+    CYScope(bool transparent, CYContext &context);
 
     void Declare(CYContext &context, CYIdentifier *identifier, CYIdentifierFlags flags);
     virtual CYIdentifier *Lookup(CYContext &context, CYIdentifier *identifier);
     void Merge(CYContext &context, CYIdentifier *identifier);
-    void Scope(CYContext &context, CYStatement *&statements);
+    void Close(CYContext &context, CYStatement *&statements);
 };
 
 struct CYProgram :
     CYThing
 {
-    CYStatement *statements_;
+    CYStatement *code_;
 
-    CYProgram(CYStatement *statements) :
-        statements_(statements)
+    CYProgram(CYStatement *code) :
+        code_(code)
     {
     }
 
@@ -374,14 +372,18 @@ struct CYContext {
     virtual ~CYContext() {
     }
 
-    template <typename Type_>
-    void ReplaceAll(Type_ *&values) {
-        Type_ **last(&values);
-        CYForEach (next, values) {
-            Replace(*last = next);
-            if (*last != NULL)
-                last = &(*last)->next_;
-        }
+    void ReplaceAll(CYStatement *&statement) {
+        if (statement == NULL)
+            return;
+        CYStatement *next(statement->next_);
+
+        Replace(statement);
+        ReplaceAll(next);
+
+        if (statement == NULL)
+            statement = next;
+        else
+            statement->SetNext(next);
     }
 
     template <typename Type_>
@@ -435,29 +437,20 @@ struct CYThisScope :
 };
 
 struct CYBlock :
-    CYStatement,
-    CYThing
+    CYStatement
 {
-    CYStatement *statements_;
+    CYStatement *code_;
 
-    CYBlock(CYStatement *statements) :
-        statements_(statements)
+    CYBlock(CYStatement *code) :
+        code_(code)
     {
     }
 
-    operator CYStatement *() const {
-        return statements_;
-    }
-
-    void AddPrev(CYStatement *statement) {
-        CYSetLast(statement) = statements_;
-        statements_ = statement;
-    }
-
     virtual CYStatement *Replace(CYContext &context);
 
-    virtual void Output(CYOutput &out) const;
     virtual void Output(CYOutput &out, CYFlags flags) const;
+
+    virtual CYStatement *Return();
 };
 
 struct CYForInitialiser {
@@ -481,6 +474,8 @@ struct CYForInInitialiser {
     virtual void Output(CYOutput &out, CYFlags flags) const = 0;
 };
 
+struct CYFunctionParameter;
+
 struct CYNumber;
 struct CYString;
 
@@ -515,6 +510,8 @@ struct CYExpression :
         return NULL;
     }
 
+    virtual CYFunctionParameter *Parameter() const;
+
     virtual CYNumber *Number(CYContext &context) {
         return NULL;
     }
@@ -550,13 +547,12 @@ struct CYCompound :
     CYExpression *expression_;
     CYExpression *next_;
 
-    CYCompound(CYExpression *expression, CYExpression *next = NULL) :
+    CYCompound(CYExpression *expression, CYExpression *next) :
         expression_(expression),
         next_(next)
     {
-        if (expression_ == NULL)
-            throw;
         _assert(expression_ != NULL);
+        _assert(next != NULL);
     }
 
     CYPrecedence(17)
@@ -564,7 +560,23 @@ struct CYCompound :
     virtual CYExpression *Replace(CYContext &context);
     void Output(CYOutput &out, CYFlags flags) const;
 
-    virtual CYExpression *Primitive(CYContext &context);
+    virtual CYFunctionParameter *Parameter() const;
+};
+
+struct CYParenthetical :
+    CYExpression
+{
+    CYExpression *expression_;
+
+    CYParenthetical(CYExpression *expression) :
+        expression_(expression)
+    {
+    }
+
+    CYPrecedence(0)
+
+    virtual CYExpression *Replace(CYContext &context);
+    void Output(CYOutput &out, CYFlags flags) const;
 };
 
 struct CYDeclaration;
@@ -581,7 +593,7 @@ struct CYFunctionParameter :
     {
     }
 
-    void Replace(CYContext &context, CYBlock &code);
+    void Replace(CYContext &context, CYStatement *&statements);
     void Output(CYOutput &out) const;
 };
 
@@ -910,6 +922,8 @@ struct CYVariable :
 
     virtual CYExpression *Replace(CYContext &context);
     virtual void Output(CYOutput &out, CYFlags flags) const;
+
+    virtual CYFunctionParameter *Parameter() const;
 };
 
 struct CYPrefix :
@@ -1035,11 +1049,11 @@ struct CYClause :
     CYNext<CYClause>
 {
     CYExpression *case_;
-    CYStatement *statements_;
+    CYStatement *code_;
 
-    CYClause(CYExpression *_case, CYStatement *statements) :
+    CYClause(CYExpression *_case, CYStatement *code) :
         case_(_case),
-        statements_(statements)
+        code_(code)
     {
     }
 
@@ -1132,7 +1146,7 @@ struct CYDeclarations :
 
     void Replace(CYContext &context);
 
-    CYCompound *Compound(CYContext &context);
+    CYExpression *Expression(CYContext &context);
     CYProperty *Property(CYContext &context);
     CYArgument *Argument(CYContext &context);
     CYFunctionParameter *Parameter(CYContext &context);
@@ -1151,7 +1165,7 @@ struct CYForDeclarations :
     {
     }
 
-    virtual CYCompound *Replace(CYContext &context);
+    virtual CYExpression *Replace(CYContext &context);
     virtual void Output(CYOutput &out, CYFlags flags) const;
 };
 
@@ -1389,6 +1403,8 @@ struct CYIf :
 
     virtual CYStatement *Replace(CYContext &context);
     virtual void Output(CYOutput &out, CYFlags flags) const;
+
+    virtual CYStatement *Return();
 };
 
 struct CYDoWhile :
@@ -1427,16 +1443,18 @@ struct CYWhile :
 struct CYFunction {
     CYIdentifier *name_;
     CYFunctionParameter *parameters_;
-    CYBlock code_;
+    CYStatement *code_;
 
     CYNonLocal *nonlocal_;
+    bool implicit_;
     CYThisScope this_;
 
-    CYFunction(CYIdentifier *name, CYFunctionParameter *parameters, CYStatement *statements) :
+    CYFunction(CYIdentifier *name, CYFunctionParameter *parameters, CYStatement *code) :
         name_(name),
         parameters_(parameters),
-        code_(statements),
-        nonlocal_(NULL)
+        code_(code),
+        nonlocal_(NULL),
+        implicit_(false)
     {
     }
 
@@ -1453,8 +1471,8 @@ struct CYFunctionExpression :
     CYFunction,
     CYExpression
 {
-    CYFunctionExpression(CYIdentifier *name, CYFunctionParameter *parameters, CYStatement *statements) :
-        CYFunction(name, parameters, statements)
+    CYFunctionExpression(CYIdentifier *name, CYFunctionParameter *parameters, CYStatement *code) :
+        CYFunction(name, parameters, code)
     {
     }
 
@@ -1470,8 +1488,8 @@ struct CYFatArrow :
     CYFunction,
     CYExpression
 {
-    CYFatArrow(CYFunctionParameter *parameters, CYStatement *statements) :
-        CYFunction(NULL, parameters, statements)
+    CYFatArrow(CYFunctionParameter *parameters, CYStatement *code) :
+        CYFunction(NULL, parameters, code)
     {
     }
 
@@ -1486,8 +1504,8 @@ struct CYFatArrow :
 struct CYRubyProc :
     CYFunctionExpression
 {
-    CYRubyProc(CYFunctionParameter *parameters, CYStatement *statements) :
-        CYFunctionExpression(NULL, parameters, statements)
+    CYRubyProc(CYFunctionParameter *parameters, CYStatement *code) :
+        CYFunctionExpression(NULL, parameters, code)
     {
     }
 
@@ -1500,8 +1518,8 @@ struct CYFunctionStatement :
     CYFunction,
     CYStatement
 {
-    CYFunctionStatement(CYIdentifier *name, CYFunctionParameter *parameters, CYStatement *statements) :
-        CYFunction(name, parameters, statements)
+    CYFunctionStatement(CYIdentifier *name, CYFunctionParameter *parameters, CYStatement *code) :
+        CYFunction(name, parameters, code)
     {
     }
 
@@ -1523,6 +1541,8 @@ struct CYExpress :
 
     virtual CYStatement *Replace(CYContext &context);
     virtual void Output(CYOutput &out, CYFlags flags) const;
+
+    virtual CYStatement *Return();
 };
 
 struct CYContinue :
@@ -1577,10 +1597,10 @@ struct CYEmpty :
 struct CYFinally :
     CYThing
 {
-    CYBlock code_;
+    CYStatement *code_;
 
-    CYFinally(CYStatement *statements) :
-        code_(statements)
+    CYFinally(CYStatement *code) :
+        code_(code)
     {
     }
 
@@ -1689,6 +1709,8 @@ struct CYTypeShort :
     virtual void Output(CYOutput &out) const;
 };
 
+struct CYTypeFunctionWith;
+
 struct CYTypeModifier :
     CYNext<CYTypeModifier>
 {
@@ -1704,6 +1726,8 @@ struct CYTypeModifier :
 
     virtual void Output(CYOutput &out, CYIdentifier *identifier) const = 0;
     void Output(CYOutput &out, int precedence, CYIdentifier *identifier) const;
+
+    virtual CYTypeFunctionWith *Function() { return NULL; }
 };
 
 struct CYTypeArrayOf :
@@ -1769,11 +1793,13 @@ struct CYTypedIdentifier :
     CYNext<CYTypedIdentifier>,
     CYThing
 {
+    CYLocation location_;
     CYIdentifier *identifier_;
     CYTypeSpecifier *specifier_;
     CYTypeModifier *modifier_;
 
-    CYTypedIdentifier(CYIdentifier *identifier = NULL) :
+    CYTypedIdentifier(const CYLocation &location, CYIdentifier *identifier = NULL) :
+        location_(location),
         identifier_(identifier),
         specifier_(NULL),
         modifier_(NULL)
@@ -1794,6 +1820,8 @@ struct CYTypedIdentifier :
 
     virtual CYExpression *Replace(CYContext &context);
     virtual void Output(CYOutput &out) const;
+
+    CYTypeFunctionWith *Function();
 };
 
 struct CYEncodedType :
@@ -1836,12 +1864,12 @@ struct CYLambda :
 {
     CYTypedIdentifier *typed_;
     CYTypedParameter *parameters_;
-    CYStatement *statements_;
+    CYStatement *code_;
 
-    CYLambda(CYTypedIdentifier *typed, CYTypedParameter *parameters, CYStatement *statements) :
+    CYLambda(CYTypedIdentifier *typed, CYTypedParameter *parameters, CYStatement *code) :
         typed_(typed),
         parameters_(parameters),
-        statements_(statements)
+        code_(code)
     {
     }
 
@@ -1943,6 +1971,8 @@ struct CYTypeFunctionWith :
 
     virtual CYExpression *Replace_(CYContext &context, CYExpression *type);
     virtual void Output(CYOutput &out, CYIdentifier *identifier) const;
+
+    virtual CYTypeFunctionWith *Function() { return this; }
 };
 
 namespace cy {
@@ -1952,11 +1982,11 @@ struct Catch :
     CYThing
 {
     CYIdentifier *name_;
-    CYBlock code_;
+    CYStatement *code_;
 
-    Catch(CYIdentifier *name, CYStatement *statements) :
+    Catch(CYIdentifier *name, CYStatement *code) :
         name_(name),
-        code_(statements)
+        code_(code)
     {
     }
 
@@ -1967,12 +1997,12 @@ struct Catch :
 struct Try :
     CYStatement
 {
-    CYBlock code_;
+    CYStatement *code_;
     Catch *catch_;
     CYFinally *finally_;
 
-    Try(CYStatement *statements, Catch *_catch, CYFinally *finally) :
-        code_(statements),
+    Try(CYStatement *code, Catch *_catch, CYFinally *finally) :
+        code_(code),
         catch_(_catch),
         finally_(finally)
     {