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 }
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 }
// // 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 }
// // 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 }
public static void TrimNewline(Water.TextReader reader) { #region TrimNewline while (reader.Peek() != -1) { if (!IsNewline((char)reader.Peek())) { return; } reader.Read(); } #endregion }
// // 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 }
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 }
public static void ParseComment(Water.TextReader reader) { #region ParseComment while (reader.Peek() != -1) { if (IsNewline((char)reader.Peek())) { return; } else { reader.Read(); } } #endregion }
private bool IsSpace(Water.TextReader reader) { #region IsSpace char ch = (char)reader.Peek(); return(IsSpace(ch)); #endregion }
public static bool IsNewline(Water.TextReader reader) { #region IsNewline char ch = (char)reader.Peek(); return(IsNewline(ch)); #endregion }
// // Comment // public static bool IsComment(Water.TextReader reader) { #region IsComment char ch = (char)reader.Peek(); return(IsComment(ch)); #endregion }
// // 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 }
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 }
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."); }
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 }
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)); }
// // 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 }