Skip to content
This repository
Browse code

Experimental rewrite of the core

  • Loading branch information...
commit 561382f274fc0f511af50c8d54818af9c671d801 1 parent 9229d1d
Isaac Wolkerstorfer authored

Showing 3 changed files with 82 additions and 33 deletions. Show diff stats Hide diff stats

  1. +39 0 lib/common.js
  2. +13 33 lib/index.js
  3. +30 0 lib/parser.js
39 lib/common.js
... ... @@ -0,0 +1,39 @@
  1 +var syntax = {}
  2 +
  3 +var keywords = {
  4 + glob : "$JSONExp GLOB"
  5 + capture : "$JSONExp CAPTURE"
  6 + any : "$JSONExp ANY"
  7 + many : "$JSONExp MANY"
  8 +}
  9 +
  10 +Pattern = function() {
  11 +}
  12 +
  13 +Pattern.prototype = {}
  14 +Pattern.prototype.toString = function() { return JSON.stringify(this) }
  15 +
  16 +var fn = {
  17 + glob : function glob(obj) {
  18 + obj[keywords.glob] = true
  19 + return obj
  20 + }
  21 +
  22 + capture : function capture(key) {
  23 + var value = {}
  24 + value[keywords.capture] = key
  25 + return value
  26 + }
  27 +
  28 + any : function() {
  29 + var value = {}
  30 + value[keywords.any] = true
  31 + return value
  32 + }
  33 +
  34 + many : function() {
  35 + var value = {}
  36 + value[keywords.many] = true
  37 + return value
  38 + }
  39 +}
46 lib/index.js
... ... @@ -1,6 +1,18 @@
  1 +/**
  2 + * JSONExp:
  3 + * Regex-like pattern matching for JSON
  4 + *
  5 + * Example:
  6 + *
  7 + * var exp = new JSONExp('{ "foo": "bar" }')
  8 + *
  9 + * exp.match({ foo: "bar" }) => true
  10 + * exp.match({ foo: "baz" }) => false
  11 + */
  12 +
1 13 var assert = require('assert')
2 14
3   -var captureRE = /\[([A-Za-z_][A-Za-z_0-9]*)\]/g
  15 +var captureRE = /\@([A-Za-z_][A-Za-z_0-9]*)/g
4 16
5 17 function mergeCaptures(current, next, noOverwrite) {
6 18 for (var key in next) {
@@ -39,38 +51,6 @@ var specials = function(obj) {
39 51 return opts
40 52 }
41 53
42   -specials.glob = "$JSONExp GLOB"
43   -specials.capture = "$JSONExp CAPTURE"
44   -specials.any = "$JSONExp ANY"
45   -specials.many = "$JSONExp MANY"
46   -
47   -function unicodeEscape(str) {
48   - var output = '', code
49   - for (var i = 0; i < str.length; i++) {
50   - code = str.charCodeAt(i).toString(16)
51   - while (code.length < 4) code = '0' + code
52   - output += '\\u' + code
53   - }
54   - return output
55   -}
56   -
57   -function escapeString(match, contents) {
58   - return '"' + unicodeEscape(contents) + '"'
59   -}
60   -
61   -JSONExp.preprocess = function(src) {
62   - return src
63   - .replace(/"(.*?[^\\])"/g, escapeString)
64   - .replace(/\*\*/g, '{ "' + specials.many + '": true }')
65   - .replace(/\*/g, '{ "' + specials.any + '": true }')
66   - .replace(/\.\.\./g, '"' + specials.glob + '": true')
67   - .replace(captureRE, '{ "' + specials.capture + '": "$1" }')
68   -}
69   -
70   -JSONExp.parse = function(src) {
71   - return JSON.parse(JSONExp.preprocess(src))
72   -}
73   -
74 54 JSONExp.prototype = new Function
75 55
76 56 JSONExp.prototype.exec = function(obj, options) {
30 lib/parser.js
... ... @@ -0,0 +1,30 @@
  1 +function unicodeEscape(str) {
  2 + var output = '', code
  3 + for (var i = 0; i < str.length; i++) {
  4 + code = str.charCodeAt(i).toString(16)
  5 + while (code.length < 4) code = '0' + code
  6 + output += '\\u' + code
  7 + }
  8 + return output
  9 +}
  10 +
  11 +function escapeString(match, contents) {
  12 + return '"' + unicodeEscape(contents) + '"'
  13 +}
  14 +
  15 +function preprocess(src) {
  16 + return src
  17 + .replace(/"(.*?[^\\])"/g, escapeString)
  18 + .replace(/\*\*/g, '{ "' + specials.many + '": true }')
  19 + .replace(/\[\s*\.\.\./g, '[{ "' + specials.many + '": true }')
  20 + .replace(/\.\.\.\s*\]/g, '{ "' + specials.many + '": true }]')
  21 + .replace(/\*/g, '{ "' + specials.any + '": true }')
  22 + .replace(/\.\.\./g, '"' + specials.glob + '": true')
  23 + .replace(captureRE, '{ "' + specials.capture + '": "$1" }')
  24 +}
  25 +
  26 +function parse(src) {
  27 + return JSON.parse(preprocess(src))
  28 +}
  29 +
  30 +module.exports(parse)

0 comments on commit 561382f

Please sign in to comment.
Something went wrong with that request. Please try again.