|
1 | 1 | /*! Copyright (c) 2011, Lloyd Hilaiel, ISC License */ |
2 | 2 | (function() { |
3 | 3 | var w = window; |
4 | | - var jsonParse = (w.JSON ? w.JSON.parse : w.eval); |
| 4 | + var jp = (w.JSON ? w.JSON.parse : w.eval); |
| 5 | + function jsonParse(s) { try { return jp(s); } catch(e) { te("ijs"); }; } |
5 | 6 |
|
6 | 7 | // emitted error codes. Strip this table for an, uh, "optimized build" |
7 | 8 | var _es = {}; // overshadow any globals when the table is stripped |
|
30 | 31 | str: 4, // string |
31 | 32 | }; |
32 | 33 |
|
33 | | - var tPat = /^(?:string|boolean|null|array|object|number)/; |
34 | | - var pcPat = /^(?:root|first-child|last-child|only-child)/; |
35 | | - var jsPat = /^(?:\"(?:[^\\]|\\[^\"])*\")/; |
36 | | - var iPat = /^(?:[_a-zA-Z]|[^\0-\0177]|\\[^\r\n\f0-9a-fA-F])(?:[_a-zA-Z0-9-]|[^\u0000-\u0177]|(?:\\[^\r\n\f0-9a-fA-F]))*/; |
| 34 | + var pat = /^(?:([\r\n\t\ ]+)|([*#,>])|(string|boolean|null|array|object|number)|(:(?:root|first-child|last-child|only-child))|(:\w+)|(\"(?:[^\\]|\\[^\"])*\")|(\")|((?:[_a-zA-Z]|[^\0-\0177]|\\[^\r\n\f0-9a-fA-F])(?:[_a-zA-Z0-9-]|[^\u0000-\u0177]|(?:\\[^\r\n\f0-9a-fA-F]))*))/; |
37 | 35 | var lex = function (str, off) { |
38 | | - if (off == undefined) off = 0; |
39 | | - while (str.length > off) { |
40 | | - switch(str.charCodeAt(off)) { |
41 | | - // for simple 1 char tokens, we'll let them represent themselves. |
42 | | - case 0x23: case 0x2a: case 0x2c: |
43 | | - case 0x3e: case 0x7e: |
44 | | - return [off+1, str.charAt(off)]; |
45 | | - // whitespace: space, nl, tab, cr, represented as the space char |
46 | | - case 0x20: case 0x0a: case 0x0d: case 0x09: |
47 | | - do { off++; } while (off < str.length && "\t\r\n ".indexOf(str.charAt(off)) !== -1); |
48 | | - return [off, " "]; |
49 | | - // colon ':' indicates psuedo class |
50 | | - case 0x3a: |
51 | | - var m; |
52 | | - var ss = str.substr(off+1); |
53 | | - if (m = pcPat.exec(ss)) { |
54 | | - return [off + 1 + m[0].length, toks.psc, ":" + m[0]]; |
55 | | - } else if (false) { |
56 | | - te("pcny"); |
57 | | - } |
58 | | - te("upc"); |
59 | | - // quote '"' indicates embedded JSON string |
60 | | - case 0x22: |
61 | | - var m; |
62 | | - if (m = jsPat.exec(str.substr(off))) { |
63 | | - try { |
64 | | - // using JSON parsing directly here is bad, it kills our |
65 | | - // portability. Can we safely use eval considering we know |
66 | | - // this is a value enclosed in quotes? |
67 | | - return [off + m[0].length, toks.str, jsonParse(m[0])]; |
68 | | - } catch(e) { |
69 | | - te("ijs"); |
70 | | - } |
71 | | - } |
72 | | - te("ujs"); |
73 | | - default: |
74 | | - var m; |
75 | | - var ss = str.substr(off); |
76 | | - // test for types |
77 | | - if (m = tPat.exec(ss)) { |
78 | | - return [off + m[0].length, toks.typ, m[0]]; |
79 | | - } |
80 | | - // test for idents |
81 | | - else if (m = iPat.exec(ss)) { |
82 | | - return [off + m[0].length, toks.str, m[0].replace(/\\([^\r\n\f0-9a-fA-F])/g,"$1")]; |
83 | | - } |
84 | | - te("uc"); |
85 | | - } |
86 | | - } |
| 36 | + if (!off) off = 0; |
| 37 | + var m = pat.exec(str.substr(off)); |
| 38 | + if (!m) return undefined; |
| 39 | + off+=m[0].length; |
| 40 | + if (m[1]) return [off, " "]; |
| 41 | + if (m[2]) return [off, m[0]]; |
| 42 | + else if (m[3]) return [off, toks.typ, m[0]]; |
| 43 | + else if (m[4]) return [off, toks.psc, m[0]]; |
| 44 | + else if (m[5]) te("upc"); |
| 45 | + else if (m[6]) return [off, toks.str, jsonParse(m[0])]; |
| 46 | + else if (m[7]) te("ujs"); |
| 47 | + else if (m[8]) return [off, toks.str, m[0].replace(/\\([^\r\n\f0-9a-fA-F])/g,"$1")]; |
87 | 48 | }; |
88 | 49 |
|
89 | 50 | // THE PARSER |
|
0 commit comments