ql.io-uri-template
Advanced tools
Comparing version 0.5.0 to 0.5.1
module.exports = (function(){ | ||
/* Generated by PEG.js 0.6.2 (http://pegjs.majda.cz/). */ | ||
/* | ||
* Generated by PEG.js 0.7.0. | ||
* | ||
* http://pegjs.majda.cz/ | ||
*/ | ||
function quote(s) { | ||
/* | ||
* ECMA-262, 5th ed., 7.8.4: All characters may appear literally in a | ||
* string literal except for the closing quote character, backslash, | ||
* carriage return, line separator, paragraph separator, and line feed. | ||
* Any character may appear in the form of an escape sequence. | ||
* | ||
* For portability, we also escape escape all control and non-ASCII | ||
* characters. Note that "\0" and "\v" escape sequences are not used | ||
* because JSHint does not like the first and IE the second. | ||
*/ | ||
return '"' + s | ||
.replace(/\\/g, '\\\\') // backslash | ||
.replace(/"/g, '\\"') // closing quote character | ||
.replace(/\x08/g, '\\b') // backspace | ||
.replace(/\t/g, '\\t') // horizontal tab | ||
.replace(/\n/g, '\\n') // line feed | ||
.replace(/\f/g, '\\f') // form feed | ||
.replace(/\r/g, '\\r') // carriage return | ||
.replace(/[\x00-\x07\x0B\x0E-\x1F\x80-\uFFFF]/g, escape) | ||
+ '"'; | ||
} | ||
var result = { | ||
@@ -14,11 +41,11 @@ /* | ||
"URITemplate": parse_URITemplate, | ||
"literal": parse_literal, | ||
"expression": parse_expression, | ||
"operator": parse_operator, | ||
"required": parse_required, | ||
"blockMerge": parse_blockMerge, | ||
"digits": parse_digits, | ||
"dontencode": parse_dontencode, | ||
"expression": parse_expression, | ||
"literal": parse_literal, | ||
"multivalued": parse_multivalued, | ||
"operator": parse_operator, | ||
"required": parse_required, | ||
"variable": parse_variable | ||
"variable": parse_variable, | ||
"digits": parse_digits | ||
}; | ||
@@ -35,5 +62,5 @@ | ||
var pos = 0; | ||
var reportMatchFailures = true; | ||
var rightmostMatchFailuresPos = 0; | ||
var rightmostMatchFailuresExpected = []; | ||
var reportFailures = 0; | ||
var rightmostFailuresPos = 0; | ||
var rightmostFailuresExpected = []; | ||
var cache = {}; | ||
@@ -54,9 +81,11 @@ | ||
var charCode = ch.charCodeAt(0); | ||
var escapeChar; | ||
var length; | ||
if (charCode <= 0xFF) { | ||
var escapeChar = 'x'; | ||
var length = 2; | ||
escapeChar = 'x'; | ||
length = 2; | ||
} else { | ||
var escapeChar = 'u'; | ||
var length = 4; | ||
escapeChar = 'u'; | ||
length = 4; | ||
} | ||
@@ -67,33 +96,17 @@ | ||
function quote(s) { | ||
/* | ||
* ECMA-262, 5th ed., 7.8.4: All characters may appear literally in a | ||
* string literal except for the closing quote character, backslash, | ||
* carriage return, line separator, paragraph separator, and line feed. | ||
* Any character may appear in the form of an escape sequence. | ||
*/ | ||
return '"' + s | ||
.replace(/\\/g, '\\\\') // backslash | ||
.replace(/"/g, '\\"') // closing quote character | ||
.replace(/\r/g, '\\r') // carriage return | ||
.replace(/\n/g, '\\n') // line feed | ||
.replace(/[\x80-\uFFFF]/g, escape) // non-ASCII characters | ||
+ '"'; | ||
} | ||
function matchFailed(failure) { | ||
if (pos < rightmostMatchFailuresPos) { | ||
if (pos < rightmostFailuresPos) { | ||
return; | ||
} | ||
if (pos > rightmostMatchFailuresPos) { | ||
rightmostMatchFailuresPos = pos; | ||
rightmostMatchFailuresExpected = []; | ||
if (pos > rightmostFailuresPos) { | ||
rightmostFailuresPos = pos; | ||
rightmostFailuresExpected = []; | ||
} | ||
rightmostMatchFailuresExpected.push(failure); | ||
rightmostFailuresExpected.push(failure); | ||
} | ||
function parse_URITemplate() { | ||
var cacheKey = 'URITemplate@' + pos; | ||
var cacheKey = "URITemplate@" + pos; | ||
var cachedResult = cache[cacheKey]; | ||
@@ -105,197 +118,188 @@ if (cachedResult) { | ||
var result0, result1; | ||
var pos0; | ||
var savedPos0 = pos; | ||
var result1 = []; | ||
var result5 = parse_literal(); | ||
if (result5 !== null) { | ||
var result3 = result5; | ||
} else { | ||
var result4 = parse_expression(); | ||
if (result4 !== null) { | ||
var result3 = result4; | ||
} else { | ||
var result3 = null;; | ||
}; | ||
pos0 = pos; | ||
result0 = []; | ||
result1 = parse_literal(); | ||
if (result1 === null) { | ||
result1 = parse_expression(); | ||
} | ||
while (result3 !== null) { | ||
result1.push(result3); | ||
var result5 = parse_literal(); | ||
if (result5 !== null) { | ||
var result3 = result5; | ||
} else { | ||
var result4 = parse_expression(); | ||
if (result4 !== null) { | ||
var result3 = result4; | ||
} else { | ||
var result3 = null;; | ||
}; | ||
while (result1 !== null) { | ||
result0.push(result1); | ||
result1 = parse_literal(); | ||
if (result1 === null) { | ||
result1 = parse_expression(); | ||
} | ||
} | ||
var result2 = result1 !== null | ||
? (function(c) { | ||
var o = [], i, current = 0; | ||
o.push(c[0]); | ||
for(i = 1; i < c.length; i++) { | ||
if(c[i].constructor === String && o[current].constructor === String) { | ||
o[current] = o[current] + c[i]; | ||
} | ||
else { | ||
o.push(c[i]); | ||
current++; | ||
} | ||
} | ||
function select(path, obj) { | ||
var splits = !path ? [] : path.split('.'); | ||
var curr = obj; | ||
for(var i = 0; i < splits.length; i++) { | ||
if(curr[splits[i]]) { | ||
curr = curr[splits[i]]; | ||
if(i < splits.length - 1 && curr.constructor === Array && curr.length > 0) { | ||
curr = curr[0]; | ||
} | ||
} | ||
else { | ||
return null; | ||
} | ||
} | ||
return curr; | ||
} | ||
function _append(str, val, encode) { | ||
var j; | ||
if(str.constructor === Array) { | ||
for(j = 0; j < str.length; j++) { | ||
str[j] = str[j] + (encode ? encodeURIComponent(val) : val); | ||
} | ||
} | ||
else { | ||
str = str + (encode ? encodeURIComponent(val) : val); | ||
} | ||
return str; | ||
} | ||
function _format(str, values, defaults, stream) { | ||
values = values || {}; | ||
defaults = defaults || {}; | ||
var i, j, val, split = false, arr, subset, key; | ||
stream = stream || o; | ||
var ele; | ||
for(i = 0; i < stream.length; i++) { | ||
ele = stream[i]; | ||
if(ele.constructor === String) { | ||
str = _append(str, ele, false); | ||
} | ||
else { | ||
if(ele.variable.constructor == Array) { | ||
// Case of nested token - only single valued for now | ||
key = _format('', values, defaults, ele.variable); | ||
val = select(key, values) || select(key, defaults); | ||
str = str + val; | ||
} | ||
else { | ||
val = select(ele.variable, values) || select(ele.variable, defaults); | ||
if(val) { | ||
var encode = !ele.dontencode; | ||
if(val.constructor == Array) { | ||
// But is the token multivalued? | ||
if(val.length === 0 && ele.required) { | ||
throw { | ||
error: 'Token ' + ele.variable + ' not specified. Processed ' + str | ||
} | ||
} | ||
else if(val.length === 1) { | ||
if(!val[0] && ele.required) { | ||
throw { | ||
error: 'Token ' + ele.variable + ' not specified. Processed ' + str | ||
} | ||
} | ||
str = _append(str, val, encode); | ||
} | ||
else if(ele.multivalued) { | ||
if(ele.max) { | ||
if(val.length <= ele.max) { | ||
// Append as usual | ||
str = _append(str, val, encode); | ||
} | ||
else { | ||
// Split the values into multiple and append each | ||
if(split) { | ||
throw { | ||
error: 'Template can not have multiple single-valued params with multiple values' | ||
} | ||
} | ||
else { | ||
split = true; | ||
// Split and continue. | ||
arr = []; | ||
subset = []; | ||
var start = 0, end = ele.max; | ||
for(j = 0; j < val.length/ele.max; j++) { | ||
subset = val.slice(start, end); | ||
arr.push(_append(str, subset, encode)); | ||
start += ele.max; | ||
end += ele.max; | ||
} | ||
str = arr; | ||
} | ||
} | ||
} | ||
else { | ||
str = _append(str, val, encode); | ||
} | ||
} | ||
else { | ||
// Split if not already split. If already split, error | ||
if(split) { | ||
throw { | ||
error: 'Template can not have multiple single-valued params with multiple values' | ||
} | ||
} | ||
else { | ||
split = true; | ||
// Split and continue. | ||
arr = []; | ||
for(j = 0; j < val.length; j++) { | ||
arr.push(_append(str, val[j], encode)); | ||
} | ||
str = arr; | ||
} | ||
} | ||
} | ||
else { | ||
str = _append(str, val, encode); | ||
} | ||
} | ||
else if(ele.required) { | ||
throw { | ||
error: 'Token ' + ele.variable + ' not specified. Processed ' + str | ||
} | ||
} | ||
} | ||
} | ||
} | ||
return str; | ||
} | ||
return { | ||
format: function(values, defaults) { | ||
return _format('', values, defaults); | ||
}, | ||
merge: function() { | ||
for(i = 0; i < o.length; i++) { | ||
if(o[i].merge) { | ||
return o[i].merge; | ||
} | ||
} | ||
return 'field'; | ||
}, | ||
stream: o | ||
} | ||
})(result1) | ||
: null; | ||
if (result2 !== null) { | ||
var result0 = result2; | ||
} else { | ||
var result0 = null; | ||
pos = savedPos0; | ||
} | ||
if (result0 !== null) { | ||
result0 = (function(offset, c) { | ||
var o = [], i, current = 0; | ||
o.push(c[0]); | ||
for(i = 1; i < c.length; i++) { | ||
if(c[i].constructor === String && o[current].constructor === String) { | ||
o[current] = o[current] + c[i]; | ||
} | ||
else { | ||
o.push(c[i]); | ||
current++; | ||
} | ||
} | ||
function select(path, obj) { | ||
var splits = !path ? [] : path.split('.'); | ||
var curr = obj; | ||
for(var i = 0; i < splits.length; i++) { | ||
if(curr[splits[i]]) { | ||
curr = curr[splits[i]]; | ||
if(i < splits.length - 1 && curr.constructor === Array && curr.length > 0) { | ||
curr = curr[0]; | ||
} | ||
} | ||
else { | ||
return null; | ||
} | ||
} | ||
return curr; | ||
} | ||
function _append(str, val, encode) { | ||
var j; | ||
if(str.constructor === Array) { | ||
for(j = 0; j < str.length; j++) { | ||
str[j] = str[j] + (encode ? encodeURIComponent(val) : val); | ||
} | ||
} | ||
else { | ||
str = str + (encode ? encodeURIComponent(val) : val); | ||
} | ||
return str; | ||
} | ||
function _format(str, values, defaults, stream) { | ||
values = values || {}; | ||
defaults = defaults || {}; | ||
var i, j, val, split = false, arr, subset, key; | ||
stream = stream || o; | ||
var ele; | ||
for(i = 0; i < stream.length; i++) { | ||
ele = stream[i]; | ||
if(ele.constructor === String) { | ||
str = _append(str, ele, false); | ||
} | ||
else { | ||
if(ele.variable.constructor == Array) { | ||
// Case of nested token - only single valued for now | ||
key = _format('', values, defaults, ele.variable); | ||
val = select(key, values) || select(key, defaults); | ||
str = str + val; | ||
} | ||
else { | ||
val = select(ele.variable, values) || select(ele.variable, defaults); | ||
if(val) { | ||
var encode = !ele.dontencode; | ||
if(val.constructor == Array) { | ||
// But is the token multivalued? | ||
if(val.length === 0 && ele.required) { | ||
throw { | ||
error: 'Token ' + ele.variable + ' not specified. Processed ' + str | ||
} | ||
} | ||
else if(val.length === 1) { | ||
if(!val[0] && ele.required) { | ||
throw { | ||
error: 'Token ' + ele.variable + ' not specified. Processed ' + str | ||
} | ||
} | ||
str = _append(str, val, encode); | ||
} | ||
else if(ele.multivalued) { | ||
if(ele.max) { | ||
if(val.length <= ele.max) { | ||
// Append as usual | ||
str = _append(str, val, encode); | ||
} | ||
else { | ||
// Split the values into multiple and append each | ||
if(split) { | ||
throw { | ||
error: 'Template can not have multiple single-valued params with multiple values' | ||
} | ||
} | ||
else { | ||
split = true; | ||
// Split and continue. | ||
arr = []; | ||
subset = []; | ||
var start = 0, end = ele.max; | ||
// Remove duplicates | ||
val = val.sort(); | ||
val = val.filter(function(v,i,o) { | ||
return (i === 0) || | ||
v !== o[i-1]; | ||
}); | ||
for(j = 0; j < val.length/ele.max; j++) { | ||
subset = val.slice(start, end); | ||
arr.push(_append(str, subset, encode)); | ||
start += ele.max; | ||
end += ele.max; | ||
} | ||
str = arr; | ||
} | ||
} | ||
} | ||
else { | ||
str = _append(str, val, encode); | ||
} | ||
} | ||
else { | ||
// Split if not already split. If already split, error | ||
if(split) { | ||
throw { | ||
error: 'Template can not have multiple single-valued params with multiple values' | ||
} | ||
} | ||
else { | ||
split = true; | ||
// Split and continue. | ||
arr = []; | ||
for(j = 0; j < val.length; j++) { | ||
arr.push(_append(str, val[j], encode)); | ||
} | ||
str = arr; | ||
} | ||
} | ||
} | ||
else { | ||
str = _append(str, val, encode); | ||
} | ||
} | ||
else if(ele.required) { | ||
throw { | ||
error: 'Token ' + ele.variable + ' not specified. Processed ' + str | ||
} | ||
} | ||
} | ||
} | ||
} | ||
return str; | ||
} | ||
return { | ||
format: function(values, defaults) { | ||
return _format('', values, defaults); | ||
}, | ||
merge: function() { | ||
for(i = 0; i < o.length; i++) { | ||
if(o[i].merge) { | ||
return o[i].merge; | ||
} | ||
} | ||
return 'field'; | ||
}, | ||
stream: o | ||
} | ||
})(pos0, result0); | ||
} | ||
if (result0 === null) { | ||
pos = pos0; | ||
} | ||
@@ -310,3 +314,3 @@ cache[cacheKey] = { | ||
function parse_literal() { | ||
var cacheKey = 'literal@' + pos; | ||
var cacheKey = "literal@" + pos; | ||
var cachedResult = cache[cacheKey]; | ||
@@ -318,25 +322,17 @@ if (cachedResult) { | ||
var result0; | ||
if (input.substr(pos).match(/^[^^ "'<>`{|}]/) !== null) { | ||
var result2 = input.charAt(pos); | ||
if (/^[^^ "'<>`{|}]/.test(input.charAt(pos))) { | ||
result0 = input.charAt(pos); | ||
pos++; | ||
} else { | ||
var result2 = null; | ||
if (reportMatchFailures) { | ||
result0 = null; | ||
if (reportFailures === 0) { | ||
matchFailed("[^^ \"'<>`{|}]"); | ||
} | ||
} | ||
if (result2 !== null) { | ||
var result0 = result2; | ||
} else { | ||
var result1 = parse_expression(); | ||
if (result1 !== null) { | ||
var result0 = result1; | ||
} else { | ||
var result0 = null;; | ||
}; | ||
if (result0 === null) { | ||
result0 = parse_expression(); | ||
} | ||
cache[cacheKey] = { | ||
@@ -350,3 +346,3 @@ nextPos: pos, | ||
function parse_expression() { | ||
var cacheKey = 'expression@' + pos; | ||
var cacheKey = "expression@" + pos; | ||
var cachedResult = cache[cacheKey]; | ||
@@ -358,75 +354,72 @@ if (cachedResult) { | ||
var result0, result1, result2, result3; | ||
var pos0, pos1; | ||
var savedPos0 = pos; | ||
var savedPos1 = pos; | ||
if (input.substr(pos, 1) === "{") { | ||
var result3 = "{"; | ||
pos += 1; | ||
pos0 = pos; | ||
pos1 = pos; | ||
if (input.charCodeAt(pos) === 123) { | ||
result0 = "{"; | ||
pos++; | ||
} else { | ||
var result3 = null; | ||
if (reportMatchFailures) { | ||
result0 = null; | ||
if (reportFailures === 0) { | ||
matchFailed("\"{\""); | ||
} | ||
} | ||
if (result3 !== null) { | ||
var result4 = []; | ||
var result7 = parse_operator(); | ||
while (result7 !== null) { | ||
result4.push(result7); | ||
var result7 = parse_operator(); | ||
if (result0 !== null) { | ||
result1 = []; | ||
result2 = parse_operator(); | ||
while (result2 !== null) { | ||
result1.push(result2); | ||
result2 = parse_operator(); | ||
} | ||
if (result4 !== null) { | ||
var result5 = parse_variable(); | ||
if (result5 !== null) { | ||
if (input.substr(pos, 1) === "}") { | ||
var result6 = "}"; | ||
pos += 1; | ||
if (result1 !== null) { | ||
result2 = parse_variable(); | ||
if (result2 !== null) { | ||
if (input.charCodeAt(pos) === 125) { | ||
result3 = "}"; | ||
pos++; | ||
} else { | ||
var result6 = null; | ||
if (reportMatchFailures) { | ||
result3 = null; | ||
if (reportFailures === 0) { | ||
matchFailed("\"}\""); | ||
} | ||
} | ||
if (result6 !== null) { | ||
var result1 = [result3, result4, result5, result6]; | ||
if (result3 !== null) { | ||
result0 = [result0, result1, result2, result3]; | ||
} else { | ||
var result1 = null; | ||
pos = savedPos1; | ||
result0 = null; | ||
pos = pos1; | ||
} | ||
} else { | ||
var result1 = null; | ||
pos = savedPos1; | ||
result0 = null; | ||
pos = pos1; | ||
} | ||
} else { | ||
var result1 = null; | ||
pos = savedPos1; | ||
result0 = null; | ||
pos = pos1; | ||
} | ||
} else { | ||
var result1 = null; | ||
pos = savedPos1; | ||
result0 = null; | ||
pos = pos1; | ||
} | ||
var result2 = result1 !== null | ||
? (function(op, v) { | ||
var token = { | ||
variable: v | ||
}; | ||
for(var i = 0; i < op.length; i++) { | ||
for(p in op[i]) { | ||
if(op[i].hasOwnProperty(p)) { | ||
token[p] = op[i][p]; | ||
} | ||
} | ||
} | ||
return token; | ||
})(result1[1], result1[2]) | ||
: null; | ||
if (result2 !== null) { | ||
var result0 = result2; | ||
} else { | ||
var result0 = null; | ||
pos = savedPos0; | ||
if (result0 !== null) { | ||
result0 = (function(offset, op, v) { | ||
var token = { | ||
variable: v | ||
}; | ||
for(var i = 0; i < op.length; i++) { | ||
for(p in op[i]) { | ||
if(op[i].hasOwnProperty(p)) { | ||
token[p] = op[i][p]; | ||
} | ||
} | ||
} | ||
return token; | ||
})(pos0, result0[1], result0[2]); | ||
} | ||
if (result0 === null) { | ||
pos = pos0; | ||
} | ||
cache[cacheKey] = { | ||
@@ -440,3 +433,3 @@ nextPos: pos, | ||
function parse_operator() { | ||
var cacheKey = 'operator@' + pos; | ||
var cacheKey = "operator@" + pos; | ||
var cachedResult = cache[cacheKey]; | ||
@@ -448,27 +441,15 @@ if (cachedResult) { | ||
var result0; | ||
var result4 = parse_required(); | ||
if (result4 !== null) { | ||
var result0 = result4; | ||
} else { | ||
var result3 = parse_blockMerge(); | ||
if (result3 !== null) { | ||
var result0 = result3; | ||
} else { | ||
var result2 = parse_multivalued(); | ||
if (result2 !== null) { | ||
var result0 = result2; | ||
} else { | ||
var result1 = parse_dontencode(); | ||
if (result1 !== null) { | ||
var result0 = result1; | ||
} else { | ||
var result0 = null;; | ||
}; | ||
}; | ||
}; | ||
result0 = parse_required(); | ||
if (result0 === null) { | ||
result0 = parse_blockMerge(); | ||
if (result0 === null) { | ||
result0 = parse_multivalued(); | ||
if (result0 === null) { | ||
result0 = parse_dontencode(); | ||
} | ||
} | ||
} | ||
cache[cacheKey] = { | ||
@@ -482,3 +463,3 @@ nextPos: pos, | ||
function parse_required() { | ||
var cacheKey = 'required@' + pos; | ||
var cacheKey = "required@" + pos; | ||
var cachedResult = cache[cacheKey]; | ||
@@ -490,29 +471,26 @@ if (cachedResult) { | ||
var result0; | ||
var pos0; | ||
var savedPos0 = pos; | ||
if (input.substr(pos, 1) === "^") { | ||
var result1 = "^"; | ||
pos += 1; | ||
pos0 = pos; | ||
if (input.charCodeAt(pos) === 94) { | ||
result0 = "^"; | ||
pos++; | ||
} else { | ||
var result1 = null; | ||
if (reportMatchFailures) { | ||
result0 = null; | ||
if (reportFailures === 0) { | ||
matchFailed("\"^\""); | ||
} | ||
} | ||
var result2 = result1 !== null | ||
? (function() { | ||
return { | ||
required: true | ||
} | ||
})() | ||
: null; | ||
if (result2 !== null) { | ||
var result0 = result2; | ||
} else { | ||
var result0 = null; | ||
pos = savedPos0; | ||
if (result0 !== null) { | ||
result0 = (function(offset) { | ||
return { | ||
required: true | ||
} | ||
})(pos0); | ||
} | ||
if (result0 === null) { | ||
pos = pos0; | ||
} | ||
cache[cacheKey] = { | ||
@@ -526,3 +504,3 @@ nextPos: pos, | ||
function parse_blockMerge() { | ||
var cacheKey = 'blockMerge@' + pos; | ||
var cacheKey = "blockMerge@" + pos; | ||
var cachedResult = cache[cacheKey]; | ||
@@ -534,29 +512,26 @@ if (cachedResult) { | ||
var result0; | ||
var pos0; | ||
var savedPos0 = pos; | ||
if (input.substr(pos, 1) === "#") { | ||
var result1 = "#"; | ||
pos += 1; | ||
pos0 = pos; | ||
if (input.charCodeAt(pos) === 35) { | ||
result0 = "#"; | ||
pos++; | ||
} else { | ||
var result1 = null; | ||
if (reportMatchFailures) { | ||
result0 = null; | ||
if (reportFailures === 0) { | ||
matchFailed("\"#\""); | ||
} | ||
} | ||
var result2 = result1 !== null | ||
? (function() { | ||
return { | ||
merge: 'block' | ||
} | ||
})() | ||
: null; | ||
if (result2 !== null) { | ||
var result0 = result2; | ||
} else { | ||
var result0 = null; | ||
pos = savedPos0; | ||
if (result0 !== null) { | ||
result0 = (function(offset) { | ||
return { | ||
merge: 'block' | ||
} | ||
})(pos0); | ||
} | ||
if (result0 === null) { | ||
pos = pos0; | ||
} | ||
cache[cacheKey] = { | ||
@@ -570,3 +545,3 @@ nextPos: pos, | ||
function parse_dontencode() { | ||
var cacheKey = 'dontencode@' + pos; | ||
var cacheKey = "dontencode@" + pos; | ||
var cachedResult = cache[cacheKey]; | ||
@@ -578,29 +553,26 @@ if (cachedResult) { | ||
var result0; | ||
var pos0; | ||
var savedPos0 = pos; | ||
if (input.substr(pos, 1) === "`") { | ||
var result1 = "`"; | ||
pos += 1; | ||
pos0 = pos; | ||
if (input.charCodeAt(pos) === 96) { | ||
result0 = "`"; | ||
pos++; | ||
} else { | ||
var result1 = null; | ||
if (reportMatchFailures) { | ||
result0 = null; | ||
if (reportFailures === 0) { | ||
matchFailed("\"`\""); | ||
} | ||
} | ||
var result2 = result1 !== null | ||
? (function() { | ||
return { | ||
dontencode: true | ||
} | ||
})() | ||
: null; | ||
if (result2 !== null) { | ||
var result0 = result2; | ||
} else { | ||
var result0 = null; | ||
pos = savedPos0; | ||
if (result0 !== null) { | ||
result0 = (function(offset) { | ||
return { | ||
dontencode: true | ||
} | ||
})(pos0); | ||
} | ||
if (result0 === null) { | ||
pos = pos0; | ||
} | ||
cache[cacheKey] = { | ||
@@ -614,3 +586,3 @@ nextPos: pos, | ||
function parse_multivalued() { | ||
var cacheKey = 'multivalued@' + pos; | ||
var cacheKey = "multivalued@" + pos; | ||
var cachedResult = cache[cacheKey]; | ||
@@ -622,48 +594,45 @@ if (cachedResult) { | ||
var result0, result1; | ||
var pos0, pos1; | ||
var savedPos0 = pos; | ||
var savedPos1 = pos; | ||
var result5 = parse_digits(); | ||
var result3 = result5 !== null ? result5 : ''; | ||
if (result3 !== null) { | ||
if (input.substr(pos, 1) === "|") { | ||
var result4 = "|"; | ||
pos += 1; | ||
pos0 = pos; | ||
pos1 = pos; | ||
result0 = parse_digits(); | ||
result0 = result0 !== null ? result0 : ""; | ||
if (result0 !== null) { | ||
if (input.charCodeAt(pos) === 124) { | ||
result1 = "|"; | ||
pos++; | ||
} else { | ||
var result4 = null; | ||
if (reportMatchFailures) { | ||
result1 = null; | ||
if (reportFailures === 0) { | ||
matchFailed("\"|\""); | ||
} | ||
} | ||
if (result4 !== null) { | ||
var result1 = [result3, result4]; | ||
if (result1 !== null) { | ||
result0 = [result0, result1]; | ||
} else { | ||
var result1 = null; | ||
pos = savedPos1; | ||
result0 = null; | ||
pos = pos1; | ||
} | ||
} else { | ||
var result1 = null; | ||
pos = savedPos1; | ||
result0 = null; | ||
pos = pos1; | ||
} | ||
var result2 = result1 !== null | ||
? (function(d) { | ||
var ret = { | ||
multivalued: true | ||
} | ||
d = parseInt(d); | ||
if(d > 0) { | ||
ret.max = d; | ||
} | ||
return ret; | ||
})(result1[0]) | ||
: null; | ||
if (result2 !== null) { | ||
var result0 = result2; | ||
} else { | ||
var result0 = null; | ||
pos = savedPos0; | ||
if (result0 !== null) { | ||
result0 = (function(offset, d) { | ||
var ret = { | ||
multivalued: true | ||
} | ||
d = parseInt(d); | ||
if(d > 0) { | ||
ret.max = d; | ||
} | ||
return ret; | ||
})(pos0, result0[0]); | ||
} | ||
if (result0 === null) { | ||
pos = pos0; | ||
} | ||
cache[cacheKey] = { | ||
@@ -677,3 +646,3 @@ nextPos: pos, | ||
function parse_variable() { | ||
var cacheKey = 'variable@' + pos; | ||
var cacheKey = "variable@" + pos; | ||
var cachedResult = cache[cacheKey]; | ||
@@ -685,36 +654,33 @@ if (cachedResult) { | ||
var result0, result1; | ||
var pos0; | ||
var savedPos0 = pos; | ||
var result1 = []; | ||
var result3 = parse_literal(); | ||
while (result3 !== null) { | ||
result1.push(result3); | ||
var result3 = parse_literal(); | ||
pos0 = pos; | ||
result0 = []; | ||
result1 = parse_literal(); | ||
while (result1 !== null) { | ||
result0.push(result1); | ||
result1 = parse_literal(); | ||
} | ||
var result2 = result1 !== null | ||
? (function(l) { | ||
var o = []; | ||
o.push(l[0]); | ||
var current = 0; | ||
for(var i = 1; i < l.length; i++) { | ||
if(typeof l[i] === 'string' && typeof o[current] === 'string') { | ||
o[current] = o[current] + l[i]; | ||
} | ||
else { | ||
o.push(l[i]); | ||
current++; | ||
} | ||
} | ||
return (o.length === 1) ? o[0] : o; | ||
})(result1) | ||
: null; | ||
if (result2 !== null) { | ||
var result0 = result2; | ||
} else { | ||
var result0 = null; | ||
pos = savedPos0; | ||
if (result0 !== null) { | ||
result0 = (function(offset, l) { | ||
var o = []; | ||
o.push(l[0]); | ||
var current = 0; | ||
for(var i = 1; i < l.length; i++) { | ||
if(typeof l[i] === 'string' && typeof o[current] === 'string') { | ||
o[current] = o[current] + l[i]; | ||
} | ||
else { | ||
o.push(l[i]); | ||
current++; | ||
} | ||
} | ||
return (o.length === 1) ? o[0] : o; | ||
})(pos0, result0); | ||
} | ||
if (result0 === null) { | ||
pos = pos0; | ||
} | ||
cache[cacheKey] = { | ||
@@ -728,3 +694,3 @@ nextPos: pos, | ||
function parse_digits() { | ||
var cacheKey = 'digits@' + pos; | ||
var cacheKey = "digits@" + pos; | ||
var cachedResult = cache[cacheKey]; | ||
@@ -736,22 +702,24 @@ if (cachedResult) { | ||
var result0, result1; | ||
var pos0; | ||
var savedPos0 = pos; | ||
var result1 = []; | ||
if (input.substr(pos).match(/^[0-9]/) !== null) { | ||
var result3 = input.charAt(pos); | ||
pos0 = pos; | ||
result0 = []; | ||
if (/^[0-9]/.test(input.charAt(pos))) { | ||
result1 = input.charAt(pos); | ||
pos++; | ||
} else { | ||
var result3 = null; | ||
if (reportMatchFailures) { | ||
result1 = null; | ||
if (reportFailures === 0) { | ||
matchFailed("[0-9]"); | ||
} | ||
} | ||
while (result3 !== null) { | ||
result1.push(result3); | ||
if (input.substr(pos).match(/^[0-9]/) !== null) { | ||
var result3 = input.charAt(pos); | ||
while (result1 !== null) { | ||
result0.push(result1); | ||
if (/^[0-9]/.test(input.charAt(pos))) { | ||
result1 = input.charAt(pos); | ||
pos++; | ||
} else { | ||
var result3 = null; | ||
if (reportMatchFailures) { | ||
result1 = null; | ||
if (reportFailures === 0) { | ||
matchFailed("[0-9]"); | ||
@@ -761,20 +729,15 @@ } | ||
} | ||
var result2 = result1 !== null | ||
? (function(d) { | ||
var str = ''; | ||
for(var i = 0; i < d.length; i++) { | ||
str += d[i]; | ||
} | ||
return str; | ||
})(result1) | ||
: null; | ||
if (result2 !== null) { | ||
var result0 = result2; | ||
} else { | ||
var result0 = null; | ||
pos = savedPos0; | ||
if (result0 !== null) { | ||
result0 = (function(offset, d) { | ||
var str = ''; | ||
for(var i = 0; i < d.length; i++) { | ||
str += d[i]; | ||
} | ||
return str; | ||
})(pos0, result0); | ||
} | ||
if (result0 === null) { | ||
pos = pos0; | ||
} | ||
cache[cacheKey] = { | ||
@@ -787,34 +750,15 @@ nextPos: pos, | ||
function buildErrorMessage() { | ||
function buildExpected(failuresExpected) { | ||
failuresExpected.sort(); | ||
var lastFailure = null; | ||
var failuresExpectedUnique = []; | ||
for (var i = 0; i < failuresExpected.length; i++) { | ||
if (failuresExpected[i] !== lastFailure) { | ||
failuresExpectedUnique.push(failuresExpected[i]); | ||
lastFailure = failuresExpected[i]; | ||
} | ||
} | ||
function cleanupExpected(expected) { | ||
expected.sort(); | ||
switch (failuresExpectedUnique.length) { | ||
case 0: | ||
return 'end of input'; | ||
case 1: | ||
return failuresExpectedUnique[0]; | ||
default: | ||
return failuresExpectedUnique.slice(0, failuresExpectedUnique.length - 1).join(', ') | ||
+ ' or ' | ||
+ failuresExpectedUnique[failuresExpectedUnique.length - 1]; | ||
var lastExpected = null; | ||
var cleanExpected = []; | ||
for (var i = 0; i < expected.length; i++) { | ||
if (expected[i] !== lastExpected) { | ||
cleanExpected.push(expected[i]); | ||
lastExpected = expected[i]; | ||
} | ||
} | ||
var expected = buildExpected(rightmostMatchFailuresExpected); | ||
var actualPos = Math.max(pos, rightmostMatchFailuresPos); | ||
var actual = actualPos < input.length | ||
? quote(input.charAt(actualPos)) | ||
: 'end of input'; | ||
return 'Expected ' + expected + ' but ' + actual + ' found.'; | ||
return cleanExpected; | ||
} | ||
@@ -834,9 +778,9 @@ | ||
for (var i = 0; i < rightmostMatchFailuresPos; i++) { | ||
for (var i = 0; i < Math.max(pos, rightmostFailuresPos); i++) { | ||
var ch = input.charAt(i); | ||
if (ch === '\n') { | ||
if (ch === "\n") { | ||
if (!seenCR) { line++; } | ||
column = 1; | ||
seenCR = false; | ||
} else if (ch === '\r' | ch === '\u2028' || ch === '\u2029') { | ||
} else if (ch === "\r" || ch === "\u2028" || ch === "\u2029") { | ||
line++; | ||
@@ -855,3 +799,2 @@ column = 1; | ||
var result = parseFunctions[startRule](); | ||
@@ -866,3 +809,3 @@ | ||
* - |pos === input.length| | ||
* - |rightmostMatchFailuresExpected| may or may not contain something | ||
* - |rightmostFailuresExpected| may or may not contain something | ||
* | ||
@@ -873,3 +816,3 @@ * 2. The parser successfully parsed only a part of the input. | ||
* - |pos < input.length| | ||
* - |rightmostMatchFailuresExpected| may or may not contain something | ||
* - |rightmostFailuresExpected| may or may not contain something | ||
* | ||
@@ -880,3 +823,3 @@ * 3. The parser did not successfully parse any part of the input. | ||
* - |pos === 0| | ||
* - |rightmostMatchFailuresExpected| contains at least one failure | ||
* - |rightmostFailuresExpected| contains at least one failure | ||
* | ||
@@ -887,5 +830,10 @@ * All code following this comment (including called functions) must | ||
if (result === null || pos !== input.length) { | ||
var offset = Math.max(pos, rightmostFailuresPos); | ||
var found = offset < input.length ? input.charAt(offset) : null; | ||
var errorPosition = computeErrorPosition(); | ||
throw new this.SyntaxError( | ||
buildErrorMessage(), | ||
cleanupExpected(rightmostFailuresExpected), | ||
found, | ||
offset, | ||
errorPosition.line, | ||
@@ -905,5 +853,29 @@ errorPosition.column | ||
result.SyntaxError = function(message, line, column) { | ||
this.name = 'SyntaxError'; | ||
this.message = message; | ||
result.SyntaxError = function(expected, found, offset, line, column) { | ||
function buildMessage(expected, found) { | ||
var expectedHumanized, foundHumanized; | ||
switch (expected.length) { | ||
case 0: | ||
expectedHumanized = "end of input"; | ||
break; | ||
case 1: | ||
expectedHumanized = expected[0]; | ||
break; | ||
default: | ||
expectedHumanized = expected.slice(0, expected.length - 1).join(", ") | ||
+ " or " | ||
+ expected[expected.length - 1]; | ||
} | ||
foundHumanized = found ? quote(found) : "end of input"; | ||
return "Expected " + expectedHumanized + " but " + foundHumanized + " found."; | ||
} | ||
this.name = "SyntaxError"; | ||
this.expected = expected; | ||
this.found = found; | ||
this.message = buildMessage(expected, found); | ||
this.offset = offset; | ||
this.line = line; | ||
@@ -910,0 +882,0 @@ this.column = column; |
@@ -5,3 +5,3 @@ { | ||
"name": "ql.io-uri-template", | ||
"version": "0.5.0", | ||
"version": "0.5.1", | ||
"repository": { | ||
@@ -8,0 +8,0 @@ "type": "git", |
Sorry, the diff of this file is not supported yet
38269
779