public static object ParseStringLiteral(char ch, StringIterator reader) { if (ch != '"') { return(null); } var builder = new StringBuilder(); reader.Read(); while (true) { var pk = reader.Peek(); if (pk == -1) { break; } ch = (char)pk; if (ch == '"') { reader.Read(); break; } builder.Append(ch); reader.Read(); } return(builder); }
public IEnumerable <object> Process(string str, int index = 0) { char ch; var reader = new StringIterator(str, index); while (reader.Peek() != -1) { ch = (char)reader.Peek(); bool resolved = false; foreach (var func in functions) { var result = func(ch, reader); if (result == null) { continue; } if (result != Eat) { yield return(result); } resolved = true; break; } if (!resolved) { throw new Exception("Unexpected character " + ch); } } }
public static object ParseIdentifier(char ch, StringIterator reader) { if (!char.IsLetter(ch) && ch != '_') { return(null); } var builder = new StringBuilder(); builder.Append(ch); reader.Read(); while (true) { var pk = reader.Peek(); if (pk == -1) { break; } ch = (char)pk; if (!char.IsLetterOrDigit(ch) && ch != '_') { break; } builder.Append(ch); reader.Read(); } return(builder.ToString()); }
public static object EatWhitespace(char ch, StringIterator reader) { if (char.IsWhiteSpace(ch)) { reader.Read(); return(Eat); } else { return(null); } }
public static object ParseComment(char ch, StringIterator reader) { if (ch != '-' || (char)reader.Peek(2) != '-') { return(null); } while (true) { int pk = reader.Peek(); if (pk == -1 || pk == (char)'\n') { break; } reader.Read(); } return(Eat); }
public static object ParseFloat(char ch, StringIterator reader) { if (!char.IsDigit(ch) && !(ch == '-' && char.IsDigit((char)reader.Peek(2)))) { return(null); } var builder = new StringBuilder(); builder.Append(ch); reader.Read(); //read while (true) { var pk = reader.Peek(); if (pk == -1) { break; } ch = (char)pk; if (ch == 'e') { if (reader.Peek(2) == (int)'-' || reader.Peek(2) == (int)'+') { reader.Read(); builder.Append("e"); builder.Append((char)reader.Read()); } } else if (ch != '.' && !char.IsDigit(ch)) { break; } else { builder.Append(ch); reader.Read(); } } return(float.Parse(builder.ToString(), CultureInfo.InvariantCulture)); }