Comparing version 0.8.1 to 0.9.0
@@ -108,15 +108,39 @@ // This unsightly UMD-module header is here to make this code work without | ||
function assertParser(p) { | ||
if (!(p instanceof Parser)) throw new Error('not a parser: '+p); | ||
if (!(p instanceof Parser)) { | ||
throw new Error('not a parser: ' + p); | ||
} | ||
} | ||
function assertNumber(x) { | ||
if (typeof x !== 'number') throw new Error('not a number: '+x); | ||
if (typeof x !== 'number') { | ||
throw new Error('not a number: ' + x); | ||
} | ||
} | ||
function assertRegexp(x) { | ||
if (!(x instanceof RegExp)) throw new Error('not a regex: '+x); | ||
if (!(x instanceof RegExp)) { | ||
throw new Error('not a regex: '+x); | ||
} | ||
var f = flags(x); | ||
for (var i = 0; i < f.length; i++) { | ||
var c = f.charAt(i); | ||
// Only allow regexp flags [imu] for now, since [g] and [y] specifically | ||
// mess up Parsimmon. If more non-stateful regexp flags are added in the | ||
// future, this will need to be revisited. | ||
if (c != 'i' && c != 'm' && c != 'u') { | ||
throw new Error('unsupported regexp flag "' + c + '": ' + x); | ||
} | ||
} | ||
} | ||
function assertFunction(x) { | ||
if (typeof x !== 'function') throw new Error('not a function: '+x); | ||
if (typeof x !== 'function') { | ||
throw new Error('not a function: ' + x); | ||
} | ||
} | ||
function assertString(x) { | ||
if (typeof x !== 'string') throw new Error('not a string: '+x) | ||
if (typeof x !== 'string') { | ||
throw new Error('not a string: ' + x); | ||
} | ||
} | ||
@@ -408,10 +432,17 @@ | ||
var flags = function(re) { | ||
var s = '' + re; | ||
return s.slice(s.lastIndexOf('/') + 1); | ||
}; | ||
var regex = Parsimmon.regex = function(re, group) { | ||
assertRegexp(re); | ||
if (group) assertNumber(group); | ||
if (arguments.length >= 2) { | ||
assertNumber(group); | ||
} else { | ||
group = 0; | ||
} | ||
var anchored = RegExp('^(?:'+re.source+')', (''+re).slice((''+re).lastIndexOf('/')+1)); | ||
var anchored = RegExp('^(?:' + re.source + ')', flags(re)); | ||
var expected = '' + re; | ||
if (group == null) group = 0; | ||
@@ -424,3 +455,5 @@ return Parser(function(stream, i) { | ||
var groupMatch = match[group]; | ||
if (groupMatch != null) return makeSuccess(i+fullMatch.length, groupMatch); | ||
if (groupMatch != null) { | ||
return makeSuccess(i + fullMatch.length, groupMatch); | ||
} | ||
} | ||
@@ -427,0 +460,0 @@ |
@@ -1,1 +0,1 @@ | ||
(function(r,t){if(typeof define==="function"&&define.amd){define([],t)}else if(typeof module==="object"&&module.exports){module.exports=t()}else{r.Parsimmon=t()}})(this,function(){"use strict";var r={};function t(r){if(!(this instanceof t))return new t(r);this._=r}r.Parser=t;var n=t.prototype;function e(r,t){return{status:true,index:r,value:t,furthest:-1,expected:[]}}function u(r,t){return{status:false,index:-1,value:null,furthest:r,expected:[t]}}function i(r,t){if(!t)return r;if(r.furthest>t.furthest)return r;var n=r.furthest===t.furthest?a(r.expected,t.expected):t.expected;return{status:r.status,index:r.index,value:r.value,furthest:t.furthest,expected:n}}function a(r,t){var n=r.length;var e=t.length;if(n===0){return t}else if(e===0){return r}var u={};for(var i=0;i<n;i++){u[r[i]]=true}for(var i=0;i<e;i++){u[t[i]]=true}var a=[];for(var f in u){if(u.hasOwnProperty(f)){a.push(f)}}a.sort();return a}function f(r){if(!(r instanceof t))throw new Error("not a parser: "+r)}function o(r){if(typeof r!=="number")throw new Error("not a number: "+r)}function s(r){if(!(r instanceof RegExp))throw new Error("not a regex: "+r)}function c(r){if(typeof r!=="function")throw new Error("not a function: "+r)}function v(r){if(typeof r!=="string")throw new Error("not a string: "+r)}function l(r){if(r.length===1)return r[0];return"one of "+r.join(", ")}function h(r,t){var n=t.index;var e=n.offset;if(e===r.length)return", got the end of the stream";var u=e>0?"'...":"'";var i=r.length-e>12?"...'":"'";return" at line "+n.line+" column "+n.column+", got "+u+r.slice(e,e+12)+i}var p=r.formatError=function(r,t){return"expected "+l(t.expected)+h(r,t)};n.parse=function(r){if(typeof r!=="string"){throw new Error(".parse must be called with a string as its argument")}var t=this.skip(R)._(r,0);return t.status?{status:true,value:t.value}:{status:false,index:D(r,t.furthest),expected:t.expected}};var d=r.seq=function(){var r=[].slice.call(arguments);var n=r.length;for(var u=0;u<n;u+=1){f(r[u])}return t(function(t,u){var a;var f=new Array(n);for(var o=0;o<n;o+=1){a=i(r[o]._(t,u),a);if(!a.status)return a;f[o]=a.value;u=a.index}return i(e(u,f),a)})};var g=r.seqMap=function(){var r=[].slice.call(arguments);var t=r.pop();return d.apply(null,r).map(function(r){return t.apply(null,r)})};var x=r.custom=function(r){return t(r(e,u))};var m=r.alt=function(){var r=[].slice.call(arguments);var n=r.length;if(n===0)return b("zero alternates");for(var e=0;e<n;e+=1){f(r[e])}return t(function(t,n){var e;for(var u=0;u<r.length;u+=1){e=i(r[u]._(t,n),e);if(e.status)return e}return e})};var y=r.sepBy=function(t,n){return w(t,n).or(r.of([]))};var w=r.sepBy1=function(r,t){f(r);f(t);var n=t.then(r).many();return r.chain(function(r){return n.map(function(t){return[r].concat(t)})})};n.or=function(r){return m(this,r)};n.then=function(r){if(typeof r==="function"){throw new Error("chaining features of .then are no longer supported, use .chain instead")}f(r);return d(this,r).map(function(r){return r[1]})};n.many=function(){var r=this;return t(function(t,n){var u=[];var a;var f;for(;;){a=i(r._(t,n),a);if(a.status){n=a.index;u.push(a.value)}else{return i(e(n,u),a)}}})};n.times=function(r,n){if(arguments.length<2)n=r;var u=this;o(r);o(n);return t(function(t,a){var f=[];var o=a;var s;var c;for(var v=0;v<r;v+=1){s=u._(t,a);c=i(s,c);if(s.status){a=s.index;f.push(s.value)}else return c}for(;v<n;v+=1){s=u._(t,a);c=i(s,c);if(s.status){a=s.index;f.push(s.value)}else break}return i(e(a,f),c)})};n.result=function(r){return this.map(function(t){return r})};n.atMost=function(r){return this.times(0,r)};n.atLeast=function(r){var t=this;return g(this.times(r),this.many(),function(r,t){return r.concat(t)})};n.map=function(r){c(r);var n=this;return t(function(t,u){var a=n._(t,u);if(!a.status)return a;return i(e(a.index,r(a.value)),a)})};n.skip=function(r){return d(this,r).map(function(r){return r[0]})};n.mark=function(){return g(G,this,G,function(r,t,n){return{start:r,value:t,end:n}})};n.desc=function(r){var n=this;return t(function(t,e){var u=n._(t,e);if(!u.status)u.expected=[r];return u})};var _=r.string=function(r){var n=r.length;var i="'"+r+"'";v(r);return t(function(t,a){var f=t.slice(a,a+n);if(f===r){return e(a+n,f)}else{return u(a,i)}})};var E=r.regex=function(r,n){s(r);if(n)o(n);var i=RegExp("^(?:"+r.source+")",(""+r).slice((""+r).lastIndexOf("/")+1));var a=""+r;if(n==null)n=0;return t(function(r,t){var f=i.exec(r.slice(t));if(f){var o=f[0];var s=f[n];if(s!=null)return e(t+o.length,s)}return u(t,a)})};var O=r.succeed=function(r){return t(function(t,n){return e(n,r)})};var b=r.fail=function(r){return t(function(t,n){return u(n,r)})};var k=r.letter=E(/[a-z]/i).desc("a letter");var z=r.letters=E(/[a-z]*/i);var A=r.digit=E(/[0-9]/).desc("a digit");var P=r.digits=E(/[0-9]*/);var j=r.whitespace=E(/\s+/).desc("whitespace");var q=r.optWhitespace=E(/\s*/);var B=r.any=t(function(r,t){if(t>=r.length)return u(t,"any character");return e(t+1,r.charAt(t))});var M=r.all=t(function(r,t){return e(r.length,r.slice(t))});var R=r.eof=t(function(r,t){if(t<r.length)return u(t,"EOF");return e(t,null)});var W=r.test=function(r){c(r);return t(function(t,n){var i=t.charAt(n);if(n<t.length&&r(i)){return e(n+1,i)}else{return u(n,"a character matching "+r)}})};var F=r.oneOf=function(r){return W(function(t){return r.indexOf(t)>=0})};var I=r.noneOf=function(r){return W(function(t){return r.indexOf(t)<0})};var L=r.takeWhile=function(r){c(r);return t(function(t,n){var u=n;while(u<t.length&&r(t.charAt(u)))u+=1;return e(u,t.slice(n,u))})};var C=r.lazy=function(r,n){if(arguments.length<2){n=r;r=undefined}var e=t(function(r,t){e._=n()._;return e._(r,t)});if(r)e=e.desc(r);return e};var D=function(r,t){var n=r.slice(0,t).split("\n");var e=n.length;var u=n[n.length-1].length+1;return{offset:t,line:e,column:u}};var G=r.index=t(function(r,t){return e(t,D(r,t))});n.concat=n.or;n.empty=b("empty");n.of=t.of=r.of=O;n.ap=function(r){return g(this,r,function(r,t){return r(t)})};n.chain=function(r){var n=this;return t(function(t,e){var u=n._(t,e);if(!u.status)return u;var a=r(u.value);return i(a._(t,u.index),u)})};return r}); | ||
(function(r,t){if(typeof define==="function"&&define.amd){define([],t)}else if(typeof module==="object"&&module.exports){module.exports=t()}else{r.Parsimmon=t()}})(this,function(){"use strict";var r={};function t(r){if(!(this instanceof t))return new t(r);this._=r}r.Parser=t;var n=t.prototype;function e(r,t){return{status:true,index:r,value:t,furthest:-1,expected:[]}}function u(r,t){return{status:false,index:-1,value:null,furthest:r,expected:[t]}}function i(r,t){if(!t)return r;if(r.furthest>t.furthest)return r;var n=r.furthest===t.furthest?a(r.expected,t.expected):t.expected;return{status:r.status,index:r.index,value:r.value,furthest:t.furthest,expected:n}}function a(r,t){var n=r.length;var e=t.length;if(n===0){return t}else if(e===0){return r}var u={};for(var i=0;i<n;i++){u[r[i]]=true}for(var i=0;i<e;i++){u[t[i]]=true}var a=[];for(var f in u){if(u.hasOwnProperty(f)){a.push(f)}}a.sort();return a}function f(r){if(!(r instanceof t)){throw new Error("not a parser: "+r)}}function o(r){if(typeof r!=="number"){throw new Error("not a number: "+r)}}function s(r){if(!(r instanceof RegExp)){throw new Error("not a regex: "+r)}var t=E(r);for(var n=0;n<t.length;n++){var e=t.charAt(n);if(e!="i"&&e!="m"&&e!="u"){throw new Error('unsupported regexp flag "'+e+'": '+r)}}}function c(r){if(typeof r!=="function"){throw new Error("not a function: "+r)}}function v(r){if(typeof r!=="string"){throw new Error("not a string: "+r)}}function l(r){if(r.length===1)return r[0];return"one of "+r.join(", ")}function h(r,t){var n=t.index;var e=n.offset;if(e===r.length)return", got the end of the stream";var u=e>0?"'...":"'";var i=r.length-e>12?"...'":"'";return" at line "+n.line+" column "+n.column+", got "+u+r.slice(e,e+12)+i}var p=r.formatError=function(r,t){return"expected "+l(t.expected)+h(r,t)};n.parse=function(r){if(typeof r!=="string"){throw new Error(".parse must be called with a string as its argument")}var t=this.skip(W)._(r,0);return t.status?{status:true,value:t.value}:{status:false,index:G(r,t.furthest),expected:t.expected}};var d=r.seq=function(){var r=[].slice.call(arguments);var n=r.length;for(var u=0;u<n;u+=1){f(r[u])}return t(function(t,u){var a;var f=new Array(n);for(var o=0;o<n;o+=1){a=i(r[o]._(t,u),a);if(!a.status)return a;f[o]=a.value;u=a.index}return i(e(u,f),a)})};var g=r.seqMap=function(){var r=[].slice.call(arguments);var t=r.pop();return d.apply(null,r).map(function(r){return t.apply(null,r)})};var x=r.custom=function(r){return t(r(e,u))};var m=r.alt=function(){var r=[].slice.call(arguments);var n=r.length;if(n===0)return k("zero alternates");for(var e=0;e<n;e+=1){f(r[e])}return t(function(t,n){var e;for(var u=0;u<r.length;u+=1){e=i(r[u]._(t,n),e);if(e.status)return e}return e})};var w=r.sepBy=function(t,n){return y(t,n).or(r.of([]))};var y=r.sepBy1=function(r,t){f(r);f(t);var n=t.then(r).many();return r.chain(function(r){return n.map(function(t){return[r].concat(t)})})};n.or=function(r){return m(this,r)};n.then=function(r){if(typeof r==="function"){throw new Error("chaining features of .then are no longer supported, use .chain instead")}f(r);return d(this,r).map(function(r){return r[1]})};n.many=function(){var r=this;return t(function(t,n){var u=[];var a;var f;for(;;){a=i(r._(t,n),a);if(a.status){n=a.index;u.push(a.value)}else{return i(e(n,u),a)}}})};n.times=function(r,n){if(arguments.length<2)n=r;var u=this;o(r);o(n);return t(function(t,a){var f=[];var o=a;var s;var c;for(var v=0;v<r;v+=1){s=u._(t,a);c=i(s,c);if(s.status){a=s.index;f.push(s.value)}else return c}for(;v<n;v+=1){s=u._(t,a);c=i(s,c);if(s.status){a=s.index;f.push(s.value)}else break}return i(e(a,f),c)})};n.result=function(r){return this.map(function(t){return r})};n.atMost=function(r){return this.times(0,r)};n.atLeast=function(r){var t=this;return g(this.times(r),this.many(),function(r,t){return r.concat(t)})};n.map=function(r){c(r);var n=this;return t(function(t,u){var a=n._(t,u);if(!a.status)return a;return i(e(a.index,r(a.value)),a)})};n.skip=function(r){return d(this,r).map(function(r){return r[0]})};n.mark=function(){return g(H,this,H,function(r,t,n){return{start:r,value:t,end:n}})};n.desc=function(r){var n=this;return t(function(t,e){var u=n._(t,e);if(!u.status)u.expected=[r];return u})};var _=r.string=function(r){var n=r.length;var i="'"+r+"'";v(r);return t(function(t,a){var f=t.slice(a,a+n);if(f===r){return e(a+n,f)}else{return u(a,i)}})};var E=function(r){var t=""+r;return t.slice(t.lastIndexOf("/")+1)};var O=r.regex=function(r,n){s(r);if(arguments.length>=2){o(n)}else{n=0}var i=RegExp("^(?:"+r.source+")",E(r));var a=""+r;return t(function(r,t){var f=i.exec(r.slice(t));if(f){var o=f[0];var s=f[n];if(s!=null){return e(t+o.length,s)}}return u(t,a)})};var b=r.succeed=function(r){return t(function(t,n){return e(n,r)})};var k=r.fail=function(r){return t(function(t,n){return u(n,r)})};var A=r.letter=O(/[a-z]/i).desc("a letter");var z=r.letters=O(/[a-z]*/i);var P=r.digit=O(/[0-9]/).desc("a digit");var j=r.digits=O(/[0-9]*/);var q=r.whitespace=O(/\s+/).desc("whitespace");var B=r.optWhitespace=O(/\s*/);var M=r.any=t(function(r,t){if(t>=r.length)return u(t,"any character");return e(t+1,r.charAt(t))});var R=r.all=t(function(r,t){return e(r.length,r.slice(t))});var W=r.eof=t(function(r,t){if(t<r.length)return u(t,"EOF");return e(t,null)});var F=r.test=function(r){c(r);return t(function(t,n){var i=t.charAt(n);if(n<t.length&&r(i)){return e(n+1,i)}else{return u(n,"a character matching "+r)}})};var I=r.oneOf=function(r){return F(function(t){return r.indexOf(t)>=0})};var L=r.noneOf=function(r){return F(function(t){return r.indexOf(t)<0})};var C=r.takeWhile=function(r){c(r);return t(function(t,n){var u=n;while(u<t.length&&r(t.charAt(u)))u+=1;return e(u,t.slice(n,u))})};var D=r.lazy=function(r,n){if(arguments.length<2){n=r;r=undefined}var e=t(function(r,t){e._=n()._;return e._(r,t)});if(r)e=e.desc(r);return e};var G=function(r,t){var n=r.slice(0,t).split("\n");var e=n.length;var u=n[n.length-1].length+1;return{offset:t,line:e,column:u}};var H=r.index=t(function(r,t){return e(t,G(r,t))});n.concat=n.or;n.empty=k("empty");n.of=t.of=r.of=b;n.ap=function(r){return g(this,r,function(r,t){return r(t)})};n.chain=function(r){var n=this;return t(function(t,e){var u=n._(t,e);if(!u.status)return u;var a=r(u.value);return i(a._(t,u.index),u)})};return r}); |
@@ -0,1 +1,5 @@ | ||
## version 0.9.0 (2016-07-07) | ||
* `P.regex` throws on regexps with flags other than `imu` now. | ||
## version 0.8.1 (2016-06-30) | ||
@@ -2,0 +6,0 @@ |
{ | ||
"name": "parsimmon", | ||
"version": "0.8.1", | ||
"version": "0.9.0", | ||
"description": "A monadic LL(infinity) parser combinator library", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -13,3 +13,3 @@ [![Build Status](https://secure.travis-ci.org/jneen/parsimmon.png)](http://travis-ci.org/jneen/parsimmon) | ||
Parsimmon supports IE7 and newer browsers, along with Node.js. It can be used as a standard Node module through npm, or through `build/parsimmon.browser.js` directly in the browser through a script tag, where it exports a global variable called `Parsimmon`. | ||
Parsimmon supports IE7 and newer browsers, along with Node.js. It can be used as a standard Node module through npm, or through `build/parsimmon.browser.min.js` directly in the browser through a script tag, where it exports a global variable called `Parsimmon` (you can also use the `build/parsimmon.browser.js` for local development). | ||
@@ -79,4 +79,4 @@ ## Quick Example | ||
- `Parsimmon.regex(/myregex/, group=0)` is a parser that expects the stream | ||
to match the given regex, and yields the given match group, or the | ||
entire match. | ||
to match the given regexp, and yields the given match group, or the | ||
entire match. The regexp will always match starting at the current parse location. The regexp may only use the following flags: `imu`. `Parsimmon.regex` will throw an error for any other flag. | ||
- `Parsimmon.succeed(result)` is a parser that doesn't consume any of | ||
@@ -92,2 +92,4 @@ the string, and yields `result`. | ||
and yields the value of the first one that succeeds, backtracking in between. | ||
This means that the order of parsers matters. If two parsers match the | ||
same prefix, the **longer** of the two must come first (e.g. `Parsimmon.alt(Parsimmon.string('ab'), Parsimmin.string('a'))`). | ||
- `Parsimmon.sepBy(content, separator)` accepts two parsers, and expects multiple `content`s, separated by `separator`s. Yields an array of `contents`. | ||
@@ -94,0 +96,0 @@ - `Parsimmon.sepBy1(content, separator)` same as `Parsimmon.sepBy`, but expects |
@@ -108,15 +108,39 @@ // This unsightly UMD-module header is here to make this code work without | ||
function assertParser(p) { | ||
if (!(p instanceof Parser)) throw new Error('not a parser: '+p); | ||
if (!(p instanceof Parser)) { | ||
throw new Error('not a parser: ' + p); | ||
} | ||
} | ||
function assertNumber(x) { | ||
if (typeof x !== 'number') throw new Error('not a number: '+x); | ||
if (typeof x !== 'number') { | ||
throw new Error('not a number: ' + x); | ||
} | ||
} | ||
function assertRegexp(x) { | ||
if (!(x instanceof RegExp)) throw new Error('not a regex: '+x); | ||
if (!(x instanceof RegExp)) { | ||
throw new Error('not a regex: '+x); | ||
} | ||
var f = flags(x); | ||
for (var i = 0; i < f.length; i++) { | ||
var c = f.charAt(i); | ||
// Only allow regexp flags [imu] for now, since [g] and [y] specifically | ||
// mess up Parsimmon. If more non-stateful regexp flags are added in the | ||
// future, this will need to be revisited. | ||
if (c != 'i' && c != 'm' && c != 'u') { | ||
throw new Error('unsupported regexp flag "' + c + '": ' + x); | ||
} | ||
} | ||
} | ||
function assertFunction(x) { | ||
if (typeof x !== 'function') throw new Error('not a function: '+x); | ||
if (typeof x !== 'function') { | ||
throw new Error('not a function: ' + x); | ||
} | ||
} | ||
function assertString(x) { | ||
if (typeof x !== 'string') throw new Error('not a string: '+x) | ||
if (typeof x !== 'string') { | ||
throw new Error('not a string: ' + x); | ||
} | ||
} | ||
@@ -408,10 +432,17 @@ | ||
var flags = function(re) { | ||
var s = '' + re; | ||
return s.slice(s.lastIndexOf('/') + 1); | ||
}; | ||
var regex = Parsimmon.regex = function(re, group) { | ||
assertRegexp(re); | ||
if (group) assertNumber(group); | ||
if (arguments.length >= 2) { | ||
assertNumber(group); | ||
} else { | ||
group = 0; | ||
} | ||
var anchored = RegExp('^(?:'+re.source+')', (''+re).slice((''+re).lastIndexOf('/')+1)); | ||
var anchored = RegExp('^(?:' + re.source + ')', flags(re)); | ||
var expected = '' + re; | ||
if (group == null) group = 0; | ||
@@ -424,3 +455,5 @@ return Parser(function(stream, i) { | ||
var groupMatch = match[group]; | ||
if (groupMatch != null) return makeSuccess(i+fullMatch.length, groupMatch); | ||
if (groupMatch != null) { | ||
return makeSuccess(i + fullMatch.length, groupMatch); | ||
} | ||
} | ||
@@ -427,0 +460,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
52573
1004
238