+
+ public class MStreamReader : StreamReader
+ {
+ private static char[] escaped_char = new char[128];
+ private static int[] hexadecimal = new int[128];
+ private char comment_start;
+ private bool line_oriented;
+
+ public MStreamReader (Stream stream) : base (stream)
+ {
+ comment_start = ';';
+ line_oriented = false;
+ }
+
+ public MStreamReader (Stream stream, char comment_start,
+ bool line_oriented) : base (stream)
+ {
+ this.comment_start = comment_start;
+ this.line_oriented = line_oriented;
+ }
+
+ static MStreamReader ()
+ {
+ for (int i = 0; i < 128; i++)
+ escaped_char[i] = (char) i;
+ escaped_char['0'] = (char) 0;
+ escaped_char['e'] = (char) 27;
+ escaped_char['a'] = '\a';
+ escaped_char['b'] = '\b';
+ escaped_char['f'] = '\f';
+ escaped_char['n'] = '\n';
+ escaped_char['r'] = '\r';
+ escaped_char['t'] = '\t';
+ escaped_char['v'] = '\v';
+ for (int i = 0; i < 128; i++)
+ hexadecimal[i] = -1;
+ for (int i = '0'; i <= '9'; i++)
+ hexadecimal[i] = i - '0';
+ for (int i = 'A'; i <= 'F'; i++)
+ hexadecimal[i] = hexadecimal[i + 'a' - 'A'] = i -'A' + 10;
+ }
+
+ private int ReadHexadecimal (int max)
+ {
+ int i = 0, c;
+
+ while ((c = Peek ()) >= 0 && c < 128 && (c = hexadecimal[c]) >= 0)
+ {
+ if (max >= 0 && (i * 16) + c >= max)
+ break;
+ Read ();
+ i = (i * 16) + c;
+ }
+ return i;
+ }
+
+ public bool ForwardLine ()
+ {
+ int c;
+ while ((c = Read ()) >=0 && c != '\n');
+ return (c == '\n');
+ }
+
+ public bool SkipSpace (out int c)
+ {
+ while ((c = Peek ()) == ' ' && c == '\t' && c == '\f')
+ Read ();
+ return (c >= 0);
+ }
+
+ public bool PeekChar (out int c)
+ {
+ while ((c = Peek ()) != -1)
+ {
+ if (c == comment_start)
+ ForwardLine ();
+ else if (c != ' ' && c != '\t' && c != '\n')
+ return true;
+ else if (c == '\n' && line_oriented)
+ return false;
+ else
+ Read ();
+ }
+ return false;
+ }
+
+ public bool ReadInteger (out int i)
+ {
+ int c = Peek ();
+
+ i = 0;
+ if (c < 0)
+ return false;
+ if (c == '0')
+ {
+ Read ();
+ c = Peek ();
+ if (c == 'x')
+ {
+ Read ();
+ i = ReadHexadecimal (-1);
+ return true;
+ }
+ }
+ while ((c = Peek ()) >= '0' && c <= '9')
+ i = (i * 10) + (Read () - '0');
+ return true;
+ }
+
+ public bool ReadChar (out int c)
+ {
+ c = Read ();
+ if (c < 0 || (line_oriented && c == '\n'))
+ return false;
+ if (c == '\\')
+ {
+ c = Read ();
+ if (c == '\n')
+ return ReadChar (out c);
+ if (c < 0)
+ c = '\\';
+ else if (c == 'x' || c == 'u' || c == 'U')
+ c = ReadHexadecimal (0x10FFFF);
+ else if (c < 128)
+ c = escaped_char[c];
+ }
+ return true;
+ }
+
+ private bool read_string (out string str, int prefix, bool for_symbol)
+ {
+ char[] buf = new char[256];
+ int c;
+ int i = 0;
+
+ str = null;
+ if (prefix >= 0)
+ buf[i++] = (char) prefix;
+ while ((c = Peek ()) >= 0
+ && c != '\"'
+ && (! for_symbol
+ || (c != '(' && c != ')' && c != ' ' && c != '\t' && c != '\n')))
+ {
+ if (! ReadChar (out c))
+ break;
+ if (c < 0x10000)
+ {
+ buf[i++] = (char) c;
+ }
+ else
+ {
+ buf[i++] = (char) (0xD800 + ((c - 0x10000) >> 10));
+ buf[i++] = (char) (0xDC00 + ((c - 0x10000) & 0x3FF));
+ }
+ if (i >= 255)
+ {
+ if (str == null)
+ str = new string (buf, 0, i);
+ else
+ str += new string (buf, 0, i);
+ i = 0;
+ }
+ }
+ if (c == '\"' && ! for_symbol)
+ Read ();
+ if (i > 0)
+ {
+ if (str == null)
+ str = new string (buf, 0, i);
+ else
+ str += new string (buf, 0, i);
+ }
+ return (str != null);
+ }
+
+ public bool ReadString (out string str)
+ {
+ return read_string (out str, -1, false);
+ }
+
+ public bool ReadMText (out MText mt)
+ {
+ int c = Peek ();
+
+ if (c == '"')
+ {
+ string str;
+
+ Read ();
+ if (read_string (out str, -1, false))
+ mt = new MText (str);
+ else
+ mt = new MText ();
+ return true;
+ }
+ mt = new MText ();
+ if (c == '\\')
+ {
+ while ((c = Peek ()) == '\\')
+ {
+ Read ();
+ c = Peek ();
+ if (c != 'x')
+ break;
+ Read ();
+ mt.Cat (ReadHexadecimal (0x10FFFF));
+ }
+ return true;
+ }
+ return false;
+ }
+
+ public bool ReadSymbol (out MSymbol sym, int prefix)
+ {
+ string str;
+
+ if (read_string (out str, prefix, true))
+ {
+ sym = MSymbol.Of (str);
+ return true;
+ }
+ sym = MSymbol.nil;
+ return false;
+ }
+
+ internal bool ReadElement (out MSymbol key, out object val)
+ {
+ int c;
+
+ if (! PeekChar (out c))
+ {
+ val = null;
+ key = MSymbol.nil;
+ return false;
+ }
+
+ if (c == '(')
+ {
+ Read ();
+ val = new MPlist (this);
+ key = MSymbol.plist;
+ }
+ else if (c == '"')
+ {
+ MText mt;
+ ReadMText (out mt);
+ val = mt;
+ key = MSymbol.mtext;
+ }
+ else if (c >= '0' && c <= '9')
+ {
+ int i;
+ ReadInteger (out i);
+ val = i;
+ key = MSymbol.integer;
+ }
+ else if (c == '-')
+ {
+ Read ();
+ c = Peek ();
+ if (c >= '0' && c <= '9')
+ {
+ int i;
+ ReadInteger (out i);
+ val = - i;
+ key = MSymbol.integer;
+ }
+ else
+ {
+ MSymbol sym;
+
+ ReadSymbol (out sym, '-');
+ val = sym;
+ key = MSymbol.symbol;
+ }
+ }
+ else if (c == '?')
+ {
+ Read ();
+ if (ReadChar (out c))
+ {
+ val = c;
+ key = MSymbol.integer;
+ }
+ else
+ {
+ val = null;
+ key = MSymbol.nil;
+ }
+ }
+ else if (c == '#')
+ {
+ Read ();
+ if ((c = Peek ()) == 'x' || c == 'u')
+ {
+ Read ();
+ val = ReadHexadecimal (-1);
+ key = MSymbol.integer;
+ }
+ else
+ {
+ MSymbol sym;
+
+ ReadSymbol (out sym, '#');
+ val = sym;
+ key = MSymbol.symbol;
+ }
+ }
+ else if (c == ')')
+ {
+ Read ();
+ val = null;
+ key = MSymbol.nil;
+ return false;
+ }
+ else
+ {
+ MSymbol sym;
+
+ ReadSymbol (out sym, -1);
+ val = sym;
+ key = MSymbol.symbol;
+ }
+ return true;
+ }
+ }