Ejemplo n.º 1
0
        public void RunParser(String input)
        {
            // String example="write(\"CMMM语言while循环示例:\");int a = 10;while (a <> 0) {a = a - 1;write(a);}";
            // input = example;
            ICharStream  stream = CharStreams.fromstring(input);
            ITokenSource lexer  = new CMMLexer(stream);
            ITokenStream tokens = new CommonTokenStream(lexer);
            CMMParser    parser = new CMMParser(tokens);

            parser.BuildParseTree = true;
            IParseTree tree    = parser.statements();
            var        visitor = new RefPhase();

            visitor.outputStream = this;
            visitor.Visit(tree);
        }
Ejemplo n.º 2
0
 public void StaticError(String input)
 {
     try {
         ICharStream  stream = CharStreams.fromstring(input);
         ITokenSource lexer  = new ExceptionLexer(stream, outputStream, error);
         ITokenStream tokens = new CommonTokenStream(lexer);
         CMMParser    parser = new CMMParser(tokens);
         parser.BuildParseTree = true;
         parser.RemoveErrorListeners();
         CMMErrorListener errorListener = new CMMErrorListener();
         errorListener.outputStream = outputStream;
         errorListener.errorShow    = error;
         parser.AddErrorListener(errorListener);
         CMMErrorStrategy errorStrategy = new CMMErrorStrategy();
         parser.ErrorHandler = errorStrategy;
         parser.statements();
     }
     catch (Exception e1) {
         outputStream?.Print(e1.Message);
     }
 }
Ejemplo n.º 3
0
        /// <summary>
        /// 准备生成代码
        /// </summary>
        private IParseTree prepareForCodeGen()
        {
            errorPanel.Errors = null;
            textEditor.Errors = null;

            String input = textEditor.Text;

            if (String.IsNullOrEmpty(input))
            {
                return(null);
            }

            var listener = new CMMErrorListener();

            ICharStream  stream = CharStreams.fromstring(input);
            ITokenSource lexer  = new ExceptionLexer(stream, listener);
            ITokenStream tokens = new CommonTokenStream(lexer);
            CMMParser    parser = new CMMParser(tokens);

            parser.RemoveErrorListeners();

            parser.AddErrorListener(listener);
            parser.ErrorHandler = new CMMErrorStrategy();

            parser.BuildParseTree = true;
            IParseTree tree = parser.statements();

            //如果出错,转到错误面板并返回null
            if (listener.errors.Count != 0)
            {
                handleCompileTimeError(listener.errors);
                return(null);
            }
            var graph = new ParseTreeGrapher().CreateGraph(tree, CMMParser.ruleNames);

            drawTreePanel.Graph = graph;

            return(tree);
        }
Ejemplo n.º 4
0
        private void performCheck()
        {
            Debug.WriteLine("perform check");

            Dispatcher.Invoke(() => {
                string input = textEditor.Text;
                if (string.IsNullOrEmpty(input))
                {
                    return;
                }
                errorPanel.Errors = null;
                textEditor.Errors = null;
                Task.Run(() => {
                    var listener = new CMMErrorListener();

                    ICharStream stream  = CharStreams.fromstring(input);
                    ITokenSource lexer  = new ExceptionLexer(stream, listener);
                    ITokenStream tokens = new CommonTokenStream(lexer);
                    CMMParser parser    = new CMMParser(tokens);
                    parser.RemoveErrorListeners();


                    parser.AddErrorListener(listener);
                    parser.ErrorHandler = new CMMErrorStrategy();

                    parser.BuildParseTree = true;
                    IParseTree tree       = parser.statements();

                    if (listener.errors.Count != 0)
                    {
                        Dispatcher.Invoke(() => {
                            errorPanel.Errors = listener.errors;
                            textEditor.Errors = listener.errors;
                        });
                    }
                });
            });
        }