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 2685 - (view) (download)

1 : jhr 14 (* parser.sml
2 :     *
3 : jhr 435 * COPYRIGHT (c) 2010 The Diderot Project (http://diderot-language.cs.uchicago.edu)
4 : jhr 14 * All rights reserved.
5 :     *
6 :     * Parser glue.
7 :     *)
8 :    
9 :     structure Parser : sig
10 :    
11 :     (* parse a file; return NONE if there are syntax errors *)
12 :     val parseFile : (Error.err_stream * TextIO.instream) -> ParseTree.program option
13 :    
14 :     end = struct
15 :    
16 : jhr 2685 datatype add_or_delete = datatype AntlrRepair.add_or_delete
17 :    
18 : jhr 14 (* 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 2685 (* map tokens to strings; when adding a token, we use a generic name where it makes sense *)
25 :     fun tokToString ADD (DiderotTokens.ID _) = "<identifier>"
26 :     | tokToString DEL (DiderotTokens.ID x) = Atom.toString x
27 :     | tokToString _ (DiderotTokens.STRING s) = concat["\"", String.toCString s, "\""]
28 :     | tokToString ADD (DiderotTokens.FLOAT f) = "<number>"
29 :     | tokToString DEL (DiderotTokens.FLOAT f) = FloatLit.toString f
30 :     | tokToString ADD (DiderotTokens.INT i) = "<integer>"
31 :     | tokToString DEL (DiderotTokens.INT i) = IntInf.toString i
32 :     | tokToString ADD DiderotTokens.KW_vec4 = "<type>"
33 :     | tokToString ADD DiderotTokens.KW_vec3 = "<type>"
34 :     | tokToString ADD DiderotTokens.KW_vec2 = "<type>"
35 :     | tokToString ADD DiderotTokens.KW_real = "<type>"
36 :     | tokToString ADD DiderotTokens.KW_int = "<type>"
37 :     | tokToString ADD DiderotTokens.KW_bool = "<type>"
38 :     | tokToString _ tok = DiderotTokens.toString tok
39 : jhr 1116
40 : jhr 14 (* error function for parsers *)
41 : jhr 1116 val parseErr = Error.parseError tokToString
42 : jhr 14
43 :     (* parse a file, returning a parse tree *)
44 :     fun parseFile (errStrm, file) = let
45 : jhr 2277 fun get () = TextIO.input file
46 :     val lexer = DiderotLex.lex (Error.sourceMap errStrm) (lexErr errStrm)
47 :     in
48 :     case DiderotParser.parse lexer (DiderotLex.streamify get)
49 :     of (SOME pt, _, []) => (TextIO.closeIn file; SOME pt)
50 :     | (_, _, errs) => (
51 :     TextIO.closeIn file;
52 :     List.app (parseErr errStrm) errs;
53 :     NONE)
54 :     (* end case *)
55 :     end
56 : jhr 14
57 :     end

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