use of dyvilx.tools.compiler.ast.attribute.AttributeList in project Dyvil by Dyvil.
the class SourceFileParser method parse.
@Override
public void parse(IParserManager pm, IToken token) {
final int type = token.type();
switch(this.mode) {
case SEPARATOR:
if (this.unit.classCount() > 0) {
// any classes -> only allow classes from here
this.mode = CLASS;
} else if (this.unit.importCount() > 0 || this.unit.typeAliasCount() > 0 || this.unit.operatorCount() > 0 || this.unit.getHeaderDeclaration() != null) {
// any imports, type aliases, operators or header declarations -> don't allow any package declarations
this.mode = IMPORT;
} else {
// nothing defined yet -> allow a package declaration
this.mode = PACKAGE;
}
if (!checkEnd(pm, type)) {
pm.report(token, "header.separator");
pm.reparse();
}
return;
case PACKAGE:
if (type == DyvilKeywords.PACKAGE) {
PackageDeclaration pack = new PackageDeclaration(token.raw());
this.unit.setPackageDeclaration(pack);
pm.pushParser(new PackageParser(pack));
this.mode = SEPARATOR;
return;
}
// Fallthrough
case IMPORT:
switch(type) {
case DyvilKeywords.IMPORT:
pm.pushParser(new ImportParser(this.importConsumer(token)));
this.mode = SEPARATOR;
return;
case DyvilKeywords.USING:
pm.pushParser(new ImportParser(this.importConsumer(token), KindedImport.USING_DECLARATION));
this.mode = SEPARATOR;
return;
case DyvilKeywords.OPERATOR:
pm.pushParser(new OperatorParser(this.unit, Operator.INFIX), true);
this.mode = SEPARATOR;
return;
case DyvilKeywords.PREFIX:
case DyvilKeywords.POSTFIX:
case DyvilKeywords.INFIX:
if (//
token.next().type() == DyvilKeywords.OPERATOR || token.next().type() == DyvilKeywords.POSTFIX && token.next().next().type() == DyvilKeywords.OPERATOR) {
pm.pushParser(new OperatorParser(this.unit), true);
this.mode = SEPARATOR;
return;
}
// parse as modifier (in 'case CLASS' via fallthrough)
break;
case DyvilKeywords.TYPE:
pm.pushParser(new TypeAliasParser(this.unit, new TypeAlias()));
this.mode = SEPARATOR;
return;
case DyvilKeywords.HEADER:
final IToken next = token.next();
if (!Tokens.isIdentifier(next.type())) {
this.attributes = new AttributeList();
pm.report(next, "header.declaration.identifier");
return;
}
pm.skip();
if (this.unit.getHeaderDeclaration() != null) {
this.attributes = new AttributeList();
pm.report(token, "header.declaration.duplicate");
this.mode = SEPARATOR;
return;
}
final HeaderDeclaration declaration = new HeaderDeclaration(this.unit, next.raw(), next.nameValue(), this.attributes);
this.unit.setHeaderDeclaration(declaration);
// reset
this.attributes = new AttributeList();
this.mode = SEPARATOR;
return;
}
// Fallthrough
case CLASS:
if (this.parseAttribute(pm, token)) {
return;
}
final int classType;
if ((classType = ModifierParser.parseClassTypeModifier(token, pm)) >= 0) {
if ((this.flags & NO_CLASSES) != 0) {
pm.report(token, "header.class");
}
this.attributes.addFlag(classType);
pm.pushParser(new ClassDeclarationParser(this.unit, this.attributes));
// reset
this.attributes = new AttributeList();
this.mode = SEPARATOR;
return;
}
}
if (!checkEnd(pm, type)) {
pm.report(Markers.syntaxError(token, "header.element.invalid", token.toString()));
}
}
use of dyvilx.tools.compiler.ast.attribute.AttributeList in project Dyvil by Dyvil.
the class ParameterListParser method reset.
private void reset() {
this.attributes = new AttributeList();
this.type = Types.UNKNOWN;
this.parameter = null;
this.flags &= ~VARARGS;
}
use of dyvilx.tools.compiler.ast.attribute.AttributeList 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.ast.attribute.AttributeList in project Dyvil by Dyvil.
the class Field method writeAnnotations.
private void writeAnnotations(FieldVisitor fieldVisitor, long flags) {
ModifierUtil.writeModifiers(fieldVisitor, flags);
final AttributeList annotations = this.getAttributes();
if (annotations != null) {
int count = annotations.size();
for (int i = 0; i < count; i++) {
annotations.get(i).write(fieldVisitor);
}
}
IType.writeAnnotations(this.getType(), fieldVisitor, TypeReference.newTypeReference(TypeReference.FIELD), "");
}
use of dyvilx.tools.compiler.ast.attribute.AttributeList in project Dyvil by Dyvil.
the class CodeTypeParameter method computeReifiedKind.
@Override
protected void computeReifiedKind() {
super.computeReifiedKind();
final IType type;
final Reified.Type reifiedKind = this.getReifiedKind();
if (reifiedKind == Reified.Type.TYPE) {
type = TypeOperator.LazyFields.TYPE;
} else if (reifiedKind != null) {
type = ClassOperator.LazyFields.CLASS;
} else {
return;
}
if (this.getReifiedKind() != null) {
final AttributeList attributes = AttributeList.of(Modifiers.MANDATED | Modifiers.SYNTHETIC | Modifiers.FINAL);
final Name name = Name.apply("reify_" + this.getName().qualified);
final CodeParameter parameter = new CodeParameter(null, this.getPosition(), name, type, attributes);
this.setReifyParameter(parameter);
}
}
Aggregations