<commit_before>module.exports = Repl

var repl = require('repl')
var spinner = require('char-spinner')
var CHR = require('./src/index')
var parse = require('./src/repl.peg.js').parse

function Repl () {
  var chr = new CHR()

  var r = repl.start({
    prompt: 'CHR > ',
    input: process.stdin,
    output: process.stdout,
    eval: function (cmd, context, filename, callback) {
      // try Program
      try {
        var rules = parse(cmd.trim(), { startRule: 'Program' })
      } catch (e) {
        // try Query
        try {
          var queries = parse(cmd.trim(), { startRule: 'Query' })<commit_msg>Add introspection commands to REPL<commit_after>module.exports = Repl

var repl = require('repl')
var spinner = require('char-spinner')
var CHR = require('./src/index')
var parse = require('./src/repl.peg.js').parse

function Repl () {
  var chr = new CHR()

  var r = repl.start({
    prompt: 'CHR > ',
    input: process.stdin,
    output: process.stdout,
    eval: function (cmd, context, filename, callback) {
      cmd = cmd.trim()
      var re
      var ruleName

      // is it a special command?
      if (cmd === '/exit') {
        process.exit(0)
      }
      if (cmd === '/rules') {
        chr.Rules.ForEach(function (rule) {
          r.output.write(require('util').inspect(rule._source, false, null))
        })

        return callback()
      }
      re = /^\/rule (.*)$/
      if (re.test(cmd)) {
        // e.g. `/rule rule-name`
        ruleName = cmd.replace(re, '$1')
        if (!chr.Rules[ruleName]) {
          r.output.write('No rule with name ' + ruleName)
          return callback()
        }

        r.output.write(require('util').inspect(chr.Rules[ruleName], false, null))
        return callback()
      }
      re = /^\/rule-functor ([^ ]*) (.*)$/
      if (re.test(cmd)) {
        // e.g. `/rule-functor rule-name functor-name`
        ruleName = cmd.replace(re, '$1')
        var functor = cmd.replace(re, '$2')

        if (!chr.Rules[ruleName]) {
          r.output.write('No rule with name ' + ruleName)
          return callback()
        }

        var occurrence = null
        re = /^(.*)\[([0-9]+)\]$/
        if (re.test(cmd)) {
          // has occurrence number
          occurrence = functor.replace(re, '$2')
          functor = functor.replace(re, '$1')
        }

        if (!chr.Rules[ruleName][functor]) {
          r.output.write('Rule ' + ruleName + ' has no functor ' + functor)
          return callback()
        }

        if (occurrence !== null) {
          if (!chr.Rules[ruleName][functor][occurrence]) {
            r.output.write('Rule ' + ruleName + ' has no occurrence ' + occurrence + ' of functor ' + functor)
            return callback()
          }

          r.output.write(chr.Rules[ruleName][functor][occurrence].toString())
          return callback()
        }

        r.output.write(require('util').inspect(chr.Rules[ruleName][functor], false, null))
        return callback()
      }

      // try Program
      try {
        var rules = parse(cmd, { startRule: 'Program' })
      } catch (e) {
        // try Query
        try {
          var queries = parse(cmd, { startRule: 'Query' })
{'fraction_non_alphanumeric': 0.16325888644227746, 'fraction_numerical': 0.0025165146272412707, 'mean_word_length': 1.9831144465290806, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '26698', 'n_tokens_mistral': 1010, 'n_tokens_neox': 980, 'n_words': 233}