package scotch.compiler.syntax.definition;
import static lombok.AccessLevel.PACKAGE;
import static scotch.compiler.syntax.builder.BuilderUtil.require;
import static scotch.compiler.syntax.reference.DefinitionReference.scopeRef;
import java.util.Optional;
import lombok.AllArgsConstructor;
import lombok.EqualsAndHashCode;
import lombok.ToString;
import scotch.compiler.analyzer.DependencyAccumulator;
import scotch.compiler.analyzer.NameAccumulator;
import scotch.compiler.analyzer.OperatorAccumulator;
import scotch.compiler.analyzer.PatternAnalyzer;
import scotch.compiler.analyzer.PrecedenceParser;
import scotch.compiler.analyzer.NameQualifier;
import scotch.compiler.analyzer.TypeChecker;
import scotch.compiler.intermediate.IntermediateGenerator;
import scotch.compiler.syntax.builder.SyntaxBuilder;
import scotch.compiler.syntax.reference.DefinitionReference;
import scotch.compiler.text.SourceLocation;
import scotch.symbol.Symbol;
@AllArgsConstructor(access = PACKAGE)
@EqualsAndHashCode(callSuper = false)
@ToString(exclude = "sourceLocation")
public class ScopeDefinition extends Definition {
public static Builder builder() {
return new Builder();
}
private final SourceLocation sourceLocation;
private final Symbol symbol;
@Override
public Definition accumulateDependencies(DependencyAccumulator state) {
return state.keep(this);
}
@Override
public Definition accumulateNames(NameAccumulator state) {
return state.keep(this);
}
@Override
public Definition checkTypes(TypeChecker state) {
return state.keep(this);
}
@Override
public Definition defineOperators(OperatorAccumulator state) {
return state.keep(this);
}
@Override
public Optional<DefinitionReference> generateIntermediateCode(IntermediateGenerator generator) {
throw new UnsupportedOperationException(); // TODO
}
@Override
public DefinitionReference getReference() {
return scopeRef(symbol);
}
@Override
public SourceLocation getSourceLocation() {
return sourceLocation;
}
@Override
public Optional<Definition> parsePrecedence(PrecedenceParser state) {
return Optional.of(state.keep(this));
}
@Override
public Definition qualifyNames(NameQualifier state) {
return state.keep(this);
}
@Override
public Definition reducePatterns(PatternAnalyzer state) {
throw new UnsupportedOperationException(); // TODO
}
public static class Builder implements SyntaxBuilder<ScopeDefinition> {
private Optional<Symbol> symbol;
private Optional<SourceLocation> sourceLocation;
private Builder() {
symbol = Optional.empty();
sourceLocation = Optional.empty();
}
@Override
public ScopeDefinition build() {
return Definitions.scopeDef(
require(sourceLocation, "Source location"),
require(symbol, "Scope symbol")
);
}
@Override
public Builder withSourceLocation(SourceLocation sourceLocation) {
this.sourceLocation = Optional.of(sourceLocation);
return this;
}
public Builder withSymbol(Symbol symbol) {
this.symbol = Optional.of(symbol);
return this;
}
}
}