public ScriptFuncContract(AstNodeArgs args)
     : base(args)
 {
     pre = ChildNodes[0] as ScriptFuncContractPre;
       post = ChildNodes[1] as ScriptFuncContractPost;
       inv = ChildNodes[2] as ScriptFuncContractInv;
 }
        public ScriptTypeConvertExpr(AstNodeArgs args)
            : base(args)
        {
            if (ChildNodes.Count == 2)
              {
            if (args.ChildNodes[0] is ScriptExpr &&
            !(args.ChildNodes[1] is ScriptExpr))
            {
              // ( Expr )
              expr = args.ChildNodes[0] as ScriptExpr;
            }
            else
            {
              //(Type) Expr
              typeExpr = args.ChildNodes[0] as ScriptExpr;
              expr = args.ChildNodes[1] as ScriptExpr;
            }
              }
              else
              {
            throw new ScriptException("Grammar error!");
              }

              @operator = RuntimeHost.GetBinaryOperator("+");
              if (@operator == null)
            throw new ScriptException("RuntimeHost did not initialize property. Can't find binary operators.");
        }
        public ScriptFunctionDefinition(AstNodeArgs args)
            : base(args)
        {
            Token funcName = ChildNodes[1] as Token;
              int index = 0;

              if (funcName != null)
              {
            Name = funcName.Text;
              }
              else
              //Function expression
              {
            Name = null;
            index = 1;
              }

              if (ChildNodes.Count == 5-index)
              {
            Contract = ChildNodes[3 - index] as ScriptFuncContract;
            Parameters = ChildNodes[3 - index] as ScriptFuncParameters;
              }

              if (ChildNodes.Count == 6 - index)
              {
            Parameters = ChildNodes[2 - index] as ScriptFuncParameters;
            GlobalList = ChildNodes[3 - index] as ScriptGlobalList;
            Contract = ChildNodes[4 - index] as ScriptFuncContract;
              }

              Body = (ScriptAst)ChildNodes[ChildNodes.Count - 1];
        }
 public ScriptForEachStatement(AstNodeArgs args)
     : base(args)
 {
     name = (Token)ChildNodes[1];
       expr = (ScriptExpr)ChildNodes[3];
       statement = (ScriptStatement)ChildNodes[4];
 }
        public ScriptFuncParameters(AstNodeArgs args)
            : base(args)
        {
            if (ChildNodes.Count == 1)
              {
            for (int index = 0; index < ChildNodes[0].ChildNodes.Count; index++)
            {
              AstNode astNode = ChildNodes[0].ChildNodes[index];
              Identifiers.Add((astNode as Token).Text);
            }
              }

              //if (ChildNodes[0] is Token)
              //{
              //  Identifiers.Add((ChildNodes[0] as Token).Text);
              //}
              //else
              //{
              //  for (int index = 0; index < ChildNodes[0].ChildNodes.Count; index++)
              //  {
              //    AstNode astNode = ChildNodes[0].ChildNodes[index];
              //    Identifiers.Add((astNode as Token).Text);
              //  }
              //}
        }
 public ScriptForStatement(AstNodeArgs args)
     : base(args)
 {
     init = (ScriptAst)args.ChildNodes[1];
       cond = (ScriptAst)args.ChildNodes[2];
       next = (ScriptAst)args.ChildNodes[3];
       statement = (ScriptStatement)args.ChildNodes[4];
 }
Example #7
0
 protected internal AstNode InvokeNodeCreator(AstNodeArgs args)
 {
     if (NodeCreator == null)
     {
         return(null);
     }
     return(NodeCreator(args));
 }
 public ScriptTryCatchFinallyStatement(AstNodeArgs args)
     : base(args)
 {
     tryBlock = ChildNodes[1] as ScriptStatement;
       expName = (ChildNodes[3] as Token).Text;
       catchBlock = ChildNodes[4] as ScriptStatement;
       finallyBlock = ChildNodes[6] as ScriptStatement;
 }
