Example #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);
        }
Example #2
0
        static void Main(string[] args)
        {
            string source = @"
            static class 程序入口
            {
            //中文注释
            public static void Main(string[] args)
            {
            //hello world
            Fac o;
            o = new Fac();
            System.Console.WriteLine(o.ComputeFac(123));
            }
            }

            class Fac
            {
            public int ComputeFac(int num)
            {
            int num_aux;
            if (num < 1)
            num_aux = 1;
            else
            num_aux = num * (this.ComputeFac(num - 1));
            return num_aux;
            }
            }
            ";
            CompilationErrorManager errorManager = new CompilationErrorManager();
            MiniSharpParser p = new MiniSharpParser(errorManager);
            var ast = p.Parse(source);

            if (errorManager.Errors.Count != 0)
            {
                return;
            }

            TypeDeclResolver resolver1 = new TypeDeclResolver(errorManager);
            resolver1.DefineErrors();
            resolver1.Visit(ast);

            MemberDeclResolver resolver2 = new MemberDeclResolver(errorManager, resolver1.Types);
            resolver2.DefineErrors();
            resolver2.Visit(ast);

            MethodBodyResolver resolver3 = new MethodBodyResolver(errorManager, resolver1.Types);
            resolver3.DefineErrors();
            resolver3.Visit(ast);

            ;
        }
Example #3
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);
        }
Example #4
0
 public TypeDeclResolver(CompilationErrorManager errorManager)
 {
     m_errorManager = errorManager;
     m_types = new TypeCollection();
 }
Example #5
0
 public MemberDeclResolver(CompilationErrorManager errorManager, TypeCollection types)
 {
     m_errorManager = errorManager;
     m_types = types;
 }
Example #6
0
        public void ErrorRecoveryTest()
        {
            Lexicon lexicon = new Lexicon();
            Lexer global = lexicon.Lexer;

            var ID = global.DefineToken(RE.Range('a', 'z').Concat(
                (RE.Range('a', 'z') | RE.Range('0', '9')).Many()));
            var NUM = global.DefineToken(RE.Range('0', '9').Many1());
            var WHITESPACE = global.DefineToken(RE.Symbol(' ').Many());

            ScannerInfo info = lexicon.CreateScannerInfo();
            PeekableScanner scanner = new PeekableScanner(info);

            string source = "asdf04a 1107 !@#$!@ Z if vvv xmlns 772737";
            StringReader sr = new StringReader(source);

            scanner.SetSource(new SourceReader(sr));
            scanner.SetTriviaTokens(WHITESPACE.Index);
            scanner.RecoverErrors = true;

            CompilationErrorManager em = new CompilationErrorManager();
            em.DefineError(101, 0, CompilationStage.Scanning, "Invalid token: {0}");

            scanner.ErrorManager = em;
            scanner.LexicalErrorId = 101;

            Lexeme l1 = scanner.Read();
            Assert.AreEqual(ID.Index, l1.TokenIndex);

            Lexeme l2 = scanner.Read();
            Assert.AreEqual(NUM.Index, l2.TokenIndex);

            Assert.AreEqual(0, em.Errors.Count);

            Lexeme l3 = scanner.Read();
            Assert.AreEqual(ID.Index, l3.TokenIndex);

            Assert.IsTrue(em.Errors.Count > 0);
            Assert.AreEqual(101, em.Errors[0].Info.Id);
        }
Example #7
0
        static void Main(string[] args)
        {
            string source = @"
            static class 程序入口
            {
            //中文注释
            public static void Main(string[] args)
            {
            Fac o;
            o = new Fac();
            System.Console.WriteLine(o.ComputeFac(8));
            }
            }

            class Fac
            {
            public int ComputeFac(int num)
            {
            int num_aux;
            if (num < 1)
            num_aux = 1;
            else
            num_aux = num * (this.ComputeFac(num - 1));

            return num_aux;
            }

            public int Foo()
            {
            return 1;
            }
            }

            ";

            Stopwatch sw = new Stopwatch();
            sw.Start();

            CompilationErrorManager errorManager = new CompilationErrorManager();
            CompilationErrorList errorList = errorManager.CreateErrorList();
            MiniSharpParser p = new MiniSharpParser(errorManager);
            p.Initialize();

            sw.Stop();
            Console.WriteLine("Initialize time: {0} ms", sw.ElapsedMilliseconds);
            sw.Restart();

            var ast = p.Parse(source, errorList);

            sw.Stop();
            Console.WriteLine("Parsing time: {0} ms", sw.ElapsedMilliseconds);
            sw.Restart();

            if (errorList.Count != 0)
            {
                ReportErrors(errorList);
                return;
            }

            TypeDeclResolver resolver1 = new TypeDeclResolver(errorManager);
            resolver1.DefineErrors();
            resolver1.ErrorList = errorList;
            resolver1.Visit(ast);

            MemberDeclResolver resolver2 = new MemberDeclResolver(errorManager, resolver1.Types);
            resolver2.DefineErrors();
            resolver2.ErrorList = errorList;
            resolver2.Visit(ast);

            MethodBodyResolver resolver3 = new MethodBodyResolver(errorManager, resolver1.Types);
            resolver3.DefineErrors();
            resolver3.ErrorList = errorList;
            resolver3.Visit(ast);

            sw.Stop();
            Console.WriteLine("Semantic analysis time: {0} ms", sw.ElapsedMilliseconds);

            if (errorList.Count != 0)
            {
                ReportErrors(errorList);
                return;
            }

            //generate Cil
            var codegenDomain = AppDomain.CurrentDomain;
            var cilTrans = new VBF.MiniSharp.Targets.Cil.EmitTranslator(codegenDomain, "test");

            cilTrans.Create(ast, @"test.exe");

            ;
        }
