private ExpressionNode parsePrimaryExpression() {
            ExpressionNode result = null;
            switch (lexicalUnit) {
            case CharacterLiteral:
                result = createLiteralExpression(LiteralKind.Character);
                nextLexicalUnit(false);
                break;

            case DecimalIntegerLiteral:
                result = createLiteralExpression(LiteralKind.DecimalInteger);
                nextLexicalUnit(false);
                break;

            case DoubleLiteral:
                result = createLiteralExpression(LiteralKind.Double);
                nextLexicalUnit(false);
                break;

            case FloatLiteral:
                result = createLiteralExpression(LiteralKind.Float);
                nextLexicalUnit(false);
                break;

            case HexadecimalIntegerLiteral:
                result = createLiteralExpression(LiteralKind.HexadecimalInteger);
                nextLexicalUnit(false);
                break;

            case HexadecimalLongLiteral:
                result = createLiteralExpression(LiteralKind.HexadecimalLong);
                nextLexicalUnit(false);
                break;

            case LongLiteral:
                result = createLiteralExpression(LiteralKind.Long);
                nextLexicalUnit(false);
                break;

            case RealLiteral:
                result = createLiteralExpression(LiteralKind.Real);
                nextLexicalUnit(false);
                break;

            case StringLiteral:
                result = createLiteralExpression(LiteralKind.String);
                nextLexicalUnit(false);
                break;

            case VerbatimStringLiteral:
                result = createLiteralExpression(LiteralKind.VerbatimString);
                nextLexicalUnit(false);
                break;

            case VerbatimIdentifier:
            case ContextualKeyword:
            case Identifier: {
                var name = new SimpleNameExpressionNode { NameOffset = scanner.StartPosition, NameLength = getLexicalUnitLength() };
                result = name;
                setScannerState(result);
                int endPosition = scanner.EndPosition;
                nextLexicalUnit(false);
                result.EndPosition = parseTypeArguments(name.TypeArguments, true, endPosition);
            }
            break;

            case OpenParenthesis: {
                nextLexicalUnit(true);
                result = parseExpression();
                result.Parenthesized = true;
                if (lexicalUnit != LexicalUnit.CloseParenthesis) {
                    throw error(ParseErrorId.CloseParenthesisExpected);
                }
                result.EndPosition = scanner.EndPosition;
                nextLexicalUnit(false);
            }
            break;

            case Keyword:
                switch (scanner.Keyword) {
                case True:
                    result = createLiteralExpression(LiteralKind.True);
                    nextLexicalUnit(false);
                    break;

                case False:
                    result = createLiteralExpression(LiteralKind.False);
                    nextLexicalUnit(false);
                    break;

                case Null:
                    result = createLiteralExpression(LiteralKind.Null);
                    nextLexicalUnit(false);
                    break;

                case Boolean:
                    result = createTypeExpression(TypeReferenceKind.Boolean);
                    nextLexicalUnit(false);
                    break;

                case Byte:
                    result = createTypeExpression(TypeReferenceKind.Byte);
                    nextLexicalUnit(false);
                    break;

                case Char:
                    result = createTypeExpression(TypeReferenceKind.Char);
                    nextLexicalUnit(false);
                    break;

                case Double:
                    result = createTypeExpression(TypeReferenceKind.Double);
                    nextLexicalUnit(false);
                    break;

                case Float:
                    result = createTypeExpression(TypeReferenceKind.Float);
                    nextLexicalUnit(false);
                    break;

                case Int:
                    result = createTypeExpression(TypeReferenceKind.Int);
                    nextLexicalUnit(false);
                    break;

                case Long:
                    result = createTypeExpression(TypeReferenceKind.Long);
                    nextLexicalUnit(false);
                    break;

                case Short:
                    result = createTypeExpression(TypeReferenceKind.Short);
                    nextLexicalUnit(false);
                    break;

                case String:
                    result = createTypeExpression(TypeReferenceKind.String);
                    nextLexicalUnit(false);
                    break;

                case Sizeof:
                    saveScannerState();
                    if (nextLexicalUnit(true) != LexicalUnit.OpenParenthesis) {
                        throw error(ParseErrorId.OpenParenthesisExpected);
                    }
                    nextLexicalUnit(true);
                    var sizeofExp = new SizeofExpressionNode();
                    setSavedScannerState(sizeofExp);
                    sizeofExp.Expression = parseExpression();
                    result = sizeofExp;
                    if (lexicalUnit != LexicalUnit.CloseParenthesis) {
                        throw error(ParseErrorId.CloseParenthesisExpected);
                    }
                    result.EndPosition = scanner.EndPosition;
                    nextLexicalUnit(false);
                    break;
                    
                case Typeof: {
                    saveScannerState();
                    if (nextLexicalUnit(true) != LexicalUnit.OpenParenthesis) {
                        throw error(ParseErrorId.OpenParenthesisExpected);
                    }
                    nextLexicalUnit(true);
                    var typeofExp = new TypeofExpressionNode();
                    setSavedScannerState(typeofExp);
                    if (lexicalUnit == LexicalUnit.Keyword && scanner.Keyword == Keyword.Void) {
                        typeofExp.Type = new PrimitiveTypeReferenceNode(TypeReferenceKind.Void);
                    } else {
                        typeofExp.Type = parseType(true);
                    }
                    result = typeofExp;
                    if (lexicalUnit != LexicalUnit.CloseParenthesis) {
                        throw error(ParseErrorId.CloseParenthesisExpected);
                    }
                    result.EndPosition = scanner.EndPosition;
                    nextLexicalUnit(false);
                    break;
                }
                case This: {
                    result = new ThisAccessExpressionNode { EndPosition = scanner.EndPosition };
                    setScannerState(result);
                    nextLexicalUnit(false);
                    break;
                }
                case Super: {
                    saveScannerState();
                    result = parseSuperAccessExpression();
                    setSavedScannerState(result);
                    break;
                }
                case New:
                    result = parseNewExpression();
                    break;
                }
                break;
            }
            if (result == null) {
                throw error(ParseErrorId.UnexpectedLexicalUnit);
            }
            for (;;) {
                switch (lexicalUnit) {
                case Dot: {
                    nextLexicalUnit(true);
                    if (!isIdentifier(lexicalUnit)) {
                        throw error(ParseErrorId.IdentifierExpected);
                    }
                    var memberAccess = new MemberAccessExpressionNode {
                        TargetObject = result,
                        Member = new SimpleNameExpressionNode { NameOffset = scanner.StartPosition, NameLength = getLexicalUnitLength() }
                    };
                    copyScannerState(result, memberAccess);
                    setScannerState(memberAccess.Member);
                    memberAccess.Member.EndPosition = scanner.EndPosition;
                    result = memberAccess;
                    int endPosition = scanner.EndPosition;
                    nextLexicalUnit(false);
                    memberAccess.EndPosition = parseTypeArguments(memberAccess.Member.TypeArguments, true, endPosition);
                }
                break;

                case NullSafeMemberAccess: {
                    nextLexicalUnit(true);
                    if (!isIdentifier(lexicalUnit)) {
                        throw error(ParseErrorId.IdentifierExpected);
                    }
                    var memberAccess = new NullSafeMemberAccessExpressionNode {
                        TargetObject = result,
                        Member = new SimpleNameExpressionNode { NameOffset = scanner.StartPosition, NameLength = getLexicalUnitLength() }
                    };
                    copyScannerState(result, memberAccess);
                    setScannerState(memberAccess.Member);
                    memberAccess.Member.EndPosition = scanner.EndPosition;
                    result = memberAccess;
                    int endPosition = scanner.EndPosition;
                    nextLexicalUnit(false);
                    memberAccess.EndPosition = parseTypeArguments(memberAccess.Member.TypeArguments, true, endPosition);
                }
                break;

                case OpenParenthesis: {
                    if (result.ExpressionKind == ExpressionKind.Type) {
                        throw error(ParseErrorId.UnexpectedLexicalUnit);
                    }
                    var invocation = new InvocationExpressionNode { TargetObject =  result };
                    setScannerState(invocation);
                    invocation.StartPosition = result.StartPosition;
                    result = invocation;
                    nextLexicalUnit(true);
                    result.EndPosition = parseArguments(invocation.Arguments);
                }
                break;

                case OpenBracket: {
                    switch (result.ExpressionKind) {
                    case Type:
                    case ArrayCreation:
                        throw error(ParseErrorId.UnexpectedLexicalUnit);
                    }
                    nextLexicalUnit(true);
                    var elementAccess = new ElementAccessExpressionNode { TargetObject = result };
                    copyScannerState(result, elementAccess);
                    result = elementAccess;
                    result.EndPosition = parseExpressionList(elementAccess.Indexes);
                }
                break;

                case Increment: {
                    var unary = new UnaryExpressionNode { Operator = UnaryOperator.PostIncrement, Operand = result };
                    copyScannerState(result, unary);
                    result = unary;
                    result.EndPosition = scanner.EndPosition;
                    nextLexicalUnit(false);
                    break;
                }
                case Decrement: {
                    var unary = new UnaryExpressionNode { Operator = UnaryOperator.PostDecrement, Operand = result };
                    copyScannerState(result, unary);
                    result = unary;
                    result.EndPosition = scanner.EndPosition;
                    nextLexicalUnit(false);
                    break;
                }
                default:
                    if (result.ExpressionKind == ExpressionKind.Type) {
                        throw error(ParseErrorId.UnexpectedLexicalUnit);
                    }
                    return result;
                }
            }
        }
Example #2
0
 protected virtual TResult handleSizeof(SizeofExpressionNode sizeofExpression, TSource source, bool nested)
 {
     return(defaultHandler());
 }