Esempio n. 1
0
 public virtual object ApplyOnce(object t, Func <IAstRuleReturnScope> whichRule)
 {
     if (t == null)
     {
         return((object)null);
     }
     try
     {
         this.state = new RecognizerSharedState();
         this.input = (ITreeNodeStream) new CommonTreeNodeStream(this.originalAdaptor, t);
         ((CommonTreeNodeStream)this.input).TokenStream = this.originalTokenStream;
         this.BacktrackingLevel = 1;
         IAstRuleReturnScope astRuleReturnScope = whichRule();
         this.BacktrackingLevel = 0;
         if (this.Failed)
         {
             return(t);
         }
         if (this.showTransformations && astRuleReturnScope != null && (!t.Equals(astRuleReturnScope.Tree) && astRuleReturnScope.Tree != null))
         {
             this.ReportTransformation(t, astRuleReturnScope.Tree);
         }
         if (astRuleReturnScope != null && astRuleReturnScope.Tree != null)
         {
             return(astRuleReturnScope.Tree);
         }
         return(t);
     }
     catch (RecognitionException ex)
     {
     }
     return(t);
 }
        public static List<ITagSpan<IOutliningRegionTag>> ExtractOutliningRegions(IAstRuleReturnScope parseResult, ReadOnlyCollection<IToken> tokens, AlloyOutliningTaggerProvider provider, ITextSnapshot snapshot)
        {
            BufferedTreeNodeStream input = new BufferedTreeNodeStream(parseResult.Tree);
            AlloyOutliningTaggerWalker walker = new AlloyOutliningTaggerWalker(input, tokens, provider, snapshot);
            walker.compilationUnit();

            return walker._outliningRegions;
        }
        public static List<IEditorNavigationTarget> ExtractNavigationTargets(IAstRuleReturnScope parseResult, ReadOnlyCollection<IToken> tokens, AlloyAtnEditorNavigationSourceProvider provider, ITextSnapshot snapshot)
        {
            BufferedTreeNodeStream input = new BufferedTreeNodeStream(parseResult.Tree);
            AlloyEditorNavigationSourceWalker walker = new AlloyEditorNavigationSourceWalker(input, snapshot, tokens, provider.EditorNavigationTypeRegistryService, provider.GlyphService, provider.OutputWindowService);
            walker.compilationUnit();

            return walker._targets;
        }
        public static List <IEditorNavigationTarget> ExtractNavigationTargets(IAstRuleReturnScope parseResult, ReadOnlyCollection <IToken> tokens, AlloyAtnEditorNavigationSourceProvider provider, ITextSnapshot snapshot)
        {
            BufferedTreeNodeStream            input  = new BufferedTreeNodeStream(parseResult.Tree);
            AlloyEditorNavigationSourceWalker walker = new AlloyEditorNavigationSourceWalker(input, snapshot, tokens, provider.EditorNavigationTypeRegistryService, provider.GlyphService, provider.OutputWindowService);

            walker.compilationUnit();

            return(walker._targets);
        }