Example #9
0
        public static Token CreateMultiToken(Terminal term, CompilerContext context, TokenList tokens)
        {
            SourceSpan  span  = new SourceSpan();
            AstNodeArgs args  = new AstNodeArgs(term, context, span, null);
            Token       token = new Token(args);

            token.ChildNodes.AddRange(tokens.ToArray());
            return(token);
        }
        public ScriptFlowControlStatement(AstNodeArgs args)
            : base(args)
        {
            Token oper = ChildNodes[0] as Token;
              operation = oper.Text;
              Debug.Assert(oper.Text == "return" || oper.Text == "break" || oper.Text == "continue" || oper.Text == "throw");

              if (operation == "return" || operation == "throw")
            expression = (ScriptExpr)ChildNodes[1];
        }
        public ScriptConstExpr(AstNodeArgs args)
            : base(args)
        {
            Token cons = (Token)ChildNodes[0];
              Value = cons.Value;

              if (Value.Equals("true")) Value = true;
              if (Value.Equals("false")) Value = false;
              if (Value.Equals("null")) Value = null;
        }
Example #12
0
        public static Token Create(Terminal term, CompilerContext context, SourceLocation location, string text, object value)
        {
            int         textLen = text == null ? 0 : text.Length;
            SourceSpan  span    = new SourceSpan(location, textLen);
            AstNodeArgs args    = new AstNodeArgs(term, context, span, null);
            Token       token   = new Token(args);

            token.Text  = text;
            token.Value = value;
            return(token);
        }
Example #13
0
        public ScriptBinExpr(AstNodeArgs args)
            : base(args)
        {
            left = (ScriptExpr)ChildNodes[0];
              oper = ((Token)ChildNodes[1]).Text;
              right = (ScriptExpr)ChildNodes[2];

              operatorFunction = RuntimeHost.GetBinaryOperator(oper);
              if (operatorFunction == null)
            throw new ScriptException("RuntimeHost did not initialize property. Can't find binary operators.");
        }
 public GlobalFunctionExpr(AstNodeArgs args)
     : base(args)
 {
     this.FunctionName = (Token)args.ChildNodes[0].DepthFirstTraversal().OfType<Token>().Single();
     AstNodeList funcArgs = args.ChildNodes[2].ChildNodes;
     this.InputParameter1 = (ExpressionNode)funcArgs[0];
     if (funcArgs.Count > 1)
         this.InputParameter2 = (ExpressionNode)funcArgs[1];
     if (funcArgs.Count > 2)
         this.InputParameter3 = (ExpressionNode)funcArgs[2];
 }
 public ScriptIfStatement(AstNodeArgs args)
     : base(args)
 {
     condition = (ScriptCondition) ChildNodes[1];
       statement = (ScriptStatement)ChildNodes[2];
       //Else exists
       if (ChildNodes.Count == 4 && ChildNodes[3].ChildNodes.Count == 2 && ChildNodes[3].ChildNodes[1] is ScriptStatement)
       {
     elseStatement = (ScriptStatement)ChildNodes[3].ChildNodes[1];
       }
 }
 public ScriptSwitchStatement(AstNodeArgs args)
     : base(args)
 {
     cases = new List<ScriptSwitchCaseStatement>();
       foreach (ScriptSwitchCaseStatement caseStatement in ChildNodes[0].ChildNodes)
       {
     cases.Add(caseStatement);
       }
       if (ChildNodes.Count == 2)
     defaultCase = ChildNodes[1] as ScriptSwitchDefaultStatement;
 }
Example #17
0
 public IfElseStmtNode(AstNodeArgs args)
     : base(args)
 {
     _condition = (IJSBasicNode)args.ChildNodes[1];
     _thenExpression = (IJSBasicNode)args.ChildNodes[3];
     //Child #4 is ELSE_CLAUSE
     AstNode elseClause = args.ChildNodes[4];
     if (elseClause.ChildNodes.Count > 0)
     {
         _elseExpression = (IJSBasicNode)elseClause.ChildNodes[1];
     }
 }
