Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
ParseTreeExtensions.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 using System.IO;
18 using System.Xml;
19 
20 namespace Irony.Parsing {
21 #if SILICONSTUDIO_PLATFORM_WINDOWS_DESKTOP
22  public static class ParseTreeExtensions {
23 
24  public static string ToXml(this ParseTree parseTree) {
25  if (parseTree == null || parseTree.Root == null) return string.Empty;
26  var xdoc = ToXmlDocument(parseTree);
27  StringWriter sw = new StringWriter();
28  XmlTextWriter xw = new XmlTextWriter(sw);
29  xw.Formatting = Formatting.Indented;
30  xdoc.WriteTo(xw);
31  xw.Flush();
32  return sw.ToString();
33  }
34 
35  public static XmlDocument ToXmlDocument(this ParseTree parseTree) {
36  var xdoc = new XmlDocument();
37  if (parseTree == null || parseTree.Root == null) return xdoc;
38  var xTree = xdoc.CreateElement("ParseTree");
39  xdoc.AppendChild(xTree);
40  var xRoot = parseTree.Root.ToXmlElement(xdoc);
41  xTree.AppendChild(xRoot);
42  return xdoc;
43  }
44 
45  public static XmlElement ToXmlElement(this ParseTreeNode node, XmlDocument ownerDocument) {
46  var xElem = ownerDocument.CreateElement("Node");
47  xElem.SetAttribute("Term", node.Term.Name);
48  if (node.Term.AstNodeType != null)
49  xElem.SetAttribute("AstNodeType", node.Term.AstNodeType.Name);
50  if (node.Token != null) {
51  xElem.SetAttribute("Terminal", node.Term.GetType().Name);
52  //xElem.SetAttribute("Text", node.Token.Text);
53  if (node.Token.Value != null)
54  xElem.SetAttribute("Value", node.Token.Value.ToString());
55  } else
56  foreach (var child in node.ChildNodes) {
57  var xChild = child.ToXmlElement(ownerDocument);
58  xElem.AppendChild(xChild);
59  }
60  return xElem;
61  }//method
62 
63  }//class
64 #endif
65 }//namespace
System.IO.StringWriter StringWriter