Search in sources :

Example 1 with ParameterListParser

use of dyvilx.tools.compiler.parser.method.ParameterListParser in project Dyvil by Dyvil.

the class ConstructorParser method parse.

@Override
public void parse(IParserManager pm, IToken token) {
    final int type = token.type();
    switch(this.mode) {
        case DECLARATOR:
            switch(type) {
                case DyvilSymbols.AT:
                    this.parseAnnotation(pm, token);
                    return;
                case DyvilKeywords.INIT:
                    this.member = this.consumer.createConstructor(token.raw(), this.attributes);
                    this.mode = PARAMETERS;
                    return;
            }
            if (this.parseModifier(pm, token)) {
                return;
            }
        // Fallthrough
        case PARAMETERS:
            this.mode = PARAMETERS_END;
            if (type == BaseSymbols.OPEN_PARENTHESIS) {
                pm.pushParser(new ParameterListParser(this.member));
                return;
            }
            pm.reparse();
            pm.report(token, "constructor.parameters.open_paren");
            return;
        case PARAMETERS_END:
            this.mode = INITIALIZER;
            if (type != BaseSymbols.CLOSE_PARENTHESIS) {
                pm.reparse();
                pm.report(token, "constructor.parameters.close_paren");
            }
            return;
        case INITIALIZER:
            if (type == BaseSymbols.COLON) {
                this.mode = INIT_TYPE;
                return;
            }
        // Fallthrough
        case EXCEPTIONS:
            if (type == DyvilKeywords.THROWS) {
                pm.pushParser(new TypeListParser(this.member.getExceptions()));
                this.mode = BODY;
                return;
            }
        // Fallthrough
        case BODY:
            switch(type) {
                case BaseSymbols.OPEN_CURLY_BRACKET:
                    pm.pushParser(new StatementListParser(this.member), true);
                    this.mode = END;
                    return;
                case BaseSymbols.EQUALS:
                    pm.pushParser(new ExpressionParser(this.member));
                    this.mode = END;
                    return;
            }
        // Fallthrough
        case END:
            this.consumer.addConstructor(this.member);
            pm.popParser(type != Tokens.EOF);
            return;
        case INIT_TYPE:
            boolean isSuper = false;
            switch(type) {
                case DyvilKeywords.SUPER:
                    isSuper = true;
                // Fallthrough
                case DyvilKeywords.THIS:
                    final InitializerCall init = new InitializerCall(token.raw(), isSuper);
                    this.member.setInitializer(init);
                    this.mode = INIT_ARGUMENTS;
                    return;
            }
            pm.report(token, "initializer.call.type");
            return;
        case INIT_ARGUMENTS:
            if (type == BaseSymbols.OPEN_PARENTHESIS) {
                pm.pushParser(new ArgumentListParser(this.member.getInitializer()));
                this.mode = INIT_END;
                return;
            }
            pm.report(token, "initializer.call.open_paren");
            this.mode = EXCEPTIONS;
            return;
        case INIT_END:
            if (type != BaseSymbols.CLOSE_PARENTHESIS) {
                pm.report(token, "initializer.call.close_paren");
                return;
            }
            this.mode = EXCEPTIONS;
            return;
    }
}
Also used : StatementListParser(dyvilx.tools.compiler.parser.statement.StatementListParser) ParameterListParser(dyvilx.tools.compiler.parser.method.ParameterListParser) TypeListParser(dyvilx.tools.compiler.parser.type.TypeListParser) ExpressionParser(dyvilx.tools.compiler.parser.expression.ExpressionParser) ArgumentListParser(dyvilx.tools.compiler.parser.expression.ArgumentListParser) InitializerCall(dyvilx.tools.compiler.ast.expression.access.InitializerCall)

Example 2 with ParameterListParser

use of dyvilx.tools.compiler.parser.method.ParameterListParser in project Dyvil by Dyvil.

the class ClassDeclarationParser method parse.