Example #18
0
        public ScriptMetaExpr(AstNodeArgs args)
            : base(args)
        {
            AstNodeArgs progArgs = new AstNodeArgs();
              progArgs.ChildNodes = new AstNodeList();
              progArgs.ChildNodes.Add(ChildNodes[1]);
              progArgs.Span = args.Span;
              progArgs.Term = args.Term;

              metaProg = new ScriptProg(progArgs);
              metaProg.Parent = this;
        }
        public ScriptQualifiedName(AstNodeArgs args)
            : base(args)
        {
            if (ChildNodes.Count == 2 && ChildNodes[1].ChildNodes.Count == 0)
              {
            Identifier = ((Token)ChildNodes[0]).Text;

            evaluation = EvaluateIdentifier;
            assignment = AssignIdentifier;
              }
              else
            if (ChildNodes[0] is Token && ChildNodes[1].ChildNodes.Count != 0)
            {
              Identifier = (ChildNodes[0] as Token).Text;

              //NOTE: There might be two cases:
              //      1) a()[]...()
              //      2) a<>.(NamePart)
              Modifiers = new List<ScriptAst>();
              foreach (ScriptAst node in ChildNodes[1].ChildNodes)
            Modifiers.Add(node);

              ScriptGenericsPostfix generic = Modifiers.FirstOrDefault() as ScriptGenericsPostfix;
              if (generic != null && Modifiers.Count == 1)
              {
            //Case 2
            evaluation = EvaluateGenericType;
            assignment = null;
              }
              else
              {
            //Case 1
            evaluation = EvaluateFunctionCall;
            assignment = AssignArray;
              }
            }
            else
            {
              NamePart = ChildNodes[0] as ScriptQualifiedName;
              Identifier = ((Token)ChildNodes[2]).Text;
              if (ChildNodes.Count == 4 && ChildNodes[3].ChildNodes.Count != 0)
              {
            Modifiers = new List<ScriptAst>();
            foreach (ScriptAst node in ChildNodes[3].ChildNodes)
            {
              Modifiers.Add(node);
            }
              }
              evaluation = EvaluateNamePart;
              assignment = AssignNamePart;
            }
        }
Example #20
0
 public LineNode(AstNodeArgs args)
     : base(args)
 {
     LineTypes = LineTypes.InternalLine; // overwritten later by JavaScriptGenerator
     LineNumber = (int)((Token)args.ChildNodes[0]).Value;
     if (args.ChildNodes.Count > 2)
     {
         StatementList = (GenericJsBasicNode)args.ChildNodes[1];
     }
     else
     {
         StatementList = new GenericJsBasicNode(args); //empty node
     }
 }
 public ScriptUnaryExpr(AstNodeArgs args)
     : base(args)
 {
   if (ChildNodes[0] is ScriptExpr)
   { 
     expr = (ScriptExpr)ChildNodes[0];
     oper = ((Token)ChildNodes[1]).Text;
   }
   else
   {
     expr = (ScriptExpr)ChildNodes[1];
     oper = ((Token)ChildNodes[0]).Text;
   }
 }
Example #22
0
 public ForStmtNode(AstNodeArgs args)
     : base(args)
 {
     _assignment = (AssignStmtNode)args.ChildNodes[1];
     _upperBound = (ExpressionNode)args.ChildNodes[3];
     if (args.ChildNodes.Count == 6)
     {
         _step = int.Parse(((Token)args.ChildNodes[5]).Text, CultureInfo.InvariantCulture);
     }
     else
     {
         _step = 1;
     }
 }
