Exemplo n.º 1
0
 private void AddAll(AstNodeList list)
 {
     list.Add(this);
     foreach (AstNode child in this.ChildNodes)
     {
         child.AddAll(list);
     }
 }
Exemplo n.º 2
0
 public void AddChild(AstNode child)
 {
     if (child == null)
     {
         return;
     }
     child.Parent = this;
     ChildNodes.Add(child);
 }
Exemplo n.º 3
0
 public void AddChild(string role, AstNode child)
 {
     if (child == null)
     {
         return;
     }
     child.Role = role;
     ChildNodes.Add(child);
 }
Exemplo n.º 4
0
 public virtual AstNode FoldConstants()
 {
     var childrenCopy = new AstNodeList();
     foreach (var child in ChildNodes)
     {
         var nChild = (child as CompilableNode).FoldConstants();
         if (nChild != null) childrenCopy.Add(nChild);
     }
     ChildNodes.Clear();
     ChildNodes.AddRange(childrenCopy);
     return this;
 }
Exemplo n.º 5
0
        protected AstNode AddChild(string role, ParseTreeNode childParseNode)
        {
            var child = (AstNode)childParseNode.AstNode;

            if (child == null)
            {
                child = new NullNode(childParseNode.Term); //put a stub to throw an exception with clear message on attempt to evaluate.
            }
            child.Role = role;
            child.SetParent(this);
            ChildNodes.Add(child);
            return(child);
        }
Exemplo n.º 6
0
        private void ExecuteReduceAction(ActionRecord action)
        {
            ParserState oldState = _currentState;
            int         popCnt   = action.PopCount;

            //Get new node's child nodes - these are nodes being popped from the stack
            AstNodeList childNodes = new AstNodeList();

            for (int i = 0; i < action.PopCount; i++)
            {
                AstNode child = Stack[Stack.Count - popCnt + i].Node;
                if (!child.Term.IsSet(TermOptions.IsPunctuation))
                {
                    childNodes.Add(child);
                }
            }
            //recover state, location and pop the stack
            SourceSpan newNodeSpan;

            if (popCnt == 0)
            {
                newNodeSpan = new SourceSpan(_currentToken.Location, 0);
            }
            else
            {
                SourceLocation firstPopLoc   = Stack[Stack.Count - popCnt].Node.Location;
                int            lastPopEndPos = Stack[Stack.Count - 1].Node.Span.EndPos;
                newNodeSpan   = new SourceSpan(firstPopLoc, lastPopEndPos - firstPopLoc.Position);
                _currentState = Stack[Stack.Count - popCnt].State;
                Stack.Pop(popCnt);
            }
            //Create new node
            AstNode node = CreateNode(action, newNodeSpan, childNodes);

            // Push node/current state into the stack
            Stack.Push(node, _currentState);
            //switch to new state
            ActionRecord gotoAction;

            if (_currentState.Actions.TryGetValue(action.NonTerminal.Key, out gotoAction))
            {
                _currentState = gotoAction.NewState;
            }
            else
            {
                //should never happen
                throw new CompilerException(string.Format("Cannot find transition for input {0}; state: {1}, popped state: {2}",
                                                          action.NonTerminal, oldState, _currentState));
            }
        }//method
Exemplo n.º 7
0
        private void ExecuteReduceAction(ActionRecord action)
        {
            ParserState oldState = _currentState;
            int         popCnt   = action.PopCount;

            AstNodeList childNodes = new AstNodeList();

            for (int i = 0; i < action.PopCount; i++)
            {
                AstNode child = _stack[_stack.Count - popCnt + i].Node;
                if (!child.Term.IsSet(TermOptions.IsPunctuation))
                {
                    childNodes.Add(child);
                }
            }

            SourceSpan newNodeSpan;

            if (popCnt == 0)
            {
                newNodeSpan = new SourceSpan(_currentToken.Location, 0);
            }
            else
            {
                SourceLocation firstPopLoc   = _stack[_stack.Count - popCnt].Node.Location;
                int            lastPopEndPos = _stack[_stack.Count - 1].Node.Span.EndPos;
                newNodeSpan   = new SourceSpan(firstPopLoc, lastPopEndPos - firstPopLoc.Position);
                _currentState = _stack[_stack.Count - popCnt].State;
                _stack.Pop(popCnt);
            }

            AstNode node = CreateNode(action, newNodeSpan, childNodes);

            _stack.Push(node, _currentState);

            ActionRecord gotoAction;

            if (_currentState.Actions.TryGetValue(action.NonTerminal.Key, out gotoAction))
            {
                _currentState = gotoAction.NewState;
            }
            else
            {
                throw new CompilerException(string.Format("Cannot find transition for input {0}; state: {1}, popped state: {2}",
                                                          action.NonTerminal, oldState, _currentState));
            }
        }
