import { DiagnosticSeverity, Range, Position } from "vscode-languageserver/node";
import { Herb, Visitor } from "@herb-tools/node-wasm";
class ErrorVisitor extends Visitor {
    constructor() {
        super(...arguments);
        this.source = "Herb Parser ";
        this.diagnostics = [];
    }
    visitChildNodes(node) {
        super.visitChildNodes(node);
        node.errors.forEach(error => this.addDiagnosticForError(error, node));
    }
    addDiagnosticForError(error, node) {
        const diagnostic = {
            source: this.source,
            severity: DiagnosticSeverity.Error,
            range: this.rangeFromHerbError(error),
            message: error.message,
            code: error.type,
            data: {
                error: error.toJSON(),
                node: node.toJSON()
            }
        };
        this.diagnostics.push(diagnostic);
    }
    rangeFromHerbError(error) {
        return Range.create(Position.create(error.location.start.line - 1, error.location.start.column), Position.create(error.location.end.line - 1, error.location.end.column));
    }
}
export class ParserService {
    parseDocument(textDocument) {
        const content = textDocument.getText();
        const result = Herb.parse(content);
        const errorVisitor = new ErrorVisitor();
        result.visit(errorVisitor);
        return {
            document: result.value,
            diagnostics: errorVisitor.diagnostics
        };
    }
}
//# sourceMappingURL=parser_service.js.map