private static void ParseDefSubClass(ScanReader reader, GenDataDef f, string className) { reader.ScanWhile(ScanReader.WhiteSpace); var sub = reader.ScanWhile(ScanReader.AlphaNumeric); if (reader.CheckChar('[')) { reader.SkipChar(); reader.ScanWhile(ScanReader.WhiteSpace); var field = reader.ScanWhile(ScanReader.Identifier); Contract.Assert(field.Equals("Reference", StringComparison.InvariantCultureIgnoreCase), "Data definition reference expected: " + field); reader.ScanWhile(ScanReader.WhiteSpace); Contract.Assert(reader.CheckChar('='), "Data definition [Reference=definition] expected: " + field); reader.SkipChar(); reader.ScanWhile(ScanReader.WhiteSpace); var value = reader.CheckChar('\'') ? reader.ScanQuotedString() : reader.ScanWhile(ScanReader.Identifier); f.AddSubClass(className, sub, value); reader.ScanWhile(ScanReader.WhiteSpace); Contract.Assert(reader.CheckChar(']'), "Data definition ] expected"); reader.SkipChar(); } else { f.AddSubClass(className, sub); } reader.ScanWhile(ScanReader.WhiteSpace); }
public void SkipCharTest() { const string input = "AbCd # 12345"; using (var scan = new ScanReader(input)) { scan.ScanUntilChar('#'); Assert.IsTrue(scan.CheckChar('#'), "Scan to start point"); scan.SkipChar(); Assert.IsTrue(scan.CheckChar(' '), "Space after #"); CheckScannerNotEofConditions(scan); } }
public void ScanForKeyword() { const string input = "**]"; using (var scan = new ScanReader(input)) { Assert.AreEqual("*", scan.ScanUntilKeyword("*]"), "Scan Until Keyword"); Assert.IsTrue(scan.CheckChar('*'), "First character of keyword"); } }
public void ScanKeyword() { const string input = "[* *]"; using (var scan = new ScanReader(input)) { Assert.AreEqual("[*", scan.ScanKeyword("[*"), "Scan Keyword"); Assert.IsTrue(scan.CheckChar(' '), "Space after keyword"); } }
public void MacroTest() { const string input = "Prefix #Macro1 Suffix"; const string output = "Prefix [1[2]] Suffix"; var macros = new Dictionary <string, string> { { "Macro1", "[1#Macro2]" }, { "Macro2", "[2]" } }; using (var scan = new ScanReader(input)) { var s = scan.ScanUntilChar('#'); while (scan.CheckChar('#')) { scan.SkipChar(); var t = scan.ScanWhile(ScanReader.AlphaNumeric); scan.Rescan(macros[t]); s += scan.ScanUntilChar('#'); } s += scan.ScanUntilChar(ScanReader.EofChar); Assert.AreEqual(output, s); } }
private static GenDataDef ExtractDef(ScanReader reader) { var f = new GenDataDef(); var classId = -1; var className = ""; var token = reader.ScanWhile(ScanReader.Identifier); while (token != "") { switch (token) { case "Definition": reader.ScanWhile(ScanReader.WhiteSpace); if (reader.CheckChar('=')) { reader.SkipChar(); } reader.ScanWhile(ScanReader.WhiteSpace); f.DefinitionName = reader.ScanWhile(ScanReader.QualifiedIdentifier); break; case "Class": reader.ScanWhile(ScanReader.WhiteSpace); if (reader.CheckChar('=')) { reader.SkipChar(); } reader.ScanWhile(ScanReader.WhiteSpace); className = reader.ScanWhile(ScanReader.Identifier); classId = f.AddClass(className); if (f.Classes.Count == 2) { f.AddSubClass("", className); } reader.ScanWhile(ScanReader.WhiteSpace); if (reader.CheckChar('[')) { do { reader.SkipChar(); reader.ScanWhile(ScanReader.WhiteSpace); var inheritorClassName = reader.ScanWhile(ScanReader.Identifier); f.AddInheritor(className, inheritorClassName); reader.ScanWhile(ScanReader.WhiteSpace); } while (reader.CheckChar(',')); Contract.Assert(reader.CheckChar(']'), "Definition Error for class " + className + ": ] expected"); reader.SkipChar(); } break; case "Field": reader.ScanWhile(ScanReader.WhiteSpace); if (reader.CheckChar('=')) { reader.SkipChar(); } if (reader.CheckChar('{')) { do { reader.SkipChar(); reader.ScanWhile(ScanReader.WhiteSpace); var field = reader.ScanWhile(ScanReader.Identifier); f.AddClassInstanceProperty(classId, field); reader.ScanWhile(ScanReader.WhiteSpace); } while (reader.CheckChar(',')); Contract.Assert(reader.CheckChar('}'), "Definition Error for class " + className + " fields list: } expected"); reader.SkipChar(); } else { reader.ScanWhile(ScanReader.WhiteSpace); var field = reader.ScanWhile(ScanReader.Identifier); f.AddClassInstanceProperty(classId, field); } break; case "SubClass": reader.ScanWhile(ScanReader.WhiteSpace); if (reader.CheckChar('=')) { reader.SkipChar(); } if (reader.CheckChar('{')) { while (!reader.CheckChar('}')) { reader.SkipChar(); ParseDefSubClass(reader, f, className); } reader.SkipChar(); } else { ParseDefSubClass(reader, f, className); reader.ScanWhile(ScanReader.WhiteSpace); } break; } reader.ScanWhile(ScanReader.WhiteSpace); token = reader.ScanWhile(ScanReader.Identifier); } return(f); }
/// <summary> /// Verify that the scanner state is correct when the end of the data has /// not been reached /// </summary> /// <param name="scan"></param> private static void CheckScannerNotEofConditions(ScanReader scan) { Assert.IsFalse(scan.Eof); Assert.IsFalse(scan.CheckChar(ScanReader.EofChar), "Ctrl-Z expected at EOF"); }