How to use the parsimmon/build/parsimmon.browser.regex 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 caolan / magery / parsers.js View on Github external
var trueLiteral = p.string('true').skip(p.optWhitespace)
    .result({type: 'boolean', value: true});

var falseLiteral = p.string('false').skip(p.optWhitespace)
    .result({type: 'boolean', value: false});

var booleanLiteral = p.alt(trueLiteral, falseLiteral).desc('boolean');

var numberLiteral = p.regex(/-?(0|[1-9]\d*)([.]\d+)?(e[+-]?\d+)?/i)
    .skip(p.optWhitespace)
    .desc('numeral')
    .map(function (x) {
        return {type: 'number', value: Number(x)};
    });

var stringLiteral = p.regex(/"((?:\\.|.)*?)"/, 1)
    .skip(p.optWhitespace)
    .desc('quoted string')
    .map(interpretEscapes)
    .map(function (x) {
        return {type: 'string', value: x};
    });

// thanks to parsimmon json example
function interpretEscapes(str) {
  var escapes = {
    b: '\b',
    f: '\f',
    n: '\n',
    r: '\r',
    t: '\t'
  };
github caolan / magery / parsers.js View on Github external
var variableName = p.regex(/\s*(?!\d)(\.|(?:(?:[^"\.\s\,=}/]\.?)+))/, 1)
    .desc('variable name')
    .map(function (x) {
        return {type: 'property', value: x};
     });

var trueLiteral = p.string('true').skip(p.optWhitespace)
    .result({type: 'boolean', value: true});

var falseLiteral = p.string('false').skip(p.optWhitespace)
    .result({type: 'boolean', value: false});

var booleanLiteral = p.alt(trueLiteral, falseLiteral).desc('boolean');

var numberLiteral = p.regex(/-?(0|[1-9]\d*)([.]\d+)?(e[+-]?\d+)?/i)
    .skip(p.optWhitespace)
    .desc('numeral')
    .map(function (x) {
        return {type: 'number', value: Number(x)};
    });

var stringLiteral = p.regex(/"((?:\\.|.)*?)"/, 1)
    .skip(p.optWhitespace)
    .desc('quoted string')
    .map(interpretEscapes)
    .map(function (x) {
        return {type: 'string', value: x};
    });

// thanks to parsimmon json example
function interpretEscapes(str) {
github caolan / magery / parsers.js View on Github external
if (kwargs === null) {
                kwargs = {};
            }
            kwargs[x.key] = x.value;
        }
        else {
            args.push(x);
        }
    }
    return {
        args: args,
        kwargs: kwargs
    };
});

var defineInner = p.regex(/(?!{{[\/#]define[\s}])[\s\S]+?(?={{[\/#]define[\s}])/)
        .desc('template definition block');

var defineTag = p.seqMap(
    p.optWhitespace,
    p.string("{{#define").desc("{{#define..."),
    p.whitespace,
    variableName.desc('definition name'),
    function (before, define, _, name) {
        return name.value;
    }
).chain(function (name) {
    return p.alt(
        p.string("/}}").desc("self-closed tag '/}}'"),
        p.string("}}").then(defineInner.skip(p.string('{{/define}}')))
    ).map(function (contents) {
        return {
github caolan / magery / parsers.js View on Github external
'Failed to parse ' + name
        );
        for (var k in result) {
            if (result.hasOwnProperty(k)) {
                e[k] = result[k];
            }
        }
        throw e;
    }
    return result.value;
};

var basicString = p.regex(/(?!{{)[\s\S]+?(?={{)/)
    .desc('non-template chars');

var variableName = p.regex(/\s*(?!\d)(\.|(?:(?:[^"\.\s\,=}/]\.?)+))/, 1)
    .desc('variable name')
    .map(function (x) {
        return {type: 'property', value: x};
     });

var trueLiteral = p.string('true').skip(p.optWhitespace)
    .result({type: 'boolean', value: true});

var falseLiteral = p.string('false').skip(p.optWhitespace)
    .result({type: 'boolean', value: false});

var booleanLiteral = p.alt(trueLiteral, falseLiteral).desc('boolean');

var numberLiteral = p.regex(/-?(0|[1-9]\d*)([.]\d+)?(e[+-]?\d+)?/i)
    .skip(p.optWhitespace)
    .desc('numeral')
github caolan / magery / parsers.js View on Github external
if (escapes.hasOwnProperty(type)) return escapes[type];
    return type;
  });
}

var argumentValue = p.alt(
    booleanLiteral,
    variableName,
    stringLiteral,
    numberLiteral
).skip(p.optWhitespace);

var tagPositionalArgument = p.optWhitespace.then(argumentValue)
    .desc('positional argument');

var keywordName = p.regex(/(?!\d)([0-9a-zA-Z_]+)=/, 1).desc('key name');

var tagKeywordArgument = p.optWhitespace.then(
    p.seqMap(
        keywordName,
        argumentValue,
        function (key, value) {
            return {
                type: 'kwarg',
                key: key,
                value: value
            };
        }
    )
).desc('keyword argument');

var tagParameters = p.alt(
github caolan / magery / parsers.js View on Github external
result.expected?
                'Expected: ' + result.expected.join(' or ') +
                    ' at \'' + str.substr(result.index) + '\'':
                'Failed to parse ' + name
        );
        for (var k in result) {
            if (result.hasOwnProperty(k)) {
                e[k] = result[k];
            }
        }
        throw e;
    }
    return result.value;
};

var basicString = p.regex(/(?!{{)[\s\S]+?(?={{)/)
    .desc('non-template chars');

var variableName = p.regex(/\s*(?!\d)(\.|(?:(?:[^"\.\s\,=}/]\.?)+))/, 1)
    .desc('variable name')
    .map(function (x) {
        return {type: 'property', value: x};
     });

var trueLiteral = p.string('true').skip(p.optWhitespace)
    .result({type: 'boolean', value: true});

var falseLiteral = p.string('false').skip(p.optWhitespace)
    .result({type: 'boolean', value: false});

var booleanLiteral = p.alt(trueLiteral, falseLiteral).desc('boolean');