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 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;
76 if (plist.IsSymbol && plist.Symbol == stop)
81 next = new MPlist (reader, stop);
85 public MPlist (FileStream stream, MSymbol target, MSymbol stop)
87 MStreamReader reader = new MStreamReader (stream);
97 result = reader.ReadElement (out this_key, out this_val);
100 if (this_key == MSymbol.plist)
102 MPlist plist = (MPlist) this_val;
106 if (plist.Symbol == stop)
108 if (plist.Symbol == target)
112 next = new MPlist ();
120 internal MPlist (MStreamReader reader)
122 bool result = reader.ReadElement (out key, out val);
125 next = new MPlist (reader);
128 private MPlist (MStreamReader reader, int count)
130 bool result = reader.ReadElement (out key, out val);
132 if (result && --count > 0)
133 next = new MPlist (reader, count);
135 next = new MPlist ();
138 private MPlist (MStreamReader reader, MSymbol stop)
141 MPlist next_plist = null;
150 result = reader.ReadElement (out this_key, out this_val);
153 if (this_key == MSymbol.plist)
155 MPlist plist = (MPlist) this_val;
157 if (plist.IsSymbol && plist.Symbol == stop)
160 if (next_plist == null)
164 next = next_plist = new MPlist ();
168 next_plist.key = this_key;
169 next_plist.val = this_val;
170 next_plist.next = new MPlist ();
171 next_plist = next_plist.next;
176 private MPlist (MStreamReader reader, MSymbol target, MSymbol stop)
187 result = reader.ReadElement (out this_key, out this_val);
190 if (this_key == MSymbol.plist)
192 MPlist plist = (MPlist) this_val;
196 if (plist.Symbol == stop)
198 if (plist.Symbol == target)
202 next = new MPlist ();
210 protected MPlist (MSymbol key, object val)
216 public bool IsEmpty { get { return next == null; } }
218 public bool IsSymbol { get { return Key == MSymbol.symbol; } }
219 public bool IsMText { get { return Key == MSymbol.mtext; } }
220 public bool IsPlist { get { return Key == MSymbol.plist; } }
221 public bool IsInteger { get { return Key == MSymbol.integer; } }
223 public MSymbol Symbol { get { return (MSymbol) val; } }
224 public MText Text { get { return (MText) val; } }
225 public MPlist Plist { get { return (MPlist) val; } }
226 public int Integer { get { return (int) val; } }
234 for (MPlist p = this; p.next != null; i++, p = p.next);
239 public MPlist this[int i]
243 for (p = this; ! p.IsEmpty && i > 0; i--, p = p.next);
244 return (i == 0 ? p : null);
248 public MPlist Clone ()
250 MPlist plist = new MPlist (), pl = plist;
252 for (MPlist p = this; p.next != null; p = p.next)
253 pl = pl.Add (p.key, p.val);
257 public override string ToString ()
261 for (MPlist p = this; ! p.IsEmpty; p = p.next)
265 if (! wf_keys.Contains (p.key))
267 if (p.key == MSymbol.mtext)
268 str += "\"" + p.val + "\"";
275 private MPlist find (MSymbol key)
279 for (p = this; ! p.IsEmpty; p = p.next)
285 public MPlist Find (MSymbol key)
287 MPlist p = find (key);
289 return (p.IsEmpty ? null : p);
292 public object Get (MSymbol key)
294 return find (key).val;
297 internal MPlist Assq (MSymbol key)
299 foreach (MPlist p in this)
300 if (p.IsPlist && p.Plist.IsSymbol && p.Plist.Symbol == key)
305 private delegate MPlist MPlistDelegate (MSymbol key, object val);
307 private MPlist mplist_op (MPlistDelegate op, object val)
309 Type type = val.GetType ();
311 if (Object.ReferenceEquals (type, typeof (MSymbol)))
312 return op (MSymbol.symbol, val);
313 if (Object.ReferenceEquals (type, typeof (MText)))
314 return op (MSymbol.mtext, val);
315 if (Object.ReferenceEquals (type, typeof (MPlist)))
316 return op (MSymbol.plist, val);
317 if (Object.ReferenceEquals (type, typeof (int)))
318 return op (MSymbol.integer, val);
319 return op (MSymbol.t, val);
322 public MPlist Set (MSymbol key, object val)
331 public MPlist Set (object val)
333 return mplist_op (Set, val);
336 public MPlist Put (MSymbol key, object val)
338 return find (key).Set (key, val);
341 public MPlist Put (object val)
343 return mplist_op (Put, val);
346 public MPlist Push (MSymbol key, object val)
348 MPlist p = new MPlist (this.key, this.val);
357 public MPlist Push (object val)
359 return mplist_op (Push, val);
362 public object Pop (out MSymbol key)
368 object this_val = val;
379 return Pop (out temp);
382 public MPlist Add (MSymbol key, object val)
386 for (p = this; ! p.IsEmpty; p = p.next);
387 return p.Push (key, val);
390 public MPlist Cons (MSymbol key, object val)
392 MPlist plist = new MPlist ();
399 public MPlist Clear ()
407 // Implement IEnumerable interface.
408 // foreach (MPlist p in plist) { ... }
410 public virtual IEnumerator GetEnumerator ()
412 return new Enumerator (this);
415 private class Enumerator : IEnumerator
417 private MPlist plist;
418 private MPlist current;
420 internal Enumerator (MPlist plist)
425 public object Current
428 if (current == null || current.IsEmpty)
429 throw new InvalidOperationException ();
439 public bool MoveNext ()
444 current = current.next;
445 return (! current.IsEmpty);
450 public class MStreamReader : StreamReader
452 private static char[] escaped_char = new char[128];
453 private static int[] hexadecimal = new int[128];
454 private char comment_start;
455 private bool line_oriented;
457 public MStreamReader (Stream stream) : base (stream)
460 line_oriented = false;
463 public MStreamReader (Stream stream, char comment_start,
464 bool line_oriented) : base (stream)
466 this.comment_start = comment_start;
467 this.line_oriented = line_oriented;
470 static MStreamReader ()
472 for (int i = 0; i < 128; i++)
473 escaped_char[i] = (char) i;
474 escaped_char['0'] = (char) 0;
475 escaped_char['e'] = (char) 27;
476 escaped_char['a'] = '\a';
477 escaped_char['b'] = '\b';
478 escaped_char['f'] = '\f';
479 escaped_char['n'] = '\n';
480 escaped_char['r'] = '\r';
481 escaped_char['t'] = '\t';
482 escaped_char['v'] = '\v';
483 for (int i = 0; i < 128; i++)
485 for (int i = '0'; i <= '9'; i++)
486 hexadecimal[i] = i - '0';
487 for (int i = 'A'; i <= 'F'; i++)
488 hexadecimal[i] = hexadecimal[i + 'a' - 'A'] = i -'A' + 10;
491 private int ReadHexadecimal (int max)
495 while ((c = Peek ()) >= 0 && c < 128 && (c = hexadecimal[c]) >= 0)
497 if (max >= 0 && (i * 16) + c >= max)
505 public bool ForwardLine ()
508 while ((c = Read ()) >=0 && c != '\n');
512 public bool SkipSpace (out int c)
514 while ((c = Peek ()) == ' ' && c == '\t' && c == '\f')
519 public bool PeekChar (out int c)
521 while ((c = Peek ()) != -1)
523 if (c == comment_start)
525 else if (c != ' ' && c != '\t' && c != '\n')
527 else if (c == '\n' && line_oriented)
535 public bool ReadInteger (out int i)
549 i = ReadHexadecimal (-1);
553 while ((c = Peek ()) >= '0' && c <= '9')
554 i = (i * 10) + (Read () - '0');
558 public bool ReadChar (out int c)
561 if (c < 0 || (line_oriented && c == '\n'))
567 return ReadChar (out c);
570 else if (c == 'x' || c == 'u' || c == 'U')
571 c = ReadHexadecimal (0x10FFFF);
578 private bool read_string (out string str, int prefix, bool for_symbol)
580 char[] buf = new char[256];
586 buf[i++] = (char) prefix;
587 while ((c = Peek ()) >= 0
590 || (c != '(' && c != ')' && c != ' ' && c != '\t' && c != '\n')))
592 if (! ReadChar (out c))
600 buf[i++] = (char) (0xD800 + ((c - 0x10000) >> 10));
601 buf[i++] = (char) (0xDC00 + ((c - 0x10000) & 0x3FF));
606 str = new string (buf, 0, i);
608 str += new string (buf, 0, i);
612 if (c == '\"' && ! for_symbol)
617 str = new string (buf, 0, i);
619 str += new string (buf, 0, i);
621 return (str != null);
624 public bool ReadString (out string str)
626 return read_string (out str, -1, false);
629 public bool ReadMText (out MText mt)
638 if (read_string (out str, -1, false))
639 mt = new MText (str);
647 while ((c = Peek ()) == '\\')
654 mt.Cat (ReadHexadecimal (0x10FFFF));
661 public bool ReadSymbol (out MSymbol sym, int prefix)
665 if (read_string (out str, prefix, true))
667 sym = MSymbol.Of (str);
674 internal bool ReadElement (out MSymbol key, out object val)
678 if (! PeekChar (out c))
688 val = new MPlist (this);
698 else if (c >= '0' && c <= '9')
703 key = MSymbol.integer;
709 if (c >= '0' && c <= '9')
714 key = MSymbol.integer;
720 ReadSymbol (out sym, '-');
722 key = MSymbol.symbol;
728 if (ReadChar (out c))
731 key = MSymbol.integer;
742 if ((c = Peek ()) == 'x' || c == 'u')
745 val = ReadHexadecimal (-1);
746 key = MSymbol.integer;
752 ReadSymbol (out sym, '#');
754 key = MSymbol.symbol;
768 ReadSymbol (out sym, -1);
770 key = MSymbol.symbol;