How to use the parsimmon.lazy 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 kantord / emuto / src / parsers / parentheses.js View on Github external
// @flow

import P from 'parsimmon'

import type { ParenthesesNodeType, ParserType } from '../types'

export default P.lazy((): ParserType => {
  const ProgramParser = require('./program').default
  return P.seq(P.string('('), ProgramParser, P.string(')'))
    .map((value: [mixed, ParenthesesNodeType, mixed]): ParenthesesNodeType => ({
      name: 'parentheses',
      value: value[1]
    }))
    .desc('parentheses')
})
github kantord / emuto / src / parsers / functionCallLambda.js View on Github external
// @flow

import P from 'parsimmon'
import crap from './crap'

import type {
  FunctionCallLambdaNodeType,
  NodeType,
  IdentifierNodeType
} from '../types'

const FunctionCallLambdaParser = P.lazy((): mixed => {
  const TupleParser = require('./tuple/tuple').default
  const IdentifierParser = require('./identifier').default
  return P.seq(
    IdentifierParser,
    crap,
    P.alt(
      P.string('$')
        .then(crap)
        .then(P.string('=>'))
        .then(crap),
      P.string('\\')
    ),
    crap,
    TupleParser
  ).map(
    ([left, _, __, ___, right]: [
github kantord / emuto / src / parsers / abstract / binaryOperator.js View on Github external
const BinaryOperatorParser = (
  OperandParser: ParserType,
  OperatorParser: ParserType,
  description: string
): ParserType =>
  P.lazy((): mixed => {
    const OperatorParser_ = OperatorParser.node('primitive')
    return P.seq(
      OperandParser,
      P.seq(crap.then(OperatorParser_), crap.then(OperandParser)).many()
    )
      .map(
        (value: [NodeType, Array]): NodeType =>
          value[1].length > 0
            ? {
              name: 'binaryOperation',
              value: value[1].reduce(
                (a: Array, b: NodeType): Array =>
                  a.concat(b),
                [value[0]]
              )
            }
github squiggle-lang / squiggle-lang / src / parse / pattern-factory.js View on Github external
return PatternSimple;
    },
    Match: function() {
      return P.alt(
        PatternLiteral,
        PatternSimple,
        PatternParenExpr
      );
    }
  };

  if (!helpers.hasOwnProperty(type)) {
    throw new Error("cannot make pattern factory for " + type);
  }

  var PatternLeaf = P.lazy(helpers[type]);

  var PatternSimple =
    ione(ast.PatternSimple, ps.Identifier);

  var PatternParenExpr =
    ione(ast.PatternParenExpr, ps.ParenExpr);

  var PatternLiteral =
    ione(ast.PatternLiteral,
      P.alt(
        ps.Number,
        ps.String,
        ps.NamedLiteral
      ));

  var PatternObjectPairBasic =
github afader / thompson-regex-js / lib / parser.js View on Github external
var star = Parsimmon.string('*');
var qmark = Parsimmon.string('?');
var bslash = Parsimmon.string('\\');
var suffixOperator = (operator, fn) => parser => parser.skip(operator).markMap(fn);
var plussed = suffixOperator(plus, oneOrMore);
var starred = suffixOperator(star, zeroOrMore);
var qmarked = suffixOperator(qmark, zeroOrOne);
var parenthesized = parser => lparen.then(parser).skip(rparen);
var bracketed = parser => lsqrb.then(parser).skip(rsqrb);
var bslashed = (string, map) => Parsimmon.string(`\\${string}`).markMap(map);
var escape = c => bslashed(c, exprs.atom(c)).markMap(quote).markMap(charEquals);
var sepBy = function(parser, separator) {
  var pairs = separator.then(parser).many();
  return parser.chain(r => pairs.map(rs => [r].concat(rs)));
};
var rootRegex = Parsimmon.lazy('root', () => regex.markMap(root));
var regex = Parsimmon.lazy('regex', () => sepBy(branch, pipe).markMap(alternation));
var wildcard = dot.markMap(wildcard);
var charPattern = /[^\\*+?()\[\].|]/;
var character = Parsimmon.regex(charPattern).markMap(exprs.atom).markMap(quote).markMap(charEquals);
var escChars = ['(', ')', '[', ']', '*', '+', '\\', '.', '|'];
var escaped = Parsimmon.alt.apply(null, escChars.map(escape));
var specialCharClass = Parsimmon.alt(
  bslashed('s', whitespace),
  bslashed('w', word),
  bslashed('n', newline),
  bslashed('d', digit)
);
var customClassChar = Parsimmon.regex(/[^\[\]\\]/);
var escapedBrackets = Parsimmon.alt(escape('['), escape(']'));
var escapedDash = escape('-');
var escapedBslash = escape('\\');
github mollerse / ait-lang / lib / parser / parse.js View on Github external
const rbracket = lexme(string(']'));
const blockterminator = lexme(string(';'));
const path = lexme(regex(/[a-zA-Z0-9._~!$&'()*+,;=:@%/-]+/)).desc('file-path');
const str = lexme(regex(/["']((?:\\.|.|\n)*?)["']/, 1)).desc('quoted string');
const booleanTrue = lexme(string('true')).desc('boolean true literal');
const booleanFalse = lexme(string('false')).desc('boolean false literal');
const number = lexme(regex(/[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?|[-+]?Infinity/)).desc(
  'number literal'
);
const wordLiteral = lexme(regex(/[a-zA-Z0-9.<>+=\\*%!\|&/_-]+/)).desc('word literal');
const load = lexme(string('@load'));
const arrow = lexme(string('->'));
const fatArrow = lexme(string('->>'));
const colon = lexme(string(':')).desc('colon-char');

const primitive = lazy('primivite values', function() {
  return alt(primitiveStringParser, primitiveBooleanParser, primitiveNumberParser);
});

const value = lazy('top level-values', function() {
  return alt(primitive, quotationParser);
});

const words = lazy('words', function() {
  return alt(value, setGlobalVarParser, setVarParser, wordLiteralParser);
});

const expressions = lazy('top-level expressions', function() {
  return alt(definitionParser, words, loadDirectiveParser);
});

const primitiveStringParser = str.map(primitiveString);
github squiggle-lang / squiggle-lang / src / parse / pattern-factory.js View on Github external
function patternFactory(type, ps) {
  var Pattern = P.lazy(function() {
    return P.alt(
      PatternArray,
      PatternObject,
      PatternLeaf
    );
  });

  var helpers = {
    Let: function() {
      return PatternSimple;
    },
    Match: function() {
      return P.alt(
        PatternLiteral,
        PatternSimple,
        PatternParenExpr
github keean / zenscript / src / parse.js View on Github external
function fix(f) {
   let lazy
   lazy = f(P.lazy(() => {return lazy}))
   return lazy
}
github kantord / emuto / src / parsers / collections / collectionCore.js View on Github external
const SpreadParser = P.string('...')
    .then(crap)
    .then(TupleParser)
    .node('spread')
  const SimpleListParser = P.sepBy(TupleParser, SeparatorParser).node(
    'simpleList'
  )
  return P.sepBy(P.alt(SpreadParser, SimpleListParser), SeparatorParser).map(
    (value: CollectionCoreValueType): CollectionCoreNodeType => ({
      name: 'collectionCore',
      value
    })
  )
})

const ComprehensionParser = P.lazy((): mixed => {
  const TupleParser = require('./tuple').default
  const ProgramParser = require('../program').default
  return P.seq(
    P.string('each')
      .then(crap)
      .then(TupleParser),
    crap.then(P.string('in')).then(crap.then(TupleParser)),
    crap
      .then(P.string('if'))
      .then(crap.then(TupleParser))
      .atMost(1),
    crap.then(ProgramParser).atMost(1)
  ).map(
    ([left, middle, condition, right]: [
      TupleNodeType,
      TupleNodeType,
github squiggle-lang / squiggle-lang / src / parsimmon-salad.js View on Github external
keys.forEach(function(k) {
    var f = almostParsers[k];
    var g = f.bind(null, parsers);
    parsers[k] = P.lazy(g);
  });
  Object.freeze(parsers);