Skip to content

Commit bd3bb77

Browse files
committed
Change LexBuffer type to uint16
1 parent 90415c1 commit bd3bb77

File tree

10 files changed

+65
-35
lines changed

10 files changed

+65
-35
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 Microsoft.FSharp.Compiler.AbstractIL.Internal.AsciiParser
1616
open Microsoft.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
#if !FABLE_COMPILER
2424

@@ -67,7 +67,7 @@ let UnicodeFileAsLexbuf (filename,codePage : int option, retryLocked:bool) : Le
6767
else
6868
reraise()
6969
let source = getSource 0
70-
let lexbuf = LexBuffer<_>.FromChars(source.ToCharArray())
70+
let lexbuf = LexBuffer<_>.FromString (source)
7171
lexbuf
7272

7373
#endif

src/fsharp/UnicodeLexing.fsi

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ module internal Microsoft.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

1212
#if !FABLE_COMPILER
1313
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
@@ -161,7 +161,7 @@ let shouldStartFile args lexbuf (m:range) err tok =
161161
else tok
162162

163163
let evalIfDefExpression startPos args (lookup:string->bool) (lexed:string) =
164-
let lexbuf = LexBuffer<char>.FromChars (lexed.ToCharArray ())
164+
let lexbuf = LexBuffer<_>.FromString (lexed)
165165
lexbuf.StartPos <- startPos
166166
lexbuf.EndPos <- startPos
167167
let tokenStream = Microsoft.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 Microsoft.FSharp.Compiler.SourceCodeServices
44

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

78
type Position = int * int
89
type Range = Position * Position
@@ -227,7 +228,7 @@ type FSharpLineTokenizer =
227228
type FSharpSourceTokenizer =
228229
new : conditionalDefines:string list * fileName:string option -> FSharpSourceTokenizer
229230
member CreateLineTokenizer : lineText:string -> FSharpLineTokenizer
230-
member CreateBufferTokenizer : bufferFiller:(char[] * int * int -> int) -> FSharpLineTokenizer
231+
member CreateBufferTokenizer : bufferFiller:(LexBufferChar[] * int * int -> int) -> FSharpLineTokenizer
231232

232233

233234
module internal TestExpose =

src/utils/prim-lexing.fs

Lines changed: 34 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ namespace Internal.Utilities.Text.Lexing
121121
and set b = endPos <- b
122122

123123
member lexbuf.Lexeme = Array.sub buffer bufferScanStart lexemeLength
124+
member lexbuf.LexemeChar(n) = buffer.[n+bufferScanStart]
124125

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

136143
member lexbuf.IsPastEndOfStream
137144
with get() = eof
@@ -169,18 +176,30 @@ namespace Internal.Utilities.Text.Lexing
169176
let buffer = Array.copy s
170177
LexBuffer<'Char>.FromArrayNoCopy buffer
171178

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

175-
module internal GenericImplFragments =
176-
let startInterpret(lexBuffer:LexBuffer<char>)=
194+
module GenericImplFragments =
195+
let startInterpret(lexBuffer:LexBuffer<LexBufferChar>) =
177196
lexBuffer.BufferScanStart <- lexBuffer.BufferScanStart + lexBuffer.LexemeLength;
178197
lexBuffer.BufferMaxScanLength <- lexBuffer.BufferMaxScanLength - lexBuffer.LexemeLength;
179198
lexBuffer.BufferScanLength <- 0;
180199
lexBuffer.LexemeLength <- 0;
181200
lexBuffer.BufferAcceptAction <- -1;
182201

183-
let afterRefill (trans: uint16[][],sentinel,lexBuffer:LexBuffer<char>,scanUntilSentinel,endOfScan,state,eofPos) =
202+
let afterRefill (trans: uint16[][],sentinel,lexBuffer:LexBuffer<LexBufferChar>,scanUntilSentinel,endOfScan,state,eofPos) =
184203
// end of file occurs if we couldn't extend the buffer
185204
if lexBuffer.BufferScanLength = lexBuffer.BufferMaxScanLength then
186205
let snew = int trans.[state].[eofPos] // == EOF
@@ -194,9 +213,9 @@ namespace Internal.Utilities.Text.Lexing
194213
else
195214
scanUntilSentinel lexBuffer state
196215

197-
let onAccept (lexBuffer:LexBuffer<char>,a) =
198-
lexBuffer.LexemeLength <- lexBuffer.BufferScanLength;
199-
lexBuffer.BufferAcceptAction <- a;
216+
let onAccept (lexBuffer:LexBuffer<LexBufferChar>, a) =
217+
lexBuffer.LexemeLength <- lexBuffer.BufferScanLength
218+
lexBuffer.BufferAcceptAction <- a
200219

201220
open GenericImplFragments
202221

