Skip to content

Commit

Permalink
Experimental rewrite of the core
Browse files Browse the repository at this point in the history
  • Loading branch information
agnoster committed Aug 7, 2012
1 parent 9229d1d commit 561382f
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 33 deletions.
39 changes: 39 additions & 0 deletions lib/common.js
@@ -0,0 +1,39 @@
var syntax = {}

var keywords = {
glob : "$JSONExp GLOB"
capture : "$JSONExp CAPTURE"
any : "$JSONExp ANY"
many : "$JSONExp MANY"
}

Pattern = function() {
}

Pattern.prototype = {}
Pattern.prototype.toString = function() { return JSON.stringify(this) }

var fn = {
glob : function glob(obj) {
obj[keywords.glob] = true
return obj
}

capture : function capture(key) {
var value = {}
value[keywords.capture] = key
return value
}

any : function() {
var value = {}
value[keywords.any] = true
return value
}

many : function() {
var value = {}
value[keywords.many] = true
return value
}
}
46 changes: 13 additions & 33 deletions lib/index.js
@@ -1,6 +1,18 @@
/**
* JSONExp:
* Regex-like pattern matching for JSON
*
* Example:
*
* var exp = new JSONExp('{ "foo": "bar" }')
*
* exp.match({ foo: "bar" }) => true
* exp.match({ foo: "baz" }) => false
*/

var assert = require('assert')

var captureRE = /\[([A-Za-z_][A-Za-z_0-9]*)\]/g
var captureRE = /\@([A-Za-z_][A-Za-z_0-9]*)/g

function mergeCaptures(current, next, noOverwrite) {
for (var key in next) {
Expand Down Expand Up @@ -39,38 +51,6 @@ var specials = function(obj) {
return opts
}

specials.glob = "$JSONExp GLOB"
specials.capture = "$JSONExp CAPTURE"
specials.any = "$JSONExp ANY"
specials.many = "$JSONExp MANY"

function unicodeEscape(str) {
var output = '', code
for (var i = 0; i < str.length; i++) {
code = str.charCodeAt(i).toString(16)
while (code.length < 4) code = '0' + code
output += '\\u' + code
}
return output
}

function escapeString(match, contents) {
return '"' + unicodeEscape(contents) + '"'
}

JSONExp.preprocess = function(src) {
return src
.replace(/"(.*?[^\\])"/g, escapeString)
.replace(/\*\*/g, '{ "' + specials.many + '": true }')
.replace(/\*/g, '{ "' + specials.any + '": true }')
.replace(/\.\.\./g, '"' + specials.glob + '": true')
.replace(captureRE, '{ "' + specials.capture + '": "$1" }')
}

JSONExp.parse = function(src) {
return JSON.parse(JSONExp.preprocess(src))
}

JSONExp.prototype = new Function

JSONExp.prototype.exec = function(obj, options) {
Expand Down
30 changes: 30 additions & 0 deletions lib/parser.js
@@ -0,0 +1,30 @@
function unicodeEscape(str) {
var output = '', code
for (var i = 0; i < str.length; i++) {
code = str.charCodeAt(i).toString(16)
while (code.length < 4) code = '0' + code
output += '\\u' + code
}
return output
}

function escapeString(match, contents) {
return '"' + unicodeEscape(contents) + '"'
}

function preprocess(src) {
return src
.replace(/"(.*?[^\\])"/g, escapeString)
.replace(/\*\*/g, '{ "' + specials.many + '": true }')
.replace(/\[\s*\.\.\./g, '[{ "' + specials.many + '": true }')
.replace(/\.\.\.\s*\]/g, '{ "' + specials.many + '": true }]')
.replace(/\*/g, '{ "' + specials.any + '": true }')
.replace(/\.\.\./g, '"' + specials.glob + '": true')
.replace(captureRE, '{ "' + specials.capture + '": "$1" }')
}

function parse(src) {
return JSON.parse(preprocess(src))
}

module.exports(parse)

0 comments on commit 561382f

Please sign in to comment.