2 lines
14 KiB
JavaScript
2 lines
14 KiB
JavaScript
|
(function(global){"use strict";function parse5322(opts){function inStr(){return pos<len}function curTok(){return parseString[pos]}function getPos(){return pos}function setPos(i){pos=i}function nextTok(){pos+=1}function initialize(){pos=0;len=parseString.length}function o(name,value){return{name:name,tokens:value||"",semantic:value||"",children:[]}}function wrap(name,ast){var n;if(ast===null){return null}n=o(name);n.tokens=ast.tokens;n.semantic=ast.semantic;n.children.push(ast);return n}function add(parent,child){if(child!==null){parent.tokens+=child.tokens;parent.semantic+=child.semantic}parent.children.push(child);return parent}function compareToken(fxnCompare){var tok;if(!inStr()){return null}tok=curTok();if(fxnCompare(tok)){nextTok();return o("token",tok)}return null}function literal(lit){return function literalFunc(){return wrap("literal",compareToken(function(tok){return tok===lit}))}}function and(){var args=arguments;return function andFunc(){var i,s,result,start;start=getPos();s=o("and");for(i=0;i<args.length;i+=1){result=args[i]();if(result===null){setPos(start);return null}add(s,result)}return s}}function or(){var args=arguments;return function orFunc(){var i,result,start;start=getPos();for(i=0;i<args.length;i+=1){result=args[i]();if(result!==null){return result}setPos(start)}return null}}function opt(prod){return function optFunc(){var result,start;start=getPos();result=prod();if(result!==null){return result}else{setPos(start);return o("opt")}}}function invis(prod){return function invisFunc(){var result=prod();if(result!==null){result.semantic=""}return result}}function colwsp(prod){return function collapseSemanticWhitespace(){var result=prod();if(result!==null&&result.semantic.length>0){result.semantic=" "}return result}}function star(prod,minimum){return function starFunc(){var s,result,count,start,min;start=getPos();s=o("star");count=0;min=minimum===undefined?0:minimum;while((result=prod())!==null){count=count+1;add(s,result)}if(count>=min){return s}else{setPos(start);return null}}}function collapseWhitespace(s){return s.replace(/([ \t]|\r\n)+/g," ").replace(/^\s*/,"").replace(/\s*$/,"")}function isUTF8NonAscii(tok){return tok.charCodeAt(0)>=128}function cr(){return wrap("cr",literal("\r")())}function crlf(){return wrap("crlf",and(cr,lf)())}function dquote(){return wrap("dquote",literal('"')())}function htab(){return wrap("htab",literal("\t")())}function lf(){return wrap("lf",literal("\n")())}function sp(){return wrap("sp",literal(" ")())}function vchar(){return wrap("vchar",compareToken(function vcharFunc(tok){var code=tok.charCodeAt(0);var accept=33<=code&&code<=126;if(opts.rfc6532){accept=accept||isUTF8NonAscii(tok)}return accept}))}function wsp(){return wrap("wsp",or(sp,htab)())}function quotedPair(){var qp=wrap("quoted-pair",or(and(literal("\\"),or(vchar,wsp)),obsQP)());if(qp===null){return null}qp.semantic=qp.semantic[1];return qp}function fws(){return wrap("fws",or(obsFws,and(opt(and(star(wsp),invis(crlf))),star(wsp,1)))())}function ctext(){return wrap("ctext",or(function ctextFunc1(){return compareToken(function ctextFunc2(tok){var code=tok.charCodeAt(0);var accept=33<=code&&code<=39||42<=code&&code<=91||93<=code&&code<=126;if(opts.rfc6532){accept=accept||isUTF8NonAscii(tok)}return accept})},obsCtext)())}function ccontent(){return wrap("ccontent",or(ctext,quotedPair,comment)())}function comment(){return wrap("comment",and(literal("("),star(and(opt(fws),ccontent)),opt(fws),literal(")"))())}function cfws(){return wrap("cfws",or(and(star(and(opt(fws),comment),1),opt(fws)),fws)())}function atext(){return wrap("atext",compareToken(function atextFunc(tok){var accept="a"<=tok&&tok<="z"||"A"<=tok&&tok<="Z"||"0"<=tok&&tok<="9"||["!","#","$","%","&","'","*","+","-","/","=","?","^","_","`","{","|","}","~"].indexOf(tok)>=0;if(opts.rfc6532){accept=accept||isUTF8NonAscii(tok)}return accept}))}function atom(){return wrap("atom",and(colwsp(opt(cfws)),star(atext,1),colwsp(opt(cfws)))())}function dotAtomText(){var s,maybeText;s=wrap("dot-atom-text",star(atext,1)());if(s===null){return s}maybeText=star(and(literal("."
|