<commit_before>/* global describe, it, xit */
/* jslint node: true, esnext: true */

'use strict';

const chai = require('chai'),
  assert = chai.assert,
  expect = chai.expect,
  should = chai.should();

const createGrammar = require('../lib/grammar').createGrammar;

describe('calculator',
  function () {
    function value(value) {
      return Object.create(null, {
        value: {
          value: value
        }
      });
    }

    let myGrammar = createGrammar({
      prefix: {
        '(': {
          nud(grammar) {
            const e = grammar.expression(0);
            grammar.advance(')');
            return e;
          }
        }
      },
      infix: {
        ')': {},
        '+': {
          precedence: 50,
          combine: (left, right) => value(left.value + right.value)
        },
        '-': {
          precedence: 50,
          combine: (left, right) => value(left.value - right.value)
        },
        '*': {
          precedence: 60,
          combine: (left, right) => value(left.value * right.value)
        },
        '/': {
          precedence: 60,
          combine: (left, right) => value(left.value / right.value)
        }
      }
    });

    it('evaluates', () => assert.equal(myGrammar.parse('1 + 41 * 3 - 2').value, 122));
    it('evaluates with prefix op', () => assert.equal(myGrammar.parse('(1 + 41)').value, 42));
    it('evaluates with prefix op 2', () => assert.equal(myGrammar.parse('(1 + 41) * 2').value, 84));
    it('evaluates with prefix op 3', () => assert.equal(myGrammar.parse('(1 + 1) * (2 + 7)').value, 18));
    it('evaluates with prefix op 4', () => assert.equal(myGrammar.parse('(1 + (1 + 4 * 3)) * (2 + 1)').value, 42));
  });<commit_msg>test: check that unexpected token throws<commit_after>/* global describe, it, xit */
/* jslint node: true, esnext: true */

'use strict';

const chai = require('chai'),
  assert = chai.assert,
  expect = chai.expect,
  should = chai.should();

const createGrammar = require('../lib/grammar').createGrammar;

describe('calculator',
  function () {
    function value(value) {
      return Object.create(null, {
        value: {
          value: value
        }
      });
    }

    const myGrammar = createGrammar({
      prefix: {
        '(': {
          nud(grammar) {
            const e = grammar.expression(0);
            grammar.advance(')');
            return e;
          }
        }
      },
      infix: {
        ')': {},
        '+': {
          precedence: 50,
          combine: (left, right) => value(left.value + right.value)
        },
        '-': {
          precedence: 50,
          combine: (left, right) => value(left.value - right.value)
        },
        '*': {
          precedence: 60,
          combine: (left, right) => value(left.value * right.value)
        },
        '/': {
          precedence: 60,
          combine: (left, right) => value(left.value / right.value)
        }
      }
    });

    it('evaluates', () => assert.equal(myGrammar.parse('1 + 41 * 3 - 2').value, 122));
    it('evaluates with prefix op', () => assert.equal(myGrammar.parse('(1 + 41)').value, 42));
    it('evaluates with prefix op 2', () => assert.equal(myGrammar.parse('(1 + 41) * 2').value, 84));
    it('evaluates with prefix op 3', () => assert.equal(myGrammar.parse('(1 + 1) * (2 + 7)').value, 18));
    it('evaluates with prefix op 4', () => assert.equal(myGrammar.parse('(1 + (1 + 4 * 3)) * (2 + 1)').value, 42));

    describe('unexpected token', () => {
      it('thows', () => {
        try {
          myGrammar.parse('(1 + %');
          assert.ok(false);
        } catch (e) {
          if (e.message !== 'Unknown char,1,6: {"value":"%"}') {
            throw e;
          }
        }
      });
    });

  });
{'fraction_non_alphanumeric': 0.1893838356895172, 'fraction_numerical': 0.024806615097359295, 'mean_word_length': 1.9228371005455962, 'pattern_counts': {'":': 1, '<': 3, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4704450', 'n_tokens_mistral': 1308, 'n_tokens_neox': 1228, 'n_words': 254}