Rena.js

Renaはテキストをパースするライブラリです。
トップダウンパーシングを採用しているので、数式のような再帰的なパターンを記述することもできます。
Renaは合成属性および継承属性を扱うこともできます。
'Rena'はREpetation(またはREcursion) Notation APIの接頭語です。

インストール

Rena.jsのインストール:

npm install rena-js

モジュールの使用(node.js):

var M = require('rena-js');

モジュールの使用(ブラウザ):

<script src='rena.js'></script>

CSVのパース


var csvparser = R.t(
  R.attr([]).thenMaybe(
    R.delimitArray(
      R.delimitArray(R.or(
        R('"').t(/(""|[^"])+/, function(x) { return x.replace('""', '"'); }).t('"'),
        R(/[^",\n\r]+/, R.I)), ','), R.br())))
  .thenMaybe(R.br());

// outputs [['a','b','c'],['d','e\n\"f','g'],['h']]
console.log(csvparser.parse('a,b,c\nd,"e\n""f",g\nh\n').attribute)
        

四則演算


var expr = R.Yn(function(t, f, e) {
  return R.then(f).thenMaybe(R.or(
    R.then("+").then(f, function(x, a, b) { return b + a; }),
    R.then("-").then(f, function(x, a, b) { return b - a; })));
  },
  function(t, f, e) {
    return R.then(e).thenMaybe(R.or(
      R.then("*").then(e, function(x, a, b) { return b * a; }),
      R.then("/").then(e, function(x, a, b) { return b / a; })));
  },
  function(t, f, e) {
    return R.or(R.thenInt(/[0-9]+/), R.then("(").then(t).then(")"))
  });

console.log(expr.parse("1+2*3").attribute);  // outputs 7
console.log(expr.parse("4-6/2").attribute);  // outputs 1
        

ドキュメント

Rena.jsのドキュメントはここにあります。