Example #23
0
 public RemStmtNode(AstNodeArgs args)
     : base(args)
 {
     Token token = (Token)args.ChildNodes[0];
     string text = token.Text;
     if (text.Length < 5)
     {
         _comment = "no comment";
     }
     else
     {
         _comment = text.Substring(4);
     }
 }
Example #24
0
 public AstNode(AstNodeArgs args)
 {
     Term = args.Term;
     Span = args.Span;
     if (args.ChildNodes == null || args.ChildNodes.Count == 0)
     {
         return;
     }
     //add child nodes, skipping nulls and punctuation symbols
     foreach (AstNode child in args.ChildNodes)
     {
         if (child != null && !child.Term.IsSet(TermOptions.IsPunctuation))
         {
             AddChild(child);
         }
     }//foreach
 }
Example #25
0
        public BranchStmtNode(AstNodeArgs args)
            : base(args)
        {
            Token command = (Token)args.ChildNodes[0];

            if (args.ChildNodes.Count == 1)
            {
                BranchType = BranchType.Return;
            }
            else
            {
                Token line = (Token)args.ChildNodes[1];

                BranchType = (command.Text.ToLowerInvariant().Equals("goto") ? BranchType.Goto : BranchType.Gosub);
                DestinationLine = int.Parse(line.Text, CultureInfo.InvariantCulture);
            }
        }
Example #26
0
        public ScriptMObject(AstNodeArgs args)
            : base(args)
        {
            objectParts = new List<ScriptMObjectPart>();

              if (args.ChildNodes[0] is ScriptMObjectPart)
              {
            ScriptMObjectPart part = (ScriptMObjectPart)args.ChildNodes[0];
            objectParts.Add(part);
              }
              else
              {
            foreach (ScriptMObjectPart part in args.ChildNodes[0].ChildNodes)
            {
              objectParts.Add(part);
            }
              }
        }
Example #27
0
 public ScriptTypeExpr(AstNodeArgs args)
     : base(args)
 {
     if (ChildNodes.Count == 2 && ChildNodes[1].ChildNodes.Count == 0)
       {
     Identifier = ((Token)ChildNodes[0]).Text;
       }
       else
     if (ChildNodes[0] is ScriptTypeExpr)
     {
       TypeExpr = ChildNodes[0] as ScriptTypeExpr;
       Identifier = (ChildNodes[2].ChildNodes[0] as Token).Text;
       GenericsPostfix = ChildNodes[2].ChildNodes[1] as ScriptGenericsPostfix;
     }
     else
     {
       GenericsPostfix = (ScriptGenericsPostfix)ChildNodes[1];
       Identifier = GenericsPostfix.GetGenericTypeName(((Token)ChildNodes[0]).Text);
     }
 }
Example #28
0
        public ScriptAssignExpr(AstNodeArgs args)
            : base(args)
        {
            nameExpr = (ScriptQualifiedName)args.ChildNodes[0];
              oper = ((Token)args.ChildNodes[1]).Text;
              if (args.ChildNodes.Count == 3)
            rightExpr = (ScriptExpr)args.ChildNodes[2];

              Debug.Assert(oper == "=" || oper == ":=" || oper == "+=" || oper == "-=" || oper == "++" || oper == "--" || oper == ":=");

              switch (oper)
              {
            case "=":
              assignOperation = Assign;
              break;
            case ":=":
              assignOperation = AssignEx;
              break;
            case "++":
              assignOperation = PlusPlus;
              break;
            case "--":
              assignOperation = MinusMinus;
              break;
            case "+=":
              assignOperation = PlusEqual;
              break;
            case "-=":
              assignOperation = MinusEqual;
              break;
            default:
              throw new ScriptException("Assignment operator:" + oper + " is not supported");
              }

              minus = RuntimeHost.GetBinaryOperator("-");
              plus = RuntimeHost.GetBinaryOperator("+");

              if (plus == null || minus == null)
            throw new ScriptException("RuntimeHost did not initialize property. Can't find binary operators.");
        }
