Example #1
0
 protected virtual void OnDefineParserErrors(SyntaxErrors errorDefinition, CompilationErrorManager errorManager)
 {
     errorManager.DefineError(errorDefinition.LexicalErrorId, 0, CompilationStage.Scanning, "Invalid token: {0}");
     errorManager.DefineError(errorDefinition.TokenUnexpectedId, 0, CompilationStage.Parsing, "Unexpected token: {0}");
     errorManager.DefineError(errorDefinition.TokenMissingId, 0, CompilationStage.Parsing, "Missing token: {0}");
     errorManager.DefineError(errorDefinition.OtherErrorId, 0, CompilationStage.Parsing, "Syntax error");
 }
Example #2
0
        public void DefineDefaultCompilationErrorInfo(int errorLevel)
        {
            if (m_errorManager == null)
            {
                throw new InvalidOperationException("ErrorManager is not specified");
            }

            m_errorManager.DefineError(DeletionErrorId, errorLevel, CompilationStage.Parsing, "Unexpected token \"{0}\"");
            m_errorManager.DefineError(InsertionErrorId, errorLevel, CompilationStage.Parsing, "Missing {0}");
        }
Example #3
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();

            Compiler.Scanner.Scan.Scanner scanner = new Compiler.Scanner.Scan.Scanner(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}");

            var el = em.CreateErrorList();

            scanner.ErrorList      = el;
            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, el.Count);

            Lexeme l3 = scanner.Read();

            Assert.AreEqual(ID.Index, l3.TokenIndex);

            Assert.IsTrue(el.Count > 0);
            Assert.AreEqual(101, el[0].Info.Id);
        }
Example #4
0
        public void DefineErrors()
        {
            m_errorManager.DefineError(c_SE_TypeNameMissing, 0, CompilationStage.SemanticAnalysis,
                                       "The type '{0}' could not be found.");

            m_errorManager.DefineError(c_SE_StaticBaseType, 0, CompilationStage.SemanticAnalysis,
                                       "The type '{0}' is a static class and it can't be used as a base class.");

            m_errorManager.DefineError(c_SE_CyclicBaseType, 0, CompilationStage.SemanticAnalysis,
                                       "The type '{0}' cannot be use as the base class because it is the same or one of the parent type of '{1}'.");

            m_errorManager.DefineError(c_SE_FieldDuplicates, 0, CompilationStage.SemanticAnalysis,
                                       "The type '{0}' has already defined a field named '{1}'.");

            m_errorManager.DefineError(c_SE_MethodDuplicates, 0, CompilationStage.SemanticAnalysis,
                                       "The type '{0}' has already defined a method named '{1}' with same parameter types.");

            m_errorManager.DefineError(c_SE_ParameterDuplicates, 0, CompilationStage.SemanticAnalysis,
                                       "The method '{0}' has already defined a parameter named '{1}'.");
        }
Example #5
0
        public void DefineErrors()
        {
            m_errorManager.DefineError(c_SE_VariableDuplicates, 0, CompilationStage.SemanticAnalysis,
                                       "There's already a parameter or local variable named '{0}' defined in current method.");

            m_errorManager.DefineError(c_SE_BinaryOpTypeInvalid, 0, CompilationStage.SemanticAnalysis,
                                       "Binary operator '{0}' cannot be used between '{1}' and '{2}'.");

            m_errorManager.DefineError(c_SE_UnaryOpTypeInvalid, 0, CompilationStage.SemanticAnalysis,
                                       "Unary operator '{0}' cannot be used on '{1}'.");

            m_errorManager.DefineError(c_SE_IfStmtTypeInvalid, 0, CompilationStage.SemanticAnalysis,
                                       "The type of expression used in if statement must be bool.");

            m_errorManager.DefineError(c_SE_WhileStmtTypeInvalid, 0, CompilationStage.SemanticAnalysis,
                                       "The type of expression used in while statement must be bool.");

            m_errorManager.DefineError(c_SE_WriteLineStmtTypeInvalid, 0, CompilationStage.SemanticAnalysis,
                                       "The System.Console.WriteLine statement can only output an integer value.");

            m_errorManager.DefineError(c_SE_InvalidCast, 0, CompilationStage.SemanticAnalysis,
                                       "Cannot cast from '{0}' to '{1}'.");

            m_errorManager.DefineError(c_SE_MethodMissing, 0, CompilationStage.SemanticAnalysis,
                                       "An applicable method '{0}' could not be found.");

            m_errorManager.DefineError(c_SE_MethodAmbiguous, 0, CompilationStage.SemanticAnalysis,
                                       "The call is ambiguous between the following two methods: '{0}' and '{1}'.");

            m_errorManager.DefineError(c_SE_VariableDeclMissing, 0, CompilationStage.SemanticAnalysis,
                                       "The name '{0}' is not declared.");

            m_errorManager.DefineError(c_SE_NotArray, 0, CompilationStage.SemanticAnalysis,
                                       "The name '{0}' is not an array.");

            m_errorManager.DefineError(c_SE_ExpressionNotArray, 0, CompilationStage.SemanticAnalysis,
                                       "The expression is not an array.");

            m_errorManager.DefineError(c_SE_MethodInvalidArguments, 0, CompilationStage.SemanticAnalysis,
                                       "The call to method '{0}' has some invalid arguments.");

            m_errorManager.DefineError(c_SE_ThisInStaticMethod, 0, CompilationStage.SemanticAnalysis,
                                       "The keyword 'this' cannot be used in a static method.");

            m_errorManager.DefineError(c_SE_NotSupported, 0, CompilationStage.SemanticAnalysis,
                                       "The usage is not support by miniSharp language: {0}");

            m_errorManager.DefineError(c_SE_InvalidIntLiteral, 0, CompilationStage.SemanticAnalysis,
                                       "'{0}' is not a valid integer.");
        }
