Пример #1
0
 internal static bool IsPrefix(this XSharpToken token)
 {
     if (token == null)
     {
         return(false);
     }
     switch (token.Type)
     {
     case XSharpLexer.PLUS:                  // see xsharp.g4 prefixExpression
     case XSharpLexer.MINUS:
     case XSharpLexer.TILDE:
     case XSharpLexer.ADDROF:
     case XSharpLexer.DEC:
     case XSharpLexer.INC:
     case XSharpLexer.LOGIC_NOT:
     //case XSharpLexer.FOX_NOT:
     case XSharpLexer.NOT:
         return(true);
     }
     return(false);
 }
Пример #2
0
        internal static bool IsOpen(this XSharpToken token, ref TokenType closeType)
        {
            if (token == null)
            {
                return(false);
            }
            switch (token.Type)
            {
            case XSharpLexer.LBRKT:
                closeType = XSharpLexer.RBRKT;
                return(true);

            case XSharpLexer.LCURLY:
                closeType = XSharpLexer.RCURLY;
                return(true);

            case XSharpLexer.LPAREN:
                closeType = XSharpLexer.RPAREN;
                return(true);
            }
            closeType = 0;
            return(false);
        }
Пример #3
0
 internal PPErrorMessage(XSharpToken token, string message)
 {
     Token   = token;
     Message = message;
 }
