Mercury.Formats library  1.0
The library provides format parsers for the Mercury library components.
 All Classes Namespaces Files Functions Properties
GrammarInfo.cs
Go to the documentation of this file.
1 using Mercury.Nucleus.Collections;
2 using System;
3 using System.IO;
4 using System.Runtime.Serialization;
5 using System.Xml.Serialization;
6 
7 //
8 // EXPERIMENTAL
9 // ============
10 //
11 // Do not use. For your own safety.
12 // Might format your drives or whatnot.
13 //
14 
15 namespace Mercury.Experimental
16 {
17 
18  [Serializable]
19  internal class Pair<T> : ISerializable
20  {
21  private T first;
22  private T second;
23 
24  public Pair()
25  : this(default(T), default(T))
26  { }
27 
28  public Pair(T fst, T snd)
29  {
30  first = fst;
31  second = snd;
32  }
33 
34  public Pair(SerializationInfo info, StreamingContext context)
35  {
36  first = (T) info.GetValue("First", typeof(T));
37  second = (T) info.GetValue("Second", typeof(T));
38  }
39 
40  public T First { get { return first; } }
41 
42  public T Second { get { return second; } }
43 
44  public void GetObjectData(SerializationInfo info, StreamingContext context)
45  {
46  info.AddValue("First", First, typeof(T));
47  info.AddValue("Second", Second, typeof(T));
48  }
49  }
50 
51  [Serializable]
52  internal class GrammarInfo
53  {
54  public GrammarInfo()
55  { }
56 
57  public string Name { get; set; }
58 
59  [XmlIgnore]
60  public string Path { get; set; }
61 
62  public string Root { get; set; }
63 
64  public string Epsilon { get; set; }
65 
66  [XmlArrayItem("Include")]
67  public string[] GrammarFiles { get; set; }
68 
69  [XmlArrayItem("Include")]
70  public string[] RwRulesFiles { get; set; }
71 
72  public bool UseStringActions { get; set; }
73 
74  public bool UseMercuryScanner { get; set; }
75 
76  public ListMultiDictionary<string,string> Scanners { get; set; }
77 
78  public ListMultiDictionary<string,string> Actions { get; set; }
79 
80  public Pair<string> RuleFunctions { get; set; }
81 
82  public static GrammarInfo FromXML(string path)
83  {
84  XmlSerializer serializer = new XmlSerializer(typeof(GrammarInfo));
85 
86  GrammarInfo ginf = null;
87  using (StreamReader reader = new StreamReader(path))
88  ginf = (GrammarInfo)serializer.Deserialize(reader);
89 
90  return ginf;
91  }
92 
93  public void ToXML(string path)
94  {
95  XmlSerializer serializer = new XmlSerializer(typeof(GrammarInfo));
96 
97  using (StreamWriter writer = new StreamWriter(path))
98  serializer.Serialize(writer, this);
99  }
100  }
101 }