@@ -14,7 +14,7 @@ export function parseText(ctx: ParseContext, start: number): CST.Text {
1414 loop: for ( ; i < ctx . source . length ; ++ i ) {
1515 switch ( ctx . source [ i ] ) {
1616 case '\\' : {
17- const esc = parseEscape ( ctx , 'text' , i ) ;
17+ const esc = parseEscape ( ctx , i ) ;
1818 if ( esc ) {
1919 value += ctx . source . substring ( pos , i ) + esc . value ;
2020 i += esc . length ;
@@ -80,7 +80,7 @@ export function parseQuotedLiteral(
8080 for ( let i = pos ; i < ctx . source . length ; ++ i ) {
8181 switch ( ctx . source [ i ] ) {
8282 case '\\' : {
83- const esc = parseEscape ( ctx , 'literal' , i ) ;
83+ const esc = parseEscape ( ctx , i ) ;
8484 if ( esc ) {
8585 value += ctx . source . substring ( pos , i ) + esc . value ;
8686 i += esc . length ;
@@ -142,54 +142,42 @@ export function parseVariable(
142142
143143function parseEscape (
144144 ctx : ParseContext ,
145- scope : 'text' | 'literal' ,
146145 start : number
147146) : { value : string ; length : number } | null {
148147 const raw = ctx . source [ start + 1 ] ;
149- switch ( raw ) {
150- case '\\' :
151- return { value : raw , length : 1 } ;
152- case '{' :
153- case '}' :
154- if ( scope === 'text' ) return { value : raw , length : 1 } ;
155- break ;
156- case '|' :
157- if ( scope === 'literal' ) return { value : raw , length : 1 } ;
158- break ;
159- default :
160- if ( ctx . resource ) {
161- let hexLen = 0 ;
162- switch ( raw ) {
163- case '\t' :
164- case ' ' :
165- return { value : raw , length : 1 } ;
166- case 'n' :
167- return { value : '\n' , length : 1 } ;
168- case 'r' :
169- return { value : '\r' , length : 1 } ;
170- case 't' :
171- return { value : '\t' , length : 1 } ;
172- case 'u' :
173- hexLen = 4 ;
174- break ;
175- case 'U' :
176- hexLen = 6 ;
177- break ;
178- case 'x' :
179- hexLen = 2 ;
180- break ;
181- }
182- if ( hexLen > 0 ) {
183- const h0 = start + 2 ;
184- const raw = ctx . source . substring ( h0 , h0 + hexLen ) ;
185- if ( raw . length === hexLen && / ^ [ 0 - 9 A - F a - f ] + $ / . test ( raw ) ) {
186- return {
187- value : String . fromCharCode ( parseInt ( raw , 16 ) ) ,
188- length : 1 + hexLen
189- } ;
190- }
191- }
148+ if ( '\\{|}' . includes ( raw ) ) return { value : raw , length : 1 } ;
149+ if ( ctx . resource ) {
150+ let hexLen = 0 ;
151+ switch ( raw ) {
152+ case '\t' :
153+ case ' ' :
154+ return { value : raw , length : 1 } ;
155+ case 'n' :
156+ return { value : '\n' , length : 1 } ;
157+ case 'r' :
158+ return { value : '\r' , length : 1 } ;
159+ case 't' :
160+ return { value : '\t' , length : 1 } ;
161+ case 'u' :
162+ hexLen = 4 ;
163+ break ;
164+ case 'U' :
165+ hexLen = 6 ;
166+ break ;
167+ case 'x' :
168+ hexLen = 2 ;
169+ break ;
170+ }
171+ if ( hexLen > 0 ) {
172+ const h0 = start + 2 ;
173+ const raw = ctx . source . substring ( h0 , h0 + hexLen ) ;
174+ if ( raw . length === hexLen && / ^ [ 0 - 9 A - F a - f ] + $ / . test ( raw ) ) {
175+ return {
176+ value : String . fromCharCode ( parseInt ( raw , 16 ) ) ,
177+ length : 1 + hexLen
178+ } ;
192179 }
180+ }
193181 }
194182 ctx . onError ( 'bad-escape' , start , start + 2 ) ;
195183 return null ;
0 commit comments