1(* Copyright (C) 1999-2002 Henry Cejtin, Matthew Fluet, Suresh
2 *    Jagannathan, and Stephen Weeks.
3 * Copyright (C) 1997-1999 NEC Research Institute.
4 *
5 * MLton is released under a BSD-style license.
6 * Please see the file MLton-LICENSE for license information.
7 *)
8
9structure SourceFile :> SourceFile =
10struct
11
12datatype t = T of {file: string ref,
13                   lineNum: int ref,
14                   lineStart: int ref}
15
16fun getPos (T {file, lineNum, lineStart, ...}, n) =
17   SourcePos.make {column = n - !lineStart,
18                   file = !file,
19                   line = !lineNum}
20
21fun lineStart (s as T {lineStart, ...}) = getPos (s, !lineStart)
22
23val observe_line_directives = ref true
24
25fun new file = T {file = ref file,
26                  lineNum = ref 1,
27                  lineStart = ref 0}
28
29fun newline (T {lineStart, lineNum, ...}, n) =
30   (lineNum := !lineNum + 1
31    ; lineStart := n)
32
33fun lineDirective (src as T {file, lineNum, lineStart},
34                   f,
35                   {lineNum = n, lineStart = s}) =
36    if !observe_line_directives then
37      (Option.app (fn f => file := f) f
38       ; lineNum := n
39       ; lineStart := s)
40    else newline(src,s)
41
42
43
44end;
45