Example #1
0
 private static void ReportErrors(CompilationErrorList errorList)
 {
     if (errorList.Count > 0)
     {
         foreach (var error in errorList.OrderBy(e => e.ErrorPosition.StartLocation.CharIndex))
         {
             Console.WriteLine(error.ToString());
         }
     }
 }
Example #2
0
        public T Parse(SourceReader source, CompilationErrorList errorList)
        {
            CodeContract.RequiresArgumentNotNull(source, "source");

            if (!m_isInitialized)
            {
                OnInitialize();
            }

            m_scannerBuilder.ErrorList = errorList;
            m_context.ErrorList        = errorList;
            ForkableScanner scanner = m_scannerBuilder.Create(source);

            return(m_parserRunner.Run(scanner));
        }
Example #3
0
        public object GetResult(int index, CompilationErrorList errorList)
        {
            CodeContract.RequiresArgumentInRange(index >= 0 && index < m_acceptedHeads.Count, "index", "index is out of range");

            var head = m_acceptedHeads[index];

            if (head.Errors != null && errorList != null)
            {
                //aggregate errors
                foreach (var error in head.Errors)
                {
                    int errorId = error.ErrorId ?? m_errorDef.OtherErrorId;

                    errorList.AddError(errorId, error.ErrorPosition, error.ErrorArgument, error.ErrorArgument2);
                }
            }

            return(head.TopStackValue);
        }
Example #4
0
        public object GetResult(int index, CompilationErrorList errorList)
        {
            CodeContract.RequiresArgumentInRange(index >= 0 && index < m_acceptedHeads.Count, "index", "index is out of range");

            var head = m_acceptedHeads[index];

            if (head.Errors != null && errorList != null)
            {
                //aggregate errors
                foreach (var error in head.Errors)
                {
                    int errorId = error.ErrorId ?? m_errorDef.OtherErrorId;

                    errorList.AddError(errorId, error.ErrorPosition, error.ErrorArgument, error.ErrorArgument2);
                }
            }

            return head.TopStackValue;
        }
Example #5
0
 private static void ReportErrors(CompilationErrorList errorList)
 {
     if (errorList.Count > 0)
     {
         foreach (var error in errorList.OrderBy(e => e.ErrorPosition.StartLocation.CharIndex))
         {
             Console.WriteLine(error.ToString());
         }
     }
 }
Example #6
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 #7
0
 public T Parse(string source, CompilationErrorList errorList)
 {
     CodeContract.RequiresArgumentNotNull(source, "source");
     return(Parse(new StringReader(source), errorList));
 }
Example #8
0
 public T Parse(TextReader source, CompilationErrorList errorList, CancellationToken ctoken)
 {
     CodeContract.RequiresArgumentNotNull(source, "source");
     return(Parse(new SourceReader(source), errorList, ctoken));
 }
Example #9
0
        public T Parse(SourceReader source, CompilationErrorList errorList, CancellationToken ctoken)
        {
            CodeContract.RequiresArgumentNotNull(source, "source");

            if (!m_isInitialized)
            {
                OnInitialize();
            }

            Scanner scanner;

            scanner = new Scanner(m_scannerInfo);
            scanner.SetTriviaTokens(m_triviaTokens.Select(t => t.Index).ToArray());
            scanner.ErrorList      = errorList;
            scanner.RecoverErrors  = true;
            scanner.LexicalErrorId = m_errorDefinition.LexicalErrorId;

            scanner.SetSource(source);

            ParserEngine engine = new ParserEngine(m_transitionTable, m_errorDefinition);

            engine.EnableDeletionRecovery    = EnableDeletionRecovery;
            engine.EnableInsertionRecovery   = EnableInsertionRecovery;
            engine.EnableReplacementRecovery = EnableReplacementRecovery;

            Lexeme r = scanner.Read();

            while (true)
            {
                try
                {
                    engine.Input(r, ctoken);
                }
                catch (PanicRecoverException prex)
                {
                    var follow = prex.PossibleFollow;

                    HashSet <int> validTokens = new HashSet <int>(follow.Select(p =>
                    {
                        Terminal t = p as Terminal;
                        if (t != null)
                        {
                            return(t.Token.Index);
                        }
                        else
                        {
                            return(m_scannerInfo.EndOfStreamTokenIndex);
                        }
                    }));

                    while (!validTokens.Contains(r.TokenIndex) && !r.IsEndOfStream)
                    {
                        r = scanner.Read();
                    }

                    continue;
                }

                if (r.IsEndOfStream)
                {
                    break;
                }

                r = scanner.Read();
            }

            if (engine.AcceptedCount == 0)
            {
                throw new ParsingFailureException("There's no parsing result");
            }

            if (engine.AcceptedCount > 1 && engine.GetResultInfo(0).ErrorCount == 0)
            {
                throw new ParsingFailureException("Multiple parsing results are found. There's ambiguity in your grammar");
            }

            object result = engine.GetResult(0, errorList);

            return((T)result);
        }
Example #10
0
 public T Parse(SourceReader source, CompilationErrorList errorList)
 {
     return(Parse(source, errorList, CancellationToken.None));
 }
Example #11
0
 public T Parse(SourceReader source, CompilationErrorList errorList)
 {
     return(Parse(source, errorList, Task.Factory.CancellationToken));
 }