made with requirebin
Last active
October 29, 2016 06:10
-
-
Save beaugunderson/cb15cf9e247355df6dd802f06b69e1d7 to your computer and use it in GitHub Desktop.
requirebin sketch
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var parsimmon = require('parsimmon'); | |
function lexeme(parser) { | |
return parser.skip(parsimmon.optWhitespace); | |
} | |
var Keyword = parsimmon.alt( | |
parsimmon.string('appt'), | |
parsimmon.string('provider'), | |
parsimmon.string('hba1c') | |
); | |
var DateString = parsimmon.regex(/\d{4}-\d{2}-\d{2}/).map(function (result) { | |
return new Date(result) | |
}); | |
var Float = parsimmon.seq( | |
parsimmon.regex(/-?([0-9]+\.)?[0-9]+/) | |
).map(function (result) { | |
return parseFloat(result[0], 10); | |
}); | |
var Comparison = parsimmon.seqMap( | |
parsimmon.regex(/<|>/), | |
Float, | |
function (operator, value) { | |
return { | |
operator: operator, | |
value: value | |
}; | |
} | |
); | |
var Value = parsimmon.alt( | |
DateString, | |
Comparison, | |
parsimmon.regex(/[a-z0-9.<>]+/i) | |
); | |
var Separator = parsimmon.alt( | |
parsimmon.regex(/\s+/), | |
lexeme(parsimmon.string(',')), | |
lexeme(parsimmon.eof) | |
); | |
var Pair = parsimmon.seqMap( | |
Keyword.skip(lexeme(parsimmon.string(':'))), | |
Value, | |
Separator.times(1), | |
function (keyword, value) { | |
var result = {}; | |
result[keyword] = value; | |
return result; | |
} | |
); | |
var Pairs = Pair.atLeast(1); | |
// demo | |
var tests = [ | |
'appt:2016-10-31', | |
'appt:2016-10-31', | |
'appt:tomorrow', | |
'appt: 2016-10-31', | |
'hba1c: >5.2', | |
'appt: tomorrow, provider: smagee', | |
'appt: tomorrow provider: smagee', | |
'appt:tomorrow provider:smagee', | |
'appt tomorrow', | |
]; | |
tests.forEach(function (test) { | |
var result = Pairs.parse(test); | |
document.write('<div>' + JSON.stringify(result, null, 2) + '</div>'); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
setTimeout(function(){require=function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s}({parsimmon:[function(require,module,exports){(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 a(r,t){if(!t)return r;if(r.furthest>t.furthest)return r;var n=r.furthest===t.furthest?i(r.expected,t.expected):t.expected;return{status:r.status,index:r.index,value:r.value,furthest:t.furthest,expected:n}}function i(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 a=0;a<n;a++){u[r[a]]=true}for(var a=0;a<e;a++){u[t[a]]=true}var i=[];for(var f in u){if(u.hasOwnProperty(f)){i.push(f)}}i.sort();return i}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 regexp: "+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 a=r.length-e>12?"...'":"'";return" at line "+n.line+" column "+n.column+", got "+u+r.slice(e,e+12)+a}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 i;var f=new Array(n);for(var o=0;o<n;o+=1){i=a(r[o]._(t,u),i);if(!i.status)return i;f[o]=i.value;u=i.index}return a(e(u,f),i)})};var g=r.seqMap=function(){var r=[].slice.call(arguments);if(r.length===0){throw new Error("seqMap needs at least one argument")}var t=r.pop();c(t);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=a(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 i;var f;for(;;){i=a(r._(t,n),i);if(i.status){n=i.index;u.push(i.value)}else{return a(e(n,u),i)}}})};n.times=function(r,n){if(arguments.length<2)n=r;var u=this;o(r);o(n);return t(function(t,i){var f=[];var o=i;var s;var c;for(var v=0;v<r;v+=1){s=u._(t,i);c=a(s,c);if(s.status){i=s.index;f.push(s.value)}else return c}for(;v<n;v+=1){s=u._(t,i);c=a(s,c);if(s.status){i=s.index;f.push(s.value)}else break}return a(e(i,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 i=n._(t,u);if(!i.status)return i;return a(e(i.index,r(i.value)),i)})};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 a="'"+r+"'";v(r);return t(function(t,i){var f=t.slice(i,i+n);if(f===r){return e(i+n,f)}else{return u(i,a)}})};var E=function(r){var t=""+r;return t.slice(t.lastIndexOf("/")+1)};var O=r.regexp=function(r,n){s(r);if(arguments.length>=2){o(n)}else{n=0}var a=RegExp("^(?:"+r.source+")",E(r));var i=""+r;return t(function(r,t){var f=a.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,i)})};r.regex=O;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 q=r.digit=O(/[0-9]/).desc("a digit");var M=r.digits=O(/[0-9]*/);var P=r.whitespace=O(/\s+/).desc("whitespace");var j=r.optWhitespace=O(/\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 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 a=t.charAt(n);if(n<t.length&&r(a)){return e(n+1,a)}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 i=r(u.value);return a(i._(t,u.index),u)})};return r})},{}]},{},[]);var parsimmon=require("parsimmon");function lexeme(parser){return parser.skip(parsimmon.optWhitespace)}var Keyword=parsimmon.alt(parsimmon.string("appt"),parsimmon.string("provider"),parsimmon.string("hba1c"));var DateString=parsimmon.regex(/\d{4}-\d{2}-\d{2}/).map(function(result){return new Date(result)});var Float=parsimmon.seq(parsimmon.regex(/-?([0-9]+\.)?[0-9]+/)).map(function(result){return parseFloat(result[0],10)});var Comparison=parsimmon.seqMap(parsimmon.regex(/<|>/),Float,function(operator,value){return{operator:operator,value:value}});var Value=parsimmon.alt(DateString,Comparison,parsimmon.regex(/[a-z0-9.<>]+/i));var Separator=parsimmon.alt(parsimmon.regex(/\s+/),lexeme(parsimmon.string(",")),lexeme(parsimmon.eof));var Pair=parsimmon.seqMap(Keyword.skip(lexeme(parsimmon.string(":"))),Value,Separator.times(1),function(keyword,value){var result={};result[keyword]=value;return result});var Pairs=Pair.atLeast(1);var tests=["appt:2016-10-31","appt:2016-10-31","appt:tomorrow","appt: 2016-10-31","hba1c: >5.2","appt: tomorrow, provider: smagee","appt: tomorrow provider: smagee","appt:tomorrow provider:smagee","appt tomorrow"];tests.forEach(function(test){var result=Pairs.parse(test);document.write("<div>"+JSON.stringify(result,null,2)+"</div>")})},0); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "requirebin-sketch", | |
"version": "1.0.0", | |
"dependencies": { | |
"parsimmon": "0.9.2" | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- contents of this file will be placed inside the <body> --> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- contents of this file will be placed inside the <head> --> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment