Debian Codegraph @

lib/mllpt-lib/ml-lpt/ml-ulex/BackEnds/SML/sml-tbl-output.sml

raw
1 (* sml-tbl-output.sml
2 *
3 * COPYRIGHT (c) 2005
4 * John Reppy (http://www.cs.uchicago.edu/~jhr)
5 * Aaron Turon ([email protected])
6 * All rights reserved.
7 *
8 * Code generation for SML, using a transition table
9 *)
10
11 structure SMLTblOutput : OUTPUT =
12 struct
13
14 open SMLOutputSupport
15
16 (* generate code for an individual state in the table *)
17 fun mkState actionVec s = let
18 val LO.State {id, startState, label, final, next} = s
19 fun w2s w =
20 if !Options.lexCompat
21 then "#\"" ^ (Char.toString o Char.chr o Word.toInt) w ^ "\""
22 else "0w" ^ Word.fmt StringCvt.DEC w
23 val ASCII = SIS.interval (0w0, 0w255)
24 fun mkTrans (set, state) =
25 map (fn (c1, c2) => String.concat [
26 "(", w2s c1, ",",
27 w2s c2, ",",
28 Int.toString (idOf state), ")"])
29 (if !Options.lexCompat then
30 SIS.intervals (SIS.intersect (set, ASCII))
31 else SIS.intervals set)
32 val allTransitions = List.concat (map mkTrans (!next))
33 in
34 String.concat [
35 "([",
36 String.concatWith ",\n" allTransitions,
37 "], [",
38 String.concatWith ", " (map Int.toString final),
39 "])"]
40 end
41
42 fun tableHook spec strm = let
43 val LO.Spec {actions, dfa, startStates, ...} = spec
44 in
45 if !Options.strictSML
46 then TextIO.output (strm, "Vector.fromList [")
47 else TextIO.output (strm, "#[");
48 TextIO.output (strm,
49 String.concatWith ", " (map (mkState actions) dfa));
50 TextIO.output (strm, "]")
51 end
52
53 fun lexerHook spec strm = let
54 val LO.Spec {actions, dfa, startStates, arg, eofRules, ...} = spec
55 fun matchSS (label, state) =
56 (ML_ConPat (label, []),
57 ML_App ("yygo yyactTable ",
58 [ML_Var (Int.toString (idOf state)),
59 ML_RefGet (ML_Var "yystrm"),
60 ML_Var "yyNO_MATCH"]))
61 val innerExp = ML_Case (ML_RefGet (ML_Var "yyss"),
62 List.map matchSS startStates)
63 val eofCheckExp = mkEOF (eofRules, innerExp)
64 val actList = Vector.foldri
65 (fn (i, _, ls) => (ML_Var o actName) i :: ls)
66 [] actions
67 val actTableExp = ML_Let ("yyactTable",
68 ML_App ("Vector.fromList", [ML_List actList]),
69 eofCheckExp)
70 val lexerExp = Vector.foldri mkAction actTableExp actions
71 val ppStrm = TextIOPP.openOut {dst = strm, wid = 80}
72 in
73 ML.ppML (ppStrm, lexerExp)
74 end
75
76 fun output (spec, fname) =
77 ExpandFile.expandTemplate {
78 src = if !Options.lexCompat
79 then lexTemplate else ulexTemplate,
80 dst = fname ^ ".sml",
81 hooks = [("lexer", lexerHook spec),
82 ("startstates", startStatesHook spec),
83 ("userdecls", userDeclsHook spec),
84 ("header", headerHook spec),
85 ("args", argsHook spec),
86 ("pargs", pargsHook spec),
87 ("table", tableHook spec)]
88 }
89
90 end