Esempio n. 1
0
        //
        // Dictionaries
        //

        private static Water.Dictionary ParseDictionary(Water.TextReader reader)
        {
            #region ParseDictionary

            Water.Dictionary dict = new Water.Dictionary();

            // Read the '{'
            reader.Read();

            TrimWhitespace(reader);

            while (reader.Peek() != -1)
            {
                char ch = (char)reader.Peek();

                if (ch == '}')
                {
                    //Exit state
                    reader.Read();
                    return(dict);
                }
                else
                {
                    ParseDictEntry(reader, dict);
                }

                TrimWhitespace(reader);
            }

            throw new Water.Error("Parser error: End of dictionary expected.");

            #endregion
        }
Esempio n. 2
0
        private static void ParseIndexer(Water.TextReader reader, System.Text.StringBuilder stringBuilder)
        {
            #region ParseIndexer

            stringBuilder.Append((char)reader.Read());

            while (reader.Peek() != -1)
            {
                char ch = (char)reader.Peek();

                if (ch == ']')
                {
                    stringBuilder.Append(ch);
                    reader.Read();
                    return;
                }
                else
                {
                    stringBuilder.Append(ch);
                    reader.Read();
                }
            }

            throw new Water.Error("Parser error: End of indexer expected.");

            #endregion
        }
Esempio n. 3
0
        //
        // Binary
        //

        private static byte[] ParseBytes(Water.TextReader reader)
        {
            #region ParseBytes

            System.IO.StringWriter writer = new System.IO.StringWriter();

            // Read the '['
            reader.Read();

            while (reader.Peek() != -1)
            {
                char ch = (char)reader.Read();

                if (ch == ']')
                {
                    byte[] bytes = System.Convert.FromBase64String(writer.ToString());
                    writer.Close();
                    return(bytes);
                }

                writer.Write(ch);
            }

            throw new Water.Error("Invalid bytestring.");

            #endregion
        }
Esempio n. 4
0
        public FileReader(string filename)
        {
            // Does the file exist?
            System.IO.FileInfo fileInfo = new System.IO.FileInfo(filename);
            if (!fileInfo.Exists)
            {
                throw new Water.Error(fileInfo.FullName + " does not exist.");
            }

            // Push the current directory.
            this._currentDirectory = System.Environment.CurrentDirectory;
            string newCurrentDirectory = fileInfo.DirectoryName;

            if (!newCurrentDirectory.EndsWith(System.IO.Path.DirectorySeparatorChar.ToString()))
            {
                newCurrentDirectory += System.IO.Path.DirectorySeparatorChar;
            }
            System.Environment.CurrentDirectory = newCurrentDirectory;

            //Reader
            System.IO.Stream       source_stream = fileInfo.OpenRead();
            System.IO.MemoryStream target_stream = new System.IO.MemoryStream();
            byte[] bytes = new byte[4096];
            int    numbytes;

            while ((numbytes = source_stream.Read(bytes, 0, 4096)) > 0)
            {
                target_stream.Write(bytes, 0, numbytes);
            }
            source_stream.Close();
            target_stream.Position = 0;
            this._reader           = new Water.TextReader(new System.IO.StreamReader(target_stream), filename);
        }
Esempio n. 5
0
 /// <summary>
 /// For this to work, we must only parse one statement at a time.
 /// This way we can parse multiline block statements.
 /// </summary>
 private void CheckReader()
 {
     if (this._reader == null || this._reader.Peek() == -1)
     {
         this._reader = new Water.TextReader(new System.IO.StringReader(System.Console.ReadLine() + "\n"));
     }
 }
Esempio n. 6
0
        private static string ParseName(Water.TextReader reader)
        {
            #region ParseName

            System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();

            while (reader.Peek() != -1)
            {
                char ch = (char)reader.Peek();

                if (ch == ':')
                {
                    return(stringBuilder.ToString());
                }
                else
                {
                    stringBuilder.Append(ch);
                    reader.Read();
                }
            }

            throw new Water.Error("Parser error: " + stringBuilder.ToString());

            #endregion
        }
Esempio n. 7
0
        private static void ParseDictEntry(Water.TextReader reader, Water.Dictionary dict)
        {
            #region ParseDictEntry

            TrimWhitespace(reader);

            string name = ParseName(reader);

            TrimWhitespace(reader);

            if (reader.Peek() == ':')
            {
                reader.Read();
            }
            else
            {
                throw new Water.Error("Parser error: Invalid dictionary entry name: " + name);
            }

            TrimWhitespace(reader);

            dict.Add(name, Parse(reader));

            #endregion
        }
