Exemplo n.º 1
0
        public void ParserFuncTest()
        {
            Lexicon test = new Lexicon();

            var ID = test.DefaultLexer.DefineToken(RE.Range('a', 'z').Concat(
                (RE.Range('a', 'z') | RE.Range('0', '9')).Many()));
            var NUM = test.DefaultLexer.DefineToken(RE.Range('0', '9').Many1());
            var GREATER = test.DefaultLexer.DefineToken(RE.Symbol('>'));

            var WHITESPACE = test.DefaultLexer.DefineToken(RE.Symbol(' ').Union(RE.Symbol('\t')));

            var p1 = from i in ID
                     from g in GREATER
                     from g2 in GREATER.AsParser(true)
                     from n in NUM
                     select "A";

            var p2 = from i in ID
                     from g in GREATER
                     from g2 in GREATER
                     from n in NUM
                     select "B";

            var parser1 = p1 | p2;

            var info = test.CreateScannerInfo();
            ForkableScannerBuilder builder = new ForkableScannerBuilder(info);
            builder.SetSkipTokens(WHITESPACE.Index);

            var errorManager = new CompilationErrorManager();
            var context = new ParserContext(errorManager, 1, 2);
            context.DefineDefaultCompilationErrorInfo(0);

            ParserRunner<string> runner = new ParserRunner<string>(parser1, context);

            string source1 = "abc >> 123";
            var sr1 = new SourceReader(new StringReader(source1));

            ForkableScanner scanner1 = builder.Create(sr1);

            var result1 = runner.Run(scanner1);

            Assert.AreEqual("A", result1);
            Assert.AreEqual(0, errorManager.Errors.Count);

            string source2 = "abc > > 123";
            var sr2 = new SourceReader(new StringReader(source2));

            ForkableScanner scanner2 = builder.Create(sr2);

            var result2 = runner.Run(scanner2);
            Assert.AreEqual("B", result2);
            Assert.AreEqual(0, errorManager.Errors.Count);
        }
Exemplo n.º 2
0
        public void ParserCastTest()
        {
            Lexicon test = new Lexicon();

            var ID = test.Lexer.DefineToken(RE.Range('a', 'z').Concat(
                (RE.Range('a', 'z') | RE.Range('0', '9')).Many()));
            var NUM = test.Lexer.DefineToken(RE.Range('0', '9').Many1());
            var GREATER = test.Lexer.DefineToken(RE.Symbol('>'));

            var WHITESPACE = test.Lexer.DefineToken(RE.Symbol(' ').Union(RE.Symbol('\t')));

            var p1 = from i in ID
                     from g in GREATER
                     from g2 in GREATER
                     from n in NUM
                     select "hello";

            var parser1 = p1.TryCast<object>();

            var info = test.CreateScannerInfo();
            ForkableScannerBuilder builder = new ForkableScannerBuilder(info);
            builder.SetTriviaTokens(WHITESPACE.Index);

            var errorManager = new CompilationErrorManager();
            var context = new ParserContext(errorManager, 1, 2);
            context.DefineDefaultCompilationErrorInfo(0);

            var el = errorManager.CreateErrorList();
            context.ErrorList = el;

            ParserRunner<object> runner = new ParserRunner<object>(parser1, context);

            string source1 = "abc >> 123";
            var sr1 = new SourceReader(new StringReader(source1));

            ForkableScanner scanner1 = builder.Create(sr1);

            var result1 = runner.Run(scanner1);

            Assert.AreEqual("hello", result1);
            Assert.AreEqual(0, el.Count);
        }
Exemplo n.º 3
0
        public void ForkableScannerTest()
        {
            Lexicon lexicon = new Lexicon();
            var A = lexicon.Lexer.DefineToken(RE.Range('a', 'z'));

            ScannerInfo si = lexicon.CreateScannerInfo();
            string source = "abcdefghijklmnopqrstuvwxyz";

            ForkableScannerBuilder fsBuilder = new ForkableScannerBuilder(si);
            ForkableScanner fscanner = fsBuilder.Create(new SourceReader(new StringReader(source)));

            var l1 = fscanner.Read();
            Assert.AreEqual("a", l1.Value.Content);
            var l2 = fscanner.Read();
            Assert.AreEqual("b", l2.Value.Content);

            //fork
            ForkableScanner fscanner2 = fscanner.Fork();

            for (int i = 2; i <= 4; i++)
            {
                var l = fscanner.Read();
                Assert.AreEqual(source[i].ToString(), l.Value.Content);
            }

            for (int i = 2; i <= 5; i++)
            {
                var l = fscanner2.Read();
                Assert.AreEqual(source[i].ToString(), l.Value.Content);
            }

            ForkableScanner fscanner3 = fscanner.Fork();

            var l5a = fscanner.Read();
            var l5b = fscanner3.Read();

            Assert.AreEqual(source[5].ToString(), l5a.Value.Content);
            Assert.AreEqual(source[5].ToString(), l5b.Value.Content);

            var l6b = fscanner2.Read();
            var l6a = fscanner3.Read();

            Assert.AreEqual(source[6].ToString(), l6a.Value.Content);
            Assert.AreEqual(source[6].ToString(), l6b.Value.Content);

            var l7a = fscanner2.Read();

            for (int i = 7; i < 9; i++)
            {
                var l = fscanner3.Read();
                Assert.AreEqual(source[i].ToString(), l.Value.Content);
            }
        }
Exemplo n.º 4
0
        public void Test(SourceReader sr)
        {
            Console.WriteLine("=============== CPS Parser Combinators ==================");

            SetUpScanner();
            var production = SetUpParser();

            ForkableScannerBuilder fsb = new ForkableScannerBuilder(m_scannerInfo);
            fsb.SetTriviaTokens(SPACE.Index);

            var scanner = fsb.Create(sr);

            var runner = new ParserRunner<int>(production);

            try
            {
                var result = runner.Execute(scanner);
                Console.WriteLine("Result: {0}", result);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Parse Errors:");
                Console.WriteLine(ex.Message);
            }

            Console.WriteLine();
            Console.WriteLine();
        }
        public void Test(SourceReader sr)
        {
            Console.WriteLine("=============== Error Recovery Parser Combinators ======="); 

            SetUpScanner();
            var production = SetUpParser();

            ForkableScannerBuilder fsb = new ForkableScannerBuilder(m_scannerInfo);
            fsb.SetTriviaTokens(SPACE.Index);

            var scanner = fsb.Create(sr);

            var runner = new ParserRunner<int>(production);

            List<SyntaxError> errors = new List<SyntaxError>();
            var result = runner.Execute(scanner, errors);

            if (errors.Count == 0)
            {
                Console.WriteLine("Result: {0}", result);
            }
            else
            {
                Console.WriteLine("Parse Errors:");
                foreach (var err in errors)
                {
                    Console.WriteLine("{0}:{1} {2}",
                        err.Code,
                        err.Description,
                        err.Location.StartLocation);
                }
            }

            Console.WriteLine();
            Console.WriteLine();
        }
Exemplo n.º 6
0
        public void Test(SourceReader sr)
        {
            Console.WriteLine("=============== Simple Parser Combinators ===============");
            SetUpScanner();
            var parse = SetUpParser();

            ForkableScannerBuilder fsb = new ForkableScannerBuilder(m_scannerInfo);
            fsb.SetTriviaTokens(SPACE.Index);


            var scanner = fsb.Create(sr);

            try
            {
                var result = parse(scanner);
                Console.WriteLine("Result: {0}", result.Value);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Parse Errors:");
                Console.WriteLine(ex.Message);
            }
            Console.WriteLine();
            Console.WriteLine();
        }