Home My Page Projects Code Snippets Project Openings diderot
Summary Activity Tracker Tasks SCM

SCM Repository

[diderot] Annotation of /branches/vis12/src/compiler/parser/parser.sml
ViewVC logotype

Annotation of /branches/vis12/src/compiler/parser/parser.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3291 - (view) (download)

1 : jhr 14 (* parser.sml
2 :     *
3 : jhr 3291 * 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 : jhr 2685 datatype add_or_delete = datatype AntlrRepair.add_or_delete
19 :    
20 : jhr 14 (* glue together the lexer and parser *)
21 :     structure DiderotParser = DiderotParseFn(DiderotLex)
22 :    
23 :     (* error function for lexers *)
24 :     fun lexErr errStrm (pos, msg) = Error.errorAt(errStrm, (pos, pos), msg)
25 :    
26 : jhr 2685 (* map tokens to strings; when adding a token, we use a generic name where it makes sense *)
27 :     fun tokToString ADD (DiderotTokens.ID _) = "<identifier>"
28 :     | tokToString DEL (DiderotTokens.ID x) = Atom.toString x
29 :     | tokToString _ (DiderotTokens.STRING s) = concat["\"", String.toCString s, "\""]
30 :     | tokToString ADD (DiderotTokens.FLOAT f) = "<number>"
31 :     | tokToString DEL (DiderotTokens.FLOAT f) = FloatLit.toString f
32 :     | tokToString ADD (DiderotTokens.INT i) = "<integer>"
33 :     | tokToString DEL (DiderotTokens.INT i) = IntInf.toString i
34 :     | tokToString ADD DiderotTokens.KW_vec4 = "<type>"
35 :     | tokToString ADD DiderotTokens.KW_vec3 = "<type>"
36 :     | tokToString ADD DiderotTokens.KW_vec2 = "<type>"
37 :     | tokToString ADD DiderotTokens.KW_real = "<type>"
38 :     | tokToString ADD DiderotTokens.KW_int = "<type>"
39 :     | tokToString ADD DiderotTokens.KW_bool = "<type>"
40 :     | tokToString _ tok = DiderotTokens.toString tok
41 : jhr 1116
42 : jhr 14 (* error function for parsers *)
43 : jhr 1116 val parseErr = Error.parseError tokToString
44 : jhr 14
45 :     (* parse a file, returning a parse tree *)
46 :     fun parseFile (errStrm, file) = let
47 : jhr 2277 fun get () = TextIO.input file
48 :     val lexer = DiderotLex.lex (Error.sourceMap errStrm) (lexErr errStrm)
49 :     in
50 :     case DiderotParser.parse lexer (DiderotLex.streamify get)
51 :     of (SOME pt, _, []) => (TextIO.closeIn file; SOME pt)
52 :     | (_, _, errs) => (
53 :     TextIO.closeIn file;
54 :     List.app (parseErr errStrm) errs;
55 :     NONE)
56 :     (* end case *)
57 :     end
58 : jhr 14
59 :     end

root@smlnj-gforge.cs.uchicago.edu
ViewVC Help
Powered by ViewVC 1.0.0