@@ -86,6 +86,7 @@ bool TryPeek(int forward)
8686 {
8787 break ;
8888 }
89+
8990 isVerbatim = true ;
9091 }
9192 else
@@ -115,6 +116,7 @@ bool TryPeek(int forward)
115116 continue ;
116117 }
117118 }
119+
118120 if ( inToken )
119121 {
120122 _tokenContent . Append ( source [ _pos ] ) ;
@@ -143,57 +145,98 @@ bool TryPeek(int forward)
143145 phrase = phrase . Substring ( 0 , phrase . Length - 1 ) ;
144146 }
145147
146- yield return new PhraseInvocation
148+ yield return Unescape ( new PhraseInvocation
147149 {
148150 Phrase = phrase ,
149151 Row = row ,
150152 StartChar = startChar ,
151153 EndChar = _pos ,
152154 IsEscaped = isEscaped ,
153155 StringContainer = _stringContainer
154- } ;
156+ } , isVerbatim ) ;
155157 inToken = false ;
156158 }
157159 }
158160 else
159161 {
160162 var _tail = source [ _pos - 1 ] ;
161163 var _peek = source [ _pos + 1 ] ;
162- if ( source [ _pos ] == _stringContainer && _peek != _stringContainer && _tail != _stringContainer )
164+ if ( source [ _pos ] == _stringContainer && _peek != _stringContainer &&
165+ _tail != _stringContainer )
163166 {
164- var phrase = _tokenContent . ToString ( ) . Replace ( "\n " , "\\ n" ) . Replace ( "\r " , "" ) . Replace ( "\" \" " , "\\ \" " ) ;
167+ var phrase = _tokenContent . ToString ( ) . Replace ( "\n " , "\\ n" ) . Replace ( "\r " , "" )
168+ . Replace ( "\" \" " , "\\ \" " ) ;
165169
166170 // Hoppa över sista \ om den är escape:ad
167171 if ( isEscaped )
168172 {
169173 phrase = phrase . Substring ( 0 , phrase . Length - 1 ) ;
170174 }
171175
172- yield return new PhraseInvocation
176+ yield return Unescape ( new PhraseInvocation
173177 {
174178 Phrase = phrase ,
175179 Row = row ,
176180 StartChar = startChar ,
177181 EndChar = _pos ,
178182 StringContainer = _stringContainer
179- } ;
183+ } , isVerbatim ) ;
180184 inToken = false ;
181185 }
182186 }
183-
184- if ( inToken && ! isVerbatim && source [ _pos ] == '\\ ' && TryPeek ( 1 ) && source [ _pos + 1 ] == 'n' )
185- {
186- _pos ++ ;
187- _tokenContent . Append ( '\n ' ) ;
188- }
189- else
190- {
191- _tokenContent . Append ( source [ _pos ] ) ;
192- }
187+ _tokenContent . Append ( source [ _pos ] ) ;
193188 }
189+
194190 break ;
195191 }
196192 }
197193 }
194+
195+ private PhraseInvocation Unescape ( PhraseInvocation phraseInvocation , bool isVerbatim )
196+ {
197+ if ( isVerbatim )
198+ {
199+ for ( var i = 0 ; i < phraseInvocation . Phrase . Length ; i ++ )
200+ {
201+ if ( phraseInvocation . Phrase [ i ] == '\\ ' && i + 1 < phraseInvocation . Phrase . Length )
202+ {
203+ if ( phraseInvocation . Phrase [ i + 1 ] == phraseInvocation . StringContainer )
204+ {
205+ phraseInvocation . Phrase = phraseInvocation . Phrase . Remove ( i , 1 ) ;
206+ }
207+ }
208+ }
209+ }
210+ else
211+ {
212+ for ( var i = 0 ; i < phraseInvocation . Phrase . Length ; i ++ )
213+ {
214+ if ( phraseInvocation . Phrase [ i ] == '\\ ' && i + 1 < phraseInvocation . Phrase . Length )
215+ {
216+ switch ( phraseInvocation . Phrase [ i + 1 ] )
217+ {
218+ case 'n' :
219+ phraseInvocation . Phrase = phraseInvocation . Phrase . Remove ( i , 2 ) . Insert ( i , "\n " ) ;
220+ break ;
221+ case 't' :
222+ phraseInvocation . Phrase = phraseInvocation . Phrase . Remove ( i , 2 ) . Insert ( i , "\t " ) ;
223+ break ;
224+ case '\\ ' :
225+ phraseInvocation . Phrase = phraseInvocation . Phrase . Remove ( i , 2 ) . Insert ( i , "\\ " ) ;
226+ break ;
227+ default :
228+ if ( phraseInvocation . Phrase [ i + 1 ] == phraseInvocation . StringContainer )
229+ {
230+ phraseInvocation . Phrase = phraseInvocation . Phrase . Remove ( i , 1 ) ;
231+ }
232+
233+ break ;
234+ }
235+ }
236+ }
237+ }
238+
239+ return phraseInvocation ;
240+ }
198241 }
199242}
0 commit comments