"use strict";

var _vitest = require("vitest");
var _parsec = require("../../lib/parsec");
var _genlex = require("../../lib/genlex/genlex");
var _stream = _interopRequireDefault(require("../../lib/stream"));
function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }
// Helper function from the original test file
function dateParser() {
  // Renamed from date() to avoid conflict if imported elsewhere
  return _parsec.N.digits().then(_parsec.C.charIn(['-', '/']).returns('-')) // Standardize separator for join
  .then(_parsec.N.digits()).then(_parsec.C.charIn(['-', '/']).returns('-')).then(_parsec.N.digits()).map(function (dateValues) {
    return dateValues[4] > 2000 ? dateValues.reverse() : dateValues;
  }).map(function (dateArray) {
    return dateArray.join('');
  });
}
(0, _vitest.describe)('GenLex Tests', function () {
  (0, _vitest.it)('genlex find offsets when success', function () {
    var genlex = new _genlex.GenLex();
    var plus = genlex.tokenize('+');
    var minus = genlex.tokenize('-');
    var grammar = plus.or(minus).rep().thenEos();
    var parser = genlex.use(grammar);
    var text = '+ + - --';
    var parsing = parser.parse(_stream["default"].ofString(text));
    (0, _vitest.expect)(parsing.isEos()).toBe(true);
    (0, _vitest.expect)(parsing.offset).toBe(5); // Nodeunit: test.equal(5, parsing.offset, 'there are 5 keywords');
    (0, _vitest.expect)(parsing.input.location(parsing.offset)).toBe(8); // Nodeunit: test.equal(8, parsing.input.location(parsing.offset), 'there are 8 chars')
  });
  (0, _vitest.it)('genlex find offsets when fail', function () {
    var genlex = new _genlex.GenLex();
    var plus = genlex.tokenize('+');
    var minus = genlex.tokenize('-');
    var grammar = plus.or(minus).rep().thenEos();
    var parser = genlex.use(grammar);
    var text = '+  +  +* --';
    var parsing = parser.parse(_stream["default"].ofString(text));
    (0, _vitest.expect)(parsing.isEos()).toBe(false); // Nodeunit: test.ok(! parsing.isEos(), 'an error should have occurred');
    (0, _vitest.expect)(parsing.getOffset()).toBe(3); // Nodeunit: test.equal(3, parsing.getOffset(), 'Failed at the third token');
    (0, _vitest.expect)(parsing.location()).toBe(7); // Nodeunit: test.equal(7, parsing.location(), 'fail is not 3: it must be the char offset before the error');
  });
  (0, _vitest.it)('expect Genlex to be constructed with spaces ', function () {
    var genlex = new _genlex.GenLex();
    (0, _vitest.expect)(genlex.spaces).toBeDefined(); // Nodeunit: test.ok(genlex.spaces !== undefined);
    (0, _vitest.expect)(genlex.definitions.length).toBe(0); // Nodeunit: test.ok(genlex.definitions.length === 0);
  });
  (0, _vitest.it)('expect tokenize() to add on definition', function () {
    var genlex = new _genlex.GenLex();
    genlex.tokenize(_parsec.N.number(), 'number', 500);
    (0, _vitest.expect)(genlex.definitions.length).toBe(1); // Nodeunit: test.ok(genlex.definitions.length === 1);
  });
  (0, _vitest.it)('expect use() to sort definitions by revert precedence', function () {
    var genlex = new _genlex.GenLex();
    var tkNumber = genlex.tokenize(_parsec.N.number(), 'number');
    var tkDate = genlex.tokenize(dateParser(), 'date', 800);
    var tkChar = genlex.tokenize(_parsec.C.charLiteral(), 'char', 1200);
    var grammar = tkDate.then(tkNumber.rep().or(tkChar));

    // Nodeunit: test.notEqual(genlex.definitions[0].name, 'date'); (This assert is before genlex.use)
    // We can't directly replicate this without knowing the initial unsorted state behavior precisely.
    // The important part is the state *after* .use()
    genlex.use(grammar);
    (0, _vitest.expect)(genlex.definitions[0].name).toBe('char'); // Nodeunit: test.equal(genlex.definitions[0].name, 'char'); (after sort)
    (0, _vitest.expect)(genlex.definitions[2].name).toBe('date'); // Nodeunit: test.equal(genlex.definitions[2].name, 'date'); -> Indexing might differ, check sorted order.
    (0, _vitest.expect)(genlex.definitions[1].name).toBe('number'); // Assuming number has lowest precedence here.
  });
  (0, _vitest.it)('expect use() to create an easy tokenizer', function () {
    var genlex = new _genlex.GenLex();
    var tkNumber = genlex.tokenize(_parsec.N.number(), 'number');
    var grammar = tkNumber.rep();
    var parser = genlex.use(grammar);
    var parsing = parser.parse(_stream["default"].ofString('34 23'));
    (0, _vitest.expect)(parsing.isAccepted()).toBe(true); // Nodeunit: test.ok(parsing.isAccepted());
  });
  (0, _vitest.it)('a Genlex can update its precedence', function () {
    var genlex = new _genlex.GenLex();
    var tkNumber = genlex.tokenize(_parsec.N.number(), 'number');
    var tkDate = genlex.tokenize(dateParser(), 'date', 800);
    var content = '10/05/2014 34 23';
    genlex.setSeparators(' /');
    genlex.updatePrecedence('number', 10); // Make number lower precedence than default date tokenizer
    var grammar = tkDate.or(tkNumber).rep(); // No thenEos in original test here
    var parser = genlex.use(grammar);
    var parsing = parser.parse(_stream["default"].ofString(content));
    var tokens = parsing.value.array();
    (0, _vitest.expect)(tokens).toStrictEqual([10, 5, 2014, 34, 23]); // Assuming dateParser returns a string like this
  });
  (0, _vitest.it)('GenLex can tokenize keywords', function () {
    var genlex = new _genlex.GenLex();
    var plus = genlex.tokenize('+');
    var grammar = plus.rep().then(_parsec.F.eos().drop());
    var parser = genlex.use(grammar);
    var text = '+++++';
    var parsing = parser.parse(_stream["default"].ofString(text));
    (0, _vitest.expect)(parsing.isAccepted()).toBe(true); // Nodeunit: test.ok(parsing.isAccepted(), 'GenLex can tokenize keywords');
  });
  (0, _vitest.it)('tokenize mixes with keywords', function () {
    var genlex = new _genlex.GenLex();
    var number = genlex.tokenize(_parsec.N.number(), 'number');
    var plus = genlex.tokenize('+');
    var grammar = plus.or(number).rep().then(_parsec.F.eos().drop());
    var parser = genlex.use(grammar);
    var text = '++77++4+';
    var parsing = parser.parse(_stream["default"].ofString(text));
    (0, _vitest.expect)(parsing.isEos()).toBe(true); // Nodeunit: test.ok(parsing.isEos(), 'tokenize mixes with keywords');
  });
  (0, _vitest.it)('getMathGenLex() gives a simple genlex', function () {
    var genlex = (0, _genlex.getMathGenLex)();
    var number = genlex.get('number');
    var grammar = number.rep();
    var text = '15 14';
    var parser = genlex.use(grammar);
    var parsing = parser.parse(_stream["default"].ofString(text));
    // Original test: test.deepEqual(parsing.value.array(), ['15', '14'], ...);
    // N.number() typically returns numbers, not strings. Tokens would wrap these numbers.
    var tokenValues = parsing.value.array();
    (0, _vitest.expect)(tokenValues).toEqual([15, 14]);
  });
  (0, _vitest.it)('getMathGenLex can be enhanced with a parser', function () {
    var genlex = (0, _genlex.getMathGenLex)();
    genlex.remove('-');
    var number = genlex.get('number');
    var tkDate = genlex.tokenize(dateParser(), 'date', 800);
    var grammar = tkDate.rep().then(number).then(_parsec.F.eos());
    var text = '15-12-2018      12-02-2020   12 ';
    var parser = genlex.use(grammar);
    var parsing = parser.parse(_stream["default"].ofString(text));
    (0, _vitest.expect)(parsing.isAccepted()).toBe(true); // Nodeunit: test.ok(parsing.isAccepted());
  });
  (0, _vitest.it)('getMathGenLex can be enhanced with a string', function () {
    var genlex = (0, _genlex.getMathGenLex)();
    var number = genlex.get('number');
    var dol = genlex.tokenize('$', 'dol');
    var grammar = number.then(dol).rep().then(_parsec.F.eos());
    var text = '15 $ ';
    var parser = genlex.use(grammar);
    var parsing = parser.parse(_stream["default"].ofString(text));
    (0, _vitest.expect)(parsing.isEos()).toBe(true); // Nodeunit: test.ok(parsing.isEos());
  });
  (0, _vitest.it)('getMathGenLex can be enhanced with a string and no name', function () {
    var genlex = (0, _genlex.getMathGenLex)();
    var number = genlex.get('number');
    genlex.tokenize('$');
    var dol = genlex.get('$');
    var grammar = number.then(dol).rep().then(_parsec.F.eos());
    var text = '15 $ ';
    var parsing = genlex.use(grammar).parse(_stream["default"].ofString(text));
    (0, _vitest.expect)(parsing.isEos()).toBe(true); // Nodeunit: test.ok(parsing.isEos());
  });
  (0, _vitest.it)('genlex can change separators with a given string', function () {
    var genlex = (0, _genlex.getMathGenLex)();
    var number = genlex.get('number');
    var grammar = number.rep().then(_parsec.F.eos().drop());
    genlex.setSeparators('-');
    var text = '15-12-35--';
    var parser = genlex.use(grammar);
    var parsing = parser.parse(_stream["default"].ofString(text));
    (0, _vitest.expect)(parsing.isAccepted()).toBe(true); // Nodeunit: test.ok(parsing.isAccepted());
    var tokenValues = parsing.value.array();
    (0, _vitest.expect)(tokenValues).toEqual([15, 12, 35]); // Nodeunit: test.deepEqual(parsing.value.array(), [15, 12, 35]);
  });
  (0, _vitest.it)('genlex separators must be a string', function () {
    var genlex = (0, _genlex.getMathGenLex)();
    (0, _vitest.expect)(function () {
      // Nodeunit: test.ok(found); after try-catch
      genlex.setSeparators(_parsec.C["char"]('-'));
    }).toThrow();
  });
  (0, _vitest.it)('genlex can change separators with a full Parser', function () {
    var genlex = (0, _genlex.getMathGenLex)();
    var number = genlex.get('number');
    var grammar = number.rep().then(_parsec.F.eos().drop());
    var separatorParser = _parsec.C["char"]('-').then(_parsec.C["char"]('/').opt());
    genlex.setSeparatorsParser(separatorParser);
    var text = '15-12-/35--10';
    var parser = genlex.use(grammar);
    var parsing = parser.parse(_stream["default"].ofString(text));
    (0, _vitest.expect)(parsing.isAccepted()).toBe(true); // Nodeunit: test.ok(parsing.isAccepted());
    var tokenValues = parsing.value.array();
    (0, _vitest.expect)(tokenValues).toEqual([15, 12, 35, 10]); // Nodeunit: test.deepEqual(parsing.value.array(), [15, 12, 35,10]);
  });
  (0, _vitest.it)('genlex provide all named tokens', function () {
    var genlex = (0, _genlex.getMathGenLex)();
    var _genlex$tokens = genlex.tokens(),
      number = _genlex$tokens.number,
      plus = _genlex$tokens.plus,
      mult = _genlex$tokens.mult,
      open = _genlex$tokens.open,
      close = _genlex$tokens.close;
    var grammar = number.or(plus).or(open).or(close).or(mult).rep().then(_parsec.F.eos().drop());
    var text = '12+ 35';
    var parser = genlex.use(grammar);
    var parsing = parser.parse(_stream["default"].ofString(text));
    (0, _vitest.expect)(parsing.isAccepted()).toBe(true);
    var tokenValues = parsing.value.array();
    (0, _vitest.expect)(tokenValues).toEqual([12, '+', 35]);
  });
});
//# sourceMappingURL=genlex.spec.js.map