~thon/thon

ref: c5ed5b1cb60a11eed7efc361f6e99e40ea6fe483 thon/parse/parse.sml -rw-r--r-- 1.4 KiB
c5ed5b1cEvan Bergeron Renames 9 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
structure A = Ast

signature PARSE = sig
  val parse : string -> Ast.exp
  val parseFile : string -> Ast.exp
end

structure Parse :> PARSE =
struct

  structure ThonLrVals = ThonLrValsFn (structure Token = LrParser.Token)
  structure ThonLex = ThonLexFn (structure Tokens = ThonLrVals.Tokens)
  structure ThonParse = Join
      (structure ParserData = ThonLrVals.ParserData
       structure Lex = ThonLex
       structure LrParser = LrParser)

  fun invoke lexstream =
      let fun print_error (s,i:int,_) =
              TextIO.output(TextIO.stdOut,
                            "Error, line " ^ (Int.toString i) ^ ", " ^ s ^ "\n")
       in ThonParse.parse(15,lexstream,print_error,())
      end

  fun parse' instream readline = let
      val inputFn = if not readline then TextIO.input else
                    (fn _ => case TextIO.inputLine instream of SOME s => s | _ => "")
      fun parseerror (s, i, p2) = TextIO.output(TextIO.stdOut,
                            "Error, line " ^ (Int.toString i) ^ ", " ^ s ^ "\n")
      val lexer = LrParser.Stream.streamify
                       (ThonLex.makeLexer (fn _ => TextIO.input instream))
      val (absyn, _) = ThonParse.parse(100, lexer, parseerror, ())
      in absyn end

  fun parseFile filename = parse' (TextIO.openIn filename) false

  fun parse s = parse' (TextIO.openString s) false

  fun parseIn filename = parse' (TextIO.openIn filename) true

end