@@ -223,15 +242,15 @@ namespace Internal.Utilities.Text.Lexing
223242
let baseForUnicodeCategories = numLowUnicodeChars+numSpecificUnicodeChars*2
224243
let unicodeCategory =
225244
#if FX_RESHAPED_GLOBALIZATION
226-
System.Globalization.CharUnicodeInfo.GetUnicodeCategory(inp)
245+
System.Globalization.CharUnicodeInfo.GetUnicodeCategory(char inp)
227246
#else
228-
System.Char.GetUnicodeCategory(inp)
247+
System.Char.GetUnicodeCategory(char inp)
229248
#endif
230249
//System.Console.WriteLine("inp = {0}, unicodeCategory = {1}", [| box inp; box unicodeCategory |]);
231250
int trans.[state].[baseForUnicodeCategories + int32 unicodeCategory]
232251
else
233252
// This is the specific unicode character
234-
let c = char (int trans.[state].[baseForSpecificUnicodeChars+i*2])
253+
let c = trans.[state].[baseForSpecificUnicodeChars+i*2]
235254
//System.Console.WriteLine("c = {0}, inp = {1}, i = {2}", [| box c; box inp; box i |]);
236255
// OK, have we found the entry for a specific unicode character?
237256
if c = inp
@@ -254,7 +273,7 @@ namespace Internal.Utilities.Text.Lexing
254273
afterRefill (trans,sentinel,lexBuffer,scanUntilSentinel,lexBuffer.EndOfScan,state,eofPos)
255274
else
256275
// read a character - end the scan if there are no further transitions
257-
let inp = lexBuffer.Buffer.[lexBuffer.BufferScanPos]
276+
let inp = uint16 lexBuffer.Buffer.[lexBuffer.BufferScanPos]
258277

259278
// Find the new state
260279
let snew = lookupUnicodeCharacters state inp
@@ -272,7 +291,7 @@ namespace Internal.Utilities.Text.Lexing
272291
// 30 entries, one for each UnicodeCategory
273292
// 1 entry for EOF
274293

275-
member tables.Interpret(initialState,lexBuffer : LexBuffer<char>) =
294+
member tables.Interpret(initialState, lexBuffer: LexBuffer<LexBufferChar>) =
276295
startInterpret(lexBuffer)
277296
scanUntilSentinel lexBuffer initialState
278297

src/utils/prim-lexing.fsi

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,24 +56,30 @@ type internal LexBuffer<'Char> =
5656
member Lexeme: 'Char []
5757

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

6161
/// Dynamically typed, non-lexically scoped parameter table.
6262
member BufferLocalStore : IDictionary<string,obj>
6363

6464
/// True if the refill of the buffer ever failed , or if explicitly set to True.
6565
member IsPastEndOfStream: bool with get,set
6666

67-
/// Create a lex buffer suitable for Unicode lexing that reads characters from the given array.
68-
/// Important: does take ownership of the array.
69-
static member FromChars: char[] -> LexBuffer<char>
67+
/// Create a lex buffer suitable for Unicode lexing that reads characters from the given string.
68+
static member FromString: string -> LexBuffer<LexBufferChar>
69+
7070
/// Create a lex buffer that reads character or byte inputs by using the given function.
7171
static member FromFunction: ('Char[] * int * int -> int) -> LexBuffer<'Char>
7272

73+
#if FABLE_COMPILER
74+
and internal LexBufferChar = uint16
75+
#else
76+
and internal LexBufferChar = char
77+
#endif
78+
7379
/// The type of tables for an unicode lexer generated by <c>fslex.exe</c>.
7480
[<Sealed>]
7581
type internal UnicodeTables =
7682
static member Create : uint16[][] * uint16[] -> UnicodeTables
7783
/// Interpret tables for a unicode lexer generated by <c>fslex.exe</c>.
78-
member Interpret: initialState:int * LexBuffer<char> -> int
84+
member Interpret: initialState:int * LexBuffer<LexBufferChar> -> int
7985

src/utils/prim-parsing.fs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ exception RecoverableParseError
1111
exception Accept of obj
1212

1313
[<Sealed>]
14-
type internal IParseState(ruleStartPoss:Position[],ruleEndPoss:Position[],lhsPos:Position[],ruleValues:obj[],lexbuf:LexBuffer<char>) =
14+
type internal IParseState(ruleStartPoss:Position[],ruleEndPoss:Position[],lhsPos:Position[],ruleValues:obj[],lexbuf:LexBuffer<LexBufferChar>) =
1515
member p.LexBuffer = lexbuf
1616
member p.InputRange n = ruleStartPoss.[n-1], ruleEndPoss.[n-1];
1717
member p.InputStartPosition n = ruleStartPoss.[n-1]

src/utils/prim-parsing.fsi

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ type internal IParseState =
3636
/// Raise an error in this parse context.
3737
member RaiseError<'b> : unit -> 'b
3838
/// Return the LexBuffer for this parser instance.
39-
member LexBuffer : LexBuffer<char>
39+
member LexBuffer : LexBuffer<LexBufferChar>
4040

4141

4242
[<Sealed>]
@@ -96,7 +96,7 @@ type internal Tables<'tok> =
9696

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

101101
/// Indicates an accept action has occurred.
102102
exception internal Accept of obj

0 commit comments

Comments
 (0)