Skip to content

Commit cdf9e34

Browse files
committed
thread through changes for Ranges through all projects
1 parent b617c9d commit cdf9e34

File tree

14 files changed

+320
-282
lines changed

14 files changed

+320
-282
lines changed

src/FsLex.Core/fslexlex.fs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -579,7 +579,7 @@ and code p buff lexbuf =
579579
match _fslex_tables.Interpret(28,lexbuf) with
580580
| 0 -> (
581581
# 155 "fslexlex.fsl"
582-
CODE (buff.ToString(), p)
582+
CODE (buff.ToString(), { startPos = p; endPos = lexbuf.EndPos })
583583
# 583 "fslexlex.fs"
584584
)
585585
| 1 -> (

src/FsLex.Core/fslexpars.fs

Lines changed: 141 additions & 128 deletions
Large diffs are not rendered by default.

src/FsLex.Core/fslexpars.fsi

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ type tokenId =
5353
type nonTerminalId =
5454
| NONTERM__startspec
5555
| NONTERM_spec
56+
| NONTERM_ident
5657
| NONTERM_codeopt
5758
| NONTERM_Macros
5859
| NONTERM_macro

src/FsLexYacc.Runtime/Lexing.fsi

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ type Position =
7171
static member FirstLine : filename:string -> Position
7272

7373
type [<Struct>] Range = { startPos: Position; endPos: Position }
74+
with static member Empty: Range
7475

7576
[<Sealed>]
7677
/// Input buffers consumed by lexers generated by <c>fslex.exe </c>

src/FsYacc.Core/fsyaccast.fs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,8 @@ let ProcessParserSpecAst (spec: ParserSpec) =
116116
match sym with
117117
| NonTerminal nt ->
118118
checkNonTerminal nt
119-
| Terminal (name, range) as t ->
120-
if not (IsTerminal t) then failwith (sprintf "token %s is not declared" name)
119+
| Terminal t ->
120+
if not (IsTerminal t) then failwith (sprintf "token %s is not declared" (fst t))
121121

122122
if spec.StartSymbols = [] then (failwith "at least one start declaration is required");
123123

src/FsYacc.Core/fsyaccdriver.fs

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,8 @@ type Writer(outputFileName, outputFileInterface) =
102102

103103
member x.WriteUInt16 (i: int) = fprintf os "%dus;" i
104104

105-
member x.WriteCode (code, pos) =
106-
x.WriteLine "# %d \"%s\"" pos.pos_lnum pos.pos_fname
105+
member x.WriteCode (code, range: Range) =
106+
x.WriteLine "# %d \"%s\"" range.startPos.pos_lnum range.startPos.pos_fname
107107
x.WriteLine "%s" code
108108
let codeLines = code.Replace("\r","").Split([| '\n' |]).Length
109109
outputLineCount <- outputLineCount + codeLines
@@ -131,7 +131,7 @@ type Writer(outputFileName, outputFileInterface) =
131131

132132

133133
// This is to avoid name conflicts against keywords.
134-
let generic_nt_name nt = "'gentype_" + nt
134+
let generic_nt_name (nt, _range) = "'gentype_" + nt
135135
let anyMarker = 0xffff
136136

137137
let actionCoding =
@@ -215,7 +215,7 @@ let writeSpecToFile (generatorState: GeneratorState) (spec: ParserSpec) (compile
215215

216216
writer.WriteLine "type token = ";
217217
writer.WriteLineInterface "type token = ";
218-
for id,typ in spec.Tokens do
218+
for (id, _range), typ in spec.Tokens do
219219
match typ with
220220
| None ->
221221
writer.WriteLine " | %s" id
@@ -228,7 +228,7 @@ let writeSpecToFile (generatorState: GeneratorState) (spec: ParserSpec) (compile
228228
writer.WriteLine "// This type is used to give symbolic names to token indexes, useful for error messages";
229229
writer.WriteLine "type tokenId = ";
230230
writer.WriteLineInterface "type tokenId = ";
231-
for id,typ in spec.Tokens do
231+
for (id, _range), typ in spec.Tokens do
232232
writer.WriteLine " | TOKEN_%s" id;
233233
writer.WriteLineInterface " | TOKEN_%s" id;
234234
writer.WriteLine " | TOKEN_end_of_input";
@@ -239,7 +239,7 @@ let writeSpecToFile (generatorState: GeneratorState) (spec: ParserSpec) (compile
239239
writer.WriteLine "// This type is used to give symbolic names to token indexes, useful for error messages";
240240
writer.WriteLine "type nonTerminalId = ";
241241
writer.WriteLineInterface "type nonTerminalId = ";
242-
for nt in compiledSpec.nonTerminals do
242+
for (nt, _range) in compiledSpec.nonTerminals do
243243
writer.WriteLine " | NONTERM_%s" nt;
244244
writer.WriteLineInterface " | NONTERM_%s" nt;
245245

@@ -248,7 +248,7 @@ let writeSpecToFile (generatorState: GeneratorState) (spec: ParserSpec) (compile
248248
writer.WriteLine "// This function maps tokens to integer indexes";
249249
writer.WriteLine "let tagOfToken (t:token) = ";
250250
writer.WriteLine " match t with";
251-
spec.Tokens |> List.iteri (fun i (id,typ) ->
251+
spec.Tokens |> List.iteri (fun i ((id, _range), typ) ->
252252
writer.WriteLine " | %s %s -> %d " id (match typ with Some _ -> "_" | None -> "") i);
253253
writer.WriteLineInterface "/// This function maps tokens to integer indexes";
254254
writer.WriteLineInterface "val tagOfToken: token -> int";
@@ -257,7 +257,7 @@ let writeSpecToFile (generatorState: GeneratorState) (spec: ParserSpec) (compile
257257
writer.WriteLine "// This function maps integer indexes to symbolic token ids";
258258
writer.WriteLine "let tokenTagToTokenId (tokenIdx:int) = ";
259259
writer.WriteLine " match tokenIdx with";
260-
spec.Tokens |> List.iteri (fun i (id,typ) -> writer.WriteLine " | %d -> TOKEN_%s " i id)
260+
spec.Tokens |> List.iteri (fun i ((id, _range), typ) -> writer.WriteLine " | %d -> TOKEN_%s " i id)
261261
writer.WriteLine " | %d -> TOKEN_end_of_input" compiledSpec.endOfInputTerminalIdx;
262262
writer.WriteLine " | %d -> TOKEN_error" compiledSpec.errorTerminalIdx;
263263
writer.WriteLine " | _ -> failwith \"tokenTagToTokenId: bad token\""
@@ -270,7 +270,7 @@ let writeSpecToFile (generatorState: GeneratorState) (spec: ParserSpec) (compile
270270
writer.WriteLine "/// This function maps production indexes returned in syntax errors to strings representing the non terminal that would be produced by that production";
271271
writer.WriteLine "let prodIdxToNonTerminal (prodIdx:int) = ";
272272
writer.WriteLine " match prodIdx with";
273-
compiledSpec.prods |> Array.iteri (fun i (nt,ntIdx,syms,code) -> writer.WriteLine " | %d -> NONTERM_%s " i nt);
273+
compiledSpec.prods |> Array.iteri (fun i ((nt, _range), ntIdx, syms, code) -> writer.WriteLine " | %d -> NONTERM_%s " i nt);
274274
writer.WriteLine " | _ -> failwith \"prodIdxToNonTerminal: bad production index\""
275275

276276
writer.WriteLineInterface "";
@@ -284,7 +284,7 @@ let writeSpecToFile (generatorState: GeneratorState) (spec: ParserSpec) (compile
284284
writer.WriteLine "// This function gets the name of a token as a string";
285285
writer.WriteLine "let token_to_string (t:token) = ";
286286
writer.WriteLine " match t with ";
287-
spec.Tokens |> List.iteri (fun i (id,typ) -> writer.WriteLine " | %s %s -> \"%s\" " id (match typ with Some _ -> "_" | None -> "") id);
287+
spec.Tokens |> List.iteri (fun i ((id, _range), typ) -> writer.WriteLine " | %s %s -> \"%s\" " id (match typ with Some _ -> "_" | None -> "") id);
288288

289289
writer.WriteLineInterface "";
290290
writer.WriteLineInterface "/// This function gets the name of a token as a string";
@@ -295,22 +295,22 @@ let writeSpecToFile (generatorState: GeneratorState) (spec: ParserSpec) (compile
295295
writer.WriteLine "let _fsyacc_dataOfToken (t:token) = ";
296296
writer.WriteLine " match t with ";
297297

298-
for (id,typ) in spec.Tokens do
298+
for ((id, _range), typ) in spec.Tokens do
299299
writer.WriteLine " | %s %s -> %s "
300300
id
301301
(match typ with Some _ -> "_fsyacc_x" | None -> "")
302302
(match typ with Some _ -> "Microsoft.FSharp.Core.Operators.box _fsyacc_x" | None -> "(null : System.Object)")
303303

304304
let tychar = "'cty"
305305

306-
for (key,_) in spec.Types |> Seq.countBy fst |> Seq.filter (fun (_,n) -> n > 1) do
306+
for ((key, _range), _) in spec.Types |> Seq.countBy fst |> Seq.filter (fun (_,n) -> n > 1) do
307307
failwithf "%s is given multiple %%type declarations" key;
308308

309-
for (key,_) in spec.Tokens |> Seq.countBy fst |> Seq.filter (fun (_,n) -> n > 1) do
309+
for ((key, _range), _) in spec.Tokens |> Seq.countBy fst |> Seq.filter (fun (_,n) -> n > 1) do
310310
failwithf "%s is given %%token declarations" key
311311

312-
let types = Map.ofList spec.Types
313-
let tokens = Map.ofList spec.Tokens
312+
let types = Map.ofList (spec.Types |> List.map (fun ((name, _range), rest) -> name, rest))
313+
let tokens = Map.ofList (spec.Tokens |> List.map (fun ((name, _range), rest) -> name, rest))
314314

315315
let nStates = compiledSpec.states.Length
316316
begin
@@ -457,7 +457,11 @@ let writeSpecToFile (generatorState: GeneratorState) (spec: ParserSpec) (compile
457457
writer.WriteLine "|]" ;
458458
end;
459459

460-
let getType nt = if types.ContainsKey nt then types.[nt] else generatorState.generate_nonterminal_name nt
460+
let getType ((name, _range) as nt) =
461+
types
462+
|> Map.tryFind name
463+
|> Option.defaultWith (fun _ -> generatorState.generate_nonterminal_name nt)
464+
461465
begin
462466
writer.Write "let _fsyacc_reductions () =" ;
463467
writer.WriteLine " [| " ;
@@ -469,10 +473,7 @@ let writeSpecToFile (generatorState: GeneratorState) (spec: ParserSpec) (compile
469473
syms |> List.iteri (fun i sym ->
470474
let tyopt =
471475
match sym with
472-
| Terminal t ->
473-
if tokens.ContainsKey t then
474-
tokens.[t]
475-
else None
476+
| Terminal (name, range) -> tokens |> Map.tryFind name |> Option.flatten
476477
| NonTerminal nt -> Some (getType nt)
477478
match tyopt with
478479
| Some ty -> writer.WriteLine " let _%d = parseState.GetInput(%d) :?> %s in" (i+1) (i+1) ty
@@ -481,7 +482,7 @@ let writeSpecToFile (generatorState: GeneratorState) (spec: ParserSpec) (compile
481482
writer.WriteLine " (";
482483
writer.WriteLine " (";
483484
match code with
484-
| Some (_,pos) -> writer.WriteLine "# %d \"%s\"" pos.pos_lnum pos.pos_fname
485+
| Some (_,{ startPos = pos }) -> writer.WriteLine "# %d \"%s\"" pos.pos_lnum pos.pos_fname
485486
| None -> ()
486487
match code with
487488
| Some (code,_) ->
@@ -500,9 +501,9 @@ let writeSpecToFile (generatorState: GeneratorState) (spec: ParserSpec) (compile
500501
writer.WriteLine " )";
501502
// Place the line count back for the type constraint
502503
match code with
503-
| Some (_,pos) -> writer.WriteLine "# %d \"%s\"" pos.pos_lnum pos.pos_fname
504+
| Some (_, { startPos = pos }) -> writer.WriteLine "# %d \"%s\"" pos.pos_lnum pos.pos_fname
504505
| None -> ()
505-
writer.WriteLine " : %s));" (if types.ContainsKey nt then types.[nt] else generatorState.generate_nonterminal_name nt);
506+
writer.WriteLine " : %s));" (getType nt);
506507
done;
507508
writer.WriteLine "|]" ;
508509
end;
@@ -530,14 +531,14 @@ let writeSpecToFile (generatorState: GeneratorState) (spec: ParserSpec) (compile
530531
writer.WriteLine " productionToNonTerminalTable = _fsyacc_productionToNonTerminalTable }"
531532
writer.WriteLine "let engine lexer lexbuf startState = tables.Interpret(lexer, lexbuf, startState)"
532533

533-
for (id,startState) in List.zip spec.StartSymbols compiledSpec.startStates do
534+
for ((id, _range), startState) in List.zip spec.StartSymbols compiledSpec.startStates do
534535
if not (types.ContainsKey id) then
535-
failwith ("a %type declaration is required for for start token "+id);
536+
failwith ("a %type declaration is required for for start token "+ id);
536537
let ty = types.[id] in
537538
writer.WriteLine "let %s lexer lexbuf : %s =" id ty;
538539
writer.WriteLine " engine lexer lexbuf %d :?> _" startState
539540

540-
for id in spec.StartSymbols do
541+
for (id, _range) in spec.StartSymbols do
541542
if not (types.ContainsKey id) then
542543
failwith ("a %type declaration is required for start token "+id);
543544
let ty = types.[id] in

src/FsYacc.Core/fsyacclex.fs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -447,7 +447,7 @@ and header p buff lexbuf =
447447
match _fslex_tables.Interpret(42,lexbuf) with
448448
| 0 -> (
449449
# 74 "fsyacclex.fsl"
450-
HEADER (buff.ToString(), p)
450+
HEADER (buff.ToString(), { startPos = p; endPos = lexbuf.EndPos })
451451
# 451 "fsyacclex.fs"
452452
)
453453
| 1 -> (
@@ -499,7 +499,7 @@ and code p buff lexbuf =
499499
match _fslex_tables.Interpret(23,lexbuf) with
500500
| 0 -> (
501501
# 95 "fsyacclex.fsl"
502-
CODE (buff.ToString(), p)
502+
CODE (buff.ToString(), { startPos = p; endPos = lexbuf.EndPos })
503503
# 503 "fsyacclex.fs"
504504
)
505505
| 1 -> (

src/FsYacc.Core/fsyacclex.fsl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ and fs_type = parse
7171
| _ { appendBuf(lexeme lexbuf); fs_type lexbuf }
7272

7373
and header p buff = parse
74-
| "%}" { HEADER (buff.ToString(), p) }
74+
| "%}" { HEADER (buff.ToString(), { startPos = p; endPos = lexbuf.EndPos }) }
7575
| newline { newline lexbuf;
7676
ignore <| buff.Append System.Environment.NewLine;
7777
header p buff lexbuf }
@@ -92,7 +92,7 @@ and header p buff = parse
9292
| _ { ignore <| buff.Append(lexeme lexbuf).[0];
9393
header p buff lexbuf }
9494
and code p buff = parse
95-
| "}" { CODE (buff.ToString(), p) }
95+
| "}" { CODE (buff.ToString(), { startPos = p; endPos = lexbuf.EndPos }) }
9696
| "{" { ignore <| buff.Append (lexeme lexbuf);
9797
ignore(code p buff lexbuf);
9898
ignore <| buff.Append "}";

0 commit comments

Comments
 (0)