@Override
public void parse(IParserManager pm, IToken token) {
    int type = token.type();
    switch(this.mode) {
        case NAME:
            if (!Tokens.isIdentifier(type)) {
                pm.report(token, "class.identifier");
                return;
            }
            final Name name = token.nameValue();
            if (name.qualified.indexOf('$') >= 0) {
                pm.report(Markers.syntaxError(token, "class.identifier.invalid", name, name.qualified));
            }
            this.theClass = this.consumer.createClass(token.raw(), name, this.classAttributes);
            this.mode = GENERICS;
            return;
        case GENERICS:
            if (type == BaseSymbols.SEMICOLON && token.isInferred() && TypeParser.isGenericStart(token.next())) {
                // allow an implicit semicolon / line break between name and generic argument list
                return;
            }
            if (TypeParser.isGenericStart(token, type)) {
                pm.splitJump(token, 1);
                pm.pushParser(new TypeParameterListParser(this.theClass));
                this.mode = GENERICS_END;
                return;
            }
        // Fallthrough
        case PARAMETERS:
            final Modifier modifier = ModifierParser.parseModifier(token, pm);
            if (modifier != null) {
                this.theClass.getConstructorAttributes().add(modifier);
                return;
            }
            if (type == DyvilSymbols.AT) {
                final Annotation annotation = new CodeAnnotation(token.raw());
                this.theClass.getConstructorAttributes().add(annotation);
                pm.pushParser(new AnnotationParser(annotation));
                return;
            }
            if (type == BaseSymbols.OPEN_PARENTHESIS) {
                pm.pushParser(new ParameterListParser(this.theClass).withFlags(ParameterListParser.ALLOW_PROPERTIES));
                this.mode = PARAMETERS_END;
                return;
            }
        // Fallthrough
        case EXTENDS:
            if (type == DyvilKeywords.EXTENDS) {
                if (this.theClass.isInterface()) {
                    pm.pushParser(new TypeListParser(this));
                    this.mode = BODY;
                    return;
                }
                pm.pushParser(new TypeParser(this));
                this.mode = EXTENDS_PARAMETERS;
                return;
            }
        // Fallthrough
        case IMPLEMENTS:
            if (type == DyvilKeywords.IMPLEMENTS) {
                pm.pushParser(new TypeListParser(this));
                this.mode = BODY;
                if (this.theClass.isInterface()) {
                    pm.report(token, "class.interface.implements");
                    return;
                }
                return;
            }
        // Fallthrough
        case BODY:
            if (type == BaseSymbols.OPEN_CURLY_BRACKET) {
                ClassBody body = new ClassBody(this.theClass);
                this.theClass.setBody(body);
                pm.pushParser(new ClassBodyParser(body), true);
                this.mode = BODY_END;
                return;
            }
            if (BaseSymbols.isTerminator(type)) {
                if (token.isInferred()) {
                    switch(token.next().type()) {
                        case DyvilKeywords.EXTENDS:
                            this.mode = EXTENDS;
                            return;
                        case DyvilKeywords.IMPLEMENTS:
                            this.mode = IMPLEMENTS;
                            return;
                        case BaseSymbols.OPEN_SQUARE_BRACKET:
                            this.mode = GENERICS;
                            return;
                        case BaseSymbols.OPEN_PARENTHESIS:
                            this.mode = PARAMETERS;
                            return;
                    }
                }
                pm.popParser(true);
                this.consumer.addClass(this.theClass);
                return;
            }
            this.mode = BODY_END;
            pm.report(token, "class.body.separator");
            return;
        case GENERICS_END:
            this.mode = PARAMETERS;
            if (TypeParser.isGenericEnd(token, type)) {
                pm.splitJump(token, 1);
                return;
            }
            pm.reparse();
            pm.report(token, "generic.close_angle");
            return;
        case PARAMETERS_END:
            this.mode = EXTENDS;
            if (type != BaseSymbols.CLOSE_PARENTHESIS) {
                pm.reparse();
                pm.report(token, "class.parameters.close_paren");
            }
            return;
        case BODY_END:
            pm.popParser();
            this.consumer.addClass(this.theClass);
            if (type != BaseSymbols.CLOSE_CURLY_BRACKET) {
                pm.reparse();
                pm.report(token, "class.body.close_brace");
            }
            return;
        case EXTENDS_PARAMETERS_END:
            this.mode = IMPLEMENTS;
            if (type != BaseSymbols.CLOSE_PARENTHESIS) {
                pm.reparse();
                pm.report(token, "class.extends.close_paren");
            }
            return;
        case EXTENDS_PARAMETERS:
            if (type == BaseSymbols.OPEN_PARENTHESIS) {
                ArgumentListParser.parseArguments(pm, token.next(), this.theClass::setSuperConstructorArguments);
                this.mode = EXTENDS_PARAMETERS_END;
                return;
            }
            this.mode = IMPLEMENTS;
            pm.reparse();
    }
}
Also used : CodeAnnotation(dyvilx.tools.compiler.ast.attribute.annotation.CodeAnnotation) AnnotationParser(dyvilx.tools.compiler.parser.annotation.AnnotationParser) TypeParser(dyvilx.tools.compiler.parser.type.TypeParser) ParameterListParser(dyvilx.tools.compiler.parser.method.ParameterListParser) TypeParameterListParser(dyvilx.tools.compiler.parser.type.TypeParameterListParser) TypeListParser(dyvilx.tools.compiler.parser.type.TypeListParser) Modifier(dyvilx.tools.compiler.ast.attribute.modifiers.Modifier) TypeParameterListParser(dyvilx.tools.compiler.parser.type.TypeParameterListParser) Annotation(dyvilx.tools.compiler.ast.attribute.annotation.Annotation) CodeAnnotation(dyvilx.tools.compiler.ast.attribute.annotation.CodeAnnotation) Name(dyvil.lang.Name) ClassBody(dyvilx.tools.compiler.ast.classes.ClassBody)