Exemplo n.º 8
0
        private void ExecuteReduceAction(ActionRecord action)
        {
            ParserState oldState = _currentState;
              int popCnt = action.PopCount;

              //Get new node's child nodes - these are nodes being popped from the stack
              AstNodeList childNodes = new AstNodeList();
              for (int i = 0; i < action.PopCount; i++) {
            AstNode child = Stack[Stack.Count - popCnt + i].Node;
            if (!child.Term.IsSet(TermOptions.IsPunctuation))
              childNodes.Add(child);
              }
              //recover state, location and pop the stack
              SourceSpan newNodeSpan;
              if (popCnt == 0) {
            newNodeSpan = new SourceSpan(_currentToken.Location, 0);
              } else {
            SourceLocation firstPopLoc = Stack[Stack.Count - popCnt].Node.Location;
            int lastPopEndPos = Stack[Stack.Count - 1].Node.Span.EndPos;
            newNodeSpan = new SourceSpan(firstPopLoc, lastPopEndPos - firstPopLoc.Position);
            _currentState = Stack[Stack.Count - popCnt].State;
            Stack.Pop(popCnt);
              }
              //Create new node
              AstNode node = CreateNode(action, newNodeSpan, childNodes);
              // Push node/current state into the stack
              Stack.Push(node, _currentState);
              //switch to new state
              ActionRecord gotoAction;
              if (_currentState.Actions.TryGetValue(action.NonTerminal.Key, out gotoAction)) {
            _currentState = gotoAction.NewState;
              } else
            //should never happen
            throw new CompilerException( string.Format("Cannot find transition for input {0}; state: {1}, popped state: {2}",
              action.NonTerminal, oldState, _currentState));
        }
Exemplo n.º 9
0
    private void ExecuteReduceAction(ActionRecord action)
    {
      ParserState oldState = _currentState;
      int popCnt = action.PopCount;

      AstNodeList childNodes = new AstNodeList();
      for (int i = 0; i < action.PopCount; i++)
      {
        AstNode child = _stack[_stack.Count - popCnt + i].Node;
        if (!child.Term.IsSet(TermOptions.IsPunctuation))
          childNodes.Add(child);
      }

      SourceSpan newNodeSpan;
      if (popCnt == 0)
      {
        newNodeSpan = new SourceSpan(_currentToken.Location, 0);
      }
      else
      {
        SourceLocation firstPopLoc = _stack[_stack.Count - popCnt].Node.Location;
        int lastPopEndPos = _stack[_stack.Count - 1].Node.Span.EndPos;
        newNodeSpan = new SourceSpan(firstPopLoc, lastPopEndPos - firstPopLoc.Position);
        _currentState = _stack[_stack.Count - popCnt].State;
        _stack.Pop(popCnt);
      }
      
      AstNode node = CreateNode(action, newNodeSpan, childNodes);
      _stack.Push(node, _currentState);

      ActionRecord gotoAction;
      if (_currentState.Actions.TryGetValue(action.NonTerminal.Key, out gotoAction))
      {
        _currentState = gotoAction.NewState;
      }
      else
        throw new CompilerException(string.Format("Cannot find transition for input {0}; state: {1}, popped state: {2}",
              action.NonTerminal, oldState, _currentState));
    }