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@1
|
50 SOME absyn
|
adamc@1
|
51 end
|
adamc@1
|
52 handle LrParser.ParseError => NONE
|
adamc@1
|
53
|
adamc@1
|
54 fun testParse filename =
|
adamc@1
|
55 case parse filename of
|
adamc@1
|
56 NONE => print "Parse error\n"
|
adamc@1
|
57 | SOME file =>
|
adamc@1
|
58 if ErrorMsg.anyErrors () then
|
adamc@1
|
59 print "Recoverable parse error\n"
|
adamc@1
|
60 else
|
adamc@1
|
61 (Print.print (LaconicPrint.p_file file);
|
adamc@1
|
62 print "\n")
|
adamc@1
|
63
|
adamc@1
|
64 end
|