How to use the parsimmon.of function in parsimmon

To help you get started, we’ve selected a few parsimmon examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github squiggle-lang / squiggle-lang / src / parse / parameters.js View on Github external
// All pieces are optional.

  var BareSlurp =
    ione(ast.Parameter,
      ione(ast.Identifier, P.of("_")));

  var ParamSlurpy = word("...").then(Parameter.or(BareSlurp));
  var ParamContext = P.string("@").then(Parameter);
  var ParamsPositional = list0(ps.Separator, Parameter);

  var Params2 =
    P.alt(
      P.seq(ParamsPositional, ps.Separator.then(ParamSlurpy)),
      P.seq(P.of([]), ParamSlurpy),
      P.seq(ParamsPositional, P.of(null)),
      P.of(null)
    );

  var Params1 =
    P.alt(
      ParamContext.chain(function(x) {
        return ps.Separator.then(Params2.map(cons(x)));
      }),
      ParamContext.map(function(x) { return [x, [], null]; }),
      Params2.map(cons(null))
    );

  return iseq(ast.Parameters, Params1);
};
github squiggle-lang / squiggle-lang / src / parse / parameters.js View on Github external
// The third piece (...xs) is the slurpy.
  // All pieces are optional.

  var BareSlurp =
    ione(ast.Parameter,
      ione(ast.Identifier, P.of("_")));

  var ParamSlurpy = word("...").then(Parameter.or(BareSlurp));
  var ParamContext = P.string("@").then(Parameter);
  var ParamsPositional = list0(ps.Separator, Parameter);

  var Params2 =
    P.alt(
      P.seq(ParamsPositional, ps.Separator.then(ParamSlurpy)),
      P.seq(P.of([]), ParamSlurpy),
      P.seq(ParamsPositional, P.of(null)),
      P.of(null)
    );

  var Params1 =
    P.alt(
      ParamContext.chain(function(x) {
        return ps.Separator.then(Params2.map(cons(x)));
      }),
      ParamContext.map(function(x) { return [x, [], null]; }),
      Params2.map(cons(null))
    );

  return iseq(ast.Parameters, Params1);
};
github squiggle-lang / squiggle-lang / src / parse / parameters.js View on Github external
module.exports = function(ps) {
  var Parameter =
    ione(ast.Parameter, ps.Identifier);

  // Parameters look like this:
  // (@this, a, b, c, ...xs)
  // The first piece (@this) is the context.
  // The second piece (a, b, c) is the positional.
  // The third piece (...xs) is the slurpy.
  // All pieces are optional.

  var BareSlurp =
    ione(ast.Parameter,
      ione(ast.Identifier, P.of("_")));

  var ParamSlurpy = word("...").then(Parameter.or(BareSlurp));
  var ParamContext = P.string("@").then(Parameter);
  var ParamsPositional = list0(ps.Separator, Parameter);

  var Params2 =
    P.alt(
      P.seq(ParamsPositional, ps.Separator.then(ParamSlurpy)),
      P.seq(P.of([]), ParamSlurpy),
      P.seq(ParamsPositional, P.of(null)),
      P.of(null)
    );

  var Params1 =
    P.alt(
      ParamContext.chain(function(x) {
github squiggle-lang / squiggle-lang / src / parse-helpers.js View on Github external
function list0(sep, par) {
  return list1(sep, par).or(P.of([]));
}
github squiggle-lang / squiggle-lang / src / parse-helpers.js View on Github external
return NameParser.chain(function(x) {
    if (x === kw) {
      return P.of(kw);
    } else {
      return P.fail(kw);
    }
  });
}
github squiggle-lang / squiggle-lang / src / parse / block.js View on Github external
function checkBlock(statements) {
  var n = statements.length;
  var i = n - 1;
  var last = statements[i];
  if (last.type !== "ExprStmt") {
    return P.fail("blocks must end with an expression");
  } else {
    return P.of([statements.slice(0, i), last]);
  }
}
github squiggle-lang / squiggle-lang / src / parse / identifier.js View on Github external
function helper(id) {
  if (keywords.indexOf(id) < 0) {
    return P.of(id);
  } else {
    return P.fail("identifier but got keyword '" + id + "'");
  }
}
github famibee / SKYNovel / core / lib / sn / PropParser.js View on Github external
            let parser = P.lazy(() => nextParser.chain((next) => P.seq(operatorsParser, P.of(next), parser).or(P.of(next))));
            return parser;
github squiggle-lang / squiggle-lang / src / parse / number.js View on Github external
.desc("fractional digits")
  .map(join(""));

var Exponential =
  P.seq(
    P.regex(/[eE]/),
    P.regex(/[+-]?/),
    Integer
  )
  .desc("exponential")
  .map(join(""));

var Float =
  P.seq(
    Integer,
    FractionalDigits.or(P.of("")),
    Exponential.or(P.of(""))
  )
  .map(join(""));

var NaN_ = P.string("NaN").desc("NaN");
var Infinity_ = P.string("Infinity").desc("Infinity");

var Number_ =
  ione(
    ast.Number,
    P.alt(Float, NaN_, Infinity_)
      .desc("number")
      .map(stripUnderscores)
      .map(global.Number)
  );
github squiggle-lang / squiggle-lang / src / parse / function.js View on Github external
module.exports = function(ps) {
  var OptionalName = ps.Identifier.or(P.of(null));
  return iseq(ast.Function,
    P.seq(
      keyword("fn").then(_).then(OptionalName).skip(_),
      wrap("(", ps.Parameters, ")").skip(_),
      ps.Expr
    ));
};