Esempio n. 5
0
        public static List <ITagSpan <IOutliningRegionTag> > ExtractOutliningRegions(IAstRuleReturnScope parseResult, ReadOnlyCollection <IToken> tokens, AlloyOutliningTaggerProvider provider, ITextSnapshot snapshot)
        {
            BufferedTreeNodeStream     input  = new BufferedTreeNodeStream(parseResult.Tree);
            AlloyOutliningTaggerWalker walker = new AlloyOutliningTaggerWalker(input, tokens, provider, snapshot);

            walker.compilationUnit();

            return(walker._outliningRegions);
        }
        private bool TryHandleParseCompleteV3(object sender, ParseResultEventArgs e)
        {
            if (!object.ReferenceEquals(sender, BackgroundParser))
            {
                return(false);
            }

            AntlrParseResultEventArgs antlrArgs = e as AntlrParseResultEventArgs;

            if (antlrArgs == null)
            {
                this.Tokens3 = null;
                return(false);
            }

            var result = antlrArgs.Result;

            this.Snapshot = e.Snapshot;
            this.Tokens3  = antlrArgs.Tokens;

            Tree.Dispatcher.Invoke(
                (Action)(() =>
            {
                try
                {
                    this.Tree.Items.Clear();
                    IAstRuleReturnScope resultArgs = result as IAstRuleReturnScope;
                    ITree tree = resultArgs != null ? resultArgs.Tree as ITree : null;
                    if (tree != null)
                    {
                        if (!tree.IsNil)
                        {
                            this.Tree.Items.Add(resultArgs.Tree);
                        }
                        else if (tree.ChildCount > 0)
                        {
                            for (int i = 0; i < tree.ChildCount; i++)
                            {
                                this.Tree.Items.Add(tree.GetChild(i));
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    if (ErrorHandler.IsCriticalException(ex))
                    {
                        throw;
                    }
                }
            }));

            return(true);
        }
        /// <summary>
        /// Is a RuleReturnScope node candidate for the left-hand-side of an in expression?
        /// </summary>
        /// <param name="lhs">The RuleReturnScope node</param>
        /// <param name="cached">The cached result of a former call to this method</param>
        /// <returns>True if so, false otherwise</returns>
        public bool IsLeftHandSideIn(IAstRuleReturnScope lhs, ref bool?cached)
        {
            if (cached.HasValue)
            {
                return(cached.Value);
            }

            bool result = IsLeftHandSideExpression(lhs) && (input.LA(1) == IN);

            cached = result;
            return(result);
        }
        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);
        }
Esempio n. 9
0
        private static void _Main(string[] args)
        {
            // input "x = 2*(3+3)"

            ICharStream input;

            if (args.Length > 0)
            {
                if (args[0].Equals("-i"))
                {
                    if (args.Length > 1)
                    {
                        input = new ANTLRFileStream(args[1]);
                    }
                    else
                    {
                        throw new Exception("No input file specified.");
                    }
                }
                else
                {
                    input = new ANTLRStringStream(args[0]);
                }
            }
            else
            {
                input = new ANTLRInputStream(Console.OpenStandardInput());
            }

            var lex    = new VecMathLexer(input);
            var tokens = new CommonTokenStream(lex);
            var g      = new VecMathParser(tokens);
            IAstRuleReturnScope <CommonTree> r = g.prog();
            CommonTree t = r.Tree;

            Console.WriteLine("Original tree:   " + t.ToStringTree());

            var simplify = new Simplify(new CommonTreeNodeStream(t));

            t = (CommonTree)simplify.Downup(t);

            var reduce = new Reduce(new CommonTreeNodeStream(t));

            t = (CommonTree)reduce.Downup(t);

            Console.WriteLine("Simplified tree: " + t.ToStringTree());
            Console.ReadKey();
        }
Esempio n. 10
0
        public virtual object ApplyOnce(object t, System.Func <IAstRuleReturnScope <TTree> > whichRule)
        {
            if (t == null)
            {
                return(null);
            }

            try
            {
                // share TreeParser object but not parsing-related state
                state = new RecognizerSharedState();
                input = new CommonTreeNodeStream(originalAdaptor, t);
                ((CommonTreeNodeStream)input).TokenStream = originalTokenStream;
                BacktrackingLevel = 1;
                IAstRuleReturnScope <TTree> r = whichRule();
                BacktrackingLevel = 0;
                if (Failed)
                {
                    return(t);
                }

                if (typeof(CommonTree).IsAssignableFrom(typeof(TTree)))
                {
                    if (r != null && !t.Equals(r.Tree) && r.Tree != null)
                    {
                        // show any transformations
                        Console.Out.WriteLine(((CommonTree)t).ToStringTree() + " -> " +
                                              ((CommonTree)(object)r.Tree).ToStringTree());
                    }
                }

                if (r != null && r.Tree != null)
                {
                    return(r.Tree);
                }
                else
                {
                    return(t);
                }
            }
            catch (RecognitionException)
            {
            }
            return(t);
        }
Esempio n. 11
0
        private void HandleBackgroundParseComplete(object sender, ParseResultEventArgs e)
        {
            AntlrParseResultEventArgs antlrParseResultArgs          = e as AntlrParseResultEventArgs;
            List <ITagSpan <IOutliningRegionTag> > outliningRegions = new List <ITagSpan <IOutliningRegionTag> >();

            if (antlrParseResultArgs != null)
            {
                IAstRuleReturnScope resultArgs = antlrParseResultArgs.Result as IAstRuleReturnScope;
                CommonTree          result     = resultArgs != null ? resultArgs.Tree as CommonTree : null;
                if (result != null && result.Children != null)
                {
                    foreach (CommonTree child in result.Children)
                    {
                        if (child == null || string.IsNullOrEmpty(child.Text))
                        {
                            continue;
                        }

                        switch (child.Type)
                        {
                        case GoLexer.KW_IMPORT:
                        case GoLexer.KW_TYPE:
                        case GoLexer.KW_CONST:
                        case GoLexer.KW_FUNC:
                            var                           startToken   = antlrParseResultArgs.Tokens[child.TokenStartIndex];
                            var                           stopToken    = antlrParseResultArgs.Tokens[child.TokenStopIndex];
                            Span                          span         = new Span(startToken.StartIndex, stopToken.StopIndex - startToken.StartIndex + 1);
                            SnapshotSpan                  snapshotSpan = new SnapshotSpan(e.Snapshot, span);
                            IOutliningRegionTag           tag          = new OutliningRegionTag();
                            TagSpan <IOutliningRegionTag> tagSpan      = new TagSpan <IOutliningRegionTag>(snapshotSpan, tag);
                            outliningRegions.Add(tagSpan);
                            break;

                        default:
                            continue;
                        }
                    }
                }
            }

            this._outliningRegions = outliningRegions;
            OnTagsChanged(new SnapshotSpanEventArgs(new SnapshotSpan(e.Snapshot, new Span(0, e.Snapshot.Length))));
        }
Esempio n. 12
0
        public virtual object ApplyOnce(object t, System.Func <IAstRuleReturnScope> whichRule)
        {
            if (t == null)
            {
                return(null);
            }

            try
            {
                // share TreeParser object but not parsing-related state
                state = new RecognizerSharedState();
                input = new CommonTreeNodeStream(originalAdaptor, t);
                ((CommonTreeNodeStream)input).TokenStream = originalTokenStream;
                BacktrackingLevel = 1;
                IAstRuleReturnScope r = whichRule();
                BacktrackingLevel = 0;
                if (Failed)
                {
                    return(t);
                }

                if (showTransformations && r != null && !t.Equals(r.Tree) && r.Tree != null)
                {
                    ReportTransformation(t, r.Tree);
                }

                if (r != null && r.Tree != null)
                {
                    return(r.Tree);
                }
                else
                {
                    return(t);
                }
            }
            catch (RecognitionException)
            {
            }

            return(t);
        }
        /// <summary>
        /// Is a RuleReturnScope node candidate for the left-hand-side of an assignment expression?
        /// </summary>
        /// <param name="lhs">The RuleReturnScope node</param>
        /// <param name="cached">The cached result of a former call to this method</param>
        /// <returns>True if so, false otherwise</returns>
        public bool IsLeftHandSideAssign(IAstRuleReturnScope lhs, ref bool?cached)
        {
            if (cached.HasValue)
            {
                return(cached.Value);
            }

            bool result;

            if (IsLeftHandSideExpression(lhs))
            {
                switch (input.LA(1))
                {
                case ASSIGN:
                case MULASS:
                case DIVASS:
                case MODASS:
                case ADDASS:
                case SUBASS:
                case SHLASS:
                case SHRASS:
                case SHUASS:
                case ANDASS:
                case XORASS:
                case ORASS:
                    result = true;
                    break;

                default:
                    result = false;
                    break;
                }
            }
            else
            {
                result = false;
            }

            cached = result;
            return(result);
        }
Esempio n. 14
0
		/// <summary>
		/// Is a RuleReturnScope node candidate for the left-hand-side of an assignment expression?
		/// </summary>
		/// <param name="lhs">The RuleReturnScope node</param>
		/// <param name="cached">The cached result of a former call to this method</param>
		/// <returns>True if so, false otherwise</returns>
		public bool IsLeftHandSideAssign(IAstRuleReturnScope lhs, ref bool? cached)
		{
			if (cached.HasValue)
			{
				return cached.Value;
			}

			bool result;
			if (IsLeftHandSideExpression(lhs))
			{
				switch (input.LA(1))
				{
					case ASSIGN:
					case MULASS:
					case DIVASS:
					case MODASS:
					case ADDASS:
					case SUBASS:
					case SHLASS:
					case SHRASS:
					case SHUASS:
					case ANDASS:
					case XORASS:
					case ORASS:
						result = true;
						break;

					default:
						result = false;
						break;
				}
			}
			else
			{
				result = false;
			}

			cached = result;
			return result;
		}
Esempio n. 15
0
		/// <summary>
		/// Is a RuleReturnScope node candidate a left-hand-side expression?
		/// </summary>
		/// <param name="lhs">The RuleReturnScope node</param>
		/// <returns>True if so, false otherwise</returns>
		private bool IsLeftHandSideExpression(IAstRuleReturnScope lhs)
		{
			if (lhs.Tree == null) // e.g. during backtracking
			{
				return true;
			}
			else
			{
				switch (((ITree)lhs.Tree).Type)
				{
					// primaryExpression
					case THIS:
					case Identifier:
					case NULL:
					case TRUE:
					case FALSE:
					case DecimalLiteral:
					case OctalIntegerLiteral:
					case HexIntegerLiteral:
					case StringLiteral:
					case RegularExpressionLiteral:
					case ARRAY:
					case OBJECT:
					case PAREXPR:
					// functionExpression
					case FUNCTION:
					// newExpression
					case NEW:
					// leftHandSideExpression
					case CALL:
					case BYFIELD:
					case BYINDEX:
						return true;

					default:
						return false;
				}
			}
		}
        /// <summary>
        /// Is a RuleReturnScope node candidate a left-hand-side expression?
        /// </summary>
        /// <param name="lhs">The RuleReturnScope node</param>
        /// <returns>True if so, false otherwise</returns>
        private bool IsLeftHandSideExpression(IAstRuleReturnScope lhs)
        {
            if (lhs.Tree == null)             // e.g. during backtracking
            {
                return(true);
            }
            else
            {
                switch (((ITree)lhs.Tree).Type)
                {
                // primaryExpression
                case THIS:
                case Identifier:
                case NULL:
                case TRUE:
                case FALSE:
                case DecimalLiteral:
                case OctalIntegerLiteral:
                case HexIntegerLiteral:
                case StringLiteral:
                case RegularExpressionLiteral:
                case ARRAY:
                case OBJECT:
                case PAREXPR:
                // functionExpression
                case FUNCTION:
                // newExpression
                case NEW:
                // leftHandSideExpression
                case CALL:
                case BYFIELD:
                case BYINDEX:
                    return(true);

                default:
                    return(false);
                }
            }
        }
Esempio n. 17
0
        /// <summary>
        /// Parses an expression in text form for later evaluation.
        /// </summary>
        /// <param name="pszCode">The expression to be parsed.</param>
        /// <param name="dwFlags">A combination of flags from the PARSEFLAGS enumeration that controls parsing.</param>
        /// <param name="nRadix">The radix to be used in parsing any numerical information in pszCode.</param>
        /// <param name="ppExpr">Returns the IDebugExpression2 object that represents the parsed expression, which is ready for binding and evaluation.</param>
        /// <param name="pbstrError">Returns the error message if the expression contains an error.</param>
        /// <param name="pichError">Returns the character index of the error in pszCode if the expression contains an error.</param>
        /// <returns>If successful, returns S_OK; otherwise, returns an error code.</returns>
        /// <remarks>
        /// When this method is called, a debug engine (DE) should parse the expression and validate it for correctness.
        /// The pbstrError and pichError parameters may be filled in if the expression is invalid.
        ///
        /// Note that the expression is not evaluated, only parsed. A later call to the IDebugExpression2.EvaluateSync
        /// or IDebugExpression2.EvaluateAsync methods evaluates the parsed expression.
        /// </remarks>
        public int ParseText(string pszCode, enum_PARSEFLAGS dwFlags, uint nRadix, out IDebugExpression2 ppExpr, out string pbstrError, out uint pichError)
        {
            if (pszCode == null)
            {
                throw new ArgumentNullException("pszCode");
            }
            if (pszCode.Length == 0)
            {
                throw new ArgumentException();
            }
            // dwFlags=0 in the Immediate window
            if (dwFlags != enum_PARSEFLAGS.PARSE_EXPRESSION && dwFlags != 0)
            {
                throw new NotImplementedException();
            }

            try
            {
                var expressionInput        = new ANTLRStringStream(pszCode);
                var expressionUnicodeInput = new JavaUnicodeStream(expressionInput);
                var expressionLexer        = new Java2Lexer(expressionUnicodeInput);
                var expressionTokens       = new CommonTokenStream(expressionLexer);
                var expressionParser       = new Java2Parser(expressionTokens);
                IAstRuleReturnScope <CommonTree> result = expressionParser.standaloneExpression();

                ppExpr     = new JavaDebugExpression(this, result.Tree, pszCode);
                pbstrError = null;
                pichError  = 0;
                return(VSConstants.S_OK);
            }
            catch (RecognitionException e)
            {
                ppExpr     = null;
                pbstrError = e.Message;
                pichError  = (uint)Math.Max(0, e.Index);
                return(VSConstants.E_FAIL);
            }
        }
Esempio n. 18
0
        private void UpdateNavigationTargets([NotNull] AntlrParseResultEventArgs antlrParseResultArgs)
        {
            Debug.Assert(antlrParseResultArgs != null);

            List <IEditorNavigationTarget> navigationTargets = new List <IEditorNavigationTarget>();

            if (antlrParseResultArgs != null)
            {
                IAstRuleReturnScope resultArgs = antlrParseResultArgs.Result as IAstRuleReturnScope;
                var result = resultArgs != null ? resultArgs.Tree as CommonTree : null;
                if (result != null)
                {
                    foreach (CommonTree child in result.Children)
                    {
                        if (child == null || string.IsNullOrEmpty(child.Text))
                        {
                            continue;
                        }

                        if (child.Text == "rule" && child.ChildCount > 0)
                        {
                            var ruleName = child.GetChild(0).Text;
                            if (string.IsNullOrEmpty(ruleName))
                            {
                                continue;
                            }

                            if (ruleName == "Tokens")
                            {
                                continue;
                            }

                            var          navigationType = char.IsUpper(ruleName[0]) ? _lexerRuleNavigationType : _parserRuleNavigationType;
                            IToken       startToken     = antlrParseResultArgs.Tokens[child.TokenStartIndex];
                            IToken       stopToken      = antlrParseResultArgs.Tokens[child.TokenStopIndex];
                            Span         span           = new Span(startToken.StartIndex, stopToken.StopIndex - startToken.StartIndex + 1);
                            SnapshotSpan ruleSpan       = new SnapshotSpan(antlrParseResultArgs.Snapshot, span);
                            SnapshotSpan ruleSeek       = new SnapshotSpan(antlrParseResultArgs.Snapshot, new Span(((CommonTree)child.GetChild(0)).Token.StartIndex, 0));
                            var          glyph          = char.IsUpper(ruleName[0]) ? _lexerRuleGlyph : _parserRuleGlyph;
                            navigationTargets.Add(new EditorNavigationTarget(ruleName, navigationType, ruleSpan, ruleSeek, glyph));
                        }
                        else if (child.Text.StartsWith("tokens"))
                        {
                            foreach (CommonTree tokenChild in child.Children)
                            {
                                if (tokenChild.Text == "=" && tokenChild.ChildCount == 2)
                                {
                                    var ruleName = tokenChild.GetChild(0).Text;
                                    if (string.IsNullOrEmpty(ruleName))
                                    {
                                        continue;
                                    }

                                    var          navigationType = char.IsUpper(ruleName[0]) ? _lexerRuleNavigationType : _parserRuleNavigationType;
                                    IToken       startToken     = antlrParseResultArgs.Tokens[tokenChild.TokenStartIndex];
                                    IToken       stopToken      = antlrParseResultArgs.Tokens[tokenChild.TokenStopIndex];
                                    Span         span           = new Span(startToken.StartIndex, stopToken.StopIndex - startToken.StartIndex + 1);
                                    SnapshotSpan ruleSpan       = new SnapshotSpan(antlrParseResultArgs.Snapshot, span);
                                    SnapshotSpan ruleSeek       = new SnapshotSpan(antlrParseResultArgs.Snapshot, new Span(((CommonTree)tokenChild.GetChild(0)).Token.StartIndex, 0));
                                    var          glyph          = char.IsUpper(ruleName[0]) ? _lexerRuleGlyph : _parserRuleGlyph;
                                    navigationTargets.Add(new EditorNavigationTarget(ruleName, navigationType, ruleSpan, ruleSeek, glyph));
                                }
                                else if (tokenChild.ChildCount == 0)
                                {
                                    var ruleName = tokenChild.Text;
                                    if (string.IsNullOrEmpty(ruleName))
                                    {
                                        continue;
                                    }

                                    var          navigationType = char.IsUpper(ruleName[0]) ? _lexerRuleNavigationType : _parserRuleNavigationType;
                                    IToken       startToken     = antlrParseResultArgs.Tokens[tokenChild.TokenStartIndex];
                                    IToken       stopToken      = antlrParseResultArgs.Tokens[tokenChild.TokenStopIndex];
                                    Span         span           = new Span(startToken.StartIndex, stopToken.StopIndex - startToken.StartIndex + 1);
                                    SnapshotSpan ruleSpan       = new SnapshotSpan(antlrParseResultArgs.Snapshot, span);
                                    SnapshotSpan ruleSeek       = new SnapshotSpan(antlrParseResultArgs.Snapshot, new Span(tokenChild.Token.StartIndex, 0));
                                    var          glyph          = char.IsUpper(ruleName[0]) ? _lexerRuleGlyph : _parserRuleGlyph;
                                    navigationTargets.Add(new EditorNavigationTarget(ruleName, navigationType, ruleSpan, ruleSeek, glyph));
                                }
                            }
                        }
                    }
                }
            }

            this._navigationTargets = navigationTargets;
            OnNavigationTargetsChanged(EventArgs.Empty);
        }
Esempio n. 19
0
        private void HandleBackgroundParseComplete(object sender, ParseResultEventArgs e)
        {
            AntlrParseResultEventArgs      antlrParseResultArgs = e as AntlrParseResultEventArgs;
            List <IEditorNavigationTarget> navigationTargets    = new List <IEditorNavigationTarget>();

            if (antlrParseResultArgs != null)
            {
                //// add the Global Scope type
                //{
                //    var name = "Global Scope";
                //    var navigationType = EditorNavigationTypeRegistryService.GetEditorNavigationType(PredefinedEditorNavigationTypes.Types);
                //    var span = new SnapshotSpan(e.Snapshot, new Span(0, e.Snapshot.Length));
                //    var seek = new SnapshotSpan(e.Snapshot, new Span(0, 0));
                //    var glyph = GetGlyph(StandardGlyphGroup.GlyphGroupNamespace, StandardGlyphItem.GlyphItemPublic);
                //    var target = new EditorNavigationTarget(name, navigationType, span, seek, glyph);
                //    navigationTargets.Add(target);
                //}

                IAstRuleReturnScope resultArgs = antlrParseResultArgs.Result as IAstRuleReturnScope;
                var    result      = resultArgs != null ? resultArgs.Tree as CommonTree : null;
                string packageName = string.Empty;

                if (result != null && result.Children != null)
                {
                    foreach (CommonTree child in result.Children)
                    {
                        if (child == null || string.IsNullOrEmpty(child.Text))
                        {
                            continue;
                        }

                        switch (child.Type)
                        {
                        case GoLexer.KW_PACKAGE:
                        {
                            packageName = ((CommonTree)child.Children[0]).Token.Text;
                            if (string.IsNullOrWhiteSpace(packageName))
                            {
                                continue;
                            }

                            var navigationType = EditorNavigationTypeRegistryService.GetEditorNavigationType(PredefinedEditorNavigationTypes.Types);
                            var startToken     = antlrParseResultArgs.Tokens[child.TokenStartIndex];
                            var stopToken      = antlrParseResultArgs.Tokens[child.TokenStopIndex];
                            //Span span = new Span(startToken.StartIndex, stopToken.StopIndex - startToken.StartIndex + 1);
                            //SnapshotSpan ruleSpan = new SnapshotSpan(e.Snapshot, span);
                            // applies to the whole file
                            var          span     = new SnapshotSpan(e.Snapshot, new Span(0, e.Snapshot.Length));
                            SnapshotSpan ruleSeek = new SnapshotSpan(e.Snapshot, new Span(((CommonTree)child.Children[0]).Token.StartIndex, 0));
                            var          glyph    = _provider.GlyphService.GetGlyph(StandardGlyphGroup.GlyphGroupModule, StandardGlyphItem.GlyphItemPublic);
                            navigationTargets.Add(new EditorNavigationTarget(packageName, navigationType, span, ruleSeek, glyph));
                        }
                        break;

                        case GoLexer.KW_TYPE:
                            // each child tree is a typeSpec, the root of which is an identifier that names the type
                            foreach (CommonTree typeSpec in child.Children)
                            {
                                var typeName = typeSpec.Token.Text;
                                if (string.IsNullOrWhiteSpace(typeName))
                                {
                                    continue;
                                }

                                for (ITree parent = typeSpec.Parent; parent != null; parent = parent.Parent)
                                {
                                    if (parent.Type == GoParser.TYPE_IDENTIFIER)
                                    {
                                        typeName = parent.Text + "." + typeName;
                                    }
                                }

                                if (!string.IsNullOrWhiteSpace(packageName))
                                {
                                    typeName = packageName + "." + typeName;
                                }

                                var          navigationType = EditorNavigationTypeRegistryService.GetEditorNavigationType(PredefinedEditorNavigationTypes.Types);
                                var          startToken     = antlrParseResultArgs.Tokens[typeSpec.TokenStartIndex];
                                var          stopToken      = antlrParseResultArgs.Tokens[typeSpec.TokenStopIndex];
                                Span         span           = new Span(startToken.StartIndex, stopToken.StopIndex - startToken.StartIndex + 1);
                                SnapshotSpan ruleSpan       = new SnapshotSpan(e.Snapshot, span);
                                SnapshotSpan ruleSeek       = new SnapshotSpan(e.Snapshot, new Span(typeSpec.Token.StartIndex, 0));
                                var          glyph          = _provider.GlyphService.GetGlyph(GetGlyphGroupForType(typeSpec), char.IsUpper(typeName[0]) ? StandardGlyphItem.GlyphItemPublic : StandardGlyphItem.GlyphItemPrivate);
                                navigationTargets.Add(new EditorNavigationTarget(typeName, navigationType, ruleSpan, ruleSeek, glyph));

                                if (typeSpec.ChildCount > 0 && typeSpec.Children[0].Type == GoLexer.KW_STRUCT && typeSpec.Children[0].ChildCount > 0)
                                {
                                    foreach (CommonTree fieldSpec in ((CommonTree)typeSpec.Children[0]).Children)
                                    {
                                        if (fieldSpec.Type != GoParser.FIELD_DECLARATION)
                                        {
                                            continue;
                                        }

                                        foreach (CommonTree fieldNameIdentifier in ((CommonTree)fieldSpec.GetFirstChildWithType(GoParser.AST_VARS)).Children)
                                        {
                                            string fieldName = fieldNameIdentifier.Text;
                                            navigationType = EditorNavigationTypeRegistryService.GetEditorNavigationType(PredefinedEditorNavigationTypes.Members);
                                            startToken     = antlrParseResultArgs.Tokens[fieldNameIdentifier.TokenStartIndex];
                                            stopToken      = antlrParseResultArgs.Tokens[fieldSpec.TokenStopIndex];
                                            span           = new Span(startToken.StartIndex, stopToken.StopIndex - startToken.StartIndex + 1);
                                            ruleSpan       = new SnapshotSpan(e.Snapshot, span);
                                            ruleSeek       = new SnapshotSpan(e.Snapshot, new Span(fieldNameIdentifier.Token.StartIndex, 0));
                                            glyph          = _provider.GlyphService.GetGlyph(StandardGlyphGroup.GlyphGroupField, char.IsUpper(fieldName[0]) ? StandardGlyphItem.GlyphItemPublic : StandardGlyphItem.GlyphItemPrivate);
                                            navigationTargets.Add(new EditorNavigationTarget(fieldName, navigationType, ruleSpan, ruleSeek, glyph));
                                        }
                                    }
                                }
                            }

                            break;

                        case GoLexer.KW_CONST:
                        case GoLexer.KW_VAR:
                            foreach (CommonTree spec in child.Children)
                            {
                                CommonTree decl = (CommonTree)spec.Children[0];
                                foreach (CommonTree nameToken in decl.Children)
                                {
                                    var name = nameToken.Token.Text;
                                    if (string.IsNullOrWhiteSpace(name))
                                    {
                                        continue;
                                    }

                                    var          navigationType = EditorNavigationTypeRegistryService.GetEditorNavigationType(PredefinedEditorNavigationTypes.Members);
                                    var          startToken     = antlrParseResultArgs.Tokens[nameToken.TokenStartIndex];
                                    var          stopToken      = antlrParseResultArgs.Tokens[nameToken.TokenStopIndex];
                                    Span         span           = new Span(startToken.StartIndex, stopToken.StopIndex - startToken.StartIndex + 1);
                                    SnapshotSpan ruleSpan       = new SnapshotSpan(e.Snapshot, span);
                                    SnapshotSpan ruleSeek       = new SnapshotSpan(e.Snapshot, new Span(nameToken.Token.StartIndex, 0));
                                    var          group          = (child.Type == GoLexer.KW_CONST) ? StandardGlyphGroup.GlyphGroupConstant : StandardGlyphGroup.GlyphGroupVariable;
                                    var          item           = char.IsUpper(name[0]) ? StandardGlyphItem.GlyphItemPublic : StandardGlyphItem.GlyphItemPrivate;
                                    var          glyph          = _provider.GlyphService.GetGlyph(group, item);
                                    navigationTargets.Add(new EditorNavigationTarget(name, navigationType, ruleSpan, ruleSeek, glyph));
                                }
                            }
                            break;

                        case GoLexer.KW_FUNC:
                        {
                            // the first child is either a receiver (method) or an identifier with the name of the function
                            var token = ((CommonTree)child.Children[0]).Token;
                            if (token.Type == GoLexer.METHOD_RECEIVER)
                            {
                                token = ((CommonTree)child.Children[1]).Token;
                            }

                            var functionName = token.Text;
                            if (string.IsNullOrWhiteSpace(functionName))
                            {
                                continue;
                            }

                            ITree receiver = child.GetFirstChildWithType(GoParser.METHOD_RECEIVER);
                            if (receiver != null)
                            {
                                string receiverName;
                                if (receiver.ChildCount >= 2)
                                {
                                    receiverName = receiver.GetChild(receiver.ChildCount - 2).Text;
                                }
                                else
                                {
                                    receiverName = "?";
                                }

                                functionName = receiverName + "." + functionName;
                            }

                            IEnumerable <string> args   = ProcessFunctionParameters(child);
                            string       sig            = string.Format("{0}({1})", functionName, string.Join(", ", args));
                            var          navigationType = EditorNavigationTypeRegistryService.GetEditorNavigationType(PredefinedEditorNavigationTypes.Members);
                            var          startToken     = antlrParseResultArgs.Tokens[child.TokenStartIndex];
                            var          stopToken      = antlrParseResultArgs.Tokens[child.TokenStopIndex];
                            Span         span           = new Span(startToken.StartIndex, stopToken.StopIndex - startToken.StartIndex + 1);
                            SnapshotSpan ruleSpan       = new SnapshotSpan(e.Snapshot, span);
                            SnapshotSpan ruleSeek       = new SnapshotSpan(e.Snapshot, new Span(child.Token.StartIndex, 0));
                            var          glyph          = _provider.GlyphService.GetGlyph(StandardGlyphGroup.GlyphGroupMethod, char.IsUpper(functionName[0]) ? StandardGlyphItem.GlyphItemPublic : StandardGlyphItem.GlyphItemPrivate);
                            navigationTargets.Add(new EditorNavigationTarget(sig, navigationType, ruleSpan, ruleSeek, glyph));
                        }

                        break;

                        default:
                            continue;
                        }
                    }
                }
            }

            this._navigationTargets = navigationTargets;
            OnNavigationTargetsChanged(EventArgs.Empty);
        }
Esempio n. 20
0
        private void UpdateTags(AntlrParseResultEventArgs antlrParseResultArgs)
        {
            List <ITagSpan <IOutliningRegionTag> > outliningRegions = new List <ITagSpan <IOutliningRegionTag> >();

            if (antlrParseResultArgs != null)
            {
                ITextSnapshot snapshot = antlrParseResultArgs.Snapshot;

#if false
                IAstRuleReturnScope resultArgs = antlrParseResultArgs.Result as IAstRuleReturnScope;
                var result = resultArgs.Tree as CommonTree;
                if (result != null)
                {
                    foreach (CommonTree child in result.Children)
                    {
                        if (child == null || string.IsNullOrEmpty(child.Text))
                        {
                            continue;
                        }

                        if (child.Text == "rule" && child.ChildCount > 0 || child.Text.StartsWith("tokens") || child.Text.StartsWith("options"))
                        {
                            string blockHint = "...";
                            if (child.Text == "rule")
                            {
                                string ruleName = child.Children[0].Text;
                                // don't try to outline the artificial tokens rule
                                if (ruleName == "Tokens")
                                {
                                    continue;
                                }

                                blockHint = child.Children[0].Text + "...";
                            }
                            else if (child.Text.StartsWith("tokens"))
                            {
                                // this is the special tokens{} block of a combined grammar
                                blockHint = "tokens {...}";
                            }
                            else if (child.Text.StartsWith("options"))
                            {
                                // this is the special options{} block of a grammar
                                blockHint = "options {...}";
                            }

                            var startToken = antlrParseResultArgs.Tokens[child.TokenStartIndex];
                            var stopToken  = antlrParseResultArgs.Tokens[child.TokenStopIndex];

                            if (startToken.Type == ANTLRParser.DOC_COMMENT)
                            {
                                for (int index = child.TokenStartIndex; index <= child.TokenStopIndex; index++)
                                {
                                    startToken = antlrParseResultArgs.Tokens[index];
                                    if (startToken.Type != ANTLRParser.DOC_COMMENT && startToken.Channel != TokenChannels.Hidden)
                                    {
                                        break;
                                    }
                                }
                            }

                            Span span = new Span(startToken.StartIndex, stopToken.StopIndex - startToken.StartIndex + 1);
                            if (snapshot.GetLineNumberFromPosition(span.Start) == snapshot.GetLineNumberFromPosition(span.End))
                            {
                                continue;
                            }

                            SnapshotSpan                  snapshotSpan = new SnapshotSpan(antlrParseResultArgs.Snapshot, span);
                            IOutliningRegionTag           tag          = new OutliningRegionTag(blockHint, snapshotSpan.GetText());
                            TagSpan <IOutliningRegionTag> tagSpan      = new TagSpan <IOutliningRegionTag>(snapshotSpan, tag);
                            outliningRegions.Add(tagSpan);
                        }
                    }
                }

                foreach (var token in antlrParseResultArgs.Tokens)
                {
                    switch (token.Type)
                    {
                    case ANTLRParser.DOC_COMMENT:
                    case ANTLRParser.ML_COMMENT:
                        Span commentSpan = Span.FromBounds(token.StartIndex, token.StopIndex + 1);
                        if (snapshot.GetLineNumberFromPosition(commentSpan.Start) != snapshot.GetLineNumberFromPosition(commentSpan.End))
                        {
                            SnapshotSpan                  commentSnapshotSpan = new SnapshotSpan(antlrParseResultArgs.Snapshot, commentSpan);
                            IOutliningRegionTag           commentTag          = new OutliningRegionTag(string.Format("/*{0} ... */", token.Type == ANTLRParser.DOC_COMMENT ? "*" : string.Empty), commentSnapshotSpan.GetText());
                            TagSpan <IOutliningRegionTag> commentTagSpan      = new TagSpan <IOutliningRegionTag>(commentSnapshotSpan, commentTag);
                            outliningRegions.Add(commentTagSpan);
                        }
                        break;

                    default:
                        continue;
                    }
                }
#endif
            }

            this._outliningRegions = outliningRegions;
            OnTagsChanged(new SnapshotSpanEventArgs(new SnapshotSpan(antlrParseResultArgs.Snapshot, new Span(0, antlrParseResultArgs.Snapshot.Length))));
        }
        private void UpdateTags(AntlrParseResultEventArgs antlrParseResultArgs)
        {
            List <IEditorNavigationTarget> navigationTargets = new List <IEditorNavigationTarget>();

            IAstRuleReturnScope resultArgs = antlrParseResultArgs.Result as IAstRuleReturnScope;
            var result = resultArgs != null ? resultArgs.Tree as CommonTree : null;

            if (result != null)
            {
                ITextSnapshot snapshot = antlrParseResultArgs.Snapshot;

                string package = string.Empty;

                /* ^('package' qualifiedName)
                 *
                 * ^(CLASS_TYPE_IDENTIFIER modifiers .* ^(TYPE_BODY .* '}'))
                 *
                 * ^(INTERFACE_TYPE_IDENTIFIER modifiers .* ^(TYPE_BODY .* '}'))
                 *
                 * ^(ANNOTATION_TYPE_IDENTIFIER modifiers .* ^(TYPE_BODY .* '}'))
                 *
                 * ^(FIELD_DECLARATION modifiers (.* ^(VARIABLE_IDENTIFIER .*))*)
                 *
                 * ^(METHOD_IDENTIFIER modifiers .* ^(FORMAL_PARAMETERS .* ')') .* ^(METHOD_BODY .* '}'))
                 */

                /* STATEMENT COMPLETION (description unrelated to this file)
                 *
                 * IDENTIFIER ('.' IDENTIFIER)*
                 *
                 * ^(CALL IDENTIFIER .*)
                 *
                 * ^('(' ^('==' .*) ')')
                 *
                 */

                for (CommonTreeNodeStream treeNodeStream = new CommonTreeNodeStream(result);
                     treeNodeStream.LA(1) != CharStreamConstants.EndOfFile;
                     treeNodeStream.Consume())
                {
                    switch (treeNodeStream.LA(1))
                    {
                    case Java2Lexer.PACKAGE:
                        // ^('package' qualifiedName)
                    {
                        CommonTree child = treeNodeStream.LT(1) as CommonTree;
                        if (child != null && child.ChildCount > 0)
                        {
                            package = GetQualifiedIdentifier(child.GetChild(0));
                        }
                    }

                    break;

                    case Java2Lexer.VARIABLE_IDENTIFIER:
                        // ^(FIELD_DECLARATION (.* ^(VARIABLE_IDENTIFIER))*)
                    {
                        CommonTree child = treeNodeStream.LT(1) as CommonTree;
                        if (child != null && child.HasAncestor(Java2Lexer.FIELD_DECLARATION))
                        {
                            string name = child.Token.Text;
                            IEditorNavigationType navigationType = EditorNavigationTypeRegistryService.GetEditorNavigationType(PredefinedEditorNavigationTypes.Members);
                            var                   startToken     = antlrParseResultArgs.Tokens[child.TokenStartIndex];
                            var                   stopToken      = antlrParseResultArgs.Tokens[child.TokenStopIndex];
                            SnapshotSpan          span           = new SnapshotSpan(snapshot, new Span(startToken.StartIndex, stopToken.StopIndex - startToken.StartIndex + 1));
                            SnapshotSpan          seek           = new SnapshotSpan(snapshot, new Span(child.Token.StartIndex, 0));
                            StandardGlyphGroup    glyphGroup     = StandardGlyphGroup.GlyphGroupJSharpField;
                            StandardGlyphItem     glyphItem      = GetGlyphItemFromChildModifier((CommonTree)child.GetAncestor(Java2Lexer.FIELD_DECLARATION));
                            ImageSource           glyph          = _provider.GlyphService.GetGlyph(glyphGroup, glyphItem);
                            NavigationTargetStyle style          = NavigationTargetStyle.None;
                            navigationTargets.Add(new EditorNavigationTarget(name, navigationType, span, seek, glyph, style));
                        }
                    }

                    break;

                    case Java2Lexer.METHOD_IDENTIFIER:
                        // ^(METHOD_IDENTIFIER ^(FORMAL_PARAMETERS formalParameterDecls?) ^(METHOD_BODY .* END_METHOD_BODY))
                    {
                        CommonTree child = treeNodeStream.LT(1) as CommonTree;
                        if (child != null)
                        {
                            string name = child.Token.Text;
                            IEnumerable <string> args = ProcessArguments((CommonTree)child.GetFirstChildWithType(Java2Lexer.FORMAL_PARAMETERS));
                            string sig = string.Format("{0}({1})", name, string.Join(", ", args));
                            IEditorNavigationType navigationType = EditorNavigationTypeRegistryService.GetEditorNavigationType(PredefinedEditorNavigationTypes.Members);
                            var                   startToken     = antlrParseResultArgs.Tokens[child.TokenStartIndex];
                            var                   stopToken      = antlrParseResultArgs.Tokens[child.TokenStopIndex];
                            SnapshotSpan          span           = new SnapshotSpan(snapshot, new Span(startToken.StartIndex, stopToken.StopIndex - startToken.StartIndex + 1));
                            SnapshotSpan          seek           = new SnapshotSpan(snapshot, new Span(child.Token.StartIndex, 0));
                            StandardGlyphGroup    glyphGroup     = StandardGlyphGroup.GlyphGroupJSharpMethod;
                            StandardGlyphItem     glyphItem      = GetGlyphItemFromChildModifier(child);
                            ImageSource           glyph          = _provider.GlyphService.GetGlyph(glyphGroup, glyphItem);
                            NavigationTargetStyle style          = NavigationTargetStyle.None;
                            navigationTargets.Add(new EditorNavigationTarget(sig, navigationType, span, seek, glyph, style));
                        }
                    }

                    break;

                    case Java2Lexer.ENUM_TYPE_IDENTIFIER:
                    case Java2Lexer.ANNOTATION_TYPE_IDENTIFIER:
                    case Java2Lexer.INTERFACE_TYPE_IDENTIFIER:
                    case Java2Lexer.CLASS_TYPE_IDENTIFIER:
                    {
                        CommonTree child = treeNodeStream.LT(1) as CommonTree;
                        if (child != null)
                        {
                            string name = child.Token.Text;
                            for (ITree parent = child.Parent; parent != null; parent = parent.Parent)
                            {
                                switch (parent.Type)
                                {
                                case Java2Lexer.ENUM_TYPE_IDENTIFIER:
                                case Java2Lexer.ANNOTATION_TYPE_IDENTIFIER:
                                case Java2Lexer.INTERFACE_TYPE_IDENTIFIER:
                                case Java2Lexer.CLASS_TYPE_IDENTIFIER:
                                    name = parent.Text + "." + name;
                                    continue;

                                default:
                                    continue;
                                }
                            }

                            if (!string.IsNullOrEmpty(package))
                            {
                                name = package + "." + name;
                            }

                            IEditorNavigationType navigationType = EditorNavigationTypeRegistryService.GetEditorNavigationType(PredefinedEditorNavigationTypes.Types);
                            var          startToken = antlrParseResultArgs.Tokens[child.TokenStartIndex];
                            var          stopToken  = antlrParseResultArgs.Tokens[child.TokenStopIndex];
                            SnapshotSpan span       = new SnapshotSpan(snapshot, new Span(startToken.StartIndex, stopToken.StopIndex - startToken.StartIndex + 1));
                            SnapshotSpan seek       = new SnapshotSpan(snapshot, new Span(child.Token.StartIndex, 0));

                            StandardGlyphGroup glyphGroup;
                            switch (child.Type)
                            {
                            case Java2Lexer.ENUM_TYPE_IDENTIFIER:
                                glyphGroup = StandardGlyphGroup.GlyphGroupEnum;
                                break;

                            case Java2Lexer.ANNOTATION_TYPE_IDENTIFIER:
                            case Java2Lexer.INTERFACE_TYPE_IDENTIFIER:
                                glyphGroup = StandardGlyphGroup.GlyphGroupJSharpInterface;
                                break;

                            case Java2Lexer.CLASS_TYPE_IDENTIFIER:
                            default:
                                glyphGroup = StandardGlyphGroup.GlyphGroupJSharpClass;
                                break;
                            }

                            StandardGlyphItem     glyphItem = GetGlyphItemFromChildModifier(child);
                            ImageSource           glyph     = _provider.GlyphService.GetGlyph(glyphGroup, glyphItem);
                            NavigationTargetStyle style     = NavigationTargetStyle.None;
                            navigationTargets.Add(new EditorNavigationTarget(name, navigationType, span, seek, glyph, style));
                        }
                    }

                    break;

                    default:
                        continue;
                    }
                }
            }

            this._navigationTargets = navigationTargets;
            OnNavigationTargetsChanged(EventArgs.Empty);
        }
Esempio n. 22
0
        private void UpdateNavigationTargets([NotNull] PhpEditorNavigationParseResultEventArgs antlrParseResultArgs)
        {
            Debug.Assert(antlrParseResultArgs != null);

            List <IEditorNavigationTarget> navigationTargets = new List <IEditorNavigationTarget>();

            // always add the "global scope" element
            {
                string name = "Global Scope";
                IEditorNavigationType editorNavigationType = Provider.EditorNavigationTypeRegistryService.GetEditorNavigationType(PredefinedEditorNavigationTypes.Types);
                SnapshotSpan          span  = new SnapshotSpan(antlrParseResultArgs.Snapshot, 0, antlrParseResultArgs.Snapshot.Length);
                SnapshotSpan          seek  = new SnapshotSpan(span.Start, 0);
                ImageSource           glyph = Provider.GlyphService.GetGlyph(StandardGlyphGroup.GlyphGroupNamespace, StandardGlyphItem.GlyphItemPublic);
                NavigationTargetStyle style = NavigationTargetStyle.None;
                navigationTargets.Add(new EditorNavigationTarget(name, editorNavigationType, span, seek, glyph, style));
            }

            if (antlrParseResultArgs != null)
            {
                ITextSnapshot snapshot = antlrParseResultArgs.Snapshot;

                Listener listener = new Listener(Provider, snapshot, antlrParseResultArgs, navigationTargets);

                foreach (var tree in antlrParseResultArgs.NavigationTrees)
                {
                    ParseTreeWalkers.SingleTree.Walk(listener, tree);
                }
#if false
                IAstRuleReturnScope resultArgs = antlrParseResultArgs.Result as IAstRuleReturnScope;
                var result = resultArgs != null ? resultArgs.Tree as CommonTree : null;
                if (result != null)
                {
                    foreach (CommonTree child in result.Children)
                    {
                        if (child == null || string.IsNullOrEmpty(child.Text))
                        {
                            continue;
                        }

                        if (child.Text == "rule" && child.ChildCount > 0)
                        {
                            var ruleName = child.GetChild(0).Text;
                            if (string.IsNullOrEmpty(ruleName))
                            {
                                continue;
                            }

                            if (ruleName == "Tokens")
                            {
                                continue;
                            }

                            var          navigationType = char.IsUpper(ruleName[0]) ? _lexerRuleNavigationType : _parserRuleNavigationType;
                            IToken       startToken     = antlrParseResultArgs.Tokens[child.TokenStartIndex];
                            IToken       stopToken      = antlrParseResultArgs.Tokens[child.TokenStopIndex];
                            Span         span           = new Span(startToken.StartIndex, stopToken.StopIndex - startToken.StartIndex + 1);
                            SnapshotSpan ruleSpan       = new SnapshotSpan(antlrParseResultArgs.Snapshot, span);
                            SnapshotSpan ruleSeek       = new SnapshotSpan(antlrParseResultArgs.Snapshot, new Span(((CommonTree)child.GetChild(0)).Token.StartIndex, 0));
                            var          glyph          = char.IsUpper(ruleName[0]) ? _lexerRuleGlyph : _parserRuleGlyph;
                            navigationTargets.Add(new EditorNavigationTarget(ruleName, navigationType, ruleSpan, ruleSeek, glyph));
                        }
                        else if (child.Text.StartsWith("tokens"))
                        {
                            foreach (CommonTree tokenChild in child.Children)
                            {
                                if (tokenChild.Text == "=" && tokenChild.ChildCount == 2)
                                {
                                    var ruleName = tokenChild.GetChild(0).Text;
                                    if (string.IsNullOrEmpty(ruleName))
                                    {
                                        continue;
                                    }

                                    var          navigationType = char.IsUpper(ruleName[0]) ? _lexerRuleNavigationType : _parserRuleNavigationType;
                                    IToken       startToken     = antlrParseResultArgs.Tokens[tokenChild.TokenStartIndex];
                                    IToken       stopToken      = antlrParseResultArgs.Tokens[tokenChild.TokenStopIndex];
                                    Span         span           = new Span(startToken.StartIndex, stopToken.StopIndex - startToken.StartIndex + 1);
                                    SnapshotSpan ruleSpan       = new SnapshotSpan(antlrParseResultArgs.Snapshot, span);
                                    SnapshotSpan ruleSeek       = new SnapshotSpan(antlrParseResultArgs.Snapshot, new Span(((CommonTree)tokenChild.GetChild(0)).Token.StartIndex, 0));
                                    var          glyph          = char.IsUpper(ruleName[0]) ? _lexerRuleGlyph : _parserRuleGlyph;
                                    navigationTargets.Add(new EditorNavigationTarget(ruleName, navigationType, ruleSpan, ruleSeek, glyph));
                                }
                                else if (tokenChild.ChildCount == 0)
                                {
                                    var ruleName = tokenChild.Text;
                                    if (string.IsNullOrEmpty(ruleName))
                                    {
                                        continue;
                                    }

                                    var          navigationType = char.IsUpper(ruleName[0]) ? _lexerRuleNavigationType : _parserRuleNavigationType;
                                    IToken       startToken     = antlrParseResultArgs.Tokens[tokenChild.TokenStartIndex];
                                    IToken       stopToken      = antlrParseResultArgs.Tokens[tokenChild.TokenStopIndex];
                                    Span         span           = new Span(startToken.StartIndex, stopToken.StopIndex - startToken.StartIndex + 1);
                                    SnapshotSpan ruleSpan       = new SnapshotSpan(antlrParseResultArgs.Snapshot, span);
                                    SnapshotSpan ruleSeek       = new SnapshotSpan(antlrParseResultArgs.Snapshot, new Span(tokenChild.Token.StartIndex, 0));
                                    var          glyph          = char.IsUpper(ruleName[0]) ? _lexerRuleGlyph : _parserRuleGlyph;
                                    navigationTargets.Add(new EditorNavigationTarget(ruleName, navigationType, ruleSpan, ruleSeek, glyph));
                                }
                            }
                        }
                    }
                }
#endif
            }

            this._navigationTargets = navigationTargets;
            OnNavigationTargetsChanged(EventArgs.Empty);
        }
Esempio n. 23
0
        /** Compile full template with respect to a list of formal args. */
        public virtual CompiledTemplate Compile(string srcName, string name, List <FormalArgument> args, string template, IToken templateToken)
        {
            ANTLRStringStream @is = new ANTLRStringStream(template, srcName);

            @is.name = srcName != null ? srcName : name;
            TemplateLexer lexer = null;

            if (templateToken != null && templateToken.Type == GroupParser.BIGSTRING_NO_NL)
            {
                lexer = new TemplateLexerNoNewlines(ErrorManager, @is, templateToken, DelimiterStartChar, DelimiterStopChar);
            }
            else
            {
                lexer = new TemplateLexer(ErrorManager, @is, templateToken, DelimiterStartChar, DelimiterStopChar);
            }

            CommonTokenStream tokens           = new CommonTokenStream(lexer);
            TemplateParser    p                = new TemplateParser(tokens, ErrorManager, templateToken);
            IAstRuleReturnScope <CommonTree> r = null;

            try
            {
                r = p.templateAndEOF();
            }
            catch (RecognitionException re)
            {
                ReportMessageAndThrowTemplateException(tokens, templateToken, p, re);
                return(null);
            }

            if (p.NumberOfSyntaxErrors > 0 || r.Tree == null)
            {
                CompiledTemplate impl = new CompiledTemplate();
                impl.DefineFormalArguments(args);
                return(impl);
            }

            //System.out.println(((CommonTree)r.getTree()).toStringTree());
            CommonTreeNodeStream nodes = new CommonTreeNodeStream(r.Tree);

            nodes.TokenStream = tokens;
            CodeGenerator gen = new CodeGenerator(nodes, this, name, template, templateToken);

            CompiledTemplate impl2 = null;

            try
            {
                impl2             = gen.template(name, args);
                impl2.NativeGroup = Group;
                impl2.Template    = template;
                impl2.Ast         = r.Tree;
                impl2.Ast.SetUnknownTokenBoundaries();
                impl2.Tokens = tokens;
            }
            catch (RecognitionException re)
            {
                ErrorManager.InternalError(null, "bad tree structure", re);
            }

            return(impl2);
        }
Esempio n. 24
0
        private void ExtractRuleSpans()
        {
            Dictionary <string, KeyValuePair <ITrackingSpan, ITrackingPoint> > rules = new Dictionary <string, KeyValuePair <ITrackingSpan, ITrackingPoint> >();
            var antlrParseResultArgs = PreviousParseResult;

            if (antlrParseResultArgs != null)
            {
#if false
                IAstRuleReturnScope resultArgs = antlrParseResultArgs.Result as IAstRuleReturnScope;
                var result = resultArgs != null ? resultArgs.Tree as CommonTree : null;
                if (result != null)
                {
                    foreach (CommonTree child in result.Children)
                    {
                        if (child == null || string.IsNullOrEmpty(child.Text))
                        {
                            continue;
                        }

                        if (child.Text == "rule" && child.ChildCount > 0)
                        {
                            var ruleName = child.GetChild(0).Text;
                            if (string.IsNullOrEmpty(ruleName))
                            {
                                continue;
                            }

                            if (ruleName == "Tokens")
                            {
                                continue;
                            }

                            IToken         startToken    = antlrParseResultArgs.Tokens[child.TokenStartIndex];
                            IToken         stopToken     = antlrParseResultArgs.Tokens[child.TokenStopIndex];
                            Span           span          = new Span(startToken.StartIndex, stopToken.StopIndex - startToken.StartIndex + 1);
                            ITrackingSpan  trackingSpan  = antlrParseResultArgs.Snapshot.CreateTrackingSpan(span, SpanTrackingMode.EdgeNegative);
                            ITrackingPoint trackingPoint = antlrParseResultArgs.Snapshot.CreateTrackingPoint(((CommonTree)child.GetChild(0)).Token.StartIndex, PointTrackingMode.Negative);
                            rules[ruleName] = new KeyValuePair <ITrackingSpan, ITrackingPoint>(trackingSpan, trackingPoint);
                        }
                        else if (child.Text.StartsWith("tokens"))
                        {
                            foreach (CommonTree tokenChild in child.Children)
                            {
                                if (tokenChild.Text == "=" && tokenChild.ChildCount == 2)
                                {
                                    var ruleName = tokenChild.GetChild(0).Text;
                                    if (string.IsNullOrEmpty(ruleName))
                                    {
                                        continue;
                                    }

                                    IToken         startToken    = antlrParseResultArgs.Tokens[tokenChild.TokenStartIndex];
                                    IToken         stopToken     = antlrParseResultArgs.Tokens[tokenChild.TokenStopIndex];
                                    Span           span          = new Span(startToken.StartIndex, stopToken.StopIndex - startToken.StartIndex + 1);
                                    ITrackingSpan  trackingSpan  = antlrParseResultArgs.Snapshot.CreateTrackingSpan(span, SpanTrackingMode.EdgeNegative);
                                    ITrackingPoint trackingPoint = antlrParseResultArgs.Snapshot.CreateTrackingPoint(((CommonTree)tokenChild.GetChild(0)).Token.StartIndex, PointTrackingMode.Negative);
                                    rules[ruleName] = new KeyValuePair <ITrackingSpan, ITrackingPoint>(trackingSpan, trackingPoint);
                                }
                                else if (tokenChild.ChildCount == 0)
                                {
                                    var ruleName = tokenChild.Text;
                                    if (string.IsNullOrEmpty(ruleName))
                                    {
                                        continue;
                                    }

                                    IToken         startToken    = antlrParseResultArgs.Tokens[tokenChild.TokenStartIndex];
                                    IToken         stopToken     = antlrParseResultArgs.Tokens[tokenChild.TokenStopIndex];
                                    Span           span          = new Span(startToken.StartIndex, stopToken.StopIndex - startToken.StartIndex + 1);
                                    ITrackingSpan  trackingSpan  = antlrParseResultArgs.Snapshot.CreateTrackingSpan(span, SpanTrackingMode.EdgeNegative);
                                    ITrackingPoint trackingPoint = antlrParseResultArgs.Snapshot.CreateTrackingPoint(tokenChild.Token.StartIndex, PointTrackingMode.Negative);
                                    rules[ruleName] = new KeyValuePair <ITrackingSpan, ITrackingPoint>(trackingSpan, trackingPoint);
                                }
                            }
                        }
                    }
                }
#endif
            }

            RuleSpans = rules;
        }
Esempio n. 25
0
		/// <summary>
		/// Is a RuleReturnScope node candidate for the left-hand-side of an in expression?
		/// </summary>
		/// <param name="lhs">The RuleReturnScope node</param>
		/// <param name="cached">The cached result of a former call to this method</param>
		/// <returns>True if so, false otherwise</returns>
		public bool IsLeftHandSideIn(IAstRuleReturnScope lhs, ref bool? cached)
		{
			if (cached.HasValue)
			{
				return cached.Value;
			}

			bool result = IsLeftHandSideExpression(lhs) && (input.LA(1) == IN);

			cached = result;
			return result;
		}
Esempio n. 26
0
        private TeasePage CreatePage(string line)
        {
            var result = new TeasePage {
                Comments = line
            };

            var stream = new ANTLRStringStream(line);
            var lexer  = new FlashTeaseScriptLexer(stream);
            var tokens = new CommonTokenStream(lexer);
            var parser = new FlashTeaseScriptParser(tokens);

            try
            {
                IAstRuleReturnScope <CommonTree> teaseReturn = parser.tease();
                if (teaseReturn.Tree != null)
                {
                    var pageNode = teaseReturn.Tree;
                    if (pageNode.Type != FlashTeaseScriptLexer.PAGE)
                    {
                        pageNode = pageNode.GetFirstChildWithType(FlashTeaseScriptLexer.PAGE) as CommonTree;
                    }

                    if (pageNode != null && pageNode.Type == FlashTeaseScriptLexer.PAGE)
                    {
                        var idNode = pageNode.GetFirstChildWithType(FlashTeaseScriptLexer.ID) as CommonTree;
                        if (idNode != null)
                        {
                            result.Id = GetPageId(idNode);
                        }

                        var propertiesNode = pageNode.GetFirstChildWithType(FlashTeaseScriptLexer.PROPERTIES) as CommonTree;
                        if (propertiesNode != null)
                        {
                            result.Text = GetText(propertiesNode.GetFirstChildWithType(FlashTeaseScriptLexer.TEXT) as CommonTree);

                            var image = GetImage(propertiesNode.GetFirstChildWithType(FlashTeaseScriptLexer.PIC) as CommonTree);
                            if (image != null)
                            {
                                result.ImageList.Add(image);
                            }
                            var audio = GetAudio(propertiesNode.GetFirstChildWithType(FlashTeaseScriptLexer.SOUND) as CommonTree);
                            if (audio != null)
                            {
                                result.AudioList.Add(audio);
                            }
                            var delay = GetDelay(propertiesNode.GetFirstChildWithType(FlashTeaseScriptLexer.DELAY) as CommonTree);
                            if (delay != null)
                            {
                                result.DelayList.Add(delay);
                            }

                            result.ButtonList.AddRange(GetButtons(propertiesNode));

                            result.SetFlags   = GetFlags(propertiesNode.GetFirstChildWithType(FlashTeaseScriptLexer.SET) as CommonTree);
                            result.UnsetFlags = GetFlags(propertiesNode.GetFirstChildWithType(FlashTeaseScriptLexer.UNSET) as CommonTree);
                        }
                    }
                }

                if (parser.HasError)
                {
                    result.Errors = String.Format("ParserError ({0}): {1}. Please correct by hand.", parser.ErrorPosition, parser.ErrorMessage);
                }
            }
            catch (Exception)
            {
                result.Errors = String.Format("ParserError ({0}): {1}. Please correct by hand.", parser.ErrorPosition, parser.ErrorMessage);
            }

            if (String.IsNullOrEmpty(result.Id))
            {
                result.Id     = Guid.NewGuid().ToString();
                result.Errors = String.Format("This page had no id, so one is generated. {0}", result.Errors);
            }

            return(result);
        }
Esempio n. 27
0
        private void UpdateTags(AntlrParseResultEventArgs antlrParseResultArgs)
        {
            List <ITagSpan <IOutliningRegionTag> > outliningRegions = new List <ITagSpan <IOutliningRegionTag> >();

            IAstRuleReturnScope resultArgs = antlrParseResultArgs.Result as IAstRuleReturnScope;
            var result = resultArgs != null ? resultArgs.Tree as CommonTree : null;

            if (result != null)
            {
                ITextSnapshot snapshot = antlrParseResultArgs.Snapshot;

                // outline all the imports
                IList <ITree> children = result.Children ?? new ITree[0];
                for (int i = 0; i < children.Count; i++)
                {
                    /*
                     *  ^('import' 'static'? IDENTIFIER+ '*'?)
                     *
                     *  ^('import' 'static'? IDENTIFIER+ '*'? ';')
                     *
                     *  ^('import' 'static'? IDENTIFIER+ '*'? ';') ^('import' 'static'? IDENTIFIER+ '*'? ';')+
                     *
                     *  ^('import' .* ';') ^('import' .* ';')+
                     */
                    if (children[i].Type != Java2Lexer.IMPORT)
                    {
                        continue;
                    }

                    int firstImport = i;
                    while (i < children.Count - 1 && children[i + 1].Type == Java2Lexer.IMPORT)
                    {
                        i++;
                    }

                    int lastImport = i;

                    // start 1 token after the first 'import' token
                    var  startToken = antlrParseResultArgs.Tokens[children[firstImport].TokenStartIndex + 1];
                    var  stopToken  = antlrParseResultArgs.Tokens[children[lastImport].TokenStopIndex];
                    Span span       = new Span(startToken.StartIndex, stopToken.StopIndex - startToken.StartIndex + 1);
                    if (snapshot.GetLineNumberFromPosition(span.Start) == snapshot.GetLineNumberFromPosition(span.End))
                    {
                        continue;
                    }

                    SnapshotSpan                  snapshotSpan = new SnapshotSpan(antlrParseResultArgs.Snapshot, span);
                    IOutliningRegionTag           tag          = new OutliningRegionTag("...", snapshotSpan.GetText());
                    TagSpan <IOutliningRegionTag> tagSpan      = new TagSpan <IOutliningRegionTag>(snapshotSpan, tag);
                    outliningRegions.Add(tagSpan);
                }

                /*
                 * ^(TYPE_BODY .* '}')
                 *
                 * ^(METHOD_BODY .* '}')
                 */

                // outline the type and method bodies
                for (CommonTreeNodeStream treeNodeStream = new CommonTreeNodeStream(result);
                     treeNodeStream.LA(1) != CharStreamConstants.EndOfFile;
                     treeNodeStream.Consume())
                {
                    switch (treeNodeStream.LA(1))
                    {
                    case Java2Lexer.TYPE_BODY:
                    case Java2Lexer.METHOD_BODY:
                        CommonTree child = treeNodeStream.LT(1) as CommonTree;
                        if (child != null)
                        {
                            var  startToken = antlrParseResultArgs.Tokens[child.TokenStartIndex];
                            var  stopToken  = antlrParseResultArgs.Tokens[child.TokenStopIndex];
                            Span span       = new Span(startToken.StartIndex, stopToken.StopIndex - startToken.StartIndex + 1);
                            if (snapshot.GetLineNumberFromPosition(span.Start) == snapshot.GetLineNumberFromPosition(span.End))
                            {
                                continue;
                            }

                            SnapshotSpan                  snapshotSpan = new SnapshotSpan(antlrParseResultArgs.Snapshot, span);
                            IOutliningRegionTag           tag          = new OutliningRegionTag("...", snapshotSpan.GetText());
                            TagSpan <IOutliningRegionTag> tagSpan      = new TagSpan <IOutliningRegionTag>(snapshotSpan, tag);
                            outliningRegions.Add(tagSpan);
                        }

                        break;

                    default:
                        continue;
                    }
                }
            }

            this._outliningRegions = outliningRegions;
            OnTagsChanged(new SnapshotSpanEventArgs(new SnapshotSpan(antlrParseResultArgs.Snapshot, new Span(0, antlrParseResultArgs.Snapshot.Length))));
        }