Пример #1
0
        public static object ReadNumberHandler(LispReader stream, string ch, int arg)
        {
            var token = stream.ReadToken();

            switch (ch)
            {
            case "r":
            {
                return(Number.ParseNumberBase(token, arg));
            }

            case "o":
            {
                return(Number.ParseNumberBase(token, 8));
            }

            case "b":
            {
                return(Number.ParseNumberBase(token, 2));
            }

            case "x":
            {
                return(Number.ParseNumberBase(token, 16));
            }

            default:
            {
                // not reached
                return(null);
            }
            }
        }
Пример #2
0
        public string ParseSingleLineString()
        {
            // supports backslash escapes
            // single line

            StringBuilder buf = new StringBuilder();

            while (true)
            {
                var ch = ReadChar();

                if (IsEof || ch == '\n')
                {
                    throw MakeScannerException("Unterminated string");
                }

                if (ch == '"')
                {
                    break;
                }

                if (ch == '\\')
                {
                    ch = ReadChar();

                    switch (ch)
                    {
                    case 'x':
                    {
                        var ch1 = ReadChar();
                        var ch2 = ReadChar();
                        if (IsEof)
                        {
                            throw MakeScannerException("Unterminated string");
                        }
                        var n = (int)Number.ParseNumberBase(new string(new char[] { ch1, ch2 }), 16);
                        buf.Append(Convert.ToChar(n));
                        break;
                    }

                    case 'u':
                    {
                        var ch1 = ReadChar();
                        var ch2 = ReadChar();
                        var ch3 = ReadChar();
                        var ch4 = ReadChar();
                        if (IsEof)
                        {
                            throw MakeScannerException("Unterminated string");
                        }
                        var n = (int)Number.ParseNumberBase(new string(new char[] { ch1, ch2, ch3, ch4 }), 16);
                        buf.Append(Convert.ToChar(n));
                        break;
                    }

                    default:
                    {
                        if (IsEof)
                        {
                            throw MakeScannerException("Unterminated string");
                        }
                        buf.Append(Runtime.UnescapeCharacter(ch));
                        break;
                    }
                    }
                }
                else
                {
                    buf.Append(ch);
                }
            }

            return(buf.ToString());
        }