Example #6
0
 public void DefineErrors()
 {
     m_errorManager.DefineError(c_SE_TypeNameDuplicates, 0, CompilationStage.SemanticAnalysis,
                                "The program has already defined a type named '{0}'.");
 }
Example #7
0
 protected virtual void OnDefineParserErrors(CompilationErrorManager errorManager)
 {
     errorManager.DefineError(m_lexicalErrorId, 0, CompilationStage.Scanning, "Invalid token: {0}");
     m_context.DefineDefaultCompilationErrorInfo(0);
 }
Example #8
0
        public void WhereGrammaTest()
        {
            Lexicon test = new Lexicon();

            var ID = test.Lexer.DefineToken(RE.Range('a', 'z').Concat(
                                                (RE.Range('a', 'z') | RE.Range('0', '9')).Many()), "ID");
            var NUM     = test.Lexer.DefineToken(RE.Range('0', '9').Many1(), "NUM");
            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
                     where Grammar.Check(g2.PrefixTrivia.Count == 0, 4, g2.Value.Span)
                     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;

            parser1.AmbiguityAggregator = (a, b) => a == "A" ? a : b;

            var info = test.CreateScannerInfo();

            var errorManager = new CompilationErrorManager();

            errorManager.DefineError(1, 0, CompilationStage.Parsing, "Unexpected token '{0}'");
            errorManager.DefineError(2, 0, CompilationStage.Parsing, "Missing token '{0}'");
            errorManager.DefineError(3, 0, CompilationStage.Parsing, "Syntax error");
            errorManager.DefineError(4, 0, CompilationStage.Parsing, "White spaces between >> are not allowed");

            var el = errorManager.CreateErrorList();

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

            LR0Model lr0 = new LR0Model(pim);

            lr0.BuildModel();

            string dot = lr0.ToString();

            TransitionTable tt     = TransitionTable.Create(lr0, info);
            var             errdef = new SyntaxErrors()
            {
                TokenUnexpectedId = 1, TokenMissingId = 2, OtherErrorId = 3
            };
            ParserEngine driver = new ParserEngine(tt, errdef);

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

            Scanner scanner = new Scanner(info);

            scanner.SetTriviaTokens(WHITESPACE.Index);
            scanner.SetSource(sr1);

            Lexeme r;

            do
            {
                r = scanner.Read();

                driver.Input(r);
            } while (!r.IsEndOfStream);

            Assert.AreEqual(1, driver.AcceptedCount);
            Assert.AreEqual("A", driver.GetResult(0, el));
            Assert.AreEqual(0, el.Count);

            ParserEngine driver2 = new ParserEngine(tt, errdef);

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

            scanner.SetSource(sr2);
            do
            {
                r = scanner.Read();

                driver2.Input(r);
            } while (!r.IsEndOfStream);

            var el2 = errorManager.CreateErrorList();

            Assert.AreEqual(1, driver2.AcceptedCount);
            Assert.AreEqual("B", driver2.GetResult(0, el2));
            Assert.AreEqual(0, el2.Count);
        }