Example 3 with ParameterListParser

use of dyvilx.tools.compiler.parser.method.ParameterListParser in project Dyvil by Dyvil.

the class StatementListParser method parse.

@Override
public void parse(IParserManager pm, IToken token) {
    final int type = token.type();
    if (type == BaseSymbols.CLOSE_CURLY_BRACKET) {
        this.end(pm);
        return;
    }
    if (type == Tokens.EOF) {
        this.end(pm);
        pm.report(token, "statement_list.close_brace");
        return;
    }
    switch(this.mode) {
        case OPEN_BRACKET:
            {
                final IToken next = token.next();
                final IToken lambdaArrow = this.findLambdaArrow(next);
                if (lambdaArrow != null) {
                    this.lambdaExpr = new LambdaExpr(lambdaArrow.raw());
                    this.lambdaExpr.setValue(this.statementList = new StatementList(token));
                    if (next == lambdaArrow) {
                        // { ->
                        // { =>
                        this.mode = LAMBDA_TYPE_ARROW;
                        return;
                    }
                    if (next.type() == BaseSymbols.OPEN_PARENTHESIS) {
                        // { ( ... ) =>
                        // { ( ... ) ->
                        pm.skip();
                        pm.pushParser(new ParameterListParser(this.lambdaExpr));
                        this.mode = LAMBDA_PARAMETERS_END;
                        return;
                    }
                    // { ... ->
                    // { ... =>
                    pm.pushParser(new ParameterListParser(this.lambdaExpr).withFlags(LAMBDA_ARROW_END));
                    this.mode = LAMBDA_TYPE_ARROW;
                    return;
                }
                // { ...
                this.statementList = this.closure ? new Closure(token) : new StatementList(token);
                this.mode = EXPRESSION;
                if (type != BaseSymbols.OPEN_CURLY_BRACKET) {
                    pm.report(token, "statement_list.open_brace");
                    pm.reparse();
                }
                return;
            }
        case LAMBDA_PARAMETERS_END:
            this.mode = LAMBDA_TYPE_ARROW;
            if (type != BaseSymbols.CLOSE_PARENTHESIS) {
                pm.report(token, "statement_list.lambda.close_paren");
            }
            return;
        case LAMBDA_TYPE_ARROW:
            if (type == DyvilSymbols.ARROW_RIGHT) {
                pm.pushParser(LambdaOrTupleParser.returnTypeParser(this.lambdaExpr));
                this.mode = LAMBDA_RETURN_ARROW;
                return;
            }
        // Fallthrough
        case LAMBDA_RETURN_ARROW:
            if (type != DyvilSymbols.DOUBLE_ARROW_RIGHT) {
                pm.report(token, "statement_list.lambda.arrow");
                return;
            }
            this.mode = EXPRESSION;
            return;
        case EXPRESSION:
            switch(type) {
                case BaseSymbols.SEMICOLON:
                case BaseSymbols.COMMA:
                    return;
                case DyvilKeywords.LABEL:
                    this.mode = LABEL_NAME;
                    return;
            }
            this.mode = SEPARATOR;
            final MemberParser<IVariable> parser = new MemberParser<>(this).withFlags(MemberParser.NO_FIELD_PROPERTIES);
            if (this.tryParserManager.tryParse(pm, parser, token, EXIT_ON_ROOT)) {
                return;
            }
            pm.pushParser(new ExpressionParser(this));
            return;
        case LABEL_NAME:
            if (Tokens.isIdentifier(type)) {
                this.label = token.nameValue();
                this.mode = LABEL_END;
                return;
            }
            this.mode = EXPRESSION;
            if (type != BaseSymbols.COLON) {
                pm.reparse();
            }
            pm.report(token, "statement_list.label.name");
            return;
        case LABEL_END:
            switch(type) {
                case BaseSymbols.COLON:
                case BaseSymbols.SEMICOLON:
                    this.mode = EXPRESSION;
                    return;
            }
            this.mode = EXPRESSION;
            pm.reparse();
            pm.report(SourcePosition.between(token, token.next()), "statement_list.label.separator");
            return;
        case SEPARATOR:
            this.mode = EXPRESSION;
            switch(type) {
                case BaseSymbols.SEMICOLON:
                case BaseSymbols.COMMA:
                    return;
            }
            pm.report(token, "statement_list.semicolon");
    }
}
Also used : ParameterListParser(dyvilx.tools.compiler.parser.method.ParameterListParser) Closure(dyvilx.tools.compiler.ast.statement.Closure) IToken(dyvilx.tools.parsing.token.IToken) LambdaExpr(dyvilx.tools.compiler.ast.expression.LambdaExpr) StatementList(dyvilx.tools.compiler.ast.statement.StatementList) IVariable(dyvilx.tools.compiler.ast.field.IVariable) ExpressionParser(dyvilx.tools.compiler.parser.expression.ExpressionParser)

