Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
ParseTree.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.Linq;
16 using System.Text;
17 
18 namespace Irony.Parsing {
19 
20 
21  /*
22  A node for a parse tree (concrete syntax tree) - an initial syntax representation produced by parser.
23  It contains all syntax elements of the input text, each element represented by a generic node ParseTreeNode.
24  The parse tree is converted into abstract syntax tree (AST) which contains custom nodes. The conversion might
25  happen on-the-fly: as parser creates the parse tree nodes it can create the AST nodes and puts them into AstNode field.
26  Alternatively it might happen as a separate step, after completing the parse tree.
27  AST node might optinally implement IAstNodeInit interface, so Irony parser can initialize the node providing it
28  with all relevant information.
29  The ParseTreeNode also works as a stack element in the parser stack, so it has the State property to carry
30  the pushed parser state while it is in the stack.
31  */
32  public class ParseTreeNode {
33  public object AstNode;
34  public Token Token;
35  public BnfTerm Term;
36  public int Precedence;
38  public SourceSpan Span;
40  //Making ChildNodes property (not field) following request by Matt K, Bill H
41  public ParseTreeNodeList ChildNodes {get; private set;}
42  public bool IsError;
43  internal ParserState State; //used by parser to store current state when node is pushed into the parser stack
44  public object Tag; //for use by custom parsers, Irony does not use it
45 
46  private ParseTreeNode(){
47  ChildNodes = new ParseTreeNodeList();
48  }
49  public ParseTreeNode(BnfTerm term) : this() {
50  Term = term;
51  }
52 
53  public ParseTreeNode(Token token) : this() {
54  Token = token;
55  Term = token.Terminal;
56  Precedence = Term.Precedence;
58  Span = new SourceSpan(token.Location, token.Length);
59  IsError = token.IsError();
60  }
61 
62  public ParseTreeNode(ParserState initialState) : this() {
63  State = initialState;
64  }
65 
66  public ParseTreeNode(Production reduceProduction, SourceSpan span) : this(){
67  ReduceProduction = reduceProduction;
68  Span = span;
69  Term = ReduceProduction.LValue;
70  Precedence = Term.Precedence;
71  }
72 
73  public ParseTreeNode(object node, BnfTerm term, int precedence, Associativity associativity, SourceSpan span)
74  : this() {
75  AstNode = node;
76  Term = term;
77  Precedence = precedence;
78  Associativity = associativity;
79  }
80 
81  public override string ToString() {
82  if (Term == null)
83  return "(S0)"; //initial state node
84  else
85  return Term.GetParseNodeCaption(this);
86  }//method
87 
88  public string FindTokenAndGetText() {
89  var tkn = FindToken();
90  return tkn == null ? null : tkn.Text;
91  }
92  public Token FindToken() {
93  return FindFirstChildTokenRec(this);
94  }
95  private static Token FindFirstChildTokenRec(ParseTreeNode node) {
96  if (node.Token != null) return node.Token;
97  foreach (var child in node.ChildNodes) {
98  var tkn = FindFirstChildTokenRec(child);
99  if (tkn != null) return tkn;
100  }
101  return null;
102  }
103  public ParseTreeNode FirstChild {
104  get { return ChildNodes[0]; }
105  }
106  public ParseTreeNode LastChild {
107  get { return ChildNodes[ChildNodes.Count -1]; }
108  }
109 
110  }//class
111 
112  public class ParseTreeNodeList : List<ParseTreeNode> { }
113 
114  public enum ParseTreeStatus {
115  Parsing,
116  Partial,
117  Parsed,
118  Error,
119  }
120 
121  public class ParseTree {
122  public ParseTreeStatus Status {get; internal set;}
123  public readonly string SourceText;
124  public readonly string FileName;
125  public readonly TokenList Tokens = new TokenList();
126  public readonly TokenList OpenBraces = new TokenList();
128  public readonly ParserMessageList ParserMessages = new ParserMessageList();
129  public int ParseTime;
130 
131  public ParseTree(string sourceText, string fileName) {
132  SourceText = sourceText;
133  FileName = fileName;
134  Status = ParseTreeStatus.Parsing;
135  }
136 
137  public bool HasErrors() {
138  if (ParserMessages.Count == 0) return false;
139  foreach (var err in ParserMessages)
140  if (err.Level == ParserErrorLevel.Error) return true;
141  return false;
142  }//method
143 
144  public void CopyMessages(ParserMessageList others, SourceLocation baseLocation, string messagePrefix) {
145  foreach(var other in others)
146  this.ParserMessages.Add(new ParserMessage(other.Level, baseLocation + other.Location, messagePrefix + other.Message, other.ParserState));
147  }//
148 
149  }//class
150 
151 }
ParseTreeNode(ParserState initialState)
Definition: ParseTree.cs:62
ParseTreeNode(BnfTerm term)
Definition: ParseTree.cs:49
readonly string SourceText
Definition: ParseTree.cs:123
ParseTree(string sourceText, string fileName)
Definition: ParseTree.cs:131
ParseTreeNode Root
Definition: ParseTree.cs:127
Currently ignored by Parser, may be used in the future to set specific precedence value of the follow...
readonly SourceLocation Location
Location in the source code.
Definition: Token.cs:116
A List of tokens.
Definition: Token.cs:60
ParseTreeNode(object node, BnfTerm term, int precedence, Associativity associativity, SourceSpan span)
Definition: ParseTree.cs:73
override string ToString()
Definition: ParseTree.cs:81
ParseTreeNodeList ChildNodes
Definition: ParseTree.cs:41
Associativity Associativity
Definition: ParseTree.cs:37
ParseTreeNode(Token token)
Definition: ParseTree.cs:53
SiliconStudio.Shaders.Ast.SourceSpan SourceSpan
Definition: Node.cs:8
Tokens are produced by scanner and fed to parser, optionally passing through Token filters in between...
Definition: Token.cs:74
ParseTreeNode(Production reduceProduction, SourceSpan span)
Definition: ParseTree.cs:66
readonly string FileName
Definition: ParseTree.cs:124
Tokens
Summary Canonical example of MPLEX automaton
void CopyMessages(ParserMessageList others, SourceLocation baseLocation, string messagePrefix)
Definition: ParseTree.cs:144
int Length
Gets the length.
Definition: Token.cs:131