Skip to content

Commit e3ce679

Browse files
committed
fix needless collisions between keywords (like 'string') and property names. closes #17.
1 parent c4ec206 commit e3ce679

3 files changed

Lines changed: 34 additions & 12 deletions

File tree

JSONSelect.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,8 @@ class functions, and more blue sky dreaming.
104104
simple_selector_sequence
105105
/* why allow multiple HASH entities in the grammar? */
106106
: [ type_selector | universal ]
107-
[ hash | pseudo ]*
108-
| [ hash | pseudo ]+
107+
[ class | pseudo ]*
108+
| [ class | pseudo ]+
109109
;
110110

111111
type_selector
@@ -116,7 +116,7 @@ class functions, and more blue sky dreaming.
116116
: '*'
117117
;
118118

119-
hash
119+
class
120120
: `.` name
121121
| `.` json_string
122122
;

src/jsonselect.js

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,31 @@
5252
str: 4 // string
5353
};
5454

55-
var pat = /^(?:([\r\n\t\ ]+)|([~*.,>\)\(])|(string|boolean|null|array|object|number)|(:(?:root|first-child|last-child|only-child))|(:(?:nth-child|nth-last-child|has|expr|val|contains))|(:\w+)|(\"(?:[^\\]|\\[^\"])*\")|(\")|((?:[_a-zA-Z]|[^\0-\0177]|\\[^\r\n\f0-9a-fA-F])(?:[_a-zA-Z0-9\-]|[^\u0000-\u0177]|(?:\\[^\r\n\f0-9a-fA-F]))*))/;
55+
// The primary lexing regular expression in jsonselect
56+
var pat = new RegExp(
57+
"^(?:" +
58+
// (1) whitespace
59+
"([\\r\\n\\t\\ ]+)|" +
60+
// (2) one-char ops
61+
"([~*,>\\)\\(])|" +
62+
// (3) types names
63+
"(string|boolean|null|array|object|number)|" +
64+
// (4) pseudo classes
65+
"(:(?:root|first-child|last-child|only-child))|" +
66+
// (5) pseudo functions
67+
"(:(?:nth-child|nth-last-child|has|expr|val|contains))|" +
68+
// (6) bogusly named pseudo something or others
69+
"(:\\w+)|" +
70+
// (7) JSON strings
71+
"\\.(\\\"(?:[^\\\\]|\\\\[^\\\"])*\\\")|" +
72+
// (8) bogus JSON strings missing a trailing quote
73+
"(\\\")|" +
74+
// (9) identifiers (unquoted)
75+
"\\.((?:[_a-zA-Z]|[^\\0-\\0177]|\\\\[^\\r\\n\\f0-9a-fA-F])(?:[_a-zA-Z0-9\\-]|[^\\u0000-\\u0177]|(?:\\\\[^\\r\\n\\f0-9a-fA-F]))*)" +
76+
")"
77+
);
78+
79+
// A regular expression for matching "nth expressions" (see grammar, what :nth-child() eats)
5680
var nthPat = /^\s*\(\s*(?:([+\-]?)([0-9]*)n\s*(?:([+\-])\s*([0-9]))?|(odd|even)|([+\-]?[0-9]+))\s*\)/;
5781
function lex(str, off) {
5882
if (!off) off = 0;
@@ -66,17 +90,17 @@
6690
else if (m[4]) a = [off, toks.psc, m[0]];
6791
else if (m[5]) a = [off, toks.psf, m[0]];
6892
else if (m[6]) te("upc");
69-
else if (m[7]) a = [off, toks.str, jsonParse(m[0])];
93+
else if (m[7]) a = [off, toks.str, jsonParse(m[7])];
7094
else if (m[8]) te("ujs");
71-
else if (m[9]) a = [off, toks.str, m[0].replace(/\\([^\r\n\f0-9a-fA-F])/g,"$1")];
95+
else if (m[9]) a = [off, toks.str, m[9].replace(/\\([^\r\n\f0-9a-fA-F])/g,"$1")];
7296
return a;
7397
}
7498

7599
// THE EXPRESSION SUBSYSTEM
76100

77101
var exprPat = new RegExp(
78-
// skip and don't capture leading whitespace
79-
"^\\s*(?:" +
102+
// skip and don't capture leading whitespace
103+
"^\\s*(?:" +
80104
// (1) simple vals
81105
"(true|false|null)|" +
82106
// (2) numbers
@@ -296,9 +320,7 @@
296320
while (true) {
297321
if (l === undefined) {
298322
break;
299-
} else if (l[1] === ".") {
300-
l = lex(str, (off = l[0]));
301-
if (!l || l[1] !== toks.str) te("sra");
323+
} else if (l[1] === toks.str) {
302324
if (s.id) te("nmi");
303325
s.id = l[2];
304326
} else if (l[1] === toks.psc) {

0 commit comments

Comments
 (0)