<commit_before>const Gio = imports.gi.Gio;
const Options = imports.options;

const CmdOptions = [
  {
    name: 'ast',
    shortName: 'a',
    requireArgument: false,
    defaultValue: false,
  },
];

let config = Options.parseArguments(CmdOptions, ARGV);


let loadFile = function(path) {
  let file = Gio.File.new_for_path(path);
  let [, source] = file.load_contents(null);
  return '' + source;
};

let printFile = function(path) {
  print(loadFile(path));
};

let load = function(filename) {
  eval(loadFile(filename));
  //eval('(function() { ' + loadFile(filename) + ' }).apply(this);');
};

//eval(loadFile("lib.js"));
eval(loadFile("ometa-base.js"));
eval(loadFile("bs-js-compiler.js"));
eval(loadFile("bs-ometa-optimizer.js"));
eval(loadFile("bs-ometa-compiler.js"));
eval(loadFile("bs-ometa-js-compiler.js"));

let alert = log;

let indexToPosition = function(source, idx) {
  linePos = 0;
  lineNum = 0;
  for (let i = 0; i < idx; i++) {
    if (source.charAt(i) == '\n') {
      linePos = i;
      lineNum++;
    }
  }
  return { line: lineNum + 1,
           offset: idx - linePos };
};

let translateCode = function(s) {
  var translationError = function(m, i) {
      alert("Translation error - please tell Alex about this!");
      throw fail;
  },
  tree = BSOMetaJSParser.matchAll(s, "topLevel", undefined, function(m, i) {
    throw objectThatDelegatesTo(fail, {errorPos: i});
  });
  if (config.options.ast)
    return JSON.stringify(tree, null, 2);
  else
    return BSOMetaJSTranslator.match(tree, "trans", undefined, translationError)
};

let ometa = function(s) {
  return eval(translateCode(s));
};


let ometaSource = loadFile(config.arguments[0]);

try {
  print(translateCode(ometaSource));
} catch (e) {
  if (e.errorPos !== undefined) {
    let pos = indexToPosition(ometaSource, e.errorPos);
    log('Parsing error at : line ' + pos.line + ' offset ' + pos.offset + '\n\n');
  }
  throw e;
}<commit_msg>rhino: add option to generate standalone parsers/compilers<commit_after>const GLib = imports.gi.GLib;
const Gio = imports.gi.Gio;
const Options = imports.options;

const CmdOptions = [
  {
    name: 'ast',
    shortName: 'a',
    requireArgument: false,
    defaultValue: false,
    help: 'Output the AST representation of the program',
  },
  {
    name: 'base',
    shortName: 'b',
    requireArgument: false,
    defaultValue: false,
    help: 'Include the base compiler library (useful for standalone programs)',
  },
  {
    name: 'help',
    shortName: 'h',
    requireArgument: false,
    defaultValue: false,
    help: 'Print this screen',
  },
];

let loadFile = function(path) {
  let file = Gio.File.new_for_path(path);
  let [, source] = file.load_contents(null);
  return '' + source;
};

let printFile = function(path) {
  print(loadFile(path));
};

let alert = log;

let indexToPosition = function(source, idx) {
  linePos = 0;
  lineNum = 0;
  for (let i = 0; i < idx; i++) {
    if (source.charAt(i) == '\n') {
      linePos = i;
      lineNum++;
    }
  }
  return { line: lineNum + 1,
           offset: idx - linePos };
};

let translateCode = function(s, parser, translator) {
  var translationError = function(m, i) {
      alert("Translation error - please tell Alex about this!");
      throw fail;
  },
  tree = parser.matchAll(s, "topLevel", undefined, function(m, i) {
    throw objectThatDelegatesTo(fail, {errorPos: i});
  });
  if (translator)
    return translator.match(tree, "trans", undefined, translationError)
  else
    return JSON.stringify(tree, null, 2);
};

let start = function() {
  let config = Options.parseArguments(CmdOptions, ARGV);

  if (config.options.help) {
    Options.printHelp('gnometa', CmdOptions);
    return -1;
  }

  eval(loadFile("ometa-base.js"));
  eval(loadFile("bs-js-compiler.js"));
  eval(loadFile("bs-ometa-optimizer.js"));
  eval(loadFile("bs-ometa-compiler.js"));
  eval(loadFile("bs-ometa-js-compiler.js"));

  let ometaSource = loadFile(config.arguments[0]);

  try {
    if (config.options.base)
      print(loadFile('./ometa-base.js'));
    print(translateCode(ometaSource,
                        BSOMetaJSParser,
                        config.options.ast ? null : BSOMetaJSTranslator));
  } catch (e) {
    if (e.errorPos !== undefined) {
      let pos = indexToPosition(ometaSource, e.errorPos);
      log('Parsing error at : line ' + pos.line + ' offset ' + pos.offset + '\n\n');
    }
    throw e;
  }
};

start();
{'fraction_non_alphanumeric': 0.16023470999774317, 'fraction_numerical': 0.002933874971789664, 'mean_word_length': 3.3536345776031435, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, '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': '711890', 'n_tokens_mistral': 1541, 'n_tokens_neox': 1509, 'n_words': 345}