UNPKG

@masala/parser

Version:
75 lines (61 loc) 2.32 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); var _genlex = require('../../genlex/genlex'); var _index = require('../../parsec/index'); var _parsec = require('../../parsec'); // // Facilities // var genlex = new _genlex.GenLex(); /* * Parsec * https://github.com/d-plaindoux/parsec * * Copyright (c) 2016 Didier Plaindoux * Licensed under the LGPL2 license. */ genlex.keywords(['null', 'false', 'true', '{', '}', '[', ']', ':', ',']); var number = genlex.tokenize(_parsec.N.number(), 'number', 1100); var string = genlex.tokenize(_parsec.C.stringLiteral(), 'string', 800); function tkKey(s) { return genlex.get(s); } // unit -> Parser ? Token function arrayOrNothing() { // FIXME: ES2015 code not great var value = [], addValue = function addValue(e) { value = value.concat(e); }, getValue = function getValue() { return value; }, item = _index.F.lazy(expr).map(addValue); return item.then(tkKey(',').thenRight(item).optrep().array()).opt().map(getValue); } // unit -> Parser ? Token function objectOrNothing() { // FIXME: ES2015 code not great var value = {}, addValue = function addValue(e) { value[e[0]] = e[1]; }, getValue = function getValue() { return value; }, attribute = string.thenLeft(tkKey(':')).then(_index.F.lazy(expr)).array().map(addValue); return attribute.thenLeft(tkKey(',').then(attribute).optrep()).array().opt().map(getValue); } // unit -> Parser ? Token function expr() { return number.or(string).or(tkKey('null').returns(null)).or(tkKey('true').returns(true)).or(tkKey('false').returns(false)).or(tkKey('[').thenRight(_index.F.lazy(arrayOrNothing)).thenLeft(tkKey(']')).single()).or(tkKey('{').thenRight(_index.F.lazy(objectOrNothing)).thenLeft(tkKey('}')).single()); } //const parse = exports.default = { parse: function parse(source) { var parser = genlex.use(expr().thenLeft(_index.F.eos()).single()); return parser.parse(source, 0); } }; //# sourceMappingURL=jsonparser.js.map