2 using System.Collections;
3 using System.Collections.Generic;
10 public class MPlist : IEnumerable
16 public MSymbol Key { get { return key; } }
17 public object Val { get { return val; } }
18 public MPlist Next { get { return next; } }
20 private static List<MSymbol> wf_keys;
24 wf_keys = new List<MSymbol> ();
25 wf_keys.Add (MSymbol.symbol);
26 wf_keys.Add (MSymbol.mtext);
27 wf_keys.Add (MSymbol.plist);
28 wf_keys.Add (MSymbol.integer);
37 public MPlist (FileStream stream)
39 MStreamReader reader = new MStreamReader (stream);
40 bool result = reader.ReadElement (out key, out val);
43 next = new MPlist (reader);
46 public MPlist (FileStream stream, int count)
48 MStreamReader reader = new MStreamReader (stream);
49 bool result = reader.ReadElement (out key, out val);
51 if (result && --count > 0)
52 next = new MPlist (reader, count);
57 public MPlist (FileStream stream, MSymbol target, MSymbol stop)
59 MStreamReader reader = new MStreamReader (stream);
69 result = reader.ReadElement (out this_key, out this_val);
72 if (this_key == MSymbol.plist)
74 MPlist plist = (MPlist) this_val;
78 if (plist.Symbol == stop)
80 if (plist.Symbol == target)
92 internal MPlist (MStreamReader reader)
94 bool result = reader.ReadElement (out key, out val);
97 next = new MPlist (reader);
100 private MPlist (MStreamReader reader, int count)
102 bool result = reader.ReadElement (out key, out val);
104 if (result && --count > 0)
105 next = new MPlist (reader, count);
107 next = new MPlist ();
110 private MPlist (MStreamReader reader, MSymbol target, MSymbol stop)
121 result = reader.ReadElement (out this_key, out this_val);
124 if (this_key == MSymbol.plist)
126 MPlist plist = (MPlist) this_val;
130 if (plist.Symbol == stop)
132 if (plist.Symbol == target)
136 next = new MPlist ();
144 protected MPlist (MSymbol key, object val)
150 public bool IsEmpty { get { return next == null; } }
152 public bool IsSymbol { get { return Key == MSymbol.symbol; } }
153 public bool IsMText { get { return Key == MSymbol.mtext; } }
154 public bool IsPlist { get { return Key == MSymbol.plist; } }
155 public bool IsInteger { get { return Key == MSymbol.integer; } }
157 public MSymbol Symbol { get { return (MSymbol) val; } }
158 public MText Text { get { return (MText) val; } }
159 public MPlist Plist { get { return (MPlist) val; } }
160 public int Integer { get { return (int) val; } }
168 for (MPlist p = this; p.next != null; i++, p = p.next);
173 public MPlist Clone ()
175 MPlist plist = new MPlist (), pl = plist;
177 for (MPlist p = this; p.next != null; p = p.next)
178 pl = pl.Add (p.key, p.val);
182 public override string ToString ()
186 for (MPlist p = this; ! p.IsEmpty; p = p.next)
190 if (! wf_keys.Contains (p.key))
192 if (p.key == MSymbol.mtext)
193 str += "\"" + p.val + "\"";
200 private MPlist find (MSymbol key)
204 for (p = this; ! p.IsEmpty; p = p.next)
210 public MPlist Find (MSymbol key)
212 MPlist p = find (key);
214 return (p.IsEmpty ? null : p);
217 public object Get (MSymbol key)
219 return find (key).val;
222 internal MPlist Assq (MSymbol key)
224 foreach (MPlist p in this)
225 if (p.IsPlist && p.Plist.IsSymbol && p.Plist.Symbol == key)
230 private delegate MPlist MPlistDelegate (MSymbol key, object val);
232 private MPlist mplist_op (MPlistDelegate op, object val)
234 Type type = val.GetType ();
236 if (Object.ReferenceEquals (type, typeof (MSymbol)))
237 return op (MSymbol.symbol, val);
238 if (Object.ReferenceEquals (type, typeof (MText)))
239 return op (MSymbol.mtext, val);
240 if (Object.ReferenceEquals (type, typeof (MPlist)))
241 return op (MSymbol.plist, val);
242 if (Object.ReferenceEquals (type, typeof (int)))
243 return op (MSymbol.integer, val);
244 return op (MSymbol.t, val);
247 public MPlist Set (MSymbol key, object val)
256 public MPlist Set (object val)
258 return mplist_op (Set, val);
261 public MPlist Put (MSymbol key, object val)
263 return find (key).Set (key, val);
266 public MPlist Put (object val)
268 return mplist_op (Put, val);
271 public MPlist Push (MSymbol key, object val)
273 MPlist p = new MPlist (this.key, this.val);
282 public MPlist Push (object val)
284 return mplist_op (Push, val);
287 public object Pop (out MSymbol key)
293 object this_val = val;
304 return Pop (out temp);
307 public MPlist Add (MSymbol key, object val)
311 for (p = this; ! p.IsEmpty; p = p.next);
312 return p.Push (key, val);
315 public MPlist Cons (MSymbol key, object val)
317 MPlist plist = new MPlist ();
324 public MPlist Clear ()
332 // Implement IEnumerable interface.
333 // foreach (MPlist p in plist) { ... }
335 public virtual IEnumerator GetEnumerator ()
337 return new Enumerator (this);
340 private class Enumerator : IEnumerator
342 private MPlist plist;
343 private MPlist current;
345 internal Enumerator (MPlist plist)
350 public object Current
353 if (current == null || current.IsEmpty)
354 throw new InvalidOperationException ();
364 public bool MoveNext ()
369 current = current.next;
370 return (! current.IsEmpty);
375 public class MStreamReader : StreamReader
377 private static char[] escaped_char = new char[128];
378 private static int[] hexadecimal = new int[128];
379 private char comment_start;
380 private bool line_oriented;
382 public MStreamReader (Stream stream) : base (stream)
385 line_oriented = false;
388 public MStreamReader (Stream stream, char comment_start,
389 bool line_oriented) : base (stream)
391 this.comment_start = comment_start;
392 this.line_oriented = line_oriented;
395 static MStreamReader ()
397 for (int i = 0; i < 128; i++)
398 escaped_char[i] = (char) i;
399 escaped_char['0'] = (char) 0;
400 escaped_char['e'] = (char) 27;
401 escaped_char['a'] = '\a';
402 escaped_char['b'] = '\b';
403 escaped_char['f'] = '\f';
404 escaped_char['n'] = '\n';
405 escaped_char['r'] = '\r';
406 escaped_char['t'] = '\t';
407 escaped_char['v'] = '\v';
408 for (int i = 0; i < 128; i++)
410 for (int i = '0'; i <= '9'; i++)
411 hexadecimal[i] = i - '0';
412 for (int i = 'A'; i <= 'F'; i++)
413 hexadecimal[i] = hexadecimal[i + 'a' - 'A'] = i -'A' + 10;
416 private int ReadHexadecimal (int max)
420 while ((c = Peek ()) >= 0 && c < 128 && (c = hexadecimal[c]) >= 0)
422 if (max >= 0 && (i * 16) + c >= max)
430 public bool ForwardLine ()
433 while ((c = Read ()) >=0 && c != '\n');
437 public bool SkipSpace (out int c)
439 while ((c = Peek ()) == ' ' && c == '\t' && c == '\f')
444 public bool PeekChar (out int c)
446 while ((c = Peek ()) != -1)
448 if (c == comment_start)
450 else if (c != ' ' && c != '\t' && c != '\n')
452 else if (c == '\n' && line_oriented)
460 public bool ReadInteger (out int i)
474 i = ReadHexadecimal (-1);
478 while ((c = Peek ()) >= '0' && c <= '9')
479 i = (i * 10) + (Read () - '0');
483 public bool ReadChar (out int c)
486 if (c < 0 || (line_oriented && c == '\n'))
492 return ReadChar (out c);
495 else if (c == 'x' || c == 'u' || c == 'U')
496 c = ReadHexadecimal (0x10FFFF);
503 private bool read_string (out string str, int prefix, bool for_symbol)
505 char[] buf = new char[256];
511 buf[i++] = (char) prefix;
512 while ((c = Peek ()) >= 0
515 || (c != '(' && c != ')' && c != ' ' && c != '\t' && c != '\n')))
517 if (! ReadChar (out c))
525 buf[i++] = (char) (0xD800 + ((c - 0x10000) >> 10));
526 buf[i++] = (char) (0xDC00 + ((c - 0x10000) & 0x3FF));
531 str = new string (buf, 0, i);
533 str += new string (buf, 0, i);
537 if (c == '\"' && ! for_symbol)
542 str = new string (buf, 0, i);
544 str += new string (buf, 0, i);
546 return (str != null);
549 public bool ReadString (out string str)
551 return read_string (out str, -1, false);
554 public bool ReadMText (out MText mt)
563 if (read_string (out str, -1, false))
564 mt = new MText (str);
572 while ((c = Peek ()) == '\\')
579 mt.Cat (ReadHexadecimal (0x10FFFF));
586 public bool ReadSymbol (out MSymbol sym, int prefix)
590 if (read_string (out str, prefix, true))
592 sym = MSymbol.Of (str);
599 internal bool ReadElement (out MSymbol key, out object val)
603 if (! PeekChar (out c))
613 val = new MPlist (this);
616 else if (c == '"' || c == '\\')
623 else if (c >= '0' && c <= '9')
628 key = MSymbol.integer;
634 if (c >= '0' && c <= '9')
639 key = MSymbol.integer;
645 ReadSymbol (out sym, '-');
647 key = MSymbol.symbol;
653 if (ReadChar (out c))
656 key = MSymbol.integer;
667 if ((c = Peek ()) == 'x' || c == 'u')
670 val = ReadHexadecimal (-1);
671 key = MSymbol.integer;
677 ReadSymbol (out sym, '#');
679 key = MSymbol.symbol;
693 ReadSymbol (out sym, -1);
695 key = MSymbol.symbol;