"use strict";
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
    result["default"] = mod;
    return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
const nearley = __importStar(require("nearley"));
const astBuilder_1 = require("./astBuilder");
const compiledGrammar = __importStar(require("./grammar"));
var ValidationLevel;
(function (ValidationLevel) {
    ValidationLevel[ValidationLevel["ERROR"] = 0] = "ERROR";
    ValidationLevel[ValidationLevel["WARNING"] = 1] = "WARNING";
    ValidationLevel[ValidationLevel["INFO"] = 2] = "INFO";
})(ValidationLevel = exports.ValidationLevel || (exports.ValidationLevel = {}));
class Parser {
    constructor() {
        this.grammar = nearley.Grammar.fromCompiled(compiledGrammar);
    }
    parseToAST(code) {
        const parser = new nearley.Parser(this.grammar);
        parser.feed(code);
        parser.feed('\n');
        parser.finish();
        const results = parser.results;
        if (results.length === 1) {
            return results[0];
        }
        else if (results.length === 0) {
            throw new Error(`ERROR: Could not parse`);
        }
        else {
            throw new Error(`AMBIGUOUS: has ${results.length} results`);
        }
    }
    parse(code) {
        const node = this.parseToAST(code);
        const builder = new astBuilder_1.AstBuilder(code);
        const gameData = builder.build(node);
        return { data: gameData };
    }
}
exports.default = new Parser();
//# sourceMappingURL=parser.js.map