Example 4 with ParameterListParser

use of dyvilx.tools.compiler.parser.method.ParameterListParser in project Dyvil by Dyvil.

the class LambdaOrTupleParser method parse.

@Override
public void parse(IParserManager pm, IToken token) {
    final int type = token.type();
    switch(this.mode) {
        case OPEN_PARENTHESIS:
            // ( ...
            // ^
            /*
			 * The new version of this parser tries to find the matching closing parenthesis instead of try-parsing the
			 * expression. If it finds that parenthesis token and the next token is a lambda arrow, we can assume that
			 * the expression is a lambda expression. Thus, we directly push a Parameter List Parser that may also
			 * produce syntax errors.
			 */
            final IToken closeParen = BracketMatcher.findMatch(token);
            if (closeParen != null) {
                final IToken next = closeParen.next();
                final int nextType = next.type();
                if (nextType == DyvilSymbols.ARROW_RIGHT || nextType == DyvilSymbols.DOUBLE_ARROW_RIGHT) {
                    // (     ... )          =>
                    // (     ... )          ->
                    // token     closeParen next
                    final LambdaExpr lambdaExpr = new LambdaExpr(next);
                    pm.pushParser(new ParameterListParser(lambdaExpr));
                    this.value = lambdaExpr;
                    this.mode = PARAMETERS_END;
                    return;
                }
            }
        // Fallthrough
        case TUPLE:
            // ( ... )
            final TupleLikeExpr tupleExpr = new TupleLikeExpr(token);
            pm.pushParser(new ArgumentListParser(tupleExpr));
            this.value = tupleExpr;
            this.mode = TUPLE_END;
            return;
        case TUPLE_END:
            this.value.expandPosition(token);
            this.consumer.setValue(this.value);
            pm.popParser();
            if (type != BaseSymbols.CLOSE_PARENTHESIS) {
                pm.reparse();
                pm.report(token, "tuple.close_paren");
            }
            return;
        case PARAMETERS_END:
            this.mode = TYPE_ARROW;
            if (type != BaseSymbols.CLOSE_PARENTHESIS) {
                pm.reparse();
                pm.report(token, "lambda.close_paren");
            }
            return;
        case SINGLE_PARAMETER:
            if (Tokens.isIdentifier(type)) {
                final LambdaExpr lambdaExpr = new LambdaExpr(token.next());
                final IParameter parameter = lambdaExpr.createParameter(token.raw(), token.nameValue(), Types.UNKNOWN, new AttributeList());
                lambdaExpr.getParameters().add(parameter);
                this.value = lambdaExpr;
                this.mode = TYPE_ARROW;
                return;
            }
        // Fallthrough
        case TYPE_ARROW:
            if (this.value == null) {
                this.value = new LambdaExpr(token.raw());
            }
            if (type == DyvilSymbols.ARROW_RIGHT) {
                pm.pushParser(returnTypeParser((LambdaExpr) this.value));
                this.mode = RETURN_ARROW;
                return;
            }
        // Fallthrough
        case RETURN_ARROW:
            pm.pushParser(new ExpressionParser(((LambdaExpr) this.value)));
            this.mode = END;
            if (type != DyvilSymbols.DOUBLE_ARROW_RIGHT) {
                pm.reparse();
                pm.report(token, "lambda.arrow");
            }
            return;
        case END:
            pm.popParser(true);
            this.consumer.setValue(this.value);
    }
}
Also used : IParameter(dyvilx.tools.compiler.ast.parameter.IParameter) TupleLikeExpr(dyvilx.tools.compiler.ast.expression.TupleLikeExpr) ParameterListParser(dyvilx.tools.compiler.parser.method.ParameterListParser) IToken(dyvilx.tools.parsing.token.IToken) AttributeList(dyvilx.tools.compiler.ast.attribute.AttributeList) LambdaExpr(dyvilx.tools.compiler.ast.expression.LambdaExpr)

