/// <summary> /// /// </summary> /// <param name="reader"></param> /// <returns></returns> public static ABNF_Concatenation Parse(System.IO.StringReader reader) { if(reader == null){ throw new ArgumentNullException("reader"); } // concatenation = repetition *(1*c-wsp repetition) // repetition = [repeat] element ABNF_Concatenation retVal = new ABNF_Concatenation(); while(true){ ABNF_Repetition item = ABNF_Repetition.Parse(reader); if(item != null){ retVal.m_pItems.Add(item); } // We reached end of string. else if(reader.Peek() == -1){ break; } // We have next concatenation item. else if(reader.Peek() == ' '){ reader.Read(); } // We have unexpected value, probably concatenation ends. else{ break; } } return retVal; }
/// <summary> /// /// </summary> /// <param name="reader"></param> /// <returns></returns> public static ABNF_Alternation Parse(System.IO.StringReader reader) { if(reader == null){ throw new ArgumentNullException("reader"); } // alternation = concatenation *(*c-wsp "/" *c-wsp concatenation) ABNF_Alternation retVal = new ABNF_Alternation(); while(true){ ABNF_Concatenation item = ABNF_Concatenation.Parse(reader); if(item != null){ retVal.m_pItems.Add(item); } // We reached end of string. if(reader.Peek() == -1){ break; } // We have next alternation item. else if(reader.Peek() == '/'){ reader.Read(); } // We have unexpected value, probably alternation ends. else{ break; } } return retVal; }
/// <summary> /// /// </summary> /// <param name="reader"></param> /// <returns></returns> public static ABNF_RuleName Parse(System.IO.StringReader reader) { if(reader == null){ throw new ArgumentNullException("reader"); } // RFC 5234 4. // rulename = ALPHA *(ALPHA / DIGIT / "-") if(!char.IsLetter((char)reader.Peek())){ throw new ParseException("Invalid ABNF 'rulename' value '" + reader.ReadToEnd() + "'."); } StringBuilder ruleName = new StringBuilder(); while(true){ // We reached end of string. if(reader.Peek() == -1){ break; } // We have valid rule name char. else if(char.IsLetter((char)reader.Peek()) | char.IsDigit((char)reader.Peek()) | (char)reader.Peek() == '-'){ ruleName.Append((char)reader.Read()); } // Not rule name char, probably readed name. else{ break; } } return new ABNF_RuleName(ruleName.ToString()); }
//--------------------------------------------------------------------- /// <summary> /// Reads and ignores whitespace characters from a /// System.IO.TextReader. /// </summary> public static void SkipWhitespace(System.IO.TextReader reader) { int i = reader.Peek(); while (i != -1 && char.IsWhiteSpace((char) i)) { reader.Read(); i = reader.Peek(); } }
public override AbstractToken ScanToken(System.IO.StringReader sr) { string accum = ""; while (char.IsNumber((char)sr.Peek()) && sr.Peek() != -1) { accum += (char)sr.Read(); } return new TokenIntLiteral(Int32.Parse(accum)); }
public static JsonNumber Parse(System.IO.TextReader json) { StringBuilder sb = new StringBuilder(); while (json.Peek()>='0' && json.Peek() <= '9') { sb.Append((char)json.Read()); } return new JsonNumber(Int64.Parse(sb.ToString())); }
public override AbstractToken ScanToken(System.IO.StringReader sr) { string accum = ""; while ((char.IsLetterOrDigit((char)sr.Peek()) || ((char)sr.Peek()) == '_') && sr.Peek() != -1) { accum += (char)sr.Read(); } return new TokenIdentifier(accum); }
//--------------------------------------------------------------------- /// <summary> /// Reads a word from a System.IO.TextReader. A word is one or more /// adjacent non-whitespace characters. /// </summary> public static string ReadWord(System.IO.TextReader reader) { StringBuilder word = new StringBuilder(); int i = reader.Peek(); while (i != -1 && ! char.IsWhiteSpace((char) i)) { word.Append((char) reader.Read()); i = reader.Peek(); } return word.ToString(); }
public override AbstractToken ScanToken(System.IO.StringReader sr) { sr.Read(); string accum = ""; while ('"' != (char)sr.Peek() && sr.Peek() != -1) { accum += (char)sr.Read(); } return new TokenStringLiteral(accum); }
public static int SearchB64Combined(System.IO.TextReader reader, List<Found> found) { int startCount = found.Count; int lineCount = 0; int startLine = 0; string line; System.Text.StringBuilder concatd = new StringBuilder(); while (reader.Peek() != -1 && (line = reader.ReadLine()) != null) { if (s_isB64.IsMatch(line)) { concatd.Append(line); Match m = s_B64Parser.Match(concatd.ToString()); if (m.Success) { Found f = new Found() { Line = startLine, Match = m.Value }; AddGuidsFromBytes(Convert.FromBase64String(m.Value), f); found.Add(f); startLine = lineCount+1; concatd.Clear(); } } else { concatd.Clear(); startLine = lineCount+1; } lineCount++; } return found.Count - startCount; }
public static List<Employee> Load(System.IO.StreamReader stream) { List<Employee> list = new List<Employee>(); try { while (stream.Peek() >= 0) { string line = stream.ReadLine(); string[] values = line.Split(','); Employee employee = new Employee(); employee.FirstName = values[0]; employee.LastName = values[1]; employee.Email = values[2]; list.Add(employee); } } catch (Exception) { throw; } finally { stream.Close(); } return list; }
public static JsonString Parse(System.IO.TextReader json) { char startString = (char)json.Read(); StringBuilder sb = new StringBuilder(); while (startString != json.Peek() && json.Peek() != -1) { char curr = (char)json.Read(); if (curr == '\\') { curr = (char)json.Read(); } sb.Append(curr); } char endString = (char)json.Read(); System.Diagnostics.Debug.Assert(endString == startString); return new JsonString(sb.ToString()); }
private static object ReadNull(System.IO.TextReader reader) { EatWhiteSpace(reader); var ch = (char)reader.Peek(); if (ch == 'n') { reader.Read(); reader.Read(); reader.Read(); reader.Read(); // read chars n,u,l,l } return null; }
public static IEnumerable<Found> Find(System.IO.TextReader reader, string type) { IGuidSearcher searcher; if (!s_SearchesByKey.TryGetValue(type, out searcher)) throw new ArgumentOutOfRangeException(); int lineCount = 0; string line; while (reader.Peek() != -1 && (line = reader.ReadLine()) != null) { foreach(Found found in searcher.Find(line, lineCount++)) yield return found; } }
/// <summary> /// /// </summary> /// <param name="context_name"></param> /// <param name="stringCodeReader"></param> /// <returns></returns> public override object Read(string context_name, System.IO.TextReader stringCodeReader, OutputDelegate WriteLine) { string res = null; int line = 0; while (stringCodeReader.Peek() != -1) { line++; res += stringCodeReader.ReadToEnd(); } return res; } // method: Read
private static object ReadBool(System.IO.TextReader reader) { EatWhiteSpace(reader); var ch = (char)reader.Peek(); if (ch == 't') { reader.Read(); reader.Read(); reader.Read(); reader.Read(); // read chars t,r,u,e return true; } reader.Read(); reader.Read(); reader.Read(); reader.Read(); reader.Read(); // read char f,a,l,s,e return false; }
public static JsonBase BaseParse(System.IO.TextReader json) { if (json.Peek() == -1) { return null; } switch ((char)json.Peek()) { case '{': return JsonCustomObject.Parse(json); case '\'': case '\"': return JsonString.Parse(json); case '[': return JsonCustomArray.Parse(json); case '0': case '1': case '2': case '3': case '4': case '5': case '6': case'7': case '8': case '9': return JsonNumber.Parse(json); default: // we hit some character we didn't recognize - if it's proper JSON, it's just whitespace. json.Read(); return BaseParse(json); } }
internal static JsonBase Parse(System.IO.TextReader json) { JsonCustomArray result = new JsonCustomArray(); while (json.Peek() != ']') { char comma = (char)json.Read(); System.Diagnostics.Debug.Assert(comma == ',' || comma == '['); result.Properties.Add(JsonBase.BaseParse(json)); } char close = (char)json.Read(); System.Diagnostics.Debug.Assert(close == ']'); return result; }
/// <summary> /// /// </summary> /// <param name="reader"></param> /// <returns></returns> public static ABNF_Option Parse(System.IO.StringReader reader) { if(reader == null){ throw new ArgumentNullException("reader"); } // option = "[" *c-wsp alternation *c-wsp "]" if(reader.Peek() != '['){ throw new ParseException("Invalid ABNF 'option' value '" + reader.ReadToEnd() + "'."); } // Eat "[". reader.Read(); // TODO: *c-wsp ABNF_Option retVal = new ABNF_Option(); // We reached end of stream, no closing "]". if(reader.Peek() == -1){ throw new ParseException("Invalid ABNF 'option' value '" + reader.ReadToEnd() + "'."); } retVal.m_pAlternation = ABNF_Alternation.Parse(reader); // We don't have closing ")". if(reader.Peek() != ']'){ throw new ParseException("Invalid ABNF 'option' value '" + reader.ReadToEnd() + "'."); } else{ reader.Read(); } return retVal; }
internal static JsonBase Parse(System.IO.TextReader json) { JsonCustomObject result = new JsonCustomObject(); while (json.Peek() != '}') { char comma = (char)json.Read(); System.Diagnostics.Debug.Assert(comma == ',' || comma == '{'); string key = JsonString.Parse(json).Value; char c = (char)json.Read(); System.Diagnostics.Debug.Assert(c == ':'); result.Properties[key] = JsonBase.BaseParse(json); } char close = (char)json.Read(); System.Diagnostics.Debug.Assert(close== '}'); return result; }
public static int SearchB64(System.IO.TextReader reader, List<Found> found) { int startcount = found.Count; int lineCount = 0; string line; while (reader.Peek() != -1 && (line = reader.ReadLine()) != null) { MatchCollection mc = s_B64Parser_Single.Matches(line); foreach (Match m in mc) { found.Add(new Found() { Line = lineCount, Match = m.Value, Guids = new Guid[] { new Guid(Convert.FromBase64String(m.Value)) } }); } lineCount++; } return found.Count - startcount; }
/// <summary> /// /// </summary> /// <param name="reader"></param> /// <returns></returns> public static ABNF_ProseVal Parse(System.IO.StringReader reader) { if(reader == null){ throw new ArgumentNullException("reader"); } /* prose-val = "<" *(%x20-3D / %x3F-7E) ">" ; bracketed string of SP and VCHAR ; without angles ; prose description, to be used as ; last resort */ if(reader.Peek() != '<'){ throw new ParseException("Invalid ABNF 'prose-val' value '" + reader.ReadToEnd() + "'."); } // Eat "<" reader.Read(); // TODO: *c-wsp StringBuilder value = new StringBuilder(); while(true){ // We reached end of stream, no closing DQUOTE. if(reader.Peek() == -1){ throw new ParseException("Invalid ABNF 'prose-val' value '" + reader.ReadToEnd() + "'."); } // We have closing ">". else if(reader.Peek() == '>'){ reader.Read(); break; } // Allowed char. else if((reader.Peek() >= 0x20 && reader.Peek() <= 0x3D) || (reader.Peek() >= 0x3F && reader.Peek() <= 0x7E)){ value.Append((char)reader.Read()); } // Invalid value. else{ throw new ParseException("Invalid ABNF 'prose-val' value '" + reader.ReadToEnd() + "'."); } } return new ABNF_ProseVal(value.ToString()); }
private object Read(System.IO.TextReader reader) { EatWhiteSpace(reader); var ichar = reader.Peek(); if (ichar < 0) throw new InvalidDataException(@"end of stream reached"); var c = (char)ichar; // char type // 'n' null // '\d' number // '"' or '\'' string // 'f' or 't' bool // '{' object (hash) // '[' array if (c == 'n') return ReadNull(reader); if (c == 'f' || c == 't') return ReadBool(reader); if (c == '"' || c == '\'') return ReadString(reader); if (c == '[') return ReadArray(reader); return (c == '{') ? ReadObject(reader) : ReadNumber(reader); }
/// <summary> /// /// </summary> /// <param name="reader"></param> /// <returns></returns> public static ABNF_CharVal Parse(System.IO.StringReader reader) { if(reader == null){ throw new ArgumentNullException("reader"); } /* char-val = DQUOTE *(%x20-21 / %x23-7E) DQUOTE ; quoted string of SP and VCHAR ; without DQUOTE */ if(reader.Peek() != '\"'){ throw new ParseException("Invalid ABNF 'char-val' value '" + reader.ReadToEnd() + "'."); } // Eat DQUOTE reader.Read(); // TODO: *c-wsp StringBuilder value = new StringBuilder(); while(true){ // We reached end of stream, no closing DQUOTE. if(reader.Peek() == -1){ throw new ParseException("Invalid ABNF 'char-val' value '" + reader.ReadToEnd() + "'."); } // We have closing DQUOTE. else if(reader.Peek() == '\"'){ reader.Read(); break; } // Allowed char. else if((reader.Peek() >= 0x20 && reader.Peek() <= 0x21) || (reader.Peek() >= 0x23 && reader.Peek() <= 0x7E)){ value.Append((char)reader.Read()); } // Invalid value. else{ throw new ParseException("Invalid ABNF 'char-val' value '" + reader.ReadToEnd() + "'."); } } return new ABNF_CharVal(value.ToString()); }
public override bool CanScanToken(System.IO.StringReader sr) { return char.IsLetter((char)sr.Peek()); }
public override bool CanScanToken(System.IO.StringReader sr) { return ('"' == (char)sr.Peek()); }
private static object ReadString(System.IO.TextReader reader) { EatWhiteSpace(reader); var ch = (char)reader.Peek(); reader.Read(); // consume single or double quote char[] chars = { ch }; var result_raw = Seek(reader, chars); var result = result_raw.Replace(@"\u0022", @""""); result = result.Replace(@"\u005c", @"\"); reader.Read(); // consume escaped character return result; }
private static string Seek(System.IO.TextReader reader, char[] values) { var result = @""; var done = false; foreach (char ch in values) { if ((char)reader.Peek() == ch) { done = true; } } var builder = new StringBuilder(); builder.Append(result); while (!done) { var ichar = reader.Read(); if (ichar < 0) { done = true; } else { builder.Append((char)ichar); foreach (char ch in values) { if ((char)reader.Peek() == ch) { done = true; } } } } result = builder.ToString(); return result; }
private static void EatWhiteSpace(System.IO.TextReader reader) { while (Char.IsWhiteSpace((char)(reader.Peek()))) { reader.Read(); } }
private IDictionary ReadObject(System.IO.TextReader reader) { var dictionary = Activator.CreateInstance(DefaultObjectType) as IDictionary; Seek(reader, '{'); reader.Read(); // consume the '{' EatWhiteSpace(reader); var done = false; if ((char)(reader.Peek()) == '}') { done = true; reader.Read(); // consume the '}' } while (!done) { EatWhiteSpace(reader); var key = ReadString(reader) as string; var lastKey = key; EatWhiteSpace(reader); var ch = (char)reader.Peek(); reader.Read(); //consume ':' dictionary[key] = Read(reader); EatWhiteSpace(reader); ch = (char)reader.Peek(); if (ch == ',') reader.Read(); // consume ',' EatWhiteSpace(reader); ch = (char)reader.Peek(); if (ch == '}') { reader.Read(); done = true; } } return dictionary; }