Esempio n. 8
0
        //
        // Lists
        //

        public static Water.List ParseList(Water.TextReader reader)
        {
            #region ParseList

            Water.List list = new Water.List();

            // Read the '('
            reader.Read();

            TrimWhitespace(reader);

            while (reader.Peek() != -1)
            {
                char ch = (char)reader.Peek();

                if (ch == ')')
                {
                    reader.Read();
                    return(list);
                }
                else
                {
                    list.Add(Water.Parser.Parse(reader));
                }

                TrimWhitespace(reader);
            }

            throw new Water.Error("Parser error: Invalid list.");

            #endregion
        }
Esempio n. 9
0
        //
        // Comment
        //

        public static bool IsComment(Water.TextReader reader)
        {
            #region IsComment

            char ch = (char)reader.Peek();

            return(IsComment(ch));

            #endregion
        }
Esempio n. 10
0
        private bool IsSpace(Water.TextReader reader)
        {
            #region IsSpace

            char ch = (char)reader.Peek();

            return(IsSpace(ch));

            #endregion
        }
Esempio n. 11
0
        public static bool IsNewline(Water.TextReader reader)
        {
            #region IsNewline

            char ch = (char)reader.Peek();

            return(IsNewline(ch));

            #endregion
        }
Esempio n. 12
0
        //
        // Quote-prefixed expressions
        //

        private static Water.Quote ParseQuote(Water.TextReader reader)
        {
            #region ParseQuote

            // Read the '\''
            reader.Read();

            return(new Water.Quote(Parse(reader)));

            #endregion
        }
Esempio n. 13
0
        //
        // Comma-prefixed expressions
        //

        private static Water.Comma ParseComma(Water.TextReader reader)
        {
            #region ParseComma

            // Read the ','
            reader.Read();

            return(new Water.Comma(Parse(reader)));

            #endregion
        }
Esempio n. 14
0
 private static void LoadSettings(string path)
 {
     if (!System.IO.File.Exists(path))
     {
         Water.Environment.DefineConstant("Settings", new Water.Dictionary());
     }
     else
     {
         Water.TextReader reader   = new Water.TextReader(new System.IO.StreamReader(path));
         Water.Dictionary Settings = (Water.Dictionary)Water.Parser.Parse(reader);
         Water.Environment.DefineConstant("Settings", Settings);
         reader.Close();
     }
 }
Esempio n. 15
0
        public static void TrimNewline(Water.TextReader reader)
        {
            #region TrimNewline

            while (reader.Peek() != -1)
            {
                if (!IsNewline((char)reader.Peek()))
                {
                    return;
                }
                reader.Read();
            }

            #endregion
        }
Esempio n. 16
0
        public static object ParseExpression(Water.TextReader reader)
        {
            #region ParseExpression

            char ch = (char)reader.Peek();

            switch (ch)
            {
            case '(':
            {
                return(ParseList(reader));
            }

            case '{':
            {
                return(ParseDictionary(reader));
            }

            case '"':
            {
                return(ParseString(reader));
            }

            case '[':
            {
                return(ParseBytes(reader));
            }

            case '\'':
            {
                return(ParseQuote(reader));
            }

            case ',':
            {
                return(ParseComma(reader));
            }

            default:
            {
                object atom = ParseAtom(reader);
                return(atom);
            }
            }

            #endregion
        }
Esempio n. 17
0
        public static void ParseComment(Water.TextReader reader)
        {
            #region ParseComment

            while (reader.Peek() != -1)
            {
                if (IsNewline((char)reader.Peek()))
                {
                    return;
                }
                else
                {
                    reader.Read();
                }
            }

            #endregion
        }
Esempio n. 18
0
        //
        // Atoms
        //

        private static object ParseAtom(Water.TextReader reader)
        {
            #region ParseAtom

            System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();

            while (reader.Peek() != -1)
            {
                char ch = (char)reader.Peek();

                if (IsWhitespace(ch))
                {
                    break;
                }
                else if (ch == '[')
                {
                    ParseIndexer(reader, stringBuilder);
                }
                else if (ch == '(')
                {
                    return(Coerce(stringBuilder.ToString()));
                }
                else if (ch == ')')
                {
                    return(Coerce(stringBuilder.ToString()));
                }
                else if (ch == '}')
                {
                    return(Coerce(stringBuilder.ToString()));
                }
                else
                {
                    stringBuilder.Append(ch);
                    reader.Read();
                }
            }

            return(Coerce(stringBuilder.ToString()));

            #endregion
        }
Esempio n. 19
0
        public static object Parse(Water.TextReader reader)
        {
            #region Parse

            TrimWhitespace(reader);

            if (reader.Peek() == -1)
            {
                return(null);
            }

            if (IsComment(reader))
            {
                ParseComment(reader);

                // Try it again.
                return(Parse(reader));
            }

            return(ParseExpression(reader));

            #endregion
        }
