Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
AstNode.cs
Go to the documentation of this file.
1 #region License
2 /* **********************************************************************************
3  * Copyright (c) Roman Ivantsov
4  * This source code is subject to terms and conditions of the MIT License
5  * for Irony. A copy of the license can be found in the License.txt file
6  * at the root of this distribution.
7  * By using this source code in any fashion, you are agreeing to be bound by the terms of the
8  * MIT License.
9  * You must not remove this notice from this software.
10  * **********************************************************************************/
11 #endregion
12 
13 using System;
14 using System.Collections.Generic;
15 using System.Text;
16 using System.CodeDom;
17 using System.Xml;
18 using System.IO;
19 using Irony.Parsing;
20 using Irony.Interpreter;
21 
22 namespace Irony.Interpreter.Ast {
23 
24  public delegate void NodeEvaluate(EvaluationContext context, AstMode mode);
25 
26  [Flags]
27  public enum AstNodeFlags {
28  None = 0x0,
29  IsTail = 0x01, //the node is in tail position
30  IsScope = 0x10, //node defines scope for local variables
31  }
32 
33  public class AstNodeList : List<AstNode> { }
34 
35  //Base AST node class
38 
39  public AstNode() {
40  EvaluateRef = this.EvaluateNode;
41  }
42 
43 
44  #region IAstNodeInit Members
45  public virtual void Init(ParsingContext context, ParseTreeNode treeNode) {
46  this.Term = treeNode.Term;
47  Span = treeNode.Span;
48  ErrorAnchor = this.Location;
49  treeNode.AstNode = this;
50  AsString = (Term == null ? this.GetType().Name : Term.Name);
51  }
52  #endregion
53 
54  #region IInterpretedAstNode Members
55  //Important: normally you don't need to override this method - you should override EvaluateNode instead
56  // You should have strong reasons to override it - for example, if you want to change
57  // exception handling implementation in base method. Otherwise, put all derived functionality
58  // in EvaluateNode, or create other method(s) and set reference to it in EvaluateRef
59  public virtual void Evaluate(EvaluationContext context, AstMode mode) {
60  try {
61  EvaluateRef(context, mode);
62  } catch (RuntimeException) {
63  throw;
64  } catch (Exception ex) {
65  throw new RuntimeException(ex.Message, ex, this.GetErrorAnchor());
66  }
67  }
68 
69  public virtual SourceLocation GetErrorAnchor() {
70  return ErrorAnchor;
71  }
72  #endregion
73 
74  public virtual void EvaluateNode(EvaluationContext context, AstMode mode) {
75  }
76 
77  #region IBrowsableAstNode Members
79  return ChildNodes;
80  }
81  public SourceLocation Location {
82  get { return Span.Location; }
83  }
84  #endregion
85 
86  #region properties: Parent, Term, Span, Caption, Role, Flags, ChildNodes, Attributes
87  public AstNode Parent;
88  public BnfTerm Term;
89  public SourceSpan Span;
91  //Used for pointing to error location. For most nodes it would be the location of the node itself.
92  // One exception is BinExprNode: when we get "Division by zero" error evaluating
93  // x = (5 + 3) / (2 - 2)
94  // it is better to point to "/" as error location, rather than the first "(" - which is the start
95  // location of binary expression.
97  // Role is a free-form string used as prefix in ToString() representation of the node.
98  // Node's parent can set it to "property name" or role of the child node in parent's node context.
99  public string Role;
100  // node.ToString() returns 'Role: AsString', which is used for showing node in AST tree.
101  public string AsString { get; protected set; }
102 
103  //List of child nodes
104  public readonly AstNodeList ChildNodes = new AstNodeList();
105 
106  #endregion
107 
108 
109  #region Utility methods: AddChild, SetParent, FlagIsSet ...
110  protected AstNode AddChild(string role, ParseTreeNode childParseNode) {
111  var child = (AstNode)childParseNode.AstNode;
112  if (child == null)
113  child = new NullNode(childParseNode.Term); //put a stub to throw an exception with clear message on attempt to evaluate.
114  child.Role = role;
115  child.SetParent(this);
116  ChildNodes.Add(child);
117  return child;
118  }
119 
120  public void SetParent(AstNode parent) {
121  Parent = parent;
122  }
123 
124  public bool FlagIsSet(AstNodeFlags flag) {
125  return (Flags & flag) != 0;
126  }
127  #endregion
128 
129 
130  public override string ToString() {
131  return string.IsNullOrEmpty(Role) ? AsString : Role + ": " + AsString;
132  }
133 
134  protected void InvalidAstMode(string mode) {
135  throw new Exception(string.Format(Resources.ErrInvalidAstMode, this.ToString(), mode));
136  }
137 
138  #region Visitors, Iterators
139  //the first primitive Visitor facility
140  public virtual void AcceptVisitor(IAstVisitor visitor) {
141  visitor.BeginVisit(this);
142  if (ChildNodes.Count > 0)
143  foreach(AstNode node in ChildNodes)
144  node.AcceptVisitor(visitor);
145  visitor.EndVisit(this);
146  }
147 
148  //Node traversal
150  AstNodeList result = new AstNodeList();
151  AddAll(result);
152  return result;
153  }
154  private void AddAll(AstNodeList list) {
155  list.Add(this);
156  foreach (AstNode child in this.ChildNodes)
157  if (child != null)
158  child.AddAll(list);
159  }
160  #endregion
161 
162  }//class
163 
164 }//namespace
virtual System.Collections.IEnumerable GetChildNodes()
Definition: AstNode.cs:78
static string ErrInvalidAstMode
Looks up a localized string similar to Invalid AstMode value in call to Evaluate method. Node: {0}, mode: {1}..
virtual void Init(ParsingContext context, ParseTreeNode treeNode)
Definition: AstNode.cs:45
bool FlagIsSet(AstNodeFlags flag)
Definition: AstNode.cs:124
A strongly-typed resource class, for looking up localized strings, etc.
virtual SourceLocation GetErrorAnchor()
Definition: AstNode.cs:69
AstNode AddChild(string role, ParseTreeNode childParseNode)
Definition: AstNode.cs:110
Flags
Enumeration of the new Assimp's flags.
virtual void EvaluateNode(EvaluationContext context, AstMode mode)
Definition: AstNode.cs:74
delegate void NodeEvaluate(EvaluationContext context, AstMode mode)
void InvalidAstMode(string mode)
Definition: AstNode.cs:134
override string ToString()
Definition: AstNode.cs:130
void SetParent(AstNode parent)
Definition: AstNode.cs:120
virtual void Evaluate(EvaluationContext context, AstMode mode)
Definition: AstNode.cs:59
NodeEvaluate EvaluateRef
Definition: AstNode.cs:37
virtual void AcceptVisitor(IAstVisitor visitor)
Definition: AstNode.cs:140
SourceLocation ErrorAnchor
Definition: AstNode.cs:96
IEnumerable< AstNode > GetAll()
Definition: AstNode.cs:149