Debian Codegraph @

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

raw
1 (* sml-fun-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 mutually recursive functions for states.
9 *)
10
11 structure SMLFunOutput : OUTPUT =
12 struct
13
14 open SMLOutputSupport
15
16 (* transition interval representation *)
17 datatype transition_interval = TI of SIS.interval * int * ml_exp
18 fun intervalOf (TI (i, t, e)) = i
19 fun tagOf (TI (i, t, e)) = t
20 fun actionOf (TI (i, t, e)) = e
21 fun sameTag (TI (_, t1, _), TI (_, t2, _)) = t1 = t2
22 fun singleton (TI ((i, j), _, _)) = i = j
23
24 (* generate code for transitions: generate a hard-coded binary
25 * search on accepting characters
26 *)
27 fun mkTrans ([], _) = raise Fail "(BUG) SMLFunOutput: alphabet not covered"
28 | mkTrans ([t], _) = actionOf t
29 | mkTrans ([t1, t2], _) =
30 if sameTag (t1, t2) then actionOf t1
31 else let
32 val (_, t1end) = intervalOf t1
33 val (t2start, _) = intervalOf t2
34 in
35 if singleton t1 then
36 ML_If (ML_Cmp (ML.EQ, inpVar, ML_Sym t1end),
37 actionOf t1,
38 actionOf t2)
39 else if singleton t2 then
40 ML_If (ML_Cmp (ML.EQ, inpVar, ML_Sym t2start),
41 actionOf t2,
42 actionOf t1)
43 else
44 ML_If (ML_Cmp (ML.LEQ, inpVar, ML_Sym t1end),
45 actionOf t1,
46 actionOf t2)
47 end
48 | mkTrans (ts, len) = let
49 val lh = len div 2
50 fun split (ls, 0, l1) = (List.rev l1, ls)
51 | split (l::ls, cnt, l1) = split (ls, cnt-1, l::l1)
52 | split _ = raise Fail "(BUG) SMLFunOutput: split failed"
53 val (ts1, ts2) = split (ts, lh, [])
54 val (ts2start, ts2end) = intervalOf (List.hd ts2)
55 val (ts2', ts2len) = if ts2start = ts2end
56 then (List.tl ts2, len - lh - 1)
57 else (ts2, len - lh)
58 (* we want to take advantage of the special case when
59 * len = 3 and hd ts2 is a singleton. this case often
60 * occurs when we have an arrow for a single character.
61 *)
62 val elseClause =
63 if lh = 1 andalso ts2len = 1
64 then mkTrans ([List.hd ts1, List.hd ts2'], 2)
65 else ML_If (ML_Cmp (ML.LT, inpVar, ML_Sym ts2start),
66 mkTrans (ts1, lh),
67 mkTrans (ts2', ts2len))
68 in
69 ML_If (ML_Cmp (ML.EQ, inpVar, ML_Sym ts2start),
70 actionOf (List.hd ts2),
71 elseClause)
72 end
73
74 fun mkState (arg, eofRules, actionVec) (s, k) = let
75 val LO.State {id, startState, label, final, next} = s
76 fun addMatch (i, lastMatch) = let
77 val lastMatch' = if hasREJECT (Vector.sub (actionVec, i))
78 then lastMatch
79 else ML_Var "yyNO_MATCH"
80 in
81 ML_App ("yyMATCH",
82 [ML_Var "strm",
83 ML_Var (actName i),
84 lastMatch'])
85 end
86 val (curMatch, nextMatches) = (case final
87 of [] => (NONE, [])
88 | f::fs => (SOME f, fs)
89 (* end case *))
90 val lastMatch = List.foldr addMatch (ML_Var "lastMatch") nextMatches
91 (* collect all valid transition symbols *)
92 val labels = List.foldl SIS.union SIS.empty (List.map #1 (!next))
93 (* pair transition intervals with associated actions/transitions *)
94 val newFinal = (case curMatch
95 of SOME j => addMatch (j, lastMatch)
96 | NONE => lastMatch
97 (* end case *))
98 fun arrows (syms, s) =
99 mapInt
100 (fn i => TI (i, idOf s,
101 ML_App (nameOf s, [ML_Var "strm'", newFinal])))
102 syms
103 val TIs = List.map arrows (!next)
104 val errAct' =
105 (case curMatch
106 of SOME j =>
107 ML_App (actName j,
108 [ML_Var "strm",
109 if hasREJECT (Vector.sub (actionVec, j))
110 then lastMatch
111 else ML_Var "yyNO_MATCH"])
112 | NONE => ML_App ("yystuck", [lastMatch])
113 (* end case *))
114 (* if start state, check for eof *)
115 val errAct = if startState
116 then mkEOF (eofRules, errAct')
117 else errAct'
118 (* error transitions = complement(valid transitions) *)
119 val error = SIS.complement labels
120 val errTIs = mapInt (fn i => TI (i, ~1, errAct)) error
121 (* the arrows represent intervals that partition the entire
122 * alphabet, with each interval mapped to some transition or
123 * action. we sort the intervals by their smallest member.
124 *)
125 fun gt (a, b) = (#1 (intervalOf a)) > (#1 (intervalOf b))
126 val sorted = ListMergeSort.sort gt (List.concat (errTIs :: TIs))
127 (* now we want to find adjacent partitions with the same
128 * action, and merge their intervals
129 *)
130 fun merge [] = []
131 | merge [t] = [t]
132 | merge (t1::t2::ts) =
133 if sameTag (t1, t2) then let
134 val TI ((i, _), tag, act) = t1
135 val TI ((_, j), _, _ ) = t2
136 val t = TI ((i, j), tag, act)
137 in
138 merge (t::ts)
139 end
140 else
141 t1::(merge (t2::ts))
142 val merged = merge sorted
143 (* create the transition code, which at least has an error transition *)
144 val trans = mkTrans(merged, List.length merged)
145 (* create the input code *)
146 val getInp =
147 ML_Case (ML_App ("yygetc", [ML_Var "strm"]),
148 [(ML_ConPat ("NONE", []), errAct),
149 (ML_ConPat ("SOME", [ML_VarPat (inp ^ ", strm'")]),
150 trans)])
151 in
152 ML_Fun (nameOf s, ["strm", "lastMatch : yymatch"], getInp, k)
153 end
154
155 structure SCC = GraphSCCFn (
156 struct
157 type ord_key = LO.dfa_state
158 fun compare (LO.State{id = id1, ...}, LO.State{id = id2, ...}) =
159 Int.compare (id1, id2)
160 end)
161
162 fun mkStates (arg, eofRules, actions, dfa, startStates, k) = let
163 fun follow (LO.State {next, ...}) =
164 #2 (ListPair.unzip (!next))
165 val scc = SCC.topOrder' { roots = startStates, follow = follow }
166 val mkState' = mkState (arg, eofRules, actions)
167 fun mkGrp (SCC.SIMPLE state, k) = ML_NewGroup (mkState' (state, k))
168 | mkGrp (SCC.RECURSIVE states, k) =
169 ML_NewGroup (List.foldr mkState' k states)
170 in
171 List.foldl mkGrp k scc
172 end
173
174 fun lexerHook spec strm = let
175 val LO.Spec {arg, actions, dfa, startStates, eofRules, ...} = spec
176 fun matchSS (label, state) =
177 (ML_ConPat (label, []),
178 ML_App (nameOf state,
179 [ML_RefGet (ML_Var "yystrm"),
180 ML_Var "yyNO_MATCH"]))
181 val innerExp = ML_Case (ML_RefGet (ML_Var "yyss"),
182 List.map matchSS startStates)
183 val statesExp = mkStates
184 (arg, eofRules, actions, dfa,
185 #2 (ListPair.unzip startStates), innerExp)
186 val lexerExp = Vector.foldri mkAction statesExp actions
187 val ppStrm = TextIOPP.openOut {dst = strm, wid = 80}
188 in
189 ML.ppML (ppStrm, lexerExp)
190 end
191
192 fun tableHook _ strm = TextIO.output (strm, "Vector.fromList []");
193
194 fun output (spec, fname) =
195 ExpandFile.expandTemplate {
196 src = if !Options.lexCompat
197 then lexTemplate else ulexTemplate,
198 dst = fname ^ ".sml",
199 hooks = [("lexer", lexerHook spec),
200 ("startstates", startStatesHook spec),
201 ("userdecls", userDeclsHook spec),
202 ("header", headerHook spec),
203 ("args", argsHook spec),
204 ("pargs", pargsHook spec),
205 ("table", tableHook spec)]
206 }
207
208 end