Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
Parser.cs
Go to the documentation of this file.
1 #region License
2 
3 /* **********************************************************************************
4  * Copyright (c) Roman Ivantsov
5  * This source code is subject to terms and conditions of the MIT License
6  * for Irony. A copy of the license can be found in the License.txt file
7  * at the root of this distribution.
8  * By using this source code in any fashion, you are agreeing to be bound by the terms of the
9  * MIT License.
10  * You must not remove this notice from this software.
11  * **********************************************************************************/
12 #endregion
13 
14 using System;
15 
16 namespace Irony.Parsing
17 {
18  // Parser class represents combination of scanner and LALR parser (CoreParser)
19  /// <summary>
20  /// </summary>
21  public class Parser
22  {
23  #region Constants and Fields
24 
25  /// <summary>
26  /// </summary>
27  public readonly CoreParser CoreParser;
28 
29  /// <summary>
30  /// </summary>
31  public readonly LanguageData Language;
32 
33  /// <summary>
34  /// </summary>
35  public readonly NonTerminal Root;
36 
37  /// <summary>
38  /// </summary>
39  public readonly Scanner Scanner;
40 
41  internal readonly ParserState InitialState;
42 
43  #endregion
44 
45  #region Constructors and Destructors
46 
47  /// <summary>
48  /// </summary>
49  /// <param name="grammar">
50  /// </param>
51  public Parser(Grammar grammar)
52  : this(new LanguageData(grammar))
53  {
54  }
55 
56  /// <summary>
57  /// </summary>
58  /// <param name="language">
59  /// </param>
60  public Parser(LanguageData language)
61  : this(language, null, null)
62  {
63  }
64 
65  /// <summary>
66  /// Initializes a new instance of the <see cref="Parser"/> class.
67  /// </summary>
68  /// <param name="language">The language.</param>
69  /// <param name="scanner">The scanner.</param>
70  /// <param name="root">The root.</param>
71  /// <exception cref="Exception">
72  /// </exception>
73  public Parser(LanguageData language, Scanner scanner, NonTerminal root)
74  {
75  Language = language;
76  Context = new ParsingContext(this);
77  Scanner = scanner ?? language.CreateScanner();
78 
79  if (Scanner != null)
80  {
81  Scanner.Initialize(this);
82  }
83  else
84  {
85  Language.Errors.Add(GrammarErrorLevel.Error, null, "Scanner is not initialized for this grammar");
86  }
87  CoreParser = new CoreParser(this);
88  Root = root;
89  if (Root == null)
90  {
91  Root = Language.Grammar.Root;
92  InitialState = Language.ParserData.InitialState;
93  }
94  else
95  {
96  if (Root != Language.Grammar.Root && !Language.Grammar.SnippetRoots.Contains(Root))
97  {
98  throw new Exception(string.Format(Resources.ErrRootNotRegistered, root.Name));
99  }
100 
101  InitialState = Language.ParserData.InitialStates[Root];
102  }
103  }
104 
105  #endregion
106 
107  #region Public Properties
108 
109  /// <summary>
110  /// </summary>
111  public ParsingContext Context { get; internal set; }
112 
113  #endregion
114 
115  #region Public Methods
116 
117  /// <summary>
118  /// </summary>
119  /// <param name="sourceText">
120  /// </param>
121  /// <returns>
122  /// </returns>
123  public ParseTree Parse(string sourceText)
124  {
125  return Parse(sourceText, "<Source>");
126  }
127 
128  /// <summary>
129  /// </summary>
130  /// <param name="sourceText">
131  /// </param>
132  /// <param name="fileName">
133  /// </param>
134  /// <returns>
135  /// </returns>
136  public ParseTree Parse(string sourceText, string fileName)
137  {
138  if (Context.Status != ParserStatus.AcceptedPartial)
139  {
140  Reset();
141  }
142 
143  // TODO Set SourceStream on Scanner
144  // Context.SourceStream.SetText(sourceText, 0, Context.Status == ParserStatus.AcceptedPartial);
145  Scanner.SetSourceText(sourceText, fileName);
146 
147  Context.CurrentParseTree = new ParseTree(sourceText, fileName);
148  Context.Status = ParserStatus.Parsing;
149  int start = Environment.TickCount;
150  CoreParser.Parse();
151  Context.CurrentParseTree.ParseTime = Environment.TickCount - start;
152  UpdateParseTreeStatus();
153  return Context.CurrentParseTree;
154  }
155 
156  /// <summary>
157  /// </summary>
158  /// <param name="sourceText">
159  /// </param>
160  /// <param name="fileName">
161  /// </param>
162  /// <returns>
163  /// </returns>
164  public ParseTree ScanOnly(string sourceText, string fileName)
165  {
166  Context.CurrentParseTree = new ParseTree(sourceText, fileName);
167  // TODO Set SourceStream on Scanner
168  // Context.SourceStream.SetText(sourceText, 0, false);
169  while (true)
170  {
171  var token = Scanner.GetToken();
172  if (token == null || token.Terminal == Language.Grammar.Eof)
173  {
174  break;
175  }
176  }
177 
178  return Context.CurrentParseTree;
179  }
180 
181  #endregion
182 
183  #region Methods
184 
185  internal void Reset()
186  {
187  Context.Reset();
188  CoreParser.Reset();
189  Scanner.Reset();
190  }
191 
192  private void UpdateParseTreeStatus()
193  {
194  var parseTree = Context.CurrentParseTree;
195  if (parseTree.ParserMessages.Count > 0)
196  {
197  parseTree.ParserMessages.Sort(ParserMessageList.ByLocation);
198  }
199 
200  if (parseTree.HasErrors())
201  {
202  parseTree.Status = ParseTreeStatus.Error;
203  }
204  else if (Context.Status == ParserStatus.AcceptedPartial)
205  {
206  parseTree.Status = ParseTreeStatus.Partial;
207  }
208  else
209  {
210  parseTree.Status = ParseTreeStatus.Parsed;
211  }
212  }
213 
214  #endregion
215  }
216 
217  // class
218 }
219 
220 //namespace
A strongly-typed resource class, for looking up localized strings, etc.
readonly Scanner Scanner
Definition: Parser.cs:39
Scanner base class. The Scanner's function is to transform a stream of characters into aggregates/wor...
Definition: Scanner.cs:22
Parser(LanguageData language, Scanner scanner, NonTerminal root)
Initializes a new instance of the Parser class.
Definition: Parser.cs:73
static string ErrRootNotRegistered
Looks up a localized string similar to ({0}) term passed as 'root' paramater to parserr is not Root o...
Parser(LanguageData language)
Definition: Parser.cs:60
Parser(Grammar grammar)
Definition: Parser.cs:51
ParseTree Parse(string sourceText)
Definition: Parser.cs:123
Describes a language.
Definition: LanguageData.cs:23
ParseTree ScanOnly(string sourceText, string fileName)
Definition: Parser.cs:164
ParseTree Parse(string sourceText, string fileName)
Definition: Parser.cs:136
readonly NonTerminal Root
Definition: Parser.cs:35
readonly LanguageData Language
Definition: Parser.cs:31
readonly CoreParser CoreParser
Definition: Parser.cs:27