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;
}
}
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();
}
}
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");
}
}
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);
}
}
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;
}
}
Aggregations