SCM Repository
Annotation of /trunk/src/compiler/parser/parser.sml
Parent Directory
|
Revision Log
Revision 3349 - (view) (download)
1 : | jhr | 14 | (* parser.sml |
2 : | * | ||
3 : | jhr | 3349 | * This code is part of the Diderot Project (http://diderot-language.cs.uchicago.edu) |
4 : | * | ||
5 : | * COPYRIGHT (c) 2015 The University of Chicago | ||
6 : | jhr | 14 | * All rights reserved. |
7 : | * | ||
8 : | * Parser glue. | ||
9 : | *) | ||
10 : | |||
11 : | structure Parser : sig | ||
12 : | |||
13 : | (* parse a file; return NONE if there are syntax errors *) | ||
14 : | val parseFile : (Error.err_stream * TextIO.instream) -> ParseTree.program option | ||
15 : | |||
16 : | end = struct | ||
17 : | |||
18 : | (* glue together the lexer and parser *) | ||
19 : | structure DiderotParser = DiderotParseFn(DiderotLex) | ||
20 : | |||
21 : | (* error function for lexers *) | ||
22 : | fun lexErr errStrm (pos, msg) = Error.errorAt(errStrm, (pos, pos), msg) | ||
23 : | |||
24 : | jhr | 1116 | (* map tokens to strings *) |
25 : | fun tokToString (DiderotTokens.ID x) = Atom.toString x | ||
26 : | | tokToString (DiderotTokens.STRING s) = concat["\"", String.toCString s, "\""] | ||
27 : | | tokToString (DiderotTokens.FLOAT f) = FloatLit.toString f | ||
28 : | | tokToString (DiderotTokens.INT i) = IntInf.toString i | ||
29 : | | tokToString tok = DiderotTokens.toString tok | ||
30 : | |||
31 : | jhr | 14 | (* error function for parsers *) |
32 : | jhr | 1116 | val parseErr = Error.parseError tokToString |
33 : | jhr | 14 | |
34 : | (* parse a file, returning a parse tree *) | ||
35 : | fun parseFile (errStrm, file) = let | ||
36 : | jhr | 2356 | fun get () = TextIO.input file |
37 : | val lexer = DiderotLex.lex (Error.sourceMap errStrm) (lexErr errStrm) | ||
38 : | in | ||
39 : | case DiderotParser.parse lexer (DiderotLex.streamify get) | ||
40 : | of (SOME pt, _, []) => (TextIO.closeIn file; SOME pt) | ||
41 : | | (_, _, errs) => ( | ||
42 : | TextIO.closeIn file; | ||
43 : | List.app (parseErr errStrm) errs; | ||
44 : | NONE) | ||
45 : | (* end case *) | ||
46 : | end | ||
47 : | jhr | 14 | |
48 : | end |
root@smlnj-gforge.cs.uchicago.edu | ViewVC Help |
Powered by ViewVC 1.0.0 |