Skip to content

Commit 7a49017

Browse files
committed
Change LexBuffer type from char to uint16
1 parent a09bae2 commit 7a49017

File tree

10 files changed

+63
-34
lines changed

10 files changed

+63
-34
lines changed

src/absil/illex.fsl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ open FSharp.Compiler.AbstractIL.Internal.AsciiParser
1616
open FSharp.Compiler.AbstractIL.Internal.AsciiConstants
1717

1818

19-
let lexeme (lexbuf : LexBuffer<char>) = new System.String(lexbuf.Lexeme)
19+
let lexeme (lexbuf : LexBuffer<_>) = LexBuffer<_>.LexemeString (lexbuf)
2020

2121
let unexpectedChar _lexbuf =
2222
raise Parsing.RecoverableParseError ;;

src/fsharp/UnicodeLexing.fs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@ open System.IO
1212

1313
open Internal.Utilities.Text.Lexing
1414

15-
type Lexbuf = LexBuffer<char>
15+
type Lexbuf = LexBuffer<LexBufferChar>
1616

17-
let StringAsLexbuf (s:string) : Lexbuf =
18-
LexBuffer<_>.FromChars (s.ToCharArray())
17+
let StringAsLexbuf =
18+
Lexbuf.FromString
1919

20-
let FunctionAsLexbuf (bufferFiller: char[] * int * int -> int) : Lexbuf =
21-
LexBuffer<_>.FromFunction bufferFiller
20+
let FunctionAsLexbuf =
21+
Lexbuf.FromFunction
2222

2323
// The choice of 60 retries times 50 ms is not arbitrary. The NTFS FILETIME structure
2424
// uses 2 second resolution for LastWriteTime. We retry long enough to surpass this threshold
@@ -65,5 +65,5 @@ let UnicodeFileAsLexbuf (filename,codePage : int option, retryLocked:bool) : Le
6565
else
6666
reraise()
6767
let source = getSource 0
68-
let lexbuf = LexBuffer<_>.FromChars(source.ToCharArray())
68+
let lexbuf = LexBuffer<_>.FromString (source)
6969
lexbuf

src/fsharp/UnicodeLexing.fsi

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ module internal FSharp.Compiler.UnicodeLexing
55
open Microsoft.FSharp.Text
66
open Internal.Utilities.Text.Lexing
77

8-
type Lexbuf = LexBuffer<char>
8+
type Lexbuf = LexBuffer<LexBufferChar>
99
val internal StringAsLexbuf : string -> Lexbuf
10-
val public FunctionAsLexbuf : (char [] * int * int -> int) -> Lexbuf
10+
val public FunctionAsLexbuf : (LexBufferChar[] * int * int -> int) -> Lexbuf
1111
val public UnicodeFileAsLexbuf :string * int option * (*retryLocked*) bool -> Lexbuf

src/fsharp/fsi/fsi.fs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1735,7 +1735,7 @@ type internal FsiStdinLexerProvider
17351735

