public Expression ParseExpression(SnapshotSpan expressionSpan)
        {
            string                 text           = expressionSpan.GetText();
            string                 sourceName     = null;
            AlloyLexer             lexer          = new AlloyLexer(new ANTLRStringStream(text, sourceName));
            CommonTokenStream      tokens         = new CommonTokenStream(lexer);
            AlloyParser            parser         = new AlloyParser(tokens);
            IAstRuleReturnScope    tree           = parser.expr();
            BufferedTreeNodeStream treeNodeStream = new BufferedTreeNodeStream(tree.Tree);
            AlloyExpressionWalker  walker         = new AlloyExpressionWalker(expressionSpan, treeNodeStream);
            Expression             result         = walker.expr();

            return(result);
        }
Ejemplo n.º 2
0
        protected override void ReParseImpl()
        {
            var outputWindow = OutputWindowService.TryGetPane(PredefinedOutputWindowPanes.TvlIntellisense);

            Stopwatch stopwatch = Stopwatch.StartNew();

            var snapshot = TextBuffer.CurrentSnapshot;
            SnapshotCharStream         input  = new SnapshotCharStream(snapshot, new Span(0, snapshot.Length));
            AlloyLexer                 lexer  = new AlloyLexer(input);
            CommonTokenStream          tokens = new CommonTokenStream(lexer);
            AlloyParser                parser = new AlloyParser(tokens);
            List <ParseErrorEventArgs> errors = new List <ParseErrorEventArgs>();

            parser.ParseError += (sender, e) =>
            {
                errors.Add(e);

                string message = e.Message;

                ITextDocument document;
                if (TextBuffer.Properties.TryGetProperty(typeof(ITextDocument), out document) && document != null)
                {
                    string fileName = document.FilePath;
                    var    line     = snapshot.GetLineFromPosition(e.Span.Start);
                    message = string.Format("{0}({1},{2}): {3}", fileName, line.LineNumber + 1, e.Span.Start - line.Start.Position + 1, message);
                }

                if (message.Length > 100)
                {
                    message = message.Substring(0, 100) + " ...";
                }

                if (outputWindow != null)
                {
                    outputWindow.WriteLine(message);
                }

                if (errors.Count > 100)
                {
                    throw new OperationCanceledException();
                }
            };

            var result = parser.compilationUnit();

            OnParseComplete(new AntlrParseResultEventArgs(snapshot, errors, stopwatch.Elapsed, tokens.GetTokens(), result));
        }
        protected override void ReParseImpl()
        {
            var outputWindow = OutputWindowService.TryGetPane(PredefinedOutputWindowPanes.TvlIntellisense);

            Stopwatch stopwatch = Stopwatch.StartNew();

            var snapshot = TextBuffer.CurrentSnapshot;
            SnapshotCharStream input = new SnapshotCharStream(snapshot, new Span(0, snapshot.Length));
            AlloyLexer lexer = new AlloyLexer(input);
            CommonTokenStream tokens = new CommonTokenStream(lexer);
            AlloyParser parser = new AlloyParser(tokens);
            List<ParseErrorEventArgs> errors = new List<ParseErrorEventArgs>();
            parser.ParseError += (sender, e) =>
                {
                    errors.Add(e);

                    string message = e.Message;

                    ITextDocument document;
                    if (TextBuffer.Properties.TryGetProperty(typeof(ITextDocument), out document) && document != null)
                    {
                        string fileName = document.FilePath;
                        var line = snapshot.GetLineFromPosition(e.Span.Start);
                        message = string.Format("{0}({1},{2}): {3}", fileName, line.LineNumber + 1, e.Span.Start - line.Start.Position + 1, message);
                    }

                    if (message.Length > 100)
                        message = message.Substring(0, 100) + " ...";

                    if (outputWindow != null)
                        outputWindow.WriteLine(message);

                    if (errors.Count > 100)
                        throw new OperationCanceledException();
                };

            var result = parser.compilationUnit();
            OnParseComplete(new AntlrParseResultEventArgs(snapshot, errors, stopwatch.Elapsed, tokens.GetTokens(), result));
        }