home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / sml_nj / 93src.lha / src / util / source.sml < prev    next >
Encoding:
C/C++ Source or Header  |  1993-01-29  |  1.3 KB  |  45 lines

  1. (* source.sml *)
  2.  
  3. structure Source : SOURCE =
  4. struct
  5.  
  6.   (* character position in a file *)
  7.   type charpos = int
  8.  
  9.   (* region in a file delineated by two character positions, start and end
  10.      respectively. *)
  11.   type region = charpos * charpos
  12.  
  13.   type inputSource =
  14.          {fileName: string,
  15.       linePos: int list ref,
  16.       lineNum: int ref,
  17.       anyErrors: bool ref,
  18.       errConsumer: PrettyPrint.ppconsumer,
  19.       interactive: bool,
  20.       sourceStream: instream, 
  21.       indexStream: outstream option}
  22.  
  23.   fun newSource(fileName,lineNum,sourceStream,interactive,
  24.         errConsumer,indexStream) =
  25.       {fileName=fileName,sourceStream=sourceStream,interactive=interactive,
  26.        errConsumer=errConsumer,linePos=ref[1],lineNum=ref lineNum,
  27.        anyErrors=ref false, indexStream=indexStream}
  28.  
  29.   fun closeSource({fileName,sourceStream,interactive,
  30.             errConsumer,indexStream,...}: inputSource): unit =
  31.       (if interactive then () 
  32.        else close_in sourceStream handle Io _ => ();
  33.        case indexStream
  34.     of SOME f => (close_out f handle Io _ => ())
  35.      | NONE => ())
  36.  
  37.   fun filepos({fileName,linePos,lineNum,...}: inputSource) p =
  38.       let fun look(p:int,a::rest,n) = 
  39.           if a<p then (fileName,n,p-a) else look(p,rest,n-1)
  40.         | look _ = (fileName,0,0)
  41.        in look(p,!linePos,!lineNum)
  42.       end
  43.  
  44. end (* structure Source *)
  45.