-
Notifications
You must be signed in to change notification settings - Fork 1
/
parse.sml
67 lines (63 loc) · 2.14 KB
/
parse.sml
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
(* parse.sml
*
* Driver code for MLPolyR's ML-Yacc/ML-Lex-based parser.
*
* Copyright (c) 2005 by Matthias Blume ([email protected])
*)
structure Parse : sig
val parse : string -> Ast.program * Source.inputSource
end = struct
structure MLPolyRLrVals =
MLPolyRLrValsFun (structure Token = LrParser.Token)
structure Lex =
MLPolyRLexFun (structure Tokens = MLPolyRLrVals.Tokens)
structure MLPolyRP =
JoinWithArg (structure ParserData = MLPolyRLrVals.ParserData
structure Lex=Lex
structure LrParser = LrParser)
val errcons = ErrorMsg.defaultConsumer ()
fun parse filename =
let val _ = LVar.reset ()
val _ = Label.reset ()
val file = TextIO.openIn filename
val source = Source.newSource (filename, file, false, errcons)
val sm = #sourceMap source
fun error r m =
ErrorMsg.error source r ErrorMsg.COMPLAIN m
ErrorMsg.nullErrorBody
val depth = ref 0
fun enterC () = depth := !depth + 1
fun leaveC () = let val d = !depth - 1 in depth := d; d = 0 end
fun newline pos = SourceMap.newline sm pos
val curstring = ref []
val startpos = ref 0
val instring = ref false
fun newS sp = (startpos := sp; curstring := []; instring := true)
fun addS c = curstring := c :: !curstring
fun getS ep = (instring := false;
(String.implode (rev (!curstring)), !startpos, ep))
fun handleEof () = let
val pos = SourceMap.lastLinePos sm
in if !depth > 0 then
error (pos, pos) "unexpected end of input in comment"
else if !instring then
error (pos, pos) "unexpected end of input in string literal"
else ();
Source.closeSource source;
pos
end
fun get _ = TextIO.input file
val lexarg =
{ enterC = enterC, leaveC = leaveC,
newS = newS, addS = addS, getS = getS,
handleEof = handleEof,
newline = newline,
error = error }
val lexer = MLPolyRP.makeLexer get lexarg
val (ast, _) = MLPolyRP.parse
(30,lexer,
fn (s, p, p') => error (p, p') s,
())
in (ast, source)
end handle LrParser.ParseError => raise ErrorMsg.Error
end