|
16 | 16 | for (var i = 0; i < words.length; ++i) obj[words[i]] = true; |
17 | 17 | return obj; |
18 | 18 | } |
19 | | - function heredoc(delim) { |
20 | | - return function(stream, state) { |
21 | | - if (stream.match(delim)) state.tokenize = null; |
22 | | - else stream.skipToEnd(); |
23 | | - return "string"; |
24 | | - }; |
25 | | - } |
26 | 19 |
|
27 | 20 | // Helper for stringWithEscapes |
28 | | - function matchSequence(list) { |
29 | | - if (list.length == 0) return stringWithEscapes; |
| 21 | + function matchSequence(list, end) { |
| 22 | + if (list.length == 0) return stringWithEscapes(end); |
30 | 23 | return function (stream, state) { |
31 | 24 | var patterns = list[0]; |
32 | 25 | for (var i = 0; i < patterns.length; i++) if (stream.match(patterns[i][0])) { |
33 | | - state.tokenize = matchSequence(list.slice(1)); |
| 26 | + state.tokenize = matchSequence(list.slice(1), end); |
34 | 27 | return patterns[i][1]; |
35 | 28 | } |
36 | | - state.tokenize = stringWithEscapes; |
| 29 | + state.tokenize = stringWithEscapes(end); |
37 | 30 | return "string"; |
38 | 31 | }; |
39 | 32 | } |
40 | | - function stringWithEscapes(stream, state) { |
41 | | - var escaped = false, next, end = false; |
42 | | - |
43 | | - if (stream.current() == '"') return "string"; |
44 | | - |
| 33 | + function stringWithEscapes(closing) { |
| 34 | + return function(stream, state) { return stringWithEscapes_(stream, state, closing); }; |
| 35 | + } |
| 36 | + function stringWithEscapes_(stream, state, closing) { |
45 | 37 | // "Complex" syntax |
46 | 38 | if (stream.match("${", false) || stream.match("{$", false)) { |
47 | 39 | state.tokenize = null; |
48 | 40 | return "string"; |
49 | 41 | } |
50 | 42 |
|
51 | 43 | // Simple syntax |
52 | | - if (stream.match(/\$[a-zA-Z_][a-zA-Z0-9_]*/)) { |
| 44 | + if (stream.match(/^\$[a-zA-Z_][a-zA-Z0-9_]*/)) { |
53 | 45 | // After the variable name there may appear array or object operator. |
54 | 46 | if (stream.match("[", false)) { |
55 | 47 | // Match array operator |
|
59 | 51 | [/\$[a-zA-Z_][a-zA-Z0-9_]*/, "variable-2"], |
60 | 52 | [/[\w\$]+/, "variable"]], |
61 | 53 | [["]", null]] |
62 | | - ]); |
| 54 | + ], closing); |
63 | 55 | } |
64 | 56 | if (stream.match(/\-\>\w/, false)) { |
65 | 57 | // Match object operator |
66 | 58 | state.tokenize = matchSequence([ |
67 | 59 | [["->", null]], |
68 | 60 | [[/[\w]+/, "variable"]] |
69 | | - ]); |
| 61 | + ], closing); |
70 | 62 | } |
71 | 63 | return "variable-2"; |
72 | 64 | } |
73 | 65 |
|
| 66 | + var escaped = false; |
74 | 67 | // Normal string |
75 | | - while ( |
76 | | - !stream.eol() && |
77 | | - (!stream.match("{$", false)) && |
78 | | - (!stream.match(/(\$[a-zA-Z_][a-zA-Z0-9_]*|\$\{)/, false) || escaped) |
79 | | - ) { |
80 | | - next = stream.next(); |
81 | | - if (!escaped && next == '"') { end = true; break; } |
82 | | - escaped = !escaped && next == "\\"; |
83 | | - } |
84 | | - if (end) { |
85 | | - state.tokenize = null; |
86 | | - state.phpEncapsStack.pop(); |
| 68 | + while (!stream.eol() && |
| 69 | + (escaped || (!stream.match("{$", false) && |
| 70 | + !stream.match(/^(\$[a-zA-Z_][a-zA-Z0-9_]*|\$\{)/, false)))) { |
| 71 | + if (!escaped && stream.match(closing)) { |
| 72 | + state.tokenize = null; |
| 73 | + state.tokStack.pop(); state.tokStack.pop(); |
| 74 | + break; |
| 75 | + } |
| 76 | + escaped = stream.next() == "\\" && !escaped; |
87 | 77 | } |
88 | 78 | return "string"; |
89 | 79 | } |
|
115 | 105 | "<": function(stream, state) { |
116 | 106 | if (stream.match(/<</)) { |
117 | 107 | stream.eatWhile(/[\w\.]/); |
118 | | - state.tokenize = heredoc(stream.current().slice(3)); |
119 | | - return state.tokenize(stream, state); |
| 108 | + var delim = stream.current().slice(3); |
| 109 | + if (delim) { |
| 110 | + (state.tokStack || (state.tokStack = [])).push(delim, 0); |
| 111 | + state.tokenize = stringWithEscapes(delim); |
| 112 | + return "string"; |
| 113 | + } |
120 | 114 | } |
121 | 115 | return false; |
122 | 116 | }, |
|
131 | 125 | } |
132 | 126 | return false; |
133 | 127 | }, |
134 | | - '"': function(stream, state) { |
135 | | - if (!state.phpEncapsStack) |
136 | | - state.phpEncapsStack = []; |
137 | | - state.phpEncapsStack.push(0); |
138 | | - state.tokenize = stringWithEscapes; |
139 | | - return state.tokenize(stream, state); |
| 128 | + '"': function(_stream, state) { |
| 129 | + (state.tokStack || (state.tokStack = [])).push('"', 0); |
| 130 | + state.tokenize = stringWithEscapes('"'); |
| 131 | + return "string"; |
140 | 132 | }, |
141 | 133 | "{": function(_stream, state) { |
142 | | - if (state.phpEncapsStack && state.phpEncapsStack.length > 0) |
143 | | - state.phpEncapsStack[state.phpEncapsStack.length - 1]++; |
| 134 | + if (state.tokStack && state.tokStack.length) |
| 135 | + state.tokStack[state.tokStack.length - 1]++; |
144 | 136 | return false; |
145 | 137 | }, |
146 | 138 | "}": function(_stream, state) { |
147 | | - if (state.phpEncapsStack && state.phpEncapsStack.length > 0) |
148 | | - if (--state.phpEncapsStack[state.phpEncapsStack.length - 1] == 0) |
149 | | - state.tokenize = stringWithEscapes; |
| 139 | + if (state.tokStack && state.tokStack.length > 0 && |
| 140 | + !--state.tokStack[state.tokStack.length - 1]) { |
| 141 | + state.tokenize = stringWithEscapes(state.tokStack[state.tokStack.length - 2]); |
| 142 | + } |
150 | 143 | return false; |
151 | 144 | } |
152 | 145 | } |
|
0 commit comments