Exemple #1
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();
        }
Exemple #2
0
        private void OnInitialize()
        {
            m_lexicon = new Lexicon();

            OnDefineLexer(m_lexicon, m_triviaTokens);

            m_scannerInfo = OnCreateScannerInfo();

            var parser = OnDefineParser();

            if (parser == null)
            {
                throw new InvalidOperationException("Parser not defined");
            }

            m_parser = parser.SuffixedBy(Parsers.Eos());

            m_context = new ParserContext(m_errorManager, m_unexpectedTokenErrorId, m_missingTokenErrorId);

            OnDefineParserErrors(m_errorManager);

            m_parserRunner = new ParserRunner <T>(m_parser, m_context);

            m_scannerBuilder = new ForkableScannerBuilder(m_scannerInfo);
            m_scannerBuilder.SetTriviaTokens(m_triviaTokens.Select(t => t.Index).ToArray());
            m_scannerBuilder.ErrorManager   = m_errorManager;
            m_scannerBuilder.RecoverErrors  = true;
            m_scannerBuilder.LexicalErrorId = m_lexicalErrorId;

            m_isInitialized = true;
        }
        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();
        }
Exemple #4
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(l => l.PrefixTrivia.Count == 0)
                     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.SetTriviaTokens(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);
        }
Exemple #5
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);
        }
Exemple #6
0
        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();
        }