Example 5 with ParameterListParser

use of dyvilx.tools.compiler.parser.method.ParameterListParser in project Dyvil by Dyvil.

the class MethodParser method parse.

@Override
public void parse(IParserManager pm, IToken token) {
    final int type = token.type();
    switch(this.mode) {
        case DECLARATOR:
            switch(type) {
                case DyvilSymbols.AT:
                    this.parseAnnotation(pm, token);
                    return;
                case DyvilKeywords.FUNC:
                case DyvilKeywords.OPERATOR:
                    this.mode = METHOD_NAME;
                    return;
            }
            if (this.parseModifier(pm, token)) {
                return;
            }
        // Fallthrough
        case METHOD_NAME:
            if (!Tokens.isIdentifier(type)) {
                pm.report(token, "method.identifier");
                return;
            }
            this.method = this.consumer.createMethod(token.raw(), token.nameValue(), Types.UNKNOWN, this.attributes);
            this.mode = GENERICS;
            return;
        // Fallthrough
        case GENERICS:
            if (TypeParser.isGenericStart(token, type)) {
                pm.splitJump(token, 1);
                this.mode = GENERICS_END;
                pm.pushParser(new TypeParameterListParser(this.method));
                return;
            }
        // Fallthrough
        case PARAMETERS:
            if (type == BaseSymbols.OPEN_PARENTHESIS) {
                this.mode = PARAMETERS_END;
                pm.pushParser(new ParameterListParser(this.method));
                return;
            }
        // Fallthrough
        case TYPE:
            switch(type) {
                case BaseSymbols.COLON:
                    pm.report(Markers.syntaxWarning(token, "method.type.colon.deprecated"));
                // Fallthrough
                case DyvilSymbols.ARROW_RIGHT:
                    pm.pushParser(new TypeParser(this.method));
                    this.mode = EXCEPTIONS;
                    return;
            }
        // Fallthrough
        case EXCEPTIONS:
            if (type == DyvilKeywords.THROWS) {
                pm.pushParser(new TypeListParser(this.method.getExceptions()));
                this.mode = BODY;
                return;
            }
        // Fallthrough
        case BODY:
            switch(type) {
                case BaseSymbols.OPEN_CURLY_BRACKET:
                    pm.pushParser(new StatementListParser(this.method), true);
                    this.mode = END;
                    return;
                case BaseSymbols.EQUALS:
                    pm.pushParser(new ExpressionParser(this.method));
                    this.mode = END;
                    return;
            }
        // Fallthrough
        case END:
            this.consumer.addMethod(this.method);
            pm.popParser(type != Tokens.EOF);
            return;
        case GENERICS_END:
            this.mode = PARAMETERS;
            if (TypeParser.isGenericEnd(token, type)) {
                pm.splitJump(token, 1);
                return;
            }
            pm.reparse();
            pm.report(token, "generic.close_angle");
            return;
        case PARAMETERS_END:
            this.mode = TYPE;
            if (type != BaseSymbols.CLOSE_PARENTHESIS) {
                pm.reparse();
                pm.report(token, "method.parameters.close_paren");
            }
            return;
    }
}
Also used : StatementListParser(dyvilx.tools.compiler.parser.statement.StatementListParser) TypeParser(dyvilx.tools.compiler.parser.type.TypeParser) TypeParameterListParser(dyvilx.tools.compiler.parser.type.TypeParameterListParser) ParameterListParser(dyvilx.tools.compiler.parser.method.ParameterListParser) TypeListParser(dyvilx.tools.compiler.parser.type.TypeListParser) ExpressionParser(dyvilx.tools.compiler.parser.expression.ExpressionParser) TypeParameterListParser(dyvilx.tools.compiler.parser.type.TypeParameterListParser)

