2 using System.Collections;
9 public class MPlist : IEnumerable
21 public MPlist (FileStream stream)
23 MStreamReader reader = new MStreamReader (stream);
26 bool result = reader.ReadElement (out key, out val);
31 next = new MPlist (reader);
34 public MPlist (FileStream stream, int count)
36 MStreamReader reader = new MStreamReader (stream);
39 bool result = reader.ReadElement (out key, out val);
43 if (result && --count > 0)
44 next = new MPlist (reader, count);
49 public MPlist (FileStream stream, MSymbol target, MSymbol stop)
51 MStreamReader reader = new MStreamReader (stream);
60 result = reader.ReadElement (out key, out val);
63 if (key == MSymbol.plist)
65 MPlist plist = (MPlist) val;
69 if (plist.Symbol == stop)
71 if (plist.Symbol == target)
83 private MPlist (MStreamReader reader)
87 bool result = reader.ReadElement (out key, out val);
92 next = new MPlist (reader);
95 private MPlist (MStreamReader reader, int count)
99 bool result = reader.ReadElement (out key, out val);
103 if (result && --count > 0)
104 next = new MPlist (reader, count);
106 next = new MPlist ();
109 private MPlist (MStreamReader reader, MSymbol target, MSymbol stop)
119 result = reader.ReadElement (out key, out val);
122 if (key == MSymbol.plist)
124 MPlist plist = (MPlist) val;
128 if (plist.Symbol == stop)
130 if (plist.Symbol == target)
134 next = new MPlist ();
142 private MPlist (MSymbol key, object val)
148 public bool IsEmpty { get { return next == null; } }
149 public MPlist Next { get { return next; } }
151 internal bool IsSymbol { get { return Key == MSymbol.symbol; } }
152 internal bool IsMText { get { return Key == MSymbol.mtext; } }
153 internal bool IsPlist { get { return Key == MSymbol.plist; } }
154 internal bool IsInteger { get { return Key == MSymbol.integer; } }
156 internal MSymbol Symbol { get { return (MSymbol) Val; } }
157 internal MText Text { get { return (MText) Val; } }
158 internal MPlist Plist { get { return (MPlist) Val; } }
159 internal int Integer { get { return (int) Val; } }
167 for (MPlist p = this; p.next != null; i++, p = p.next);
172 public MPlist Clone ()
174 MPlist plist = new MPlist (), pl = plist;
176 for (MPlist p = this; p.next != null; p = p.next)
177 pl = pl.Add (p.Key, p.Val);
181 public override string ToString ()
185 for (MPlist p = this; ! p.IsEmpty; p = p.next)
189 if (p.Key != MSymbol.symbol
190 && p.Key != MSymbol.integer
191 && p.Key != MSymbol.plist
192 && p.Key != MSymbol.mtext)
194 if (p.Key == MSymbol.mtext)
195 str += "\"" + p.Val + "\"";
202 private MPlist find (MSymbol key)
206 for (p = this; ! p.IsEmpty; p = p.next)
212 public MPlist Find (MSymbol key)
214 MPlist p = find (key);
216 return (p.IsEmpty ? null : p);
219 public object Get (MSymbol key)
221 return find (key).Val;
224 internal MPlist Assq (MSymbol key)
226 foreach (MPlist p in this)
227 if (p.IsPlist && p.Plist.IsSymbol && p.Plist.Symbol == key)
232 private delegate MPlist MPlistDelegate (MSymbol key, object val);
234 private MPlist mplist_op (MPlistDelegate op, object val)
236 Type type = val.GetType ();
238 if (Object.ReferenceEquals (type, typeof (MSymbol)))
239 return op (MSymbol.symbol, val);
240 if (Object.ReferenceEquals (type, typeof (MText)))
241 return op (MSymbol.mtext, val);
242 if (Object.ReferenceEquals (type, typeof (MPlist)))
243 return op (MSymbol.plist, val);
244 if (Object.ReferenceEquals (type, typeof (int)))
245 return op (MSymbol.integer, val);
246 return op (MSymbol.t, val);
249 public MPlist Set (MSymbol key, object val)
258 public MPlist Set (object val)
260 return mplist_op (Set, val);
263 public MPlist Put (MSymbol key, object val)
265 return find (key).Set (key, val);
268 public MPlist Put (object val)
270 return mplist_op (Put, val);
273 public MPlist Push (MSymbol key, object val)
275 MPlist p = new MPlist (Key, Val);
284 public MPlist Push (object val)
286 return mplist_op (Push, val);
289 public object Pop (out MSymbol key)
306 return Pop (out key);
309 public MPlist Add (MSymbol key, object val)
313 for (p = this; ! p.IsEmpty; p = p.next);
314 return p.Push (key, val);
317 public MPlist Clear ()
325 // Implement IEnumerable interface.
326 // foreach (MPlist p in plist) { ... }
328 public virtual IEnumerator GetEnumerator ()
330 return new Enumerator (this);
333 private class Enumerator : IEnumerator
335 private MPlist plist;
336 private MPlist current;
338 internal Enumerator (MPlist plist)
343 public object Current
346 if (current == null || current.IsEmpty)
347 throw new InvalidOperationException ();
357 public bool MoveNext ()
362 current = current.next;
363 return (! current.IsEmpty);
367 private class MStreamReader : StreamReader
369 private static char[] escaped_char = new char[128];
370 private static int[] hexadecimal = new int[128];
372 public MStreamReader (Stream stream) : base (stream)
376 static MStreamReader ()
378 for (int i = 0; i < 128; i++)
379 escaped_char[i] = (char) i;
380 escaped_char['e'] = (char) 27;
381 escaped_char['b'] = '\b';
382 escaped_char['f'] = '\f';
383 escaped_char['n'] = '\n';
384 escaped_char['r'] = '\r';
385 escaped_char['t'] = '\t';
386 escaped_char['\\'] = '\\';
387 for (int i = 0; i < 128; i++)
389 for (int i = '0'; i <= '9'; i++)
390 hexadecimal[i] = i - '0';
391 for (int i = 'A'; i <= 'F'; i++)
392 hexadecimal[i] = hexadecimal[i + 'a' - 'A'] = i -'A' + 10;
395 private int PeekChar ()
397 bool comment = false;
400 while ((c = Peek ()) != -1)
404 if ((c = Read ()) == '\n')
411 else if (c != ' ' && c != '\t' && c != '\n')
419 private int ReadHexadecimal ()
423 while ((c = Peek ()) >= 0 && c < 128 && (c = hexadecimal[c]) >= 0)
431 private int ReadInteger ()
435 while ((c = Peek ()) >= '0' && c <= '9')
436 i = (i * 10) + (Read () - '0');
440 private int ReadChar ()
449 if (c == 'x' || c == 'u')
450 return ReadHexadecimal ();
457 private MText ReadMtext ()
459 MText mt = new MText ();
462 while ((c = Peek ()) != -1 && c != '"')
487 private string ReadSymbolName ()
491 if (c == -1 || c == '(' || c == ')' || c == ' ' || c == '\n' || c == '"')
500 return (char) c + ReadSymbolName ();
503 public bool ReadElement (out MSymbol key, out object val)
510 val = new MPlist (this);
519 else if (c >= '0' && c <= '9')
521 int i = ReadInteger ();
524 key = MSymbol.integer;
530 if (c >= '0' && c <= '9')
532 int i = ReadInteger ();
534 key = MSymbol.integer;
538 string str = ReadSymbolName ();
540 val = MSymbol.Of ("-" + str);
541 key = MSymbol.symbol;
548 key = MSymbol.integer;
553 if ((c = Peek ()) == 'x' || c == 'u')
556 val = ReadHexadecimal ();
557 key = MSymbol.integer;
561 val = MSymbol.Of ("#" + (char) c + ReadSymbolName ());
562 key = MSymbol.symbol;
565 else if (c == -1 || c == ')')
575 val = MSymbol.Of (ReadSymbolName ());
576 key = MSymbol.symbol;