adamc@1
|
1 (* Copyright (c) 2008, Adam Chlipala
|
adamc@1
|
2 * All rights reserved.
|
adamc@1
|
3 *
|
adamc@1
|
4 * Redistribution and use in source and binary forms, with or without
|
adamc@1
|
5 * modification, are permitted provided that the following conditions are met:
|
adamc@1
|
6 *
|
adamc@1
|
7 * - Redistributions of source code must retain the above copyright notice,
|
adamc@1
|
8 * this list of conditions and the following disclaimer.
|
adamc@1
|
9 * - Redistributions in binary form must reproduce the above copyright notice,
|
adamc@1
|
10 * this list of conditions and the following disclaimer in the documentation
|
adamc@1
|
11 * and/or other materials provided with the distribution.
|
adamc@1
|
12 * - The names of contributors may not be used to endorse or promote products
|
adamc@1
|
13 * derived from this software without specific prior written permission.
|
adamc@1
|
14 *
|
adamc@1
|
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
adamc@1
|
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
adamc@1
|
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
adamc@1
|
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
adamc@1
|
19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
adamc@1
|
20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
adamc@1
|
21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
adamc@1
|
22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
adamc@1
|
23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
adamc@1
|
24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
adamc@1
|
25 * POSSIBILITY OF SUCH DAMAGE.
|
adamc@1
|
26 *)
|
adamc@1
|
27
|
adamc@1
|
28 (* Laconic/Web language parser *)
|
adamc@1
|
29
|
adamc@1
|
30 structure Compiler :> COMPILER = struct
|
adamc@1
|
31
|
adamc@1
|
32 structure LacwebLrVals = LacwebLrValsFn(structure Token = LrParser.Token)
|
adamc@1
|
33 structure Lex = LacwebLexFn(structure Tokens = LacwebLrVals.Tokens)
|
adamc@1
|
34 structure LacwebP = Join(structure ParserData = LacwebLrVals.ParserData
|
adamc@1
|
35 structure Lex = Lex
|
adamc@1
|
36 structure LrParser = LrParser)
|
adamc@1
|
37
|
adamc@1
|
38 (* The main parsing routine *)
|
adamc@1
|
39 fun parse filename =
|
adamc@1
|
40 let
|
adamc@1
|
41 val () = (ErrorMsg.resetErrors ();
|
adamc@1
|
42 ErrorMsg.resetPositioning filename)
|
adamc@1
|
43 val file = TextIO.openIn filename
|
adamc@1
|
44 fun get _ = TextIO.input file
|
adamc@1
|
45 fun parseerror (s, p1, p2) = ErrorMsg.errorAt' (p1, p2) s
|
adamc@1
|
46 val lexer = LrParser.Stream.streamify (Lex.makeLexer get)
|
adamc@1
|
47 val (absyn, _) = LacwebP.parse (30, lexer, parseerror, ())
|
adamc@1
|
48 in
|
adamc@1
|
49 TextIO.closeIn file;
|
adamc@5
|
50 if ErrorMsg.anyErrors () then
|
adamc@5
|
51 NONE
|
adamc@5
|
52 else
|
adamc@5
|
53 SOME absyn
|
adamc@1
|
54 end
|
adamc@1
|
55 handle LrParser.ParseError => NONE
|
adamc@1
|
56
|
adamc@5
|
57 fun elaborate env filename =
|
adamc@5
|
58 case parse filename of
|
adamc@5
|
59 NONE => NONE
|
adamc@5
|
60 | SOME file =>
|
adamc@5
|
61 let
|
adamc@5
|
62 val out = Elaborate.elabFile env file
|
adamc@5
|
63 in
|
adamc@5
|
64 if ErrorMsg.anyErrors () then
|
adamc@5
|
65 NONE
|
adamc@5
|
66 else
|
adamc@5
|
67 SOME out
|
adamc@5
|
68 end
|
adamc@16
|
69
|
adamc@16
|
70 fun corify eenv cenv filename =
|
adamc@16
|
71 case elaborate eenv filename of
|
adamc@16
|
72 NONE => NONE
|
adamc@25
|
73 | SOME (_, file) =>
|
adamc@25
|
74 if ErrorMsg.anyErrors () then
|
adamc@25
|
75 NONE
|
adamc@25
|
76 else
|
adamc@25
|
77 SOME (Corify.corify file)
|
adamc@5
|
78
|
adamc@20
|
79 fun reduce eenv cenv filename =
|
adamc@20
|
80 case corify eenv cenv filename of
|
adamc@20
|
81 NONE => NONE
|
adamc@25
|
82 | SOME file =>
|
adamc@25
|
83 if ErrorMsg.anyErrors () then
|
adamc@25
|
84 NONE
|
adamc@25
|
85 else
|
adamc@25
|
86 SOME (Reduce.reduce (Shake.shake file))
|
adamc@20
|
87
|
adamc@23
|
88 fun shake eenv cenv filename =
|
adamc@23
|
89 case reduce eenv cenv filename of
|
adamc@23
|
90 NONE => NONE
|
adamc@25
|
91 | SOME file =>
|
adamc@25
|
92 if ErrorMsg.anyErrors () then
|
adamc@25
|
93 NONE
|
adamc@25
|
94 else
|
adamc@25
|
95 SOME (Shake.shake file)
|
adamc@25
|
96
|
adamc@25
|
97 fun monoize eenv cenv filename =
|
adamc@25
|
98 case shake eenv cenv filename of
|
adamc@25
|
99 NONE => NONE
|
adamc@25
|
100 | SOME file =>
|
adamc@25
|
101 if ErrorMsg.anyErrors () then
|
adamc@25
|
102 NONE
|
adamc@25
|
103 else
|
adamc@25
|
104 SOME (Monoize.monoize cenv file)
|
adamc@23
|
105
|
adamc@1
|
106 fun testParse filename =
|
adamc@1
|
107 case parse filename of
|
adamc@5
|
108 NONE => print "Failed\n"
|
adamc@1
|
109 | SOME file =>
|
adamc@5
|
110 (Print.print (SourcePrint.p_file file);
|
adamc@5
|
111 print "\n")
|
adamc@5
|
112
|
adamc@5
|
113 fun testElaborate filename =
|
adamc@14
|
114 (case elaborate ElabEnv.basis filename of
|
adamc@5
|
115 NONE => print "Failed\n"
|
adamc@5
|
116 | SOME (_, file) =>
|
adamc@14
|
117 (Print.print (ElabPrint.p_file ElabEnv.basis file);
|
adamc@5
|
118 print "\n"))
|
adamc@5
|
119 handle ElabEnv.UnboundNamed n =>
|
adamc@5
|
120 print ("Unbound named " ^ Int.toString n ^ "\n")
|
adamc@1
|
121
|
adamc@16
|
122 fun testCorify filename =
|
adamc@16
|
123 (case corify ElabEnv.basis CoreEnv.basis filename of
|
adamc@16
|
124 NONE => print "Failed\n"
|
adamc@16
|
125 | SOME file =>
|
adamc@16
|
126 (Print.print (CorePrint.p_file CoreEnv.basis file);
|
adamc@16
|
127 print "\n"))
|
adamc@16
|
128 handle CoreEnv.UnboundNamed n =>
|
adamc@16
|
129 print ("Unbound named " ^ Int.toString n ^ "\n")
|
adamc@16
|
130
|
adamc@20
|
131 fun testReduce filename =
|
adamc@20
|
132 (case reduce ElabEnv.basis CoreEnv.basis filename of
|
adamc@20
|
133 NONE => print "Failed\n"
|
adamc@20
|
134 | SOME file =>
|
adamc@20
|
135 (Print.print (CorePrint.p_file CoreEnv.basis file);
|
adamc@20
|
136 print "\n"))
|
adamc@20
|
137 handle CoreEnv.UnboundNamed n =>
|
adamc@20
|
138 print ("Unbound named " ^ Int.toString n ^ "\n")
|
adamc@20
|
139
|
adamc@23
|
140 fun testShake filename =
|
adamc@23
|
141 (case shake ElabEnv.basis CoreEnv.basis filename of
|
adamc@23
|
142 NONE => print "Failed\n"
|
adamc@23
|
143 | SOME file =>
|
adamc@23
|
144 (Print.print (CorePrint.p_file CoreEnv.basis file);
|
adamc@23
|
145 print "\n"))
|
adamc@23
|
146 handle CoreEnv.UnboundNamed n =>
|
adamc@23
|
147 print ("Unbound named " ^ Int.toString n ^ "\n")
|
adamc@23
|
148
|
adamc@25
|
149 fun testMonoize filename =
|
adamc@25
|
150 (case monoize ElabEnv.basis CoreEnv.basis filename of
|
adamc@25
|
151 NONE => print "Failed\n"
|
adamc@25
|
152 | SOME file =>
|
adamc@25
|
153 (Print.print (MonoPrint.p_file MonoEnv.basis file);
|
adamc@25
|
154 print "\n"))
|
adamc@25
|
155 handle MonoEnv.UnboundNamed n =>
|
adamc@25
|
156 print ("Unbound named " ^ Int.toString n ^ "\n")
|
adamc@25
|
157
|
adamc@1
|
158 end
|