"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = void 0;
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 }; }
var _default = exports["default"] = {
  setUp: function setUp(done) {
    done();
  },
  'genlex find offsets when success': function genlex_find_offsets_when_success(test) {
    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));
    test.ok(parsing.isEos(), 'input is consumed');
    test.equal(5, parsing.offset, 'there are 5 keywords');
    test.equal(8, parsing.input.location(parsing.offset), 'there are 8 chars');
    test.done();
  },
  'genlex find offsets when fail': function genlex_find_offsets_when_fail(test) {
    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 = '+  +  +* --';
    //            0  1  23
    var parsing = parser.parse(_stream["default"].ofString(text));
    test.ok(!parsing.isEos(), 'an error should have occurred');
    test.equal(3, parsing.getOffset(), 'Failed at the third token');
    test.equal(7, parsing.location(), 'fail is not 3: it must be the char offset before the error');
    test.done();
  },
  'expect Genlex to be constructed with spaces ': function expect_Genlex_to_be_constructed_with_spaces_(test) {
    var genlex = new _genlex.GenLex();
    test.ok(genlex.spaces !== undefined);
    test.ok(genlex.definitions.length === 0);
    test.done();
  },
  'expect tokenize() to add on definition': function expect_tokenize_to_add_on_definition(test) {
    var genlex = new _genlex.GenLex();
    genlex.tokenize(_parsec.N.number(), 'number', 500);
    test.ok(genlex.definitions.length === 1);
    test.done();
  },
  'expect use() to sort definitions by revert precedence': function expect_use_to_sort_definitions_by_revert_precedence(test) {
    var genlex = new _genlex.GenLex();
    var tkNumber = genlex.tokenize(_parsec.N.number(), 'number');
    var tkDate = genlex.tokenize(date(), 'date', 800);
    var tkChar = genlex.tokenize(_parsec.C.charLiteral(), 'char', 1200);
    var grammar = tkDate.then(tkNumber.rep().or(tkChar));
    test.notEqual(genlex.definitions[0].name, 'date');
    genlex.use(grammar);
    test.equal(genlex.definitions[2].name, 'date');
    test.equal(genlex.definitions[0].name, 'char');
    test.done();
  },
  'expect use() to create an easy tokenizer': function expect_use_to_create_an_easy_tokenizer(test) {
    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'));
    test.ok(parsing.isAccepted());
    test.done();
  },
  'a Genlex can update its precedence': function a_Genlex_can_update_its_precedence(test) {
    var genlex = new _genlex.GenLex();
    var tkNumber = genlex.tokenize(_parsec.N.number(), 'number');
    var tkDate = genlex.tokenize(date(), 'date', 800);
    var content = '10/05/2014 34 23';
    genlex.setSeparators(' /');
    genlex.updatePrecedence('number', 10);
    var grammar = tkDate.or(tkNumber).rep();
    var parser = genlex.use(grammar);
    var parsing = parser.parse(_stream["default"].ofString(content));
    test.deepEqual(parsing.value.array(), [10, 5, 2014, 34, 23]);
    test.done();
  },
  'GenLex can tokenize keywords': function GenLex_can_tokenize_keywords(test) {
    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));
    test.ok(parsing.isAccepted(), 'GenLex can tokenize keywords');
    test.done();
  },
  'tokenize mixes with keywords': function tokenize_mixes_with_keywords(test) {
    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));
    test.ok(parsing.isEos(), 'tokenize mixes with keywords');
    test.done();
  },
  'getMathGenLex() gives a simple genlex': function getMathGenLex_gives_a_simple_genlex(test) {
    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));
    test.deepEqual(parsing.value.array(), ['15', '14'], 'getMathGenLex() gives a simple genlex');
    test.done();
  },
  'getMathGenLex can be enhanced with a parser': function getMathGenLex_can_be_enhanced_with_a_parser(test) {
    var genlex = (0, _genlex.getMathGenLex)();
    genlex.remove('-');
    var number = genlex.get('number');
    //const digits = genlex.get('digits');
    var tkDate = genlex.tokenize(date(), '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));
    test.ok(parsing.isAccepted());
    test.done();
  },
  'getMathGenLex can be enhanced with a string': function getMathGenLex_can_be_enhanced_with_a_string(test) {
    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));
    test.ok(parsing.isEos());
    test.done();
  },
  'getMathGenLex can be enhanced with a string and no name': function getMathGenLex_can_be_enhanced_with_a_string_and_no_name(test) {
    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));
    test.ok(parsing.isEos());
    test.done();
  },
  'genlex can change separators with a given string': function genlex_can_change_separators_with_a_given_string(test) {
    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));
    test.ok(parsing.isAccepted());
    test.deepEqual(parsing.value.array(), [15, 12, 35]);
    test.done();
  },
  'genlex separators must be a string': function genlex_separators_must_be_a_string(test) {
    var genlex = (0, _genlex.getMathGenLex)();
    var found = false;
    try {
      genlex.setSeparators(_parsec.C["char"]('-'));
    } catch (_unused) {
      found = true;
    }
    test.ok(found);
    test.done();
  },
  'genlex can change separators with a full Parser': function genlex_can_change_separators_with_a_full_Parser(test) {
    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));
    test.ok(parsing.isAccepted());
    test.deepEqual(parsing.value.array(), [15, 12, 35, 10]);
    test.done();
  },
  'genlex provide all named tokens': function genlex_provide_all_named_tokens(test) {
    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;
    //const number = genlex.get('number');

    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));
    test.ok(parsing.isAccepted());
    test.deepEqual(parsing.value.array(), [12, '+', 35]);
    test.done();
  }
};
function date() {
  return _parsec.N.digits().then(_parsec.C.charIn('-/').returns('-')).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('');
  });
}
//# sourceMappingURL=genlex-test.js.map