public void ExplicitSentinelCharTest() { var lang = Language.Get(typeof(WithSentinel)); int TOK1 = lang.Identify(typeof(ASB)); var input = "a\0b"; using (var interp = new Interpreter<WithSentinel>()) { var tokens = interp.Scan(input).ToArray(); Assert.AreEqual( new[] { new Msg(TOK1, ASB.Value, new Loc(0, 3)) }, tokens); } }
public void ImplicitSentinelTest() { var lang = Language.Get(typeof(LineComment)); int TOK1 = lang.Identify(typeof(string)); using (var interp = new Interpreter<LineComment>()) { var input = "b"; var tokens = interp.Scan(input).ToArray(); Assert.AreEqual( new[] { new Msg(TOK1, "b", new Loc(0, 1)) }, tokens); } }
public void Test() { var context = new LCLang(); using (var interp = new Interpreter<LCLang>(context)) { var lang = Language.Get(typeof(LCLang)); string text = "at-1\r\natNL-2\nat-3\nat-4\nbegin-5\r\n\n\r\nend-8\r\nat-9"; var hlocs = interp.Scan(text).Select(msg => msg.HLocation).ToArray(); Assert.AreEqual(context.Result[0], hlocs[0]); Assert.AreEqual(context.Result[1], hlocs[1]); Assert.AreEqual(context.Result[2], hlocs[2]); Assert.AreEqual(context.Result[3], hlocs[3]); Assert.AreEqual(context.Result[4], hlocs[4]); } }
public void Test() { var lang = Language.Get(typeof(Re2cSample)); using (var interp = new Interpreter<Re2cSample>()) { var tokens = interp.Scan("print x 123 0x434af").ToArray(); var expected = new object[] { "$print$", "x", new Decimal("123"), new HexDecimal("0x434af") }; var got = tokens.Select(msg => msg.Value).ToArray(); Assert.AreEqual(expected, got); Assert.AreEqual( new int[] { lang.Identify("print") , lang.Identify(typeof(string)), lang.Identify(typeof(Decimal)), lang.Identify(typeof(HexDecimal)) }, tokens.Select(msg => msg.Id)); } }
private static void Benchmarks( string path, string title, Type langDef, int trialCount = 1, BenchFlags flags = BenchFlags.All) { var timer = new Stopwatch(); timer.Start(); var lang = Language.Get(langDef); using (var interp = new Interpreter(lang)) { interp.Parse("a+a"); } timer.Stop(); Log("--------------- " + title + " ---------------"); Log("heatup time = {0}", timer.Elapsed); timer.Reset(); using (var testFile = new StreamReader(path)) { timer.Start(); testFile.ReadToEnd(); timer.Stop(); Log("read time = {0}", timer.Elapsed); } using (var interp = new Interpreter(lang)) { for (int trial = 0; trial != trialCount; ++trial) { timer.Reset(); using (var testFile = new StreamReader(path)) { timer.Start(); int count = interp.Scan(testFile, path).Count(); timer.Stop(); Log("scan time = {0} tokenCount = {1}", timer.Elapsed, count); } } // Recognize if ((flags & BenchFlags.Recognize) == BenchFlags.Recognize) { for (int trial = 0; trial != trialCount; ++trial) { timer.Reset(); using (var testFile = new StreamReader(path)) { timer.Start(); bool success = interp.Recognize(testFile, path); timer.Stop(); Assert.IsTrue(success); Log("parse (recognize) = {0}", timer.Elapsed); } } } // Parse actions if ((flags & BenchFlags.ParseActions) == BenchFlags.ParseActions) { for (int trial = 0; trial != trialCount; ++trial) { timer.Reset(); using (var testFile = new StreamReader(path)) { timer.Start(); bool success = interp.Parse(testFile, path); timer.Stop(); Assert.IsTrue(success); Log("parse (actions) = {0}", timer.Elapsed); } } } // Parse tree if ((flags & BenchFlags.ParseTree) == BenchFlags.ParseTree) { for (int trial = 0; trial != trialCount; ++trial) { timer.Reset(); using (var testFile = new StreamReader(path)) { timer.Start(); interp.BuildTree(testFile, path); timer.Stop(); Log("parse (tree) = {0}", timer.Elapsed); } } } } }
private static List<Msg> Scan(string input) { var context = new MyMiniLexer(); using (var interp = new Interpreter<MyMiniLexer>()) { var result = interp.Scan(input).ToList(); return result; } }