package p.studio.compiler.pbs.parser;

import p.studio.compiler.pbs.lexer.PbsToken;
import p.studio.compiler.source.Span;
import p.studio.compiler.source.diagnostics.DiagnosticSink;
import p.studio.compiler.source.identifiers.FileId;

final class PbsParserContext {
    private final PbsTokenCursor cursor;
    private final FileId fileId;
    private final DiagnosticSink diagnostics;
    private final PbsParser.ParseMode parseMode;
    private int loopDepth;

    PbsParserContext(
            final PbsTokenCursor cursor,
            final FileId fileId,
            final DiagnosticSink diagnostics,
            final PbsParser.ParseMode parseMode) {
        this.cursor = cursor;
        this.fileId = fileId;
        this.diagnostics = diagnostics;
        this.parseMode = parseMode;
    }

    PbsTokenCursor cursor() {
        return cursor;
    }

    DiagnosticSink diagnostics() {
        return diagnostics;
    }

    PbsParser.ParseMode parseMode() {
        return parseMode;
    }

    void enterLoop() {
        loopDepth++;
    }

    void exitLoop() {
        loopDepth--;
    }

    boolean isInsideLoop() {
        return loopDepth > 0;
    }

    Span span(final long start, final long end) {
        return new Span(fileId, start, end);
    }

    void report(final PbsToken token, final ParseErrors parseErrors, final String message) {
        p.studio.compiler.source.diagnostics.Diagnostics.error(
                diagnostics,
                parseErrors.name(),
                message,
                span(token.start(), token.end()));
    }
}
