Esempio n. 1
0
        public static bool TryParse(IParser parser, out MemberAccessNameExpressionPiece node)
        {
            node = null;
            bool result = false;

            if (parser.PeekToken(TokenKind.Dot))
            {
                StringBuilder sb = new StringBuilder();
                sb.Append(".");
                result = true;
                node   = new MemberAccessNameExpressionPiece();
                parser.NextToken();
                node.StartIndex = parser.Token.Span.Start;

                if (parser.PeekToken(TokenKind.Multiply) || parser.PeekToken(TokenCategory.Identifier) || parser.PeekToken(TokenCategory.Keyword))
                {
                    sb.Append(parser.NextToken().Value.ToString());
                    node.Text       = sb.ToString();
                    node.EndIndex   = parser.Token.Span.End;
                    node.IsComplete = true;
                }
                else
                {
                    parser.ReportSyntaxError("Invalid token found in member access.");
                }
            }

            return(result);
        }
Esempio n. 2
0
        public static bool TryParseNode(IParser parser, out NameExpression node, TokenKind breakToken = TokenKind.EndOfFile)
        {
            node = null;
            bool result = false;

            if (parser.PeekToken(TokenCategory.Identifier) || parser.PeekToken(TokenCategory.Keyword))
            {
                result = true;
                node   = new NameExpression();
                parser.NextToken();
                node.StartIndex = parser.Token.Span.Start;

                node._firstPiece = parser.Token.Token.Value.ToString();
                StringBuilder sb = new StringBuilder(node._firstPiece);
                node.EndIndex = parser.Token.Span.End;
                while (true)
                {
                    if (breakToken != TokenKind.EndOfFile &&
                        parser.PeekToken(breakToken))
                    {
                        break;
                    }

                    MemberAccessNameExpressionPiece memberAccess;
                    if (MemberAccessNameExpressionPiece.TryParse(parser, out memberAccess) && memberAccess != null)
                    {
                        node.Children.Add(memberAccess.StartIndex, memberAccess);
                        node.EndIndex   = memberAccess.EndIndex;
                        node.IsComplete = true;
                        sb.Append(memberAccess.ToString());
                    }
                    else if (parser.PeekToken(TokenKind.Ampersand))
                    {
                        parser.NextToken();
                        sb.Append("@");
                        if (parser.PeekToken(TokenCategory.Identifier) ||
                            parser.PeekToken(TokenCategory.Keyword))
                        {
                            sb.Append(parser.NextToken().Value.ToString());
                        }
                    }
                    else
                    {
                        break;
                    }
                }
                node.Name = sb.ToString();
            }

            return(result);
        }