Example #29
0
 protected Token(AstNodeArgs args)  : base(args)
 {
 }
    public ScriptCompoundStatement(AstNodeArgs args)
      : base(args)
    {

    }
Example #31
0
        }//method

        private AstNode CreateNode(ActionRecord reduceAction, SourceSpan sourceSpan, AstNodeList childNodes)
        {
            NonTerminal nt = reduceAction.NonTerminal;
            AstNode     result;

            AstNodeArgs args = new AstNodeArgs(nt, _context, sourceSpan, childNodes);

            result = nt.InvokeNodeCreator(args);
            if (result != null)
            {
                return(result);
            }

            Type defaultNodeType = _context.Compiler.Grammar.DefaultNodeType;
            Type ntNodeType      = nt.NodeType ?? defaultNodeType ?? typeof(AstNode);

            // Check if NonTerminal is a list
            // List nodes are produced by .Plus() or .Star() methods of BnfElement
            // In this case, we have a left-recursive list formation production:
            //     ntList -> ntList + delim? + ntElem
            //  We check if we have already created the list node for ntList (in the first child);
            //  if yes, we use this child as a result directly, without creating new list node.
            //  The other incoming child - the last one - is a new list member;
            // we simply add it to child list of the result ntList node. Optional "delim" node is simply thrown away.
            bool isList = nt.IsSet(TermOptions.IsList);

            if (isList && childNodes.Count > 1 && childNodes[0].Term == nt)
            {
                result = childNodes[0];
                AstNode newChild = childNodes[childNodes.Count - 1];
                newChild.Parent = result;
                result.ChildNodes.Add(newChild);
                return(result);
            }
            //Check for StarList produced by MakeStarList; in this case the production is:  ntList -> Empty | Elem+
            // where Elem+ is non-empty list of elements. The child list we are actually interested in is one-level lower
            if (nt.IsSet(TermOptions.IsStarList) && childNodes.Count == 1)
            {
                childNodes = childNodes[0].ChildNodes;
            }
            // Check for "node-bubbling" case. For identity productions like
            //   A -> B
            // the child node B is usually a subclass of node A,
            // so child node B can be used directly in place of the A. So we simply return child node as a result.
            // TODO: probably need a grammar option to enable/disable this behavior explicitly
            if (!isList && !nt.IsSet(TermOptions.IsPunctuation) && childNodes.Count == 1)
            {
                Type childNodeType = childNodes[0].Term.NodeType ?? defaultNodeType ?? typeof(AstNode);
                if (childNodeType == ntNodeType || childNodeType.IsSubclassOf(ntNodeType))
                {
                    return(childNodes[0]);
                }
            }
            // Try using Grammar's CreateNode method
            result = Data.Grammar.CreateNode(_context, reduceAction, sourceSpan, childNodes);
            if (result == null)
            {
                //Finally create node directly. For perf reasons we try using "new" for AstNode type (faster), and
                // activator for all custom types (slower)
                if (ntNodeType == typeof(AstNode))
                {
                    result = new AstNode(args);
                }
                else
#if PocketPC || SILVERLIGHT
                {
                    ConstructorInfo ctor = ntNodeType.GetConstructor(new Type[] { typeof(AstNodeArgs) });
                    if (ctor == null)
                    {
                        throw new Exception("Failed to located constructor: " + ntNodeType.ToString() + "(AstNodeArgs args)");
                    }
                    result = (AstNode)ctor.Invoke(new object[] { args });
                }
#else
                {
                    result = (AstNode)Activator.CreateInstance(ntNodeType, args);
                }
#endif
            }
            if (result != null)
            {
                nt.OnNodeCreated(result);
            }
            return(result);
        }
 public ScriptNewArrStmt(AstNodeArgs args)
     : base(args)
 {
     constrExpr = ChildNodes[1] as ScriptTypeExpr;
       arrRank = ChildNodes[2] as ScriptArrayResolution;
 }
 public ScriptStatement(AstNodeArgs args)
     : base(args)
 {
 }
 public ScriptFuncContractPost(AstNodeArgs args)
   : base(args)
 {
   list = ChildNodes[1] as ScriptExprList;
 }