Example #9
0
        public void ParserErrorRecoveryTest()
        {
            Lexicon binaryTreeSyntax = new Lexicon();
            var     lex = binaryTreeSyntax.Lexer;

            //lex
            Token LEFTPH  = lex.DefineToken(RE.Symbol('('));
            Token RIGHTPH = lex.DefineToken(RE.Symbol(')'));
            Token COMMA   = lex.DefineToken(RE.Symbol(','));
            Token LETTER  = lex.DefineToken(RE.Range('a', 'z') | RE.Range('A', 'Z'), "ID");

            //grammar
            Production <Node> NodeParser = new Production <Node>();

            NodeParser.Rule =
                (from a in LETTER
                 from _1 in LEFTPH
                 from left in NodeParser
                 from _2 in COMMA
                 from right in NodeParser
                 from _3 in RIGHTPH
                 select new Node(a.Value.Content, left, right))
                | Grammar.Empty <Node>(null);

            var builder = new ForkableScannerBuilder(binaryTreeSyntax.CreateScannerInfo());

            const string correct = "A(B(,),C(,))";

            string       source = "A((B(,),C(,)";
            SourceReader sr     = new SourceReader(
                new StringReader(source));

            var     info    = binaryTreeSyntax.CreateScannerInfo();
            Scanner scanner = new Scanner(info);

            scanner.SetSource(sr);

            CompilationErrorManager errorManager = new CompilationErrorManager();

            errorManager.DefineError(1, 0, CompilationStage.Parsing, "Unexpected token '{0}'");
            errorManager.DefineError(2, 0, CompilationStage.Parsing, "Missing token '{0}'");
            errorManager.DefineError(3, 0, CompilationStage.Parsing, "Invalid token found, did you mean '{0}' ?");
            errorManager.DefineError(4, 0, CompilationStage.Parsing, "Syntax error");

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

            LR0Model lr0 = new LR0Model(pim);

            lr0.BuildModel();

            string dot = lr0.ToString();

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

            SyntaxErrors errDef = new SyntaxErrors()
            {
                TokenUnexpectedId = 1, TokenMissingId = 2, OtherErrorId = 4, TokenMistakeId = 3
            };

            ParserEngine driver = new ParserEngine(tt, errDef);

            Lexeme r;

            do
            {
                r = scanner.Read();

                driver.Input(r);
            } while (!r.IsEndOfStream);

            var result = driver.GetResult(0, errorManager.CreateErrorList());

            ;
        }
Example #10
0
        public void MultipleLexerParsingTest()
        {
            Lexicon lexicon  = new Lexicon();
            Lexer   global   = lexicon.Lexer;
            Lexer   keywords = global.CreateSubLexer();

            var PROPERTY = global.DefineToken(RE.Literal("property"));
            var ID       = global.DefineToken(RE.Range('a', 'z').Concat(
                                                  (RE.Range('a', 'z') | RE.Range('0', '9')).Many()), "ID");
            var NUM        = global.DefineToken(RE.Range('0', '9').Many1(), "NUM");
            var EQ         = global.DefineToken(RE.Symbol('='));
            var SEMICOLON  = global.DefineToken(RE.Symbol(';'));
            var LB         = global.DefineToken(RE.Symbol('{'));
            var RB         = global.DefineToken(RE.Symbol('}'));
            var WHITESPACE = global.DefineToken(RE.Symbol(' ').Union(RE.Symbol('\t')), "[ ]");

            var GET = keywords.DefineToken(RE.Literal("get"));

            var assignStatement =
                from id in ID
                from eq in EQ
                from value in NUM
                from st in SEMICOLON
                select id.Value + "=" + value.Value;

            var getDef =
                from _get in GET
                from lb in LB
                from statements in assignStatement.Many()
                from rb in RB
                select new GetDef {
                Statements = statements
            };

            var propDef =
                from _prop in PROPERTY
                from id in ID
                from lb in LB
                from getdef in getDef
                from rb in RB
                select new PropDef {
                PropName = id.Value.Content, GetDef = getdef
            };

            string       source = "property get { get { get = 1; } }";
            SourceReader sr     = new SourceReader(
                new StringReader(source));

            var     info    = lexicon.CreateScannerInfo();
            Scanner scanner = new Scanner(info);

            scanner.SetTriviaTokens(WHITESPACE.Index);
            scanner.SetSource(sr);

            CompilationErrorManager errorManager = new CompilationErrorManager();

            errorManager.DefineError(1, 0, CompilationStage.Parsing, "Unexpected token '{0}'");
            errorManager.DefineError(2, 0, CompilationStage.Parsing, "Missing token '{0}'");
            errorManager.DefineError(3, 0, CompilationStage.Parsing, "Syntax error");

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

            LR0Model lr0 = new LR0Model(pim);

            lr0.BuildModel();

            string dot = lr0.ToString();

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

            SyntaxErrors errDef = new SyntaxErrors()
            {
                TokenUnexpectedId = 1, TokenMissingId = 2, OtherErrorId = 3
            };

            ParserEngine driver = new ParserEngine(tt, errDef);

            Lexeme r;

            do
            {
                r = scanner.Read();

                driver.Input(r);
            } while (!r.IsEndOfStream);

            var el = errorManager.CreateErrorList();

            var result = (PropDef)driver.GetResult(0, el);

            Assert.AreEqual(0, el.Count);
            Assert.AreEqual("get", result.PropName);
            Assert.AreEqual("get=1", result.GetDef.Statements.First());
        }