How to use parsimmon - 10 common examples

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 osnr / livegb / lib / assembler / index.ts View on Github external
const simplePseudoOp: Parser = (function() {
  // FIXME: No expressions allowed in DB statements, to make it fast to parse.
  const dbConst: Parser = alt(
    numberLiteral.map(n => [n]), // 8BIT but no exprs pls
    stringLiteral.map(s => s.split('').map(c => c.charCodeAt(0)))
  );
  const db = symbol('DB').skip(optSpace)
    .then(sepBy(dbConst, string(',').then(optSpace)))
    // Support both DB as nullary var declaration and DB as data constants.
    .map(data => data.length > 0 ? [].concat.apply([], data) : [0]);

  const dw = symbol('DW').skip(optSpace)
    .then(sepBy(const_16bit, string(',').then(optSpace)))
    .map(data => [].concat.apply([], data));

  return alt(
    db,
    dw,
    binaryOp('SECTION', stringLiteral, seq(sectionType, indirect(numberLiteral)))
      .map(([name, [sectionType, address]]) => [{
        kind: 'section',
        name,
        sectionType,
        address
github osnr / livegb / lib / assembler / macro.ts View on Github external
seq(string(';'), takeWhile(c => c !== '\n'), string('\n')),
    string('\n')
  )
);

type Macro = { kind: 'macro', name: string; body: string; };

const equ: Parser = seqMap(
  optLineSpace.then(token).skip(lineSpace), string('EQU').skip(lineSpace),
  takeWhile(c => c !== ';' && c !== '\n'), eol,
  (name, _, body, _2) => ({ kind: 'macro', name, body } as Macro)
);

const equs: Parser = seqMap(
  optLineSpace.then(token).skip(lineSpace), string('EQUS').skip(lineSpace),
  string('"').then(takeWhile(c => c !== '"')).skip(string('"')), eol,
  (name, _, body, _2) => ({ kind: 'macro', name, body } as Macro)
);

// This parser needs to be information-preserving.
type Statement = { kind: 'statement', tokens: string[] };

const statement: Parser =
  optLineSpace.then(
    alt(token, lineSpace)
      .many()
      .map((tokens) => ({ kind: 'statement', tokens } as Statement))
  ).skip(eol);

const file: Parser<(Macro|Statement)[]> = alt(
  equs,
  equ,
github kantord / emuto / src / parsers / lambda.js View on Github external
// @flow

import P from 'parsimmon'
import crap from './crap'
import TupleParser from './collections/tuple'
import IdentifierParser from './identifier'

import type { LambdaNodeValueType, NodeType, VariableNodeType } from '../types'

export default P.alt(
  P.seq(
    P.string('$')
      .then(IdentifierParser.atMost(1))
      .skip(crap)
      .skip(P.string('=>')),
    crap.then(TupleParser)
  ).map(
    ([variable, definition]: [
      Array,
      NodeType
    ]): LambdaNodeValueType => ({
      variable: variable.length ? variable[0].value : 'input',
      definition
    })
  ),
  P.string('\\')
github kantord / emuto / src / parsers / primitives / number.js View on Github external
// @flow

import P from 'parsimmon'
import type { NodeType } from '../../types'

export default P.regexp(
  /(?:0|[1-9]\d*)(?:\.\d+)?(?:[eE][+-]?\d+)?/
).map((value: string): NodeType => ({
  name: 'primitive',
  value
}))
github microsoft / dtslint / bin / rules / definitelytyped-header-parser.js View on Github external
function contributorsParser(strict) {
    // Need to remove '^' and '$' from the regex, parsimmon does not expect those.
    const contributor = strict
        ? pm.seqMap(pm.regexp(/([^<]+) /, 1), pm.regexp(/\/, 1), (name, username) => ({ name, url: "https://github.com/" + username }))
        : pm.seqMap(pm.regexp(/([^<]+) /, 1), pm.regexp(/<([^>]+)>/, 1), (name, url) => ({ name, url }));
    const contributors = pm.sepBy1(contributor, separator);
    if (!strict) {
        // Allow trailing whitespace.
        return pm.seqMap(contributors, pm.regexp(/ */), a => a);
    }
    return contributors;
}
// TODO: Should we do something with the URL?
github kantord / emuto / src / parsers / inputProp.js View on Github external
// @flow

import P from 'parsimmon'
import type { InputPropNodeType } from '../types'

export default P.regexp(/\.[$A-Z_][0-9A-Z_$]*/i)
  .map((value: string): InputPropNodeType => ({
    name: 'inputProp',
    value
  }))
  .desc('inputProp')
github microsoft / dtslint / bin / rules / definitelytyped-header-parser.js View on Github external
function contributorsParser(strict) {
    // Need to remove '^' and '$' from the regex, parsimmon does not expect those.
    const contributor = strict
        ? pm.seqMap(pm.regexp(/([^<]+) /, 1), pm.regexp(/\/, 1), (name, username) => ({ name, url: "https://github.com/" + username }))
        : pm.seqMap(pm.regexp(/([^<]+) /, 1), pm.regexp(/<([^>]+)>/, 1), (name, url) => ({ name, url }));
    const contributors = pm.sepBy1(contributor, separator);
    if (!strict) {
        // Allow trailing whitespace.
        return pm.seqMap(contributors, pm.regexp(/ */), a => a);
    }
    return contributors;
}
// TODO: Should we do something with the URL?
github ant-move / Antmove / packages / @antmove / wx-alipay-plugin / parse / parseWxml.js View on Github external
/* eslint-disable */

const fs = require('fs');
const P = require('parsimmon');
const config = require('../config');

const lTagArrow = P.string('<');
const rTagArrow = P.string('>');
const endLine = P.string('/');
const doubleQuote = P.string('"');
const singleQuote = P.string("'");
const whitespaces = P.regex(/\s*/).desc('whitespaces');
const Wxml = P.createLanguage({
    Symbol: function () {
        return P.regexp(/[a-zA-Z_-][a-zA-Z0-9_-]*/).desc("symbol");
    },
    StringExpression: function () {
        // return P.regexp(/[^<]+/);
        return P.regex(/((?!<\/).)+/);
    },
    singleQuoteString: function () {
        return P.seqMap(
            singleQuote,
            P.regexp(/[^']*/),
            singleQuote,
            function (r1, r2, r3) {
                return {
                    type: 'single',
github beaugunderson / emoji-aware / parsers / emoji.js View on Github external
)
  ),
  Parsimmon.string('\udb40\udc7f') // cancel tag
);

var ZeroWidthJoiner = Parsimmon.string('\u200d')
  .desc('zero-width joiner (\\u200d)');

var OptionalFitzpatrickModifier =
  Parsimmon.regex(/(\ud83c[\udffb-\udfff]){0,1}/)
    .desc('an optional Fitzpatrick modifier');

var SimpleEmoji = Parsimmon.alt(
  // Simple Unicode emoji
  Parsimmon.regex(/[\u203c-\u2bff]/),
  Parsimmon.regex(/[\u2702-\u27b0]/),
  // Enclosed CJK Letters and Months
  Parsimmon.regex(/[\u3200-\u32ff]/),
  // Emoji flags
  FlagEmoji,
  GreatBritainEmoji,
  // Surrogate pairs
  Parsimmon.regex(/\ud83c[\udc04-\udfff]/),
  Parsimmon.regex(/\ud83d[\udc00-\udfff]/),
  Parsimmon.regex(/\ud83e[\udc00-\udfff]/)
);

var VariationSelectorEmoji = Parsimmon.seq(
  // Single characters that become emoji only with a variation selector
  Parsimmon.alt(
    Parsimmon.string('\u00a9'), // trademark
    Parsimmon.string('\u00ae'), // copyright
github brotchie / protobuf-textformat / src / parser.js View on Github external
"use strict";

/* Parser combinator based parser for the
 * protobuf text format.
 */

var Parsimmon = require('parsimmon');

var regex = Parsimmon.regex
  , string = Parsimmon.string
  , optWhitespace = Parsimmon.optWhitespace
  , lazy = Parsimmon.lazy
  , alt = Parsimmon.alt
  , seq = Parsimmon.seq;

var comment = regex(/#.+/).then(optWhitespace.atMost(1));
var whitespace = optWhitespace.then(comment.atMost(1));

var lexeme = function(p){ return p.skip(whitespace); }

var colon = lexeme(string(':'));

var lbrace = lexeme(string('{'))
  , rbrace = lexeme(string('}'));

var stripFirstLast = function(x) {
  return x.substr(1, x.length-2);