private static NcParser GetSqlParser() { var assembly = Assembly.GetExecutingAssembly(); return(NcParser.LoadFromAssembly(assembly, "Nccc.Tests.SQL.sql.grammer", settings => { settings.CaseSensitive = false; })); }
public void TestSample() { var src = Assembly.GetExecutingAssembly().ReadString("Nccc.Tests.Json.sample.json"); var parser = NcParser.LoadFromAssembly(Assembly.GetExecutingAssembly(), "Nccc.Tests.Json.json.grammer"); var parseResult = parser.Parse(src); Console.WriteLine(parseResult.ToSExp().ToPrettyString()); Assert.IsTrue(parseResult.IsSuccess()); }
public void TestParse() { var parser = NcParser.Load(grammer); var sexpCode = @" (define (double x) (+ x x)) (define (gcd a b) (if (= a 0) b (gcd (remainder b a) a))) "; var parseResult = parser.Parse(sexpCode); Console.WriteLine(parseResult.ToSExp().ToPrettyString()); Assert.IsTrue(parseResult.IsSuccess()); }
public void TestCIfFail() { var grmr = @" ::root @include-builtin root = (@+ alpha) \eof "; var parser = NcParser.Load(grmr); var result = parser.Parse("a1"); Console.WriteLine(result.ToSExp().ToPrettyString()); Assert.IsFalse(result.IsSuccess()); }
private void ParseAndPrint(string grammer, string source) { var postProcessedGrammer = @" ::root @include-builtin spacing = ~(@* \space) " + grammer; var parser = NcParser.Load(postProcessedGrammer); Console.WriteLine($"source: \"{source}\""); var result = parser.Parse(source); Console.WriteLine(result.ToSExp().ToPrettyString()); Assert.IsTrue(result.IsSuccess()); }
public void TestErr() { var ncccParser0 = new NcGP(); var ncGrammer = NcGP.GetNcGrammerSource(); var ncccParser1 = NcParser.Load(ncGrammer); var ncccParser2 = _P2P(ncccParser1, ncGrammer); var errGrammer = @"::dummy @hehe '123'"; var pr0 = ncccParser0.Parse(errGrammer); var pr1 = ncccParser1.Parse(errGrammer); var pr2 = ncccParser2.Parse(errGrammer); Assert.IsFalse(pr0.IsSuccess()); Assert.IsFalse(pr1.IsSuccess()); Assert.IsFalse(pr2.IsSuccess()); Assert.AreEqual(0, Utils.DiffAndShow(pr0, pr1).Count()); Assert.AreEqual(0, Utils.DiffAndShow(pr0, pr2).Count()); }
public void TestSelf() { var ncccParser0 = new NcGP(); var ncGrammer = NcGP.GetNcGrammerSource(); var ncccParser1 = NcParser.Load(ncGrammer); var ncccParser2 = _P2P(ncccParser1, ncGrammer); var pr0 = ncccParser0.Parse(ncGrammer); Assert.IsTrue(pr0.IsSuccess()); var pr1 = ncccParser1.Parse(ncGrammer); Assert.IsTrue(pr1.IsSuccess()); Assert.AreEqual(0, Utils.DiffAndShow(pr0, pr1).Count()); var pr2 = ncccParser2.Parse(ncGrammer); Assert.IsTrue(pr2.IsSuccess()); Assert.AreEqual(0, Utils.DiffAndShow(pr0, pr2).Count()); }
public void TestError() { var expectMessage = "哈哈"; var src = "[1,2,3"; var parser = NcParser.LoadFromAssembly(Assembly.GetExecutingAssembly(), "Nccc.Tests.Json.json.grammer", settings => { settings.Locale.Language = "zh-cn"; settings.Locale.Set("zh-cn", new Dictionary <string, string> { { "expect", expectMessage } }); }); var parseResult = parser.Parse(src); Console.WriteLine(parseResult.ToSExp().ToPrettyString()); Assert.IsFalse(parseResult.IsSuccess()); Assert.IsTrue(parseResult.Message.Contains(expectMessage)); }
public void TestMessageLocale() { var parser = NcParser.Load(grammer, settings => { settings.Locale.Language = "zh-cn"; settings.Locale.Set("zh-cn", new Dictionary <string, string> { { "expect", "盼望着" }, { "or", "或" }, }); }); var source = "C"; var result = parser.Parse(source); Console.WriteLine(result.ToSExp().ToPrettyString()); Assert.IsFalse(result.IsSuccess()); Assert.AreEqual("盼望着 A 或 B", result.Message); }
private void Bootstrap3Test(string grammerPath, string samplePath) { var ncGrammer = NcGP.GetNcGrammerSource(); var ncccParser1 = NcParser.Load(ncGrammer); var ncccParser2 = _P2P(ncccParser1, ncGrammer); var testGrammer = assembly.ReadString(grammerPath); var jsonParser0 = NcParser.Load(testGrammer); var jsonParser1 = _P2P(ncccParser1, testGrammer); var jsonParser2 = _P2P(ncccParser2, testGrammer); var testSample = assembly.ReadString(samplePath); var pr0 = jsonParser0.Parse(testSample); var pr1 = jsonParser1.Parse(testSample); var pr2 = jsonParser2.Parse(testSample); Assert.IsTrue(pr0.IsSuccess()); Assert.IsTrue(pr1.IsSuccess()); Assert.IsTrue(pr2.IsSuccess()); Assert.AreEqual(0, Utils.DiffAndShow(pr0, pr1).Count()); Assert.AreEqual(0, Utils.DiffAndShow(pr0, pr2).Count()); }
public void EofTests() { var grammer = @" root = (@+ num:number) "; var postProcessedGrammer = @" ::root @include-builtin spacing = ~(@* \space) " + grammer; var parser = NcParser.Load(postProcessedGrammer); var source = "1.1 1.2 abc ef"; Console.WriteLine($"source: \"{source}\""); var result = parser.Parse(source); Console.WriteLine(result.ToSExp().ToPrettyString()); Assert.IsFalse(result.IsSuccess()); Assert.IsTrue(result.Message.Contains("EOF")); }
public void Default_Spacing_Should_Be_Globbed() { var grammer = @" root = header footer header = (@+ num:number) footer = (@+ var:(@+ alpha) spacing) "; var postProcessedGrammer = @" ::root @include-builtin spacing = ~(@* \space) " + grammer; var parser = NcParser.Load(postProcessedGrammer); var source = "1.1 1.2 abc ef"; Console.WriteLine($"source: \"{source}\""); var result = parser.Parse(source); Console.WriteLine(result.ToSExp().ToPrettyString()); Assert.IsTrue(result.IsSuccess()); Assert.IsFalse(result.Nodes.First().StringValue().EndsWith(' ')); }
private static NcParser _P2P(NcParser ncParser, string grammer, Action <NcParser.Settings> init = null) { var ast = ncParser.Parse(grammer).Nodes.First(); return(NcParser.Load(ast, init)); }