Aggregations

ParameterListParser (dyvilx.tools.compiler.parser.method.ParameterListParser)5 ExpressionParser (dyvilx.tools.compiler.parser.expression.ExpressionParser)3 TypeListParser (dyvilx.tools.compiler.parser.type.TypeListParser)3 LambdaExpr (dyvilx.tools.compiler.ast.expression.LambdaExpr)2 StatementListParser (dyvilx.tools.compiler.parser.statement.StatementListParser)2 TypeParameterListParser (dyvilx.tools.compiler.parser.type.TypeParameterListParser)2 TypeParser (dyvilx.tools.compiler.parser.type.TypeParser)2 IToken (dyvilx.tools.parsing.token.IToken)2 Name (dyvil.lang.Name)1 AttributeList (dyvilx.tools.compiler.ast.attribute.AttributeList)1 Annotation (dyvilx.tools.compiler.ast.attribute.annotation.Annotation)1 CodeAnnotation (dyvilx.tools.compiler.ast.attribute.annotation.CodeAnnotation)1 Modifier (dyvilx.tools.compiler.ast.attribute.modifiers.Modifier)1 ClassBody (dyvilx.tools.compiler.ast.classes.ClassBody)1 TupleLikeExpr (dyvilx.tools.compiler.ast.expression.TupleLikeExpr)1 InitializerCall (dyvilx.tools.compiler.ast.expression.access.InitializerCall)1 IVariable (dyvilx.tools.compiler.ast.field.IVariable)1 IParameter (dyvilx.tools.compiler.ast.parameter.IParameter)1 Closure (dyvilx.tools.compiler.ast.statement.Closure)1 StatementList (dyvilx.tools.compiler.ast.statement.StatementList)1