]>
Commit | Line | Data |
---|---|---|
6ade8b0a | 1 | (* L3 Compiler |
12aa4087 JW |
2 | * Parsing |
3 | * Author: Kaustuv Chaudhuri <kaustuv+@cs.cmu.edu> | |
4 | * Modified: Frank Pfenning <fp@cs.cmu.edu> | |
5 | * | |
6 | * Glueing together the pieces produced by ML-Lex and ML-Yacc | |
7 | *) | |
8 | ||
9 | signature PARSE = | |
10 | sig | |
11 | (* parse filename = ast | |
12 | * will raise ErrorMsg.Error in case of lexing or parsing error | |
13 | *) | |
14 | val parse : string -> Ast.program | |
15 | end | |
16 | ||
17 | structure Parse :> PARSE = | |
18 | struct | |
19 | ||
6ade8b0a JW |
20 | structure L3LrVals = L3LrValsFn (structure Token = LrParser.Token) |
21 | structure L3Lex = L3LexFn (structure Tokens = L3LrVals.Tokens) | |
22 | structure L3Parse = Join (structure ParserData = L3LrVals.ParserData | |
23 | structure Lex = L3Lex | |
12aa4087 JW |
24 | structure LrParser = LrParser) |
25 | ||
26 | (* Main parsing function *) | |
27 | fun parse filename = | |
28 | SafeIO.withOpenIn filename (fn instream => | |
29 | let | |
30 | val _ = ErrorMsg.reset() (* no errors, no messages so far *) | |
31 | val _ = ParseState.setfile filename (* start at position 0 in filename *) | |
32 | fun parseerror (s, p1, p2) = ErrorMsg.error (ParseState.ext (p1,p2)) s | |
33 | val lexer = LrParser.Stream.streamify | |
6ade8b0a | 34 | (L3Lex.makeLexer (fn _ => TextIO.input instream)) |
12aa4087 | 35 | (* 0 = no error correction, 15 = reasonable lookahead for correction *) |
6ade8b0a | 36 | val (absyn, _) = L3Parse.parse(0, lexer, parseerror, ()) |
12aa4087 JW |
37 | val _ = if !ErrorMsg.anyErrors |
38 | then raise ErrorMsg.Error | |
39 | else () | |
40 | in | |
41 | absyn | |
42 | end) | |
0a24e44d | 43 | handle Fail s => ( ErrorMsg.error NONE ("lexer error: "^s) ; |
6ade8b0a | 44 | raise ErrorMsg.Error ) |
12aa4087 JW |
45 | | LrParser.ParseError => raise ErrorMsg.Error (* always preceded by msg *) |
46 | | e as IO.Io _ => ( ErrorMsg.error NONE (exnMessage e); | |
47 | raise ErrorMsg.Error ) | |
48 | ||
49 | end |