Example #35
0
 public ScriptExprList(AstNodeArgs args)
     : base(args)
 {
   exprList = (ChildNodes[0] is ScriptExpr) ? ChildNodes : ChildNodes[0].ChildNodes;
 }
Example #36
0
        private AstNode CreateNode(ActionRecord reduceAction, SourceSpan sourceSpan, AstNodeList childNodes)
        {
            NonTerminal nt = reduceAction.NonTerminal;
              AstNode result;

              AstNodeArgs args = new AstNodeArgs(nt, _context, sourceSpan, childNodes);
              result = nt.InvokeNodeCreator(args);
              if (result != null) return result;

              Type defaultNodeType = _context.Compiler.Grammar.DefaultNodeType;
              Type ntNodeType = nt.NodeType ?? defaultNodeType ?? typeof(AstNode);

              // Check if NonTerminal is a list
              // List nodes are produced by .Plus() or .Star() methods of BnfElement
              // In this case, we have a left-recursive list formation production:
              //     ntList -> ntList + delim? + ntElem
              //  We check if we have already created the list node for ntList (in the first child);
              //  if yes, we use this child as a result directly, without creating new list node.
              //  The other incoming child - the last one - is a new list member;
              // we simply add it to child list of the result ntList node. Optional "delim" node is simply thrown away.
              bool isList = nt.IsSet(TermOptions.IsList);
              if (isList && childNodes.Count > 1 && childNodes[0].Term == nt) {
            result = childNodes[0];
            AstNode newChild = childNodes[childNodes.Count - 1];
            newChild.Parent = result;
            result.ChildNodes.Add(newChild);
            return result;
              }
              //Check for StarList produced by MakeStarList; in this case the production is:  ntList -> Empty | Elem+
              // where Elem+ is non-empty list of elements. The child list we are actually interested in is one-level lower
              if (nt.IsSet(TermOptions.IsStarList) && childNodes.Count == 1) {
            childNodes = childNodes[0].ChildNodes;
              }
              // Check for "node-bubbling" case. For identity productions like
              //   A -> B
              // the child node B is usually a subclass of node A,
              // so child node B can be used directly in place of the A. So we simply return child node as a result.
              // TODO: probably need a grammar option to enable/disable this behavior explicitly
              if (!isList && !nt.IsSet(TermOptions.IsPunctuation) && childNodes.Count == 1) {
            Type childNodeType = childNodes[0].Term.NodeType ?? defaultNodeType ?? typeof(AstNode);
            if (childNodeType == ntNodeType || childNodeType.IsSubclassOf(ntNodeType))
              return childNodes[0];
              }
              // Try using Grammar's CreateNode method
              result = Data.Grammar.CreateNode(_context, reduceAction, sourceSpan, childNodes);
              if (result == null) {
            //Finally create node directly. For perf reasons we try using "new" for AstNode type (faster), and
            // activator for all custom types (slower)
            if (ntNodeType == typeof(AstNode))
              result = new AstNode(args);
            else
            #if PocketPC || SILVERLIGHT
            {
              ConstructorInfo ctor = ntNodeType.GetConstructor(new Type[] { typeof(AstNodeArgs) });
              if (ctor == null)
            throw new Exception("Failed to located constructor: " + ntNodeType.ToString() + "(AstNodeArgs args)");
              result = (AstNode)ctor.Invoke(new object[] { args });
            }
            #else
            {
              result = (AstNode)Activator.CreateInstance(ntNodeType, args);
            }
            #endif
              }
              if (result != null)
            nt.OnNodeCreated(result);
              return result;
        }