Example #8
0
 public CompilationErrorList(CompilationErrorManager errorManager)
 {
     CodeContract.RequiresArgumentNotNull(errorManager, "errorManager");
     m_errors       = new List <CompilationError>();
     m_errorManager = errorManager;
 }
Example #9
0
 public MethodBodyResolver(CompilationErrorManager errorManager, TypeCollection types)
 {
     m_errorManager = errorManager;
     m_types = types;
 }
Example #10
0
 private static void ReportErrors(CompilationErrorManager errorManager)
 {
     if (errorManager.Errors.Count > 0)
     {
         foreach (var error in errorManager.Errors.OrderBy(e => e.ErrorPosition.StartLocation.CharIndex))
         {
             Console.WriteLine(error.ToString());
         }
     }
 }
Example #11
0
        public void ParserDriverConflictTest()
        {
            Lexicon test = new Lexicon();

            var X        = test.Lexer.DefineToken(RE.Symbol('x'));
            var PLUS     = test.Lexer.DefineToken(RE.Symbol('+'));
            var ASTERISK = test.Lexer.DefineToken(RE.Symbol('*'));

            var scannerinfo = test.CreateScannerInfo();

            Production <object> E = new Production <object>(), T = new Production <object>();

            E.Rule =
                (from e1 in E
                 from plus in PLUS
                 from e2 in E
                 select(object)(((int)e1) + ((int)e2))) |
                (from e1 in E
                 from mul in ASTERISK
                 from e2 in E
                 select(object)(((int)e1) * ((int)e2))) | T;

            T.Rule =
                from x in X
                select(object) 2;

            ProductionInfoManager pim = new ProductionInfoManager(E.SuffixedBy(Grammar.Eos()));

            LR0Model lr0 = new LR0Model(pim);

            lr0.BuildModel();

            string dot = lr0.ToString();

            TransitionTable tt = TransitionTable.Create(lr0, scannerinfo);

            ParserEngine driver = new ParserEngine(tt, new SyntaxErrors());

            ForkableScannerBuilder builder = new ForkableScannerBuilder(scannerinfo);

            var em = new VBF.Compilers.CompilationErrorManager();
            var el = em.CreateErrorList();

            builder.ErrorList = el;
            var scanner = builder.Create(new VBF.Compilers.SourceReader(new StringReader("x+x*x")));

            var z1 = scanner.Read();

            driver.Input(z1);

            var z2 = scanner.Read();

            driver.Input(z2);

            var z3 = scanner.Read();

            driver.Input(z3);

            var z4 = scanner.Read();

            driver.Input(z4);

            var z5 = scanner.Read();

            driver.Input(z5);

            var z6 = scanner.Read();

            driver.Input(z6);

            Assert.AreEqual(0, driver.CurrentStackCount);
            Assert.AreEqual(2, driver.AcceptedCount);

            var results = new[] { (int)driver.GetResult(0, null), (int)driver.GetResult(1, null) };

            Assert.IsTrue(results.Contains(8));
            Assert.IsTrue(results.Contains(6));
        }
Example #12
0
        public void ParserDriverSimpleTest()
        {
            Lexicon test = new Lexicon();

            var X    = test.Lexer.DefineToken(RE.Symbol('x'));
            var PLUS = test.Lexer.DefineToken(RE.Symbol('+'));

            var scannerinfo = test.CreateScannerInfo();

            Production <object> E = new Production <object>(), T = new Production <object>();

            E.Rule =
                (from t in T
                 from plus in PLUS
                 from e in E
                 select(object)(((int)t) + ((int)e))) | T;

            T.Rule =
                from x in X
                select(object) 1;

            ProductionInfoManager pim = new ProductionInfoManager(E.SuffixedBy(Grammar.Eos()));

            LR0Model lr0 = new LR0Model(pim);

            lr0.BuildModel();

            string dot = lr0.ToString();

            TransitionTable tt = TransitionTable.Create(lr0, scannerinfo);

            ParserEngine driver = new ParserEngine(tt, new SyntaxErrors()
            {
                TokenUnexpectedId = 1
            });

            ForkableScannerBuilder builder = new ForkableScannerBuilder(scannerinfo);
            var em = new VBF.Compilers.CompilationErrorManager();;
            var el = em.CreateErrorList();

            builder.ErrorList = el;
            var scanner = builder.Create(new VBF.Compilers.SourceReader(new StringReader("x+x+x")));

            var z1 = scanner.Read();

            driver.Input(z1);

            var z2 = scanner.Read();

            driver.Input(z2);

            var z3 = scanner.Read();

            driver.Input(z3);

            var z4 = scanner.Read();

            driver.Input(z4);

            var z5 = scanner.Read();

            driver.Input(z5);

            var z6 = scanner.Read();

            driver.Input(z6);

            Assert.AreEqual(0, driver.CurrentStackCount);
            Assert.AreEqual(1, driver.AcceptedCount);
            Assert.AreEqual(3, driver.GetResult(0, null));
        }