@@ -31,6 +31,9 @@ type ISourceText =
3131type StringText ( str : string ) =
3232
3333 let getLines ( str : string ) =
34+ #if FABLE_ COMPILER
35+ System.Text.RegularExpressions.Regex.Split( str, " \r\n |\r |\n " );
36+ #else
3437 use reader = new StringReader( str)
3538 [|
3639 let mutable line = reader.ReadLine()
@@ -42,6 +45,7 @@ type StringText(str: string) =
4245 // http://stackoverflow.com/questions/19365404/stringreader-omits-trailing-linebreak
4346 yield String.Empty
4447 |]
48+ #endif
4549
4650 let getLines =
4751 // This requires allocating and getting all the lines.
@@ -169,6 +173,12 @@ namespace Internal.Utilities.Text.Lexing
169173 0 ,
170174 0 )
171175
176+ #if FABLE_ COMPILER
177+ type internal LexBufferChar = uint16
178+ #else
179+ type internal LexBufferChar = char
180+ #endif
181+
172182 type internal LexBufferFiller < 'Char > = ( LexBuffer< 'Char> -> unit)
173183
174184 and [<Sealed>]
@@ -217,6 +227,8 @@ namespace Internal.Utilities.Text.Lexing
217227 and set b = endPos <- b
218228
219229 member lexbuf.Lexeme = Array.sub buffer bufferScanStart lexemeLength
230+ member lexbuf.LexemeChar ( n ) = buffer.[ n+ bufferScanStart]
231+
220232 member lexbuf.BufferLocalStore = ( context :> IDictionary<_,_>)
221233 member lexbuf.LexemeLength with get() : int = lexemeLength and set v = lexemeLength <- v
222234 member lexbuf.Buffer with get() : 'Char [] = buffer and set v = buffer <- v
@@ -225,8 +237,14 @@ namespace Internal.Utilities.Text.Lexing
225237 member lexbuf.BufferScanStart with get() : int = bufferScanStart and set v = bufferScanStart <- v
226238 member lexbuf.BufferAcceptAction with get() = bufferAcceptAction and set v = bufferAcceptAction <- v
227239 member lexbuf.RefillBuffer () = filler lexbuf
228- static member LexemeString ( lexbuf : LexBuffer < char >) =
229- new System.String( lexbuf.Buffer, lexbuf.BufferScanStart, lexbuf.LexemeLength)
240+
241+ static member LexemeString ( lexbuf : LexBuffer < LexBufferChar >) =
242+ #if FABLE_ COMPILER
243+ let chars = Array.init lexbuf.LexemeLength ( lexbuf.LexemeChar >> char)
244+ new System.String( chars)
245+ #else
246+ new System.String( lexbuf.Buffer, lexbuf.BufferScanStart, lexbuf.LexemeLength)
247+ #endif
230248
231249 member lexbuf.IsPastEndOfStream
232250 with get() = eof
@@ -266,9 +284,13 @@ namespace Internal.Utilities.Text.Lexing
266284 LexBuffer< 'Char>. FromArrayNoCopy( supportsFeature, buffer)
267285
268286 // Important: This method takes ownership of the array
269- static member FromChars ( supportsFeature : LanguageFeature -> bool , arr : char []) = LexBuffer.FromArrayNoCopy ( supportsFeature, arr)
287+ static member FromChars ( supportsFeature : LanguageFeature -> bool , arr : LexBufferChar []) = LexBuffer.FromArrayNoCopy ( supportsFeature, arr)
270288
271289 static member FromSourceText ( supportsFeature : LanguageFeature -> bool , sourceText : ISourceText ) =
290+ #if FABLE_ COMPILER
291+ let arr = Array.init sourceText.Length ( fun i -> uint16 ( sourceText.Item i))
292+ LexBuffer.FromArrayNoCopy ( supportsFeature, arr)
293+ #else
272294 let mutable currentSourceIndex = 0
273295 LexBuffer< char>. FromFunction( supportsFeature, fun ( chars , start , length ) ->
274296 let lengthToCopy =
@@ -283,16 +305,25 @@ namespace Internal.Utilities.Text.Lexing
283305 currentSourceIndex <- currentSourceIndex + lengthToCopy
284306 lengthToCopy
285307 )
308+ #endif
309+
310+ static member FromString ( supportsFeature : LanguageFeature -> bool , s : string ) =
311+ #if FABLE_ COMPILER
312+ let arr = Array.init s.Length ( fun i -> uint16 s.[ i])
313+ LexBuffer.FromArrayNoCopy ( supportsFeature, arr)
314+ #else
315+ LexBuffer.FromArrayNoCopy ( supportsFeature, s.ToCharArray())
316+ #endif
286317
287318 module GenericImplFragments =
288- let startInterpret ( lexBuffer : LexBuffer < char >) =
319+ let startInterpret ( lexBuffer : LexBuffer < LexBufferChar >) =
289320 lexBuffer.BufferScanStart <- lexBuffer.BufferScanStart + lexBuffer.LexemeLength;
290321 lexBuffer.BufferMaxScanLength <- lexBuffer.BufferMaxScanLength - lexBuffer.LexemeLength;
291322 lexBuffer.BufferScanLength <- 0 ;
292323 lexBuffer.LexemeLength <- 0 ;
293324 lexBuffer.BufferAcceptAction <- - 1 ;
294325
295- let afterRefill ( trans : uint16 [][], sentinel , lexBuffer : LexBuffer < char >, scanUntilSentinel , endOfScan , state , eofPos ) =
326+ let afterRefill ( trans : uint16 [][], sentinel , lexBuffer : LexBuffer < LexBufferChar >, scanUntilSentinel , endOfScan , state , eofPos ) =
296327 // end of file occurs if we couldn't extend the buffer
297328 if lexBuffer.BufferScanLength = lexBuffer.BufferMaxScanLength then
298329 let snew = int trans.[ state].[ eofPos] // == EOF
@@ -306,9 +337,9 @@ namespace Internal.Utilities.Text.Lexing
306337 else
307338 scanUntilSentinel lexBuffer state
308339
309- let onAccept ( lexBuffer : LexBuffer < char >, a ) =
310- lexBuffer.LexemeLength <- lexBuffer.BufferScanLength;
311- lexBuffer.BufferAcceptAction <- a;
340+ let onAccept ( lexBuffer : LexBuffer < LexBufferChar >, a ) =
341+ lexBuffer.LexemeLength <- lexBuffer.BufferScanLength
342+ lexBuffer.BufferAcceptAction <- a
312343
313344 open GenericImplFragments
314345
@@ -333,12 +364,16 @@ namespace Internal.Utilities.Text.Lexing
333364 // ways
334365 let baseForUnicodeCategories = numLowUnicodeChars+ numSpecificUnicodeChars* 2
335366 let unicodeCategory =
367+ #if FABLE_ COMPILER
368+ System.Char.GetUnicodeCategory( char inp)
369+ #else
336370 System.Char.GetUnicodeCategory( inp)
371+ #endif
337372 //System.Console.WriteLine("inp = {0}, unicodeCategory = {1}", [| box inp; box unicodeCategory |]);
338373 int trans.[ state].[ baseForUnicodeCategories + int32 unicodeCategory]
339374 else
340375 // This is the specific unicode character
341- let c = char ( int trans.[ state].[ baseForSpecificUnicodeChars+ i* 2 ])
376+ let c = trans.[ state].[ baseForSpecificUnicodeChars+ i* 2 ]
342377 //System.Console.WriteLine("c = {0}, inp = {1}, i = {2}", [| box c; box inp; box i |]);
343378 // OK, have we found the entry for a specific unicode character?
344379 if c = inp
@@ -360,7 +395,7 @@ namespace Internal.Utilities.Text.Lexing
360395 afterRefill ( trans, sentinel, lexBuffer, scanUntilSentinel, lexBuffer.EndOfScan, state, eofPos)
361396 else
362397 // read a character - end the scan if there are no further transitions
363- let inp = lexBuffer.Buffer.[ lexBuffer.BufferScanPos]
398+ let inp = uint16 lexBuffer.Buffer.[ lexBuffer.BufferScanPos]
364399
365400 // Find the new state
366401 let snew = lookupUnicodeCharacters state inp
@@ -378,7 +413,7 @@ namespace Internal.Utilities.Text.Lexing
378413 // 30 entries, one for each UnicodeCategory
379414 // 1 entry for EOF
380415
381- member tables.Interpret ( initialState , lexBuffer : LexBuffer < char >) =
416+ member tables.Interpret ( initialState , lexBuffer : LexBuffer < LexBufferChar >) =
382417 startInterpret( lexBuffer)
383418 scanUntilSentinel lexBuffer initialState
384419
0 commit comments