Comparing version 0.12.1 to 0.12.2
190
clarinet.js
;(function (clarinet) { | ||
"use strict"; | ||
// non node-js needs to set clarinet debug on root | ||
var env | ||
, fastlist | ||
; | ||
var env =(typeof process === 'object' && process.env) | ||
? process.env | ||
: window; | ||
if(typeof process === 'object' && process.env) env = process.env; | ||
else env = window; | ||
clarinet.parser = function (opt) { return new CParser(opt);}; | ||
@@ -73,2 +72,40 @@ clarinet.CParser = CParser; | ||
const Char = { | ||
tab : 0x09, // \t | ||
lineFeed : 0x0A, // \n | ||
carriageReturn : 0x0D, // \r | ||
space : 0x20, // " " | ||
doubleQuote : 0x22, // " | ||
plus : 0x2B, // + | ||
comma : 0x2C, // , | ||
minus : 0x2D, // - | ||
period : 0x2E, // . | ||
_0 : 0x30, // 0 | ||
_9 : 0x39, // 9 | ||
colon : 0x3A, // : | ||
E : 0x45, // E | ||
openBracket : 0x5B, // [ | ||
backslash : 0x5C, // \ | ||
closeBracket : 0x5D, // ] | ||
a : 0x61, // a | ||
b : 0x62, // b | ||
e : 0x65, // e | ||
f : 0x66, // f | ||
l : 0x6C, // l | ||
n : 0x6E, // n | ||
r : 0x72, // r | ||
s : 0x73, // s | ||
t : 0x74, // t | ||
u : 0x75, // u | ||
openBrace : 0x7B, // { | ||
closeBrace : 0x7D, // } | ||
} | ||
if (!Object.create) { | ||
@@ -343,2 +380,6 @@ Object.create = function (o) { | ||
function isWhitespace(c) { | ||
return c === Char.carriageReturn || c === Char.lineFeed || c === Char.space || c === Char.tab; | ||
} | ||
function write (chunk) { | ||
@@ -350,7 +391,7 @@ var parser = this; | ||
if (chunk === null) return end(parser); | ||
var i = 0, c = chunk[0], p = parser.p; | ||
var i = 0, c = chunk.charCodeAt(0), p = parser.p; | ||
if (clarinet.DEBUG) console.log('write -> [' + chunk + ']'); | ||
while (c) { | ||
p = c; | ||
parser.c = c = chunk.charAt(i++); | ||
parser.c = c = chunk.charCodeAt(i++); | ||
// if chunk doesnt have next, like streaming char by char | ||
@@ -367,3 +408,3 @@ // this way we need to check if previous is really previous | ||
parser.position ++; | ||
if (c === "\n") { | ||
if (c === Char.lineFeed) { | ||
parser.line ++; | ||
@@ -375,5 +416,5 @@ parser.column = 0; | ||
case S.BEGIN: | ||
if (c === "{") parser.state = S.OPEN_OBJECT; | ||
else if (c === "[") parser.state = S.OPEN_ARRAY; | ||
else if (c !== '\r' && c !== '\n' && c !== ' ' && c !== '\t') | ||
if (c === Char.openBrace) parser.state = S.OPEN_OBJECT; | ||
else if (c === Char.openBracket) parser.state = S.OPEN_ARRAY; | ||
else if (!isWhitespace(c)) | ||
error(parser, "Non-whitespace before {[."); | ||
@@ -384,6 +425,6 @@ continue; | ||
case S.OPEN_OBJECT: | ||
if (c === '\r' || c === '\n' || c === ' ' || c === '\t') continue; | ||
if (isWhitespace(c)) continue; | ||
if(parser.state === S.OPEN_KEY) parser.stack.push(S.CLOSE_KEY); | ||
else { | ||
if(c === '}') { | ||
if(c === Char.closeBrace) { | ||
emit(parser, 'onopenobject'); | ||
@@ -397,3 +438,3 @@ this.depth++; | ||
} | ||
if(c === '"') parser.state = S.STRING; | ||
if(c === Char.doubleQuote) parser.state = S.STRING; | ||
else error(parser, "Malformed object key should start with \""); | ||
@@ -404,5 +445,5 @@ continue; | ||
case S.CLOSE_OBJECT: | ||
if (c === '\r' || c === '\n' || c === ' ' || c === '\t') continue; | ||
if (isWhitespace(c)) continue; | ||
var event = (parser.state === S.CLOSE_KEY) ? 'key' : 'object'; | ||
if(c===':') { | ||
if(c === Char.colon) { | ||
if(parser.state === S.CLOSE_OBJECT) { | ||
@@ -414,7 +455,7 @@ parser.stack.push(S.CLOSE_OBJECT); | ||
parser.state = S.VALUE; | ||
} else if (c==='}') { | ||
} else if (c === Char.closeBrace) { | ||
emitNode(parser, 'oncloseobject'); | ||
this.depth--; | ||
parser.state = parser.stack.pop() || S.VALUE; | ||
} else if(c===',') { | ||
} else if(c === Char.comma) { | ||
if(parser.state === S.CLOSE_OBJECT) | ||
@@ -429,3 +470,3 @@ parser.stack.push(S.CLOSE_OBJECT); | ||
case S.VALUE: | ||
if (c === '\r' || c === '\n' || c === ' ' || c === '\t') continue; | ||
if (isWhitespace(c)) continue; | ||
if(parser.state===S.OPEN_ARRAY) { | ||
@@ -435,3 +476,3 @@ emit(parser, 'onopenarray'); | ||
parser.state = S.VALUE; | ||
if(c === ']') { | ||
if(c === Char.closeBracket) { | ||
emit(parser, 'onclosearray'); | ||
@@ -445,16 +486,13 @@ this.depth--; | ||
} | ||
if(c === '"') parser.state = S.STRING; | ||
else if(c === '{') parser.state = S.OPEN_OBJECT; | ||
else if(c === '[') parser.state = S.OPEN_ARRAY; | ||
else if(c === 't') parser.state = S.TRUE; | ||
else if(c === 'f') parser.state = S.FALSE; | ||
else if(c === 'n') parser.state = S.NULL; | ||
else if(c === '-') { // keep and continue | ||
parser.numberNode += c; | ||
} else if(c==='0') { | ||
parser.numberNode += c; | ||
if(c === Char.doubleQuote) parser.state = S.STRING; | ||
else if(c === Char.openBrace) parser.state = S.OPEN_OBJECT; | ||
else if(c === Char.openBracket) parser.state = S.OPEN_ARRAY; | ||
else if(c === Char.t) parser.state = S.TRUE; | ||
else if(c === Char.f) parser.state = S.FALSE; | ||
else if(c === Char.n) parser.state = S.NULL; | ||
else if(c === Char.minus) { // keep and continue | ||
parser.numberNode += "-"; | ||
} else if(Char._0 <= c && c <= Char._9) { | ||
parser.numberNode += String.fromCharCode(c); | ||
parser.state = S.NUMBER_DIGIT; | ||
} else if('123456789'.indexOf(c) !== -1) { | ||
parser.numberNode += c; | ||
parser.state = S.NUMBER_DIGIT; | ||
} else error(parser, "Bad value"); | ||
@@ -464,11 +502,11 @@ continue; | ||
case S.CLOSE_ARRAY: | ||
if(c===',') { | ||
if(c === Char.comma) { | ||
parser.stack.push(S.CLOSE_ARRAY); | ||
closeValue(parser, 'onvalue'); | ||
parser.state = S.VALUE; | ||
} else if (c===']') { | ||
} else if (c === Char.closeBracket) { | ||
emitNode(parser, 'onclosearray'); | ||
this.depth--; | ||
parser.state = parser.stack.pop() || S.VALUE; | ||
} else if (c === '\r' || c === '\n' || c === ' ' || c === '\t') | ||
} else if (isWhitespace(c)) | ||
continue; | ||
@@ -494,4 +532,4 @@ else error(parser, 'Bad array'); | ||
while (unicodeI > 0) { | ||
parser.unicodeS += c; | ||
c = chunk.charAt(i++); | ||
parser.unicodeS += String.fromCharCode(c); | ||
c = chunk.charCodeAt(i++); | ||
parser.position++; | ||
@@ -509,3 +547,3 @@ if (unicodeI === 4) { | ||
} | ||
if (c === '"' && !slashed) { | ||
if (c === Char.doubleQuote && !slashed) { | ||
parser.state = parser.stack.pop() || S.VALUE; | ||
@@ -516,7 +554,7 @@ parser.textNode += chunk.substring(starti, i-1); | ||
} | ||
if (c === '\\' && !slashed) { | ||
if (c === Char.backslash && !slashed) { | ||
slashed = true; | ||
parser.textNode += chunk.substring(starti, i-1); | ||
parser.position += i - 1 - starti; | ||
c = chunk.charAt(i++); | ||
c = chunk.charCodeAt(i++); | ||
parser.position++; | ||
@@ -527,8 +565,8 @@ if (!c) break; | ||
slashed = false; | ||
if (c === 'n') { parser.textNode += '\n'; } | ||
else if (c === 'r') { parser.textNode += '\r'; } | ||
else if (c === 't') { parser.textNode += '\t'; } | ||
else if (c === 'f') { parser.textNode += '\f'; } | ||
else if (c === 'b') { parser.textNode += '\b'; } | ||
else if (c === 'u') { | ||
if (c === Char.n) { parser.textNode += '\n'; } | ||
else if (c === Char.r) { parser.textNode += '\r'; } | ||
else if (c === Char.t) { parser.textNode += '\t'; } | ||
else if (c === Char.f) { parser.textNode += '\f'; } | ||
else if (c === Char.b) { parser.textNode += '\b'; } | ||
else if (c === Char.u) { | ||
// \uxxxx. meh! | ||
@@ -538,5 +576,5 @@ unicodeI = 1; | ||
} else { | ||
parser.textNode += c; | ||
parser.textNode += String.fromCharCode(c); | ||
} | ||
c = chunk.charAt(i++); | ||
c = chunk.charCodeAt(i++); | ||
parser.position++; | ||
@@ -557,3 +595,3 @@ starti = i-1; | ||
i = reResult.index+1; | ||
c = chunk.charAt(reResult.index); | ||
c = chunk.charCodeAt(reResult.index); | ||
if (!c) { | ||
@@ -570,4 +608,3 @@ parser.textNode += chunk.substring(starti, i-1); | ||
case S.TRUE: | ||
if (c==='') continue; // strange buffers | ||
if (c==='r') parser.state = S.TRUE2; | ||
if (c === Char.r) parser.state = S.TRUE2; | ||
else error(parser, 'Invalid true started with t'+ c); | ||
@@ -577,4 +614,3 @@ continue; | ||
case S.TRUE2: | ||
if (c==='') continue; | ||
if (c==='u') parser.state = S.TRUE3; | ||
if (c === Char.u) parser.state = S.TRUE3; | ||
else error(parser, 'Invalid true started with tr'+ c); | ||
@@ -584,4 +620,3 @@ continue; | ||
case S.TRUE3: | ||
if (c==='') continue; | ||
if(c==='e') { | ||
if(c === Char.e) { | ||
emit(parser, "onvalue", true); | ||
@@ -593,4 +628,3 @@ parser.state = parser.stack.pop() || S.VALUE; | ||
case S.FALSE: | ||
if (c==='') continue; | ||
if (c==='a') parser.state = S.FALSE2; | ||
if (c === Char.a) parser.state = S.FALSE2; | ||
else error(parser, 'Invalid false started with f'+ c); | ||
@@ -600,4 +634,3 @@ continue; | ||
case S.FALSE2: | ||
if (c==='') continue; | ||
if (c==='l') parser.state = S.FALSE3; | ||
if (c === Char.l) parser.state = S.FALSE3; | ||
else error(parser, 'Invalid false started with fa'+ c); | ||
@@ -607,4 +640,3 @@ continue; | ||
case S.FALSE3: | ||
if (c==='') continue; | ||
if (c==='s') parser.state = S.FALSE4; | ||
if (c === Char.s) parser.state = S.FALSE4; | ||
else error(parser, 'Invalid false started with fal'+ c); | ||
@@ -614,4 +646,3 @@ continue; | ||
case S.FALSE4: | ||
if (c==='') continue; | ||
if (c==='e') { | ||
if (c === Char.e) { | ||
emit(parser, "onvalue", false); | ||
@@ -623,4 +654,3 @@ parser.state = parser.stack.pop() || S.VALUE; | ||
case S.NULL: | ||
if (c==='') continue; | ||
if (c==='u') parser.state = S.NULL2; | ||
if (c === Char.u) parser.state = S.NULL2; | ||
else error(parser, 'Invalid null started with n'+ c); | ||
@@ -630,4 +660,3 @@ continue; | ||
case S.NULL2: | ||
if (c==='') continue; | ||
if (c==='l') parser.state = S.NULL3; | ||
if (c === Char.l) parser.state = S.NULL3; | ||
else error(parser, 'Invalid null started with nu'+ c); | ||
@@ -637,4 +666,3 @@ continue; | ||
case S.NULL3: | ||
if (c==='') continue; | ||
if(c==='l') { | ||
if(c === Char.l) { | ||
emit(parser, "onvalue", null); | ||
@@ -646,4 +674,4 @@ parser.state = parser.stack.pop() || S.VALUE; | ||
case S.NUMBER_DECIMAL_POINT: | ||
if(c==='.') { | ||
parser.numberNode += c; | ||
if(c === Char.period) { | ||
parser.numberNode += "."; | ||
parser.state = S.NUMBER_DIGIT; | ||
@@ -654,16 +682,16 @@ } else error(parser, 'Leading zero not followed by .'); | ||
case S.NUMBER_DIGIT: | ||
if('0123456789'.indexOf(c) !== -1) parser.numberNode += c; | ||
else if (c==='.') { | ||
if(Char._0 <= c && c <= Char._9) parser.numberNode += String.fromCharCode(c); | ||
else if (c === Char.period) { | ||
if(parser.numberNode.indexOf('.')!==-1) | ||
error(parser, 'Invalid number has two dots'); | ||
parser.numberNode += c; | ||
} else if (c==='e' || c==='E') { | ||
parser.numberNode += "."; | ||
} else if (c === Char.e || c === Char.E) { | ||
if(parser.numberNode.indexOf('e')!==-1 || | ||
parser.numberNode.indexOf('E')!==-1 ) | ||
error(parser, 'Invalid number has two exponential'); | ||
parser.numberNode += c; | ||
} else if (c==="+" || c==="-") { | ||
if(!(p==='e' || p==='E')) | ||
parser.numberNode += "e"; | ||
} else if (c === Char.plus || c === Char.minus) { | ||
if(!(p === Char.e || p === Char.E)) | ||
error(parser, 'Invalid symbol in number'); | ||
parser.numberNode += c; | ||
parser.numberNode += String.fromCharCode(c); | ||
} else { | ||
@@ -670,0 +698,0 @@ closeNumber(parser); |
@@ -10,15 +10,18 @@ var clarinet = require("clarinet") | ||
}; | ||
parser.onopenobject = function () { | ||
console.log("New Object"); | ||
parser.onopenobject = function (key) { | ||
console.log("New Object, first key: " + key); | ||
} | ||
parser.oncloseobject = function () { | ||
console.log("Close Object"); | ||
console.log("Close Object"); | ||
} | ||
parser.onopenarray = function () { | ||
console.log("New Array"); | ||
console.log("New Array"); | ||
} | ||
parser.onclosearray = function () { | ||
console.log("Close Array"); | ||
console.log("Close Array"); | ||
} | ||
parser.onend = function () { | ||
console.log('End'); | ||
} | ||
parser.write('{ "firstName": "John", "lastName" : "Smith", "age" : 25, "address" : { "streetAddress": "21 2nd Street", "city" : "New York", "state" : "NY", "postalCode" : "10021" }, "phoneNumber": [ { "type" : "home", "number": "212 555-1234" }, { "type" : "fax", "number": "646 555-4567" } ] }').close(); |
@@ -11,3 +11,3 @@ { | ||
], | ||
"version": "0.12.1", | ||
"version": "0.12.2", | ||
"main": "./clarinet.js", | ||
@@ -42,3 +42,5 @@ "homepage": "https://github.com/dscape/clarinet", | ||
"scripts": { | ||
"test": "mocha -r should -t 10000 -s 2000 test/clarinet.js test/npm.js test/utf8-chunks.js test/position.js" | ||
"test": "mocha -r should -t 10000 -s 2000 test/parser.spec.js test/clarinet.js test/npm.js test/utf8-chunks.js test/position.js", | ||
"bench": "cd benchmark && npm test", | ||
"postinstall": "cd benchmark && npm i" | ||
}, | ||
@@ -45,0 +47,0 @@ "engines": { |
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
Install scripts
Supply chain riskInstall scripts are run when the package is installed. The majority of malware in npm is hidden in install scripts.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
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
38122
10
731
1
3