17361736
let LexbufFromLineReader (fsiStdinSyphon: FsiStdinSyphon) readf =
17371737
UnicodeLexing.FunctionAsLexbuf
1738-
(fun (buf: char[], start, len) ->
1738+
(fun (buf, start, len) ->
17391739
//fprintf fsiConsoleOutput.Out "Calling ReadLine\n"
17401740
let inputOption = try Some(readf()) with :? EndOfStreamException -> None
17411741
inputOption |> Option.iter (fun t -> fsiStdinSyphon.Add (t + "\n"))
@@ -1749,7 +1749,11 @@ type internal FsiStdinLexerProvider
17491749
if ninput > len then fprintf fsiConsoleOutput.Error "%s" (FSIstrings.SR.fsiLineTooLong())
17501750
let ntrimmed = min len ninput
17511751
for i = 0 to ntrimmed-1 do
1752+
#if FABLE_COMPILER
1753+
buf.[i+start] <- uint16 input.[i]
1754+
#else
17521755
buf.[i+start] <- input.[i]
1756+
#endif
17531757
ntrimmed
17541758
)
17551759

src/fsharp/lex.fsl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ let shouldStartFile args lexbuf (m:range) err tok =
152152
else tok
153153

154154
let evalIfDefExpression startPos args (lookup:string->bool) (lexed:string) =
155-
let lexbuf = LexBuffer<char>.FromChars (lexed.ToCharArray ())
155+
let lexbuf = LexBuffer<_>.FromString (lexed)
156156
lexbuf.StartPos <- startPos
157157
lexbuf.EndPos <- startPos
158158
let tokenStream = FSharp.Compiler.PPLexer.tokenstream args

src/fsharp/service/ServiceLexing.fsi

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace FSharp.Compiler.SourceCodeServices
44

55
open FSharp.Compiler
6+
open Internal.Utilities.Text.Lexing
67

78
type Position = int * int
89
type Range = Position * Position
@@ -237,7 +238,7 @@ type FSharpLineTokenizer =
237238
type FSharpSourceTokenizer =
238239
new : conditionalDefines:string list * fileName:string option -> FSharpSourceTokenizer
239240
member CreateLineTokenizer : lineText:string -> FSharpLineTokenizer
240-
member CreateBufferTokenizer : bufferFiller:(char[] * int * int -> int) -> FSharpLineTokenizer
241+
member CreateBufferTokenizer : bufferFiller:(LexBufferChar[] * int * int -> int) -> FSharpLineTokenizer
241242

242243
module internal TestExpose =
243244
val TokenInfo : Parser.token -> (FSharpTokenColorKind * FSharpTokenCharKind * FSharpTokenTriggerClass)

src/utils/prim-lexing.fs

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ namespace Internal.Utilities.Text.Lexing
119119
and set b = endPos <- b
120120

121121
member lexbuf.Lexeme = Array.sub buffer bufferScanStart lexemeLength
122+
member lexbuf.LexemeChar(n) = buffer.[n+bufferScanStart]
122123

123124
member lexbuf.BufferLocalStore = (context :> IDictionary<_,_>)
124125
member lexbuf.LexemeLength with get() : int = lexemeLength and set v = lexemeLength <- v
@@ -128,8 +129,14 @@ namespace Internal.Utilities.Text.Lexing
128129
member lexbuf.BufferScanStart with get() : int = bufferScanStart and set v = bufferScanStart <- v
129130
member lexbuf.BufferAcceptAction with get() = bufferAcceptAction and set v = bufferAcceptAction <- v
130131
member lexbuf.RefillBuffer () = filler lexbuf
131-
static member LexemeString(lexbuf:LexBuffer<char>) =
132-
new System.String(lexbuf.Buffer,lexbuf.BufferScanStart,lexbuf.LexemeLength)
132+
133+
static member LexemeString (lexbuf: LexBuffer<LexBufferChar>) =
134+
#if FABLE_COMPILER
135+
let chars = Array.init lexbuf.LexemeLength (lexbuf.LexemeChar >> char)
136+
new System.String(chars)
137+
#else
138+
new System.String(lexbuf.Buffer, lexbuf.BufferScanStart, lexbuf.LexemeLength)
139+
#endif
133140

134141
member lexbuf.IsPastEndOfStream
135142
with get() = eof
@@ -167,18 +174,30 @@ namespace Internal.Utilities.Text.Lexing
167174
let buffer = Array.copy s
168175
LexBuffer<'Char>.FromArrayNoCopy buffer
169176

170-
// Important: This method takes ownership of the array
171-
static member FromChars (arr:char[]) = LexBuffer.FromArrayNoCopy arr
177+
static member FromString (s: string) =
178+
#if FABLE_COMPILER
179+
let arr = Array.init s.Length (fun i -> uint16 s.[i])
180+
LexBuffer.FromArrayNoCopy arr
181+
#else
182+
LexBuffer.FromArrayNoCopy (s.ToCharArray())
183+
#endif
184+
185+
#if FABLE_COMPILER
186+
and internal LexBufferChar = uint16
187+
#else
188+
and internal LexBufferChar = char
189+
#endif
190+
172191

173192
module GenericImplFragments =
174-
let startInterpret(lexBuffer:LexBuffer<char>)=
193+
let startInterpret(lexBuffer:LexBuffer<LexBufferChar>)=
175194
lexBuffer.BufferScanStart <- lexBuffer.BufferScanStart + lexBuffer.LexemeLength;
176195
lexBuffer.BufferMaxScanLength <- lexBuffer.BufferMaxScanLength - lexBuffer.LexemeLength;
177196
lexBuffer.BufferScanLength <- 0;
178197
lexBuffer.LexemeLength <- 0;
179198
lexBuffer.BufferAcceptAction <- -1;
180199

181-
let afterRefill (trans: uint16[][],sentinel,lexBuffer:LexBuffer<char>,scanUntilSentinel,endOfScan,state,eofPos) =
200+
let afterRefill (trans: uint16[][],sentinel,lexBuffer:LexBuffer<LexBufferChar>,scanUntilSentinel,endOfScan,state,eofPos) =
182201
// end of file occurs if we couldn't extend the buffer
183202
if lexBuffer.BufferScanLength = lexBuffer.BufferMaxScanLength then
184203
let snew = int trans.[state].[eofPos] // == EOF
@@ -192,9 +211,9 @@ namespace Internal.Utilities.Text.Lexing
192211
else
193212
scanUntilSentinel lexBuffer state
194213

195-
let onAccept (lexBuffer:LexBuffer<char>,a) =
196-
lexBuffer.LexemeLength <- lexBuffer.BufferScanLength;
197-
lexBuffer.BufferAcceptAction <- a;
214+
let onAccept (lexBuffer:LexBuffer<LexBufferChar>, a) =
215+
lexBuffer.LexemeLength <- lexBuffer.BufferScanLength
216+
lexBuffer.BufferAcceptAction <- a
198217

199218
open GenericImplFragments
200219

@@ -221,15 +240,15 @@ namespace Internal.Utilities.Text.Lexing
221240
let baseForUnicodeCategories = numLowUnicodeChars+numSpecificUnicodeChars*2
222241
let unicodeCategory =
223242
#if FX_RESHAPED_GLOBALIZATION
224-
System.Globalization.CharUnicodeInfo.GetUnicodeCategory(inp)
243+
System.Globalization.CharUnicodeInfo.GetUnicodeCategory(char inp)
225244
#else
226-
System.Char.GetUnicodeCategory(inp)
245+
System.Char.GetUnicodeCategory(char inp)
227246
#endif
228247
//System.Console.WriteLine("inp = {0}, unicodeCategory = {1}", [| box inp; box unicodeCategory |]);
229248
int trans.[state].[baseForUnicodeCategories + int32 unicodeCategory]
230249
else
231250
// This is the specific unicode character
232-
let c = char (int trans.[state].[baseForSpecificUnicodeChars+i*2])
251+
let c = trans.[state].[baseForSpecificUnicodeChars+i*2]
233252
//System.Console.WriteLine("c = {0}, inp = {1}, i = {2}", [| box c; box inp; box i |]);
234253
// OK, have we found the entry for a specific unicode character?
235254
if c = inp
@@ -252,7 +271,7 @@ namespace Internal.Utilities.Text.Lexing
252271
afterRefill (trans,sentinel,lexBuffer,scanUntilSentinel,lexBuffer.EndOfScan,state,eofPos)
253272
else
254273
// read a character - end the scan if there are no further transitions
255-
let inp = lexBuffer.Buffer.[lexBuffer.BufferScanPos]
274+
let inp = uint16 lexBuffer.Buffer.[lexBuffer.BufferScanPos]
256275

257276
// Find the new state
258277
let snew = lookupUnicodeCharacters state inp
@@ -270,7 +289,7 @@ namespace Internal.Utilities.Text.Lexing
270289
// 30 entries, one for each UnicodeCategory
271290
// 1 entry for EOF
272291

273-
member tables.Interpret(initialState,lexBuffer : LexBuffer<char>) =
292+
member tables.Interpret(initialState, lexBuffer: LexBuffer<LexBufferChar>) =
274293
startInterpret(lexBuffer)
275294
scanUntilSentinel lexBuffer initialState
276295

src/utils/prim-lexing.fsi

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,21 +71,26 @@ type internal LexBuffer<'Char> =
7171
member Lexeme: 'Char []
7272

7373
/// Fast helper to turn the matched characters into a string, avoiding an intermediate array.
74-
static member LexemeString : LexBuffer<char> -> string
74+
static member LexemeString : LexBuffer<LexBufferChar> -> string
7575

7676
/// Dynamically typed, non-lexically scoped parameter table.
7777
member BufferLocalStore : IDictionary<string,obj>
7878

7979
/// True if the refill of the buffer ever failed , or if explicitly set to True.
8080
member IsPastEndOfStream: bool with get,set
8181

82-
/// Create a lex buffer suitable for Unicode lexing that reads characters from the given array.
83-
/// Important: does take ownership of the array.
84-
static member FromChars: char[] -> LexBuffer<char>
82+
/// Create a lex buffer suitable for Unicode lexing that reads characters from the given string.
83+
static member FromString: string -> LexBuffer<LexBufferChar>
8584

8685
/// Create a lex buffer that reads character or byte inputs by using the given function.
8786
static member FromFunction: ('Char[] * int * int -> int) -> LexBuffer<'Char>
8887

88+
#if FABLE_COMPILER
89+
and internal LexBufferChar = uint16
90+
#else
91+
and internal LexBufferChar = char
92+
#endif
93+
8994
/// The type of tables for an unicode lexer generated by <c>fslex.exe</c>.
9095
[<Sealed>]
9196
type internal UnicodeTables =
@@ -94,5 +99,5 @@ type internal UnicodeTables =
9499
static member Create : uint16[][] * uint16[] -> UnicodeTables
95100

96101
/// Interpret tables for a unicode lexer generated by <c>fslex.exe</c>.
97-
member Interpret: initialState:int * LexBuffer<char> -> int
102+
member Interpret: initialState:int * LexBuffer<LexBufferChar> -> int
98103

src/utils/prim-parsing.fs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ exception RecoverableParseError
1414
exception Accept of obj
1515

1616
[<Sealed>]
17-
type internal IParseState(ruleStartPoss:Position[], ruleEndPoss:Position[], lhsPos:Position[], ruleValues:obj[], lexbuf:LexBuffer<char>) =
17+
type internal IParseState(ruleStartPoss:Position[], ruleEndPoss:Position[], lhsPos:Position[], ruleValues:obj[], lexbuf:LexBuffer<LexBufferChar>) =
1818
member p.LexBuffer = lexbuf
1919

2020
member p.InputRange n = ruleStartPoss.[n-1], ruleEndPoss.[n-1]

src/utils/prim-parsing.fsi

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ type internal IParseState =
3737
member RaiseError<'b> : unit -> 'b
3838

3939
/// Return the LexBuffer for this parser instance.
40-
member LexBuffer : LexBuffer<char>
40+
member LexBuffer : LexBuffer<LexBufferChar>
4141

4242

4343
[<Sealed>]
@@ -118,7 +118,7 @@ type internal Tables<'tok> =
118118

119119
/// Interpret the parser table taking input from the given lexer, using the given lex buffer, and the given start state.
120120
/// Returns an object indicating the final synthesized value for the parse.
121-
member Interpret : lexer:(LexBuffer<char> -> 'tok) * lexbuf:LexBuffer<char> * startState:int -> obj
121+
member Interpret : lexer:(LexBuffer<LexBufferChar> -> 'tok) * lexbuf:LexBuffer<LexBufferChar> * startState:int -> obj
122122

123123
/// Indicates an accept action has occurred.
124124
exception internal Accept of obj

0 commit comments

Comments
 (0)