public static bool TryParseNode(IParser parser, out FglNameExpression node, TokenKind breakToken = TokenKind.EndOfFile) { node = null; bool result = false; if (parser.PeekToken(TokenCategory.Identifier) || parser.PeekToken(TokenCategory.Keyword)) { result = true; node = new FglNameExpression(); 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; ArrayIndexFglNameExpressionPiece arrayIndex; 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 (ArrayIndexFglNameExpressionPiece.TryParse(parser, out arrayIndex, breakToken) && arrayIndex != null) { node.Children.Add(arrayIndex.StartIndex, arrayIndex); node.EndIndex = arrayIndex.EndIndex; node.IsComplete = true; sb.Append(arrayIndex.ToString()); } else if (parser.PeekToken(TokenKind.AtSymbol)) { 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); }
public static bool TryParse(IParser parser, out ArrayIndexFglNameExpressionPiece node, TokenKind breakToken = TokenKind.EndOfFile) { node = null; bool result = false; if (parser.PeekToken(TokenKind.LeftBracket)) { StringBuilder sb = new StringBuilder(); sb.Append("["); result = true; node = new ArrayIndexFglNameExpressionPiece(); parser.NextToken(); node.StartIndex = parser.Token.Span.Start; // TODO: need to get an integer expression // for right now, we'll just check for a constant or a ident/keyword ExpressionNode indexExpr; while (FglExpressionNode.TryGetExpressionNode(parser, out indexExpr, new List <TokenKind> { TokenKind.RightBracket, TokenKind.Comma })) { if (node._expression == null) { node._expression = indexExpr; } else { node._expression.AppendExpression(indexExpr); } if (parser.PeekToken(TokenKind.Comma)) { parser.NextToken(); } else { break; } } //if(parser.PeekToken(TokenCategory.NumericLiteral) || // parser.PeekToken(TokenCategory.Keyword) || // parser.PeekToken(TokenCategory.Identifier)) //{ // parser.NextToken(); // sb.Append(parser.Token.Token.Value.ToString()); //} //else //{ // parser.ReportSyntaxError("The parser is unable to parse a complex expression as an array index. This may not be a syntax error."); //} //// TODO: check for a nested array index access //ArrayIndexFglNameExpressionPiece arrayIndex; //if (ArrayIndexFglNameExpressionPiece.TryParse(parser, out arrayIndex, breakToken)) //{ // sb.Append(arrayIndex._expression); //} //while(!parser.PeekToken(TokenKind.RightBracket)) //{ // if(parser.PeekToken().Kind == breakToken) // { // parser.ReportSyntaxError("Unexpected end of array index expression."); // break; // } // parser.NextToken(); // sb.Append(parser.Token.Token.Value.ToString()); //} if (parser.PeekToken(TokenKind.RightBracket)) { parser.NextToken(); node.EndIndex = parser.Token.Span.End; node.IsComplete = true; } else { parser.ReportSyntaxError("Expected right-bracket in array index."); } } return(result); }