Esempio n. 20
0
        private Water.List ParseBlockStatements(Water.TextReader reader, string name, string end, bool allowRecursion, Water.Statement start_statement)
        {
            Water.List block_statements = new Water.List();

            while (reader.Peek() != -1)
            {
                Water.Parser.TrimWhitespace(reader);

                Water.Statement statement = ParseStatement(reader);
                if (statement != null)
                {
                    if (statement.Expression.Count == 1 && statement.Expression[0].ToString().Equals(end))
                    {
                        return(block_statements);
                    }

                    block_statements.Add(statement);
                }
            }

//TODO DELETE			Water.Environment.Set("_Statement", start_statement);
            throw new Water.Error("\"" + end + "\" expected.");
        }
Esempio n. 21
0
 public Iterator(System.IO.TextReader reader)
 {
     this._reader = new Water.TextReader(reader);
     this._parser = new Water.StatementParser(this._reader);
 }
Esempio n. 22
0
 public Iterator(Water.TextReader reader)
 {
     this._reader = reader;
     this._parser = new Water.StatementParser(this._reader);
 }
Esempio n. 23
0
        //
        // Strings
        //

        private static string ParseString(Water.TextReader reader)
        {
            #region ParseString

            System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();

            // Read the '\"'
            reader.Read();

            bool isEscaped = false;
            while (reader.Peek() != -1)
            {
                char ch = (char)reader.Read();

                if (isEscaped)
                {
                    switch (ch)
                    {
                    case '\\':
                    {
                        stringBuilder.Append("\\");
                        break;
                    }

                    case '"':
                    {
                        stringBuilder.Append("\"");
                        break;
                    }

                    case '\'':
                    {
                        stringBuilder.Append("\'");
                        break;
                    }

                    case '0':
                    {
                        stringBuilder.Append("\0");
                        break;
                    }

                    case 'a':
                    {
                        stringBuilder.Append("\a");
                        break;
                    }

                    case 'b':
                    {
                        stringBuilder.Append("\b");
                        break;
                    }

                    case 'f':
                    {
                        stringBuilder.Append("\f");
                        break;
                    }

                    case 'n':
                    {
                        stringBuilder.Append("\n");
                        break;
                    }

                    case 'r':
                    {
                        stringBuilder.Append("\r");
                        break;
                    }

                    case 't':
                    {
                        stringBuilder.Append("\t");
                        break;
                    }

                    case 'v':
                    {
                        stringBuilder.Append("\v");
                        break;
                    }

                    default:
                    {
                        throw new Water.Error("Unknown escape sequence: \\" + ch);
                    }
                    }

                    isEscaped = false;
                }
                else if (ch == '\\')
                {
                    isEscaped = true;
                }
                else if (!isEscaped && ch == '"')
                {
                    return(stringBuilder.ToString());
                }
                else
                {
                    stringBuilder.Append(ch);
                }
            }

            throw new Water.Error("Parser error: End of double-quoted string expected.  " + stringBuilder.ToString());

            #endregion
        }
Esempio n. 24
0
 public ConsoleReader(string s)
 {
     this._reader = new Water.TextReader(new System.IO.StringReader(s + "\n"));
 }
Esempio n. 25
0
 public StatementParser(System.IO.TextReader reader)
 {
     this._reader = new Water.TextReader(reader);
 }
Esempio n. 26
0
        private Water.Statement ParseStatement(Water.TextReader reader)
        {
            Water.Parser.TrimWhitespace(reader);

            if (reader.Peek() == -1)
            {
                return(null);
            }

            char ch = (char)reader.Peek();

            if (ch == ';')
            {
                Water.Parser.ParseComment(reader);

                // Try it again.
                return(ParseStatement(reader));
            }

            string file   = reader.Filename;
            int    line   = reader.LineNumber;
            int    column = reader.ColumnNumber;

            Water.List expressions = new Water.List();

            while (reader.Peek() != -1)
            {
                if (Water.Parser.IsNewline(reader))
                {
//TODO DELETE					Water.Parser.TrimNewline(reader);
                    break;
                }

                if (Water.Parser.IsComment(reader))
                {
                    Water.Parser.ParseComment(reader);
                    break;
                }

                expressions.Add(Water.Parser.ParseExpression(reader));

                Water.Parser.TrimSpace(reader);
            }

            if (expressions.Count == 0)
            {
                // Try it again.
                return(ParseStatement(reader));
            }

            Water.Blocks.Block block = GetBlock(expressions);
            if (block != null)
            {
                Water.Statement statement = new Water.Statement(file, line, column, expressions);

                expressions.Add(
                    ParseBlockStatements(
                        reader,
                        block.Name,
                        "end_" + block.Name,
                        block.IsRecursive,
                        statement));

                return(statement);
            }

            return(new Water.Statement(file, line, column, expressions));
        }
Esempio n. 27
0
 public StatementParser(Water.TextReader reader)
 {
     this._reader = reader;
 }