Пример #4
0
        internal static List <XSharpToken> GetTokenList(XSharpSearchLocation location, out CompletionState state,
                                                        bool includeKeywords = false, bool underCursor = false)
        {
            location = AdjustStartLineNumber(location);
            var line = getLineFromBuffer(location);

            //
            state = CompletionState.General;
            if (line.Count == 0)
            {
                return(line);
            }
            // if the token appears after comma or paren then strip the tokens
            // now look forward and find the first token that is on or after the triggerpoint
            var  result    = new List <XSharpToken>();
            var  last      = XSharpLexer.Eof;
            bool allowdot  = location.Project?.ParseOptions?.AllowDotForInstanceMembers ?? false;
            var  cursorPos = location.Position;
            var  done      = false;
            var  list      = new XSharpTokenList(line);

            while (!done && !list.Eoi())
            {
                var         token      = list.ConsumeAndGet();
                int         openToken  = 0;
                XSharpToken closeToken = null;
                bool        isHit      = token.StartIndex <= cursorPos && token.StopIndex >= cursorPos && underCursor;
                bool        isNotLast  = token.StopIndex < location.Position - 1;
                if (token.StartIndex > cursorPos)
                {
                    // after the cursor we only include the open tokens
                    // so we can see if the id under the cursor is a method, constructor etc
                    switch (token.Type)
                    {
                    case XSharpLexer.LPAREN:
                    case XSharpLexer.LCURLY:
                    case XSharpLexer.LBRKT:
                        break;

                    case XSharpLexer.LT:
                        // if this is a generic type
                        // then add the complete
                        bool first     = true;
                        bool endoflist = false;
                        while (!endoflist)
                        {
                            endoflist = true;
                            if (list.La1 == XSharpLexer.ID || XSharpLexer.IsType(list.La1))
                            {
                                if (list.La2 == XSharpLexer.GT || list.La2 == XSharpLexer.COMMA)
                                {
                                    if (first)
                                    {
                                        result.Add(token);
                                        first = false;
                                    }
                                    result.Add(list.ConsumeAndGet());     // la1
                                    result.Add(list.ConsumeAndGet());     // la2
                                    endoflist = false;
                                }
                            }
                        }
                        continue;

                    default:
                        done = true;
                        break;
                    }
                    if (done)
                    {
                        continue;
                    }
                }
                switch (token.Type)
                {
                // after these tokens we "restart" the list
                case XSharpLexer.EOS:
                    if (token.Position < cursorPos && token != line.Last())
                    {
                        // an EOS inside a line before the cursor
                        // so there are 2 or more statements on the same line
                        // clear the first statement
                        result.Clear();
                        state = CompletionState.General;
                    }
                    else
                    {
                        // Exit loop, ignore the rest of the statements
                        done = true;
                    }
                    continue;

                case XSharpLexer.WS:
                case XSharpLexer.Eof:
                    continue;

                case XSharpLexer.TO:
                case XSharpLexer.UPTO:
                case XSharpLexer.DOWNTO:
                case XSharpLexer.IN:
                    if (!isHit)
                    {
                        result.Clear();
                        if (isNotLast)     // there has to be a space after the token
                        {
                            state = CompletionState.General;
                        }
                        else
                        {
                            state = CompletionState.None;
                        }
                    }
                    else
                    {
                        result.Add(token);
                    }
                    break;

                case XSharpLexer.LCURLY:
                    state = CompletionState.Constructors;
                    result.Add(token);
                    break;

                case XSharpLexer.LPAREN:
                    state = CompletionState.StaticMembers | CompletionState.InstanceMembers;
                    result.Add(token);
                    break;

                case XSharpLexer.LBRKT:
                    state = CompletionState.Brackets;
                    result.Add(token);
                    break;

                case XSharpLexer.ID:
                case XSharpLexer.NAMEOF:
                case XSharpLexer.TYPEOF:
                case XSharpLexer.SIZEOF:
                    result.Add(token);
                    break;

                case XSharpLexer.RCURLY:
                case XSharpLexer.RPAREN:
                case XSharpLexer.RBRKT:
                    bool add = true;
                    if (result.Count > 0 && token == list.LastOrDefault)
                    {
                        var lasttoken = result.Last();
                        if (lasttoken.Type == XSharpLexer.COLON ||
                            lasttoken.Type == XSharpLexer.DOT)
                        {
                            // closing char after colon or dot
                            add  = false;
                            done = true;
                        }
                    }
                    if (add)
                    {
                        result.Add(token);
                        // delete everything between parens, curly braces and brackets closing token before cursor pos
                        if (token.Position < location.Position)
                        {
                            closeToken = token;
                            if (token.Type == XSharpLexer.RCURLY)
                            {
                                openToken = XSharpLexer.LCURLY;
                            }
                            else if (token.Type == XSharpLexer.RPAREN)
                            {
                                openToken = XSharpLexer.LPAREN;
                            }
                            else if (token.Type == XSharpLexer.RBRKT)
                            {
                                openToken = XSharpLexer.LBRKT;
                            }
                        }
                    }
                    break;

                case XSharpLexer.STATIC: // These tokens are all before a namespace of a (namespace dot) type
                    if (isNotLast)       // there has to be a space after the token
                    {
                        state = CompletionState.General;
                    }
                    else
                    {
                        state = CompletionState.None;
                    }
                    break;

                case XSharpLexer.USING:
                    if (isNotLast)     // there has to be a space after the token
                    {
                        if (list.Expect(XSharpLexer.STATIC))
                        {
                            state = CompletionState.Namespaces | CompletionState.Types;
                            result.Clear();
                        }
                        else if (list.La1 == XSharpLexer.ID)
                        {
                            state = CompletionState.Namespaces;
                            result.Clear();
                        }
                    }
                    break;

                case XSharpLexer.MEMBER:
                    if (isNotLast)     // there has to be a space after the token
                    {
                        state = CompletionState.StaticMembers;
                    }
                    else
                    {
                        state = CompletionState.None;
                    }
                    break;

                case XSharpLexer.AS:
                case XSharpLexer.IS:
                case XSharpLexer.REF:
                case XSharpLexer.INHERIT:
                    if (!isHit)
                    {
                        result.Clear();
                    }
                    else
                    {
                        result.Add(token);
                    }
                    if (isNotLast)     // there has to be a space after the token
                    {
                        state = CompletionState.Namespaces | CompletionState.Types;
                    }
                    else
                    {
                        state = CompletionState.None;
                    }
                    break;

                case XSharpLexer.IMPLEMENTS:
                    result.Clear();
                    if (isNotLast)
                    {
                        state = CompletionState.Namespaces | CompletionState.Interfaces;
                    }
                    else
                    {
                        state = CompletionState.None;
                    }

                    break;

                case XSharpLexer.COLON:
                    state = CompletionState.InstanceMembers;
                    result.Add(token);
                    break;

                case XSharpLexer.DOT:
                    if (!state.HasFlag(CompletionState.Namespaces))
                    {
                        state = CompletionState.Namespaces | CompletionState.Types | CompletionState.StaticMembers;
                        if (allowdot)
                        {
                            state |= CompletionState.InstanceMembers;
                        }
                    }
                    result.Add(token);
                    break;

                case XSharpLexer.QMARK:
                    if (result.Count != 0)           // when at start of line then do not add. Otherwise it might be a Nullable type or conditional access expression
                    {
                        result.Add(token);
                    }
                    break;

                case XSharpLexer.QQMARK:
                    if (result.Count != 0)           // when at start of line then do not add. Otherwise it might be a binary expression
                    {
                        result.Add(token);
                    }
                    break;

                case XSharpLexer.BACKSLASH:
                case XSharpLexer.BACKBACKSLASH:
                    // this should only be seen at start of line
                    // clear the list to be sure
                    result.Clear();
                    break;

                case XSharpLexer.NAMESPACE:
                    state = CompletionState.Namespaces;
                    break;

                case XSharpLexer.COMMA:
                case XSharpLexer.ASSIGN_OP:
                case XSharpLexer.COLONCOLON:
                case XSharpLexer.SELF:
                case XSharpLexer.SUPER:
                    state = CompletionState.General;
                    result.Add(token);
                    break;

                default:
                    state = CompletionState.General;
                    if (XSharpLexer.IsOperator(token.Type))
                    {
                        result.Add(token);
                    }
                    else if (XSharpLexer.IsType(token.Type))
                    {
                        result.Add(token);
                    }
                    else if (XSharpLexer.IsConstant(token.Type))
                    {
                        result.Add(token);
                    }
                    else if (XSharpLexer.IsKeyword(token.Type) && includeKeywords)       // For code completion we want to include keywords
                    {
                        token.Text = XSettings.FormatKeyword(token.Text);
                        result.Add(token);
                    }
                    break;
                }
                last = token.Type;
                // remove everything between parens, curly braces or brackets when the closing token is before the cursor
                if (openToken != 0 && closeToken != null)
                {
                    var iLast = result.Count - 1;
                    int count = 0;
                    while (iLast >= 0 && result[iLast] != closeToken)
                    {
                        iLast--;
                    }
                    int closeType = closeToken.Type;
                    while (iLast >= 0)
                    {
                        var type = result[iLast].Type;
                        if (type == closeType)
                        {
                            count += 1;
                        }
                        else if (type == openToken)
                        {
                            count -= 1;
                            if (count == 0)
                            {
                                if (iLast < result.Count - 1)
                                {
                                    result.RemoveRange(iLast + 1, result.Count - iLast - 2);
                                }
                                break;
                            }
                        }
                        iLast -= 1;
                    }
                }
            }

            // when the list ends with a comma, drop the ending comma. Why ?
            if (result.Count > 0)
            {
                var end = result.Last();
                if (end.Type == XSharpLexer.COMMA)
                {
                    result.RemoveAt(result.Count - 1);
                }
            }
            return(result);
        }
Пример #5
0
 internal PPResultToken(XSharpToken token, PPTokenType type, string key) : this(token, type)
 {
     _key = key;
 }
Пример #6
0
 internal PPResultToken(XSharpToken token, PPTokenType type) : base(token, type)
 {
     MatchMarker = null;
 }
Пример #7
0
 }                                // Index in the flattened matchmarker list
 #endregion
 internal PPMatchToken(XSharpToken token, PPTokenType type) : base(token, type)
 {
     Children = null;
     Index    = -1;
 }
Пример #8
0
 internal static bool NeedsRight(this XSharpToken token)
 {
     return(token.IsBinary() || token.IsPrefix());
 }
Пример #9
0
 internal static bool IsEOS(this XSharpToken token)
 {
     return(token != null && (token.Type == XSharpLexer.NL || token.Type == XSharpLexer.EOS));
 }
Пример #10
0
 internal static bool IsName(this XSharpToken token)
 {
     return(token.IsIdentifier() || token.IsKeyword());
 }