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 this[int i]
177 for (p = this; ! p.IsEmpty && i > 0; i--, p = p.next);
178 return (i == 0 ? p : null);
182 public MPlist Clone ()
184 MPlist plist = new MPlist (), pl = plist;
186 for (MPlist p = this; p.next != null; p = p.next)
187 pl = pl.Add (p.key, p.val);
191 public override string ToString ()
195 for (MPlist p = this; ! p.IsEmpty; p = p.next)
199 if (! wf_keys.Contains (p.key))
201 if (p.key == MSymbol.mtext)
202 str += "\"" + p.val + "\"";
209 private MPlist find (MSymbol key)
213 for (p = this; ! p.IsEmpty; p = p.next)
219 public MPlist Find (MSymbol key)
221 MPlist p = find (key);
223 return (p.IsEmpty ? null : p);
226 public object Get (MSymbol key)
228 return find (key).val;
231 internal MPlist Assq (MSymbol key)
233 foreach (MPlist p in this)
234 if (p.IsPlist && p.Plist.IsSymbol && p.Plist.Symbol == key)
239 private delegate MPlist MPlistDelegate (MSymbol key, object val);
241 private MPlist mplist_op (MPlistDelegate op, object val)
243 Type type = val.GetType ();
245 if (Object.ReferenceEquals (type, typeof (MSymbol)))
246 return op (MSymbol.symbol, val);
247 if (Object.ReferenceEquals (type, typeof (MText)))
248 return op (MSymbol.mtext, val);
249 if (Object.ReferenceEquals (type, typeof (MPlist)))
250 return op (MSymbol.plist, val);
251 if (Object.ReferenceEquals (type, typeof (int)))
252 return op (MSymbol.integer, val);
253 return op (MSymbol.t, val);
256 public MPlist Set (MSymbol key, object val)
265 public MPlist Set (object val)
267 return mplist_op (Set, val);
270 public MPlist Put (MSymbol key, object val)
272 return find (key).Set (key, val);
275 public MPlist Put (object val)
277 return mplist_op (Put, val);
280 public MPlist Push (MSymbol key, object val)
282 MPlist p = new MPlist (this.key, this.val);
291 public MPlist Push (object val)
293 return mplist_op (Push, val);
296 public object Pop (out MSymbol key)
302 object this_val = val;
313 return Pop (out temp);
316 public MPlist Add (MSymbol key, object val)
320 for (p = this; ! p.IsEmpty; p = p.next);
321 return p.Push (key, val);
324 public MPlist Cons (MSymbol key, object val)
326 MPlist plist = new MPlist ();
333 public MPlist Clear ()
341 // Implement IEnumerable interface.
342 // foreach (MPlist p in plist) { ... }
344 public virtual IEnumerator GetEnumerator ()
346 return new Enumerator (this);
349 private class Enumerator : IEnumerator
351 private MPlist plist;
352 private MPlist current;
354 internal Enumerator (MPlist plist)
359 public object Current
362 if (current == null || current.IsEmpty)
363 throw new InvalidOperationException ();
373 public bool MoveNext ()
378 current = current.next;
379 return (! current.IsEmpty);
384 public class MStreamReader : StreamReader
386 private static char[] escaped_char = new char[128];
387 private static int[] hexadecimal = new int[128];
388 private char comment_start;
389 private bool line_oriented;
391 public MStreamReader (Stream stream) : base (stream)
394 line_oriented = false;
397 public MStreamReader (Stream stream, char comment_start,
398 bool line_oriented) : base (stream)
400 this.comment_start = comment_start;
401 this.line_oriented = line_oriented;
404 static MStreamReader ()
406 for (int i = 0; i < 128; i++)
407 escaped_char[i] = (char) i;
408 escaped_char['0'] = (char) 0;
409 escaped_char['e'] = (char) 27;
410 escaped_char['a'] = '\a';
411 escaped_char['b'] = '\b';
412 escaped_char['f'] = '\f';
413 escaped_char['n'] = '\n';
414 escaped_char['r'] = '\r';
415 escaped_char['t'] = '\t';
416 escaped_char['v'] = '\v';
417 for (int i = 0; i < 128; i++)
419 for (int i = '0'; i <= '9'; i++)
420 hexadecimal[i] = i - '0';
421 for (int i = 'A'; i <= 'F'; i++)
422 hexadecimal[i] = hexadecimal[i + 'a' - 'A'] = i -'A' + 10;
425 private int ReadHexadecimal (int max)
429 while ((c = Peek ()) >= 0 && c < 128 && (c = hexadecimal[c]) >= 0)
431 if (max >= 0 && (i * 16) + c >= max)
439 public bool ForwardLine ()
442 while ((c = Read ()) >=0 && c != '\n');
446 public bool SkipSpace (out int c)
448 while ((c = Peek ()) == ' ' && c == '\t' && c == '\f')
453 public bool PeekChar (out int c)
455 while ((c = Peek ()) != -1)
457 if (c == comment_start)
459 else if (c != ' ' && c != '\t' && c != '\n')
461 else if (c == '\n' && line_oriented)
469 public bool ReadInteger (out int i)
483 i = ReadHexadecimal (-1);
487 while ((c = Peek ()) >= '0' && c <= '9')
488 i = (i * 10) + (Read () - '0');
492 public bool ReadChar (out int c)
495 if (c < 0 || (line_oriented && c == '\n'))
501 return ReadChar (out c);
504 else if (c == 'x' || c == 'u' || c == 'U')
505 c = ReadHexadecimal (0x10FFFF);
512 private bool read_string (out string str, int prefix, bool for_symbol)
514 char[] buf = new char[256];
520 buf[i++] = (char) prefix;
521 while ((c = Peek ()) >= 0
524 || (c != '(' && c != ')' && c != ' ' && c != '\t' && c != '\n')))
526 if (! ReadChar (out c))
534 buf[i++] = (char) (0xD800 + ((c - 0x10000) >> 10));
535 buf[i++] = (char) (0xDC00 + ((c - 0x10000) & 0x3FF));
540 str = new string (buf, 0, i);
542 str += new string (buf, 0, i);
546 if (c == '\"' && ! for_symbol)
551 str = new string (buf, 0, i);
553 str += new string (buf, 0, i);
555 return (str != null);
558 public bool ReadString (out string str)
560 return read_string (out str, -1, false);
563 public bool ReadMText (out MText mt)
572 if (read_string (out str, -1, false))
573 mt = new MText (str);
581 while ((c = Peek ()) == '\\')
588 mt.Cat (ReadHexadecimal (0x10FFFF));
595 public bool ReadSymbol (out MSymbol sym, int prefix)
599 if (read_string (out str, prefix, true))
601 sym = MSymbol.Of (str);
608 internal bool ReadElement (out MSymbol key, out object val)
612 if (! PeekChar (out c))
622 val = new MPlist (this);
625 else if (c == '"' || c == '\\')
632 else if (c >= '0' && c <= '9')
637 key = MSymbol.integer;
643 if (c >= '0' && c <= '9')
648 key = MSymbol.integer;
654 ReadSymbol (out sym, '-');
656 key = MSymbol.symbol;
662 if (ReadChar (out c))
665 key = MSymbol.integer;
676 if ((c = Peek ()) == 'x' || c == 'u')
679 val = ReadHexadecimal (-1);
680 key = MSymbol.integer;
686 ReadSymbol (out sym, '#');
688 key = MSymbol.symbol;
702 ReadSymbol (out sym, -1);
704 key = MSymbol.symbol;