Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
82 additions
and
33 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} | ||
} |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) |