| 1 |
(* sml-output.sml |
| 2 |
* |
| 3 |
* COPYRIGHT (c) 2006 |
| 4 |
* John Reppy (http://www.cs.uchicago.edu/~jhr) |
| 5 |
* Aaron Turon (http://www.cs.uchicago.edu/~adrassi) |
| 6 |
* All rights reserved. |
| 7 |
* |
| 8 |
* Back end for SML code, using first-class continuations for |
| 9 |
* Burke-Fisher-style error repair/recovery |
| 10 |
*) |
| 11 |
|
| 12 |
structure SMLOutput = |
| 13 |
struct |
| 14 |
|
| 15 |
structure S = LLKSpec |
| 16 |
structure P = Predict |
| 17 |
|
| 18 |
structure TMap = Token.Map |
| 19 |
structure TSet = Token.Set |
| 20 |
structure NMap = Nonterm.Map |
| 21 |
|
| 22 |
structure NT = Nonterm |
| 23 |
|
| 24 |
datatype ml_exp = datatype ML.ml_exp |
| 25 |
datatype ml_pat = datatype ML.ml_pat |
| 26 |
datatype ml_decl = datatype ML.ml_decl |
| 27 |
datatype ml_fundecl = datatype ML.ml_fundecl |
| 28 |
datatype ml_fun_heading = datatype ML.ml_fun_heading |
| 29 |
|
| 30 |
(* the following functions compute names or small expressions |
| 31 |
* used throughout the backend |
| 32 |
*) |
| 33 |
|
| 34 |
fun NTFnName nt = NT.name nt ^ "_NT" |
| 35 |
fun NTFnVar nt = ML_Var (NTFnName nt) |
| 36 |
|
| 37 |
fun predFnName nt = NT.name nt ^ "_PRED" |
| 38 |
|
| 39 |
fun tokConName tok = "Tok." ^ Token.name tok |
| 40 |
fun tokConVar tok = ML_Var (tokConName tok) |
| 41 |
fun tokConPat tok = ML_ConPat (tokConName tok, |
| 42 |
if Token.hasTy tok |
| 43 |
then [ML_Wild] |
| 44 |
else []) |
| 45 |
fun tokConPat' tok = ML_ConPat (Token.name tok, |
| 46 |
if Token.hasTy tok |
| 47 |
then [ML_Wild] |
| 48 |
else []) |
| 49 |
fun tokMatch' tok = "match" ^ (Token.name tok) |
| 50 |
fun tokMatch tok = "" ^ tokMatch' tok |
| 51 |
|
| 52 |
fun tokExpected tok = ML_Raw [ML.Tok ("raise Fail \"expected " ^ (Token.name tok) ^ "\"")] |
| 53 |
|
| 54 |
val bindingSuffix = "_RES" |
| 55 |
val spanSuffix = "_SPAN" |
| 56 |
val fullSpan = "FULL_SPAN" |
| 57 |
val spanTySuffix = " : (Lex.pos * Lex.pos)" |
| 58 |
val rcSuffix = "_REFC" |
| 59 |
|
| 60 |
fun actionHeader (name, (bindings, formals), suffix, isPred, refcells, refSuffix) = let |
| 61 |
val withSuffix = map (fn b => Atom.toString b ^ suffix) |
| 62 |
(AtomSet.listItems (AtomSet.union (bindings, formals))) |
| 63 |
val withSpan = map (fn b => Atom.toString b ^ spanSuffix ^ spanTySuffix) |
| 64 |
(AtomSet.listItems bindings) |
| 65 |
val refs = map (fn (S.REFCELL {name, ...}) => name ^ refSuffix) refcells |
| 66 |
val args = if isPred then |
| 67 |
withSuffix @ refs |
| 68 |
else |
| 69 |
withSuffix @ withSpan @ [fullSpan ^ spanTySuffix] @ refs |
| 70 |
in |
| 71 |
String.concat [name, " (", String.concatWith ", " args, ")"] |
| 72 |
end |
| 73 |
|
| 74 |
(* make an expression that will pull the next token off the stream *) |
| 75 |
fun mkGet1 strm = ML_App ("lex", [ML_Var (strm)]) |
| 76 |
(* make an expression that will pull the kth token off the stream *) |
| 77 |
fun mkGetk (strm, 1) = mkGet1 strm |
| 78 |
| mkGetk (strm, k) = ML_App ("lex", [ML_App ("#2", [mkGetk (strm, k-1)])]) |
| 79 |
|
| 80 |
fun rawCode code = ML_Raw [ML.Tok code] |
| 81 |
|
| 82 |
(* make an expression for the given (polymorphic) decision tree *) |
| 83 |
fun mkPredict (pickFn, choiceFn, strm, tree, errAction) = let |
| 84 |
fun mkPredict (strm, P.Pick p) = |
| 85 |
pickFn p |
| 86 |
| mkPredict (strm, P.ByTok branches) = let |
| 87 |
val branches = List.concat (map mkMatch branches) |
| 88 |
val errCase = (ML_Wild, errAction) |
| 89 |
in |
| 90 |
ML_Case (mkGet1 strm, branches @ [errCase]) |
| 91 |
end |
| 92 |
| mkPredict (strm, P.Choice prods) = |
| 93 |
choiceFn prods |
| 94 |
and mkMatch (set, tree) = |
| 95 |
map (fn tok => (ML_TupPat [tokConPat tok, ML_Wild, ML_VarPat "strm'"], |
| 96 |
mkPredict ("strm'", tree))) |
| 97 |
(TSet.listItems set) |
| 98 |
in |
| 99 |
mkPredict (strm, tree) |
| 100 |
end |
| 101 |
|
| 102 |
(* make a production *) |
| 103 |
fun mkProd (grm, pm) prod = let |
| 104 |
val rhs = Prod.items prod |
| 105 |
val S.Grammar {refcells, ...} = grm |
| 106 |
fun mkTok (t, strmExp, letFn) = |
| 107 |
letFn (ML_App (tokMatch t, [strmExp])) |
| 108 |
fun mkNT (nt, strmExp, args, letFn, item) = let |
| 109 |
val name = (case (args, !Options.unitActions) |
| 110 |
of (SOME args, false) => concat [ |
| 111 |
"(", NTFnName nt, " (", |
| 112 |
actionHeader ( |
| 113 |
"UserCode.ARGS_" ^ Action.name args, |
| 114 |
Item.bindingsLeftOf (item, prod), |
| 115 |
bindingSuffix, true, refcells, rcSuffix), |
| 116 |
"))" |
| 117 |
] |
| 118 |
| _ => NTFnName nt |
| 119 |
(* end case *)) |
| 120 |
val innerExp = ML_App (name, [strmExp]) |
| 121 |
in |
| 122 |
if NT.isSubrule nt |
| 123 |
then letFn (mkNonterm (grm, pm) (nt, innerExp)) |
| 124 |
else letFn innerExp |
| 125 |
end |
| 126 |
fun mkEBNF (nt, strmExp, fname, letFn) = let |
| 127 |
val predName = predFnName nt |
| 128 |
val innerExp = letFn (ML_App (fname, [ML_Var predName, ML_Var (NTFnName nt), strmExp])) |
| 129 |
val Predict.PMaps {ebnfPredict, ...} = pm |
| 130 |
val predTree = ebnfPredict nt |
| 131 |
fun mkBool true = ML_Var "true" |
| 132 |
| mkBool false = ML_Var "false" |
| 133 |
fun choiceFn _ = raise Fail "BUG: mkEBNF: backtracking choice unexpected" |
| 134 |
val errAction = ML_Var "false" |
| 135 |
val caseExp = mkPredict (mkBool, choiceFn, "strm", predTree, errAction) |
| 136 |
val predFn = ML_Funs ([(predName, ["strm"], caseExp)], innerExp) |
| 137 |
in |
| 138 |
mkNonterm (grm, pm) (nt, predFn) |
| 139 |
end |
| 140 |
fun mkItem strm ((item, binding), k) = let |
| 141 |
val strmExp = ML_Var strm |
| 142 |
fun mkLet e = ML_Let (String.concat |
| 143 |
["(", binding, bindingSuffix, |
| 144 |
", ", binding, spanSuffix, ", strm')"], |
| 145 |
e, k) |
| 146 |
in |
| 147 |
case Item.sym item |
| 148 |
of S.TOK t => mkTok (t, strmExp, mkLet) |
| 149 |
| S.NONTERM (nt, args) |
| 150 |
=> mkNT (nt, strmExp, args, mkLet, item) |
| 151 |
| S.CLOS nt => mkEBNF (nt, strmExp, "EBNF.closure", mkLet) |
| 152 |
| S.POSCLOS nt => mkEBNF (nt, strmExp, "EBNF.posclos", mkLet) |
| 153 |
| S.OPT nt => mkEBNF (nt, strmExp, "EBNF.optional", mkLet) |
| 154 |
end |
| 155 |
val itemBindings = Prod.itemBindings prod |
| 156 |
fun debugCode () = concat[ |
| 157 |
"print \"", Nonterm.qualName (Prod.lhs prod), "\\n\"" |
| 158 |
] |
| 159 |
val action = if !Options.unitActions |
| 160 |
then "()" |
| 161 |
else (case Prod.action prod |
| 162 |
of SOME _ => actionHeader ( |
| 163 |
concat ["UserCode.", Prod.fullName prod, "_ACT"], |
| 164 |
Prod.bindingsAtAction prod, bindingSuffix, false, |
| 165 |
refcells, rcSuffix) |
| 166 |
| NONE => let |
| 167 |
val bindings = ListPair.mapPartial |
| 168 |
(fn (binding, hasValue) => |
| 169 |
if hasValue |
| 170 |
then SOME (binding ^ bindingSuffix) |
| 171 |
else NONE) |
| 172 |
(itemBindings, Prod.itemYields prod) |
| 173 |
in |
| 174 |
case bindings |
| 175 |
of [] => "()" |
| 176 |
| _ => concat["(", String.concatWith ", " bindings, ")"] |
| 177 |
(* end case *) |
| 178 |
end |
| 179 |
(* end case *)) |
| 180 |
fun innerExp strm = let |
| 181 |
val strmVar = ML_Var (strm) |
| 182 |
val span = if List.length itemBindings = 0 then |
| 183 |
ML_Tuple [ML_App ("Err.getPos", [strmVar]), |
| 184 |
ML_App ("Err.getPos", [strmVar])] |
| 185 |
else |
| 186 |
ML_Tuple [ML_App ("#1", [ML_Var (hd itemBindings ^ spanSuffix)]), |
| 187 |
ML_App ("#2", [ML_Var (hd (rev itemBindings) ^ spanSuffix)])] |
| 188 |
val act = ML_Tuple [ML_Raw [ML.Tok action], ML_Var fullSpan, strmVar] |
| 189 |
val act = if !Options.debug |
| 190 |
then ML_Seq[ |
| 191 |
ML_Raw[ML.Tok(concat[ |
| 192 |
"print \"", Nonterm.qualName (Prod.lhs prod), "\\n\"" |
| 193 |
])], |
| 194 |
act |
| 195 |
] |
| 196 |
else act |
| 197 |
val spanExp = ML_Let (fullSpan, span, act) |
| 198 |
in case (Prod.pred prod, !Options.unitActions) |
| 199 |
of (SOME pred, false) => |
| 200 |
ML_If (ML_Raw [ML.Tok ("(" |
| 201 |
^ actionHeader |
| 202 |
("UserCode." ^ Prod.fullName prod ^ "_PRED", |
| 203 |
Prod.bindingsAtAction prod, bindingSuffix, true, |
| 204 |
refcells, rcSuffix) |
| 205 |
^ ")")], |
| 206 |
spanExp, |
| 207 |
ML_App ("fail", [])) |
| 208 |
| _ => spanExp |
| 209 |
end |
| 210 |
val parse = case (ListPair.zip (rhs, itemBindings)) |
| 211 |
of [] => innerExp "strm" |
| 212 |
| fst::rst => |
| 213 |
mkItem "strm" |
| 214 |
(fst, List.foldr (mkItem "strm'") (innerExp "strm'") rst) |
| 215 |
in |
| 216 |
parse |
| 217 |
end |
| 218 |
|
| 219 |
(* make a group of productions, along with a decision tree to choose one of them *) |
| 220 |
and mknProds (grm, pm, nt) = let |
| 221 |
fun mkProdFun (prod, k) = ML_Funs ([(Prod.name prod, ["strm"], |
| 222 |
mkProd (grm, pm) prod)], k) |
| 223 |
val Predict.PMaps {prodPredict, ...} = pm |
| 224 |
val tree = prodPredict nt |
| 225 |
fun pickFn prod = ML_App (Prod.name prod, [ML_Var "strm"]) |
| 226 |
fun choiceFn prods = |
| 227 |
ML_App ("tryProds", [ML_Var "strm", |
| 228 |
ML_List (map (ML_Var o Prod.name) prods)]) |
| 229 |
val errAction = ML_App ("fail", []) |
| 230 |
val caseExp = mkPredict (pickFn, choiceFn, "strm", tree, errAction) |
| 231 |
in |
| 232 |
foldr mkProdFun caseExp (Nonterm.prods nt) |
| 233 |
end |
| 234 |
|
| 235 |
and mkNonterm' (grm, pm) nt = let |
| 236 |
val formals = (case (!Options.unitActions, Nonterm.formals nt) |
| 237 |
of (false, _::_) => concat[ |
| 238 |
" (", |
| 239 |
String.concatWithMap ", " |
| 240 |
(fn f => Atom.toString f ^ bindingSuffix) |
| 241 |
(Nonterm.formals nt), |
| 242 |
")" |
| 243 |
] |
| 244 |
| _ => "" |
| 245 |
(* end case *)) |
| 246 |
val exp = (case Nonterm.prods nt |
| 247 |
of [prod] => mkProd (grm, pm) prod |
| 248 |
| _ => mknProds(grm, pm, nt) |
| 249 |
(* end case *)) |
| 250 |
in |
| 251 |
(NTFnName nt ^ formals, ["strm"], exp) |
| 252 |
end |
| 253 |
and mkNonterm (grm, pm) (nt, k) = ML_Funs ([mkNonterm' (grm, pm) nt], k) |
| 254 |
|
| 255 |
fun mkNonterms (grm, pm) (nts, k) = |
| 256 |
ML_Funs (map (mkNonterm' (grm, pm)) nts, k) |
| 257 |
|
| 258 |
(* output the main parser body *) |
| 259 |
fun parserHook spec strm = let |
| 260 |
val (grm as S.Grammar {toks, nterms, startnt, sortedTops, entryPoints, ...}, |
| 261 |
pm) = spec |
| 262 |
val ppStrm = TextIOPP.openOut {dst = strm, wid = 80} |
| 263 |
val entries = map NTFnName (startnt :: entryPoints) |
| 264 |
val entriesVal = "val (" ^ String.concatWith ", " entries ^ ") = " |
| 265 |
val innerExp = ML_Tuple (map ML_Var entries) |
| 266 |
val parser = List.foldl (mkNonterms (grm, pm)) innerExp sortedTops |
| 267 |
fun optParam nt = if List.null (Nonterm.formals nt) then " " else " x " |
| 268 |
fun optParamFn nt = if List.null (Nonterm.formals nt) then " " else " fn x => " |
| 269 |
fun wrWrapParse nt = TextIO.output (strm, String.concat [ |
| 270 |
"val ", NTFnName nt, " = ", optParamFn nt, |
| 271 |
"fn s => unwrap (Err.launch (eh, lexFn, ", |
| 272 |
NTFnName nt, optParam nt, ", ", |
| 273 |
if Nonterm.same (nt, startnt) then "true" else "false", |
| 274 |
") s)\n" |
| 275 |
]) |
| 276 |
fun wrEntry (name, nt) = TextIO.output (strm, String.concat [ |
| 277 |
"fun ", name, " lexFn ", optParam nt, |
| 278 |
"s = let ", entriesVal, "mk lexFn in ", NTFnName nt, |
| 279 |
if List.null (Nonterm.formals nt) then " " else " x ", |
| 280 |
"s end\n\n" |
| 281 |
]) |
| 282 |
in |
| 283 |
TextIO.output (strm, entriesVal ^ "\n"); |
| 284 |
ML.ppML (ppStrm, parser); |
| 285 |
TextIO.output (strm, "\n"); |
| 286 |
app wrWrapParse (startnt::entryPoints); |
| 287 |
TextIO.output (strm, concat [ |
| 288 |
"\nin (", |
| 289 |
String.concatWith ", " entries, |
| 290 |
") end\n" |
| 291 |
]); |
| 292 |
TextIO.output (strm, " in\n"); |
| 293 |
wrEntry ("parse", startnt); |
| 294 |
app (wrEntry o (fn x => ("parse" ^ Nonterm.name x, x))) entryPoints; |
| 295 |
TextIO.output (strm, " end\n") |
| 296 |
end |
| 297 |
|
| 298 |
(* make a match function for a token *) |
| 299 |
fun ppMatch (strm, ppStrm) t = let |
| 300 |
val matchCase = |
| 301 |
(ML_TupPat |
| 302 |
[ML_ConPat (tokConName t, |
| 303 |
if Token.hasTy t |
| 304 |
then [ML_VarPat "x"] |
| 305 |
else []), |
| 306 |
ML_VarPat "span", |
| 307 |
ML_VarPat "strm'"], |
| 308 |
if Token.hasTy t |
| 309 |
then ML_Tuple [ML_Var "x", ML_Var "span", ML_Var "strm'"] |
| 310 |
else ML_Tuple [ML_Var "()", ML_Var "span", ML_Var "strm'"]) |
| 311 |
val errCase = (ML_Wild, ML_App ("fail", [])) |
| 312 |
val exp = ML_Case (mkGet1 "strm", [matchCase, errCase]) |
| 313 |
in |
| 314 |
TextIO.output (strm, "fun " ^ tokMatch' t ^ " strm = "); |
| 315 |
ML.ppML (ppStrm, exp); |
| 316 |
TextIO.output (strm, "\n") |
| 317 |
end |
| 318 |
|
| 319 |
(* a token trie used to organize the preferred changes *) |
| 320 |
datatype trie = TR of { |
| 321 |
replacements : S.token list list, (* replacements *) |
| 322 |
kids : (S.token * trie) list |
| 323 |
} |
| 324 |
|
| 325 |
val emptyTrie = TR{replacements=[], kids=[]} |
| 326 |
|
| 327 |
(* build the token trie from the list of preferred changes; we quietly |
| 328 |
* eliminate duplicate changes as part of this building process. |
| 329 |
*) |
| 330 |
fun mkTrie (changes : (S.token list * S.token list) list) = let |
| 331 |
fun insert ((old, new), tr) = let |
| 332 |
fun ins ([], tr as TR{replacements, kids}) = let |
| 333 |
fun same repl = ListPair.allEq Token.same (new, repl) |
| 334 |
in |
| 335 |
if List.exists same replacements |
| 336 |
then tr |
| 337 |
else TR{replacements=new::replacements, kids=kids} |
| 338 |
end |
| 339 |
| ins (t::toks, TR{replacements, kids}) = let |
| 340 |
fun find [] = [(t, ins(toks, emptyTrie))] |
| 341 |
| find ((t', tr)::rest) = if Token.same(t, t') |
| 342 |
then (t', ins(toks, tr)) :: rest |
| 343 |
else (t', tr) :: find rest |
| 344 |
in |
| 345 |
TR{replacements = replacements, kids = find kids} |
| 346 |
end |
| 347 |
in |
| 348 |
ins (old, tr) |
| 349 |
end |
| 350 |
in |
| 351 |
List.foldl insert emptyTrie changes |
| 352 |
end |
| 353 |
|
| 354 |
(* output the tokens structure *) |
| 355 |
fun tokenStructHook spec strm = let |
| 356 |
val (S.Grammar{name, toks, toksImport, changes, ...}, _) = spec |
| 357 |
val ppStrm = TextIOPP.openOut {dst = strm, wid = 80} |
| 358 |
fun pr s = TextIO.output (strm, s) |
| 359 |
fun prl s = TextIO.output (strm, concat s) |
| 360 |
fun prDT () = let |
| 361 |
val first::rest = toks |
| 362 |
in |
| 363 |
pr " datatype token\n"; |
| 364 |
prl [" = ", Token.def first, "\n"]; |
| 365 |
List.app (fn tok => prl [" | ", Token.def tok, "\n"]) rest |
| 366 |
end |
| 367 |
(* list of all nullary and default token values for error repair *) |
| 368 |
val allToks = let |
| 369 |
fun make (tok, ts) = if Token.hasTy tok |
| 370 |
then (case Token.default tok |
| 371 |
of SOME arg => concat[Token.name tok, "(", arg, ")"] :: ts |
| 372 |
| NONE => ts |
| 373 |
(* end case *)) |
| 374 |
else Token.name tok :: ts |
| 375 |
in |
| 376 |
List.foldr make [] toks |
| 377 |
end |
| 378 |
(* support for toString function *) |
| 379 |
fun mkMat t = (ML_TupPat [tokConPat' t], rawCode (Token.quoted t)) |
| 380 |
val casesExp = ML_Case (ML_Var "tok", List.map mkMat toks) |
| 381 |
(* support for isKW function *) |
| 382 |
fun mkKWMat t = (ML_TupPat [tokConPat' t], |
| 383 |
ML_Var (if Token.isKW t then "true" else "false")) |
| 384 |
val kwCasesExp = ML_Case (ML_Var "tok", List.map mkKWMat toks) |
| 385 |
(* support for the changes function *) |
| 386 |
(***** |
| 387 |
val changesDT = |
| 388 |
" datatype 'strm changes\n\ |
| 389 |
\ = CHANGE of 'strm * int * token list * ('strm -> 'strm changes)\n\ |
| 390 |
\ | NOCHANGE;\n" |
| 391 |
fun genChanges [] = TextIO.output (strm, " fun changes _ _ = NOCHANGE\n") |
| 392 |
| genChanges changes = let |
| 393 |
val TR{replacements, kids} = mkTrie changes |
| 394 |
val noKids = List.null kids |
| 395 |
(* map a token to an expression that evaluates to it *) |
| 396 |
fun tok2exp tok = if Token.hasTy tok |
| 397 |
then (case Token.default tok |
| 398 |
of SOME code => ML_App(Token.name tok, [ML_Raw[Tok code]]) |
| 399 |
| NONE => raise Fail(concat[ |
| 400 |
"token '", Token.name tok, "' does not have a default value" |
| 401 |
]) |
| 402 |
(* end case *)) |
| 403 |
else ML_Var(Token.name tok) |
| 404 |
(* generate the insertion states first *) |
| 405 |
fun genInsFns (idx, [], fns) = if noKids |
| 406 |
then (idx+1, (s idx, ["_"], ML_Var "NOCHANGE") :: fns) |
| 407 |
else (idx+1, fns) |
| 408 |
| genInsFns (idx, []::reps, fns) = (* ignore [] -> [] substitution *) |
| 409 |
genInsFns (idx, reps, fns) |
| 410 |
| genInsFns (idx, new::reps, fns) = let |
| 411 |
val f = (s idx, ["strm"], ML_App("CHANGE", [ |
| 412 |
ML_Var "strm", ML_Int 0, |
| 413 |
ML_List(List.map tok2exp new), |
| 414 |
s(idx+1) |
| 415 |
])) |
| 416 |
in |
| 417 |
genInsFns (idx+1, reps, f::fns) |
| 418 |
end |
| 419 |
val (idx, fns) = genInsFns (0, replacements, []) |
| 420 |
(* function to handle recursive cases *) |
| 421 |
fun gen (idx, depth, [], fns) = (idx, fns) |
| 422 |
| gen (idx, depth, kids, fns) = let |
| 423 |
fun genKid ((tok, tr), (idx, kfns, fns)) = let |
| 424 |
val (idx', fns) = genKidFn (idx, depth+1, tr, fns) |
| 425 |
in |
| 426 |
(idx', (tok, s idx)::kfns, fns) |
| 427 |
end |
| 428 |
val (idx, kfns, fns) = List.foldl genKid (idx, [], fns) kids |
| 429 |
in |
| 430 |
(idx, fns) |
| 431 |
end |
| 432 |
val (idx, fns) = gen (idx, 0, kids, fns) |
| 433 |
in |
| 434 |
FUN(FUNHEAD("changes", [ML_TypePat(ML_VarPat "getTok", T_FUN())]), |
| 435 |
ML_Funs(List.rev fns, ML_Var(s 0))) |
| 436 |
end |
| 437 |
fun genChanges (idx, depth, TR{replacements, kids}) = let |
| 438 |
val noKids = List.null kids |
| 439 |
fun s idx = "s" ^ Int.toString idx |
| 440 |
|
| 441 |
fun gen |
| 442 |
*****) |
| 443 |
in |
| 444 |
prl ["structure ", name, "Tokens"]; |
| 445 |
case toksImport |
| 446 |
of NONE => ( |
| 447 |
pr " =\n"; |
| 448 |
pr " struct\n"; |
| 449 |
prDT()) |
| 450 |
| SOME code => ( |
| 451 |
pr " : sig\n"; |
| 452 |
prDT(); |
| 453 |
List.app pr [ |
| 454 |
" val allToks : token list\n", |
| 455 |
" val toString : token -> bool\n", |
| 456 |
" val isKW : token -> bool\n", |
| 457 |
" val isEOF : token -> bool\n", |
| 458 |
" end = struct\n" |
| 459 |
]; |
| 460 |
prl [" datatype token = datatype ", Action.code code, "\n"]) |
| 461 |
(* end case *); |
| 462 |
(* "allToks" value *) |
| 463 |
prl [ |
| 464 |
" val allToks = [\n ", |
| 465 |
String.concatWith ", " allToks, "\n ]\n" |
| 466 |
]; |
| 467 |
(* "toString" function *) |
| 468 |
TextIO.output (strm, " fun toString tok =\n"); |
| 469 |
ML.ppML (ppStrm, casesExp); |
| 470 |
pr "\n"; |
| 471 |
(* "isKW" function *) |
| 472 |
TextIO.output (strm, " fun isKW tok =\n"); |
| 473 |
ML.ppML (ppStrm, kwCasesExp); |
| 474 |
pr "\n"; |
| 475 |
(* "isEOF" function *) |
| 476 |
pr " fun isEOF EOF = true\n"; |
| 477 |
pr " | isEOF _ = false\n"; |
| 478 |
(* preferred "changes" support *) |
| 479 |
(* TODO *) |
| 480 |
prl [" end (* ", name, "Tokens *)\n"] |
| 481 |
end |
| 482 |
|
| 483 |
fun matchfnsHook spec strm = let |
| 484 |
val (S.Grammar {toks, ...}, _) = spec |
| 485 |
val ppStrm = TextIOPP.openOut {dst = strm, wid = 80} |
| 486 |
in |
| 487 |
app (ppMatch (strm, ppStrm)) toks |
| 488 |
end |
| 489 |
|
| 490 |
(* output header *) |
| 491 |
fun headerHook (grm, _) strm = (case grm |
| 492 |
of S.Grammar{header = SOME h, ...} => TextIO.output (strm, h) |
| 493 |
| S.Grammar{name, ...} => |
| 494 |
TextIO.output (strm, String.concat [ |
| 495 |
"functor ", name, "ParseFn (Lex : ANTLR_LEXER)" |
| 496 |
]) |
| 497 |
(* end case *)) |
| 498 |
|
| 499 |
(* output tokens module name *) |
| 500 |
fun tokmodHook spec strm = let |
| 501 |
val (S.Grammar {name, ...}, _) = spec |
| 502 |
in |
| 503 |
TextIO.output (strm, name ^ "Tokens") |
| 504 |
end |
| 505 |
|
| 506 |
(* output user definitions *) |
| 507 |
fun defsHook spec strm = let |
| 508 |
val (S.Grammar {defs, ...}, _) = spec |
| 509 |
fun output ss = TextIO.output (strm, String.concat ss) |
| 510 |
in |
| 511 |
TextIO.output (strm, Action.toString defs) |
| 512 |
end |
| 513 |
|
| 514 |
(* output user actions *) |
| 515 |
fun actsHook spec strm = let |
| 516 |
val (S.Grammar {prods, refcells, ...}, _) = spec |
| 517 |
fun output ss = TextIO.output (strm, String.concat ss) |
| 518 |
fun actionLevel (suffix, f, isPred) prod = (case f prod |
| 519 |
of SOME code => output [ |
| 520 |
"fun ", |
| 521 |
actionHeader ( |
| 522 |
Prod.fullName prod ^ suffix, |
| 523 |
Prod.bindingsAtAction prod, "", isPred, refcells, ""), |
| 524 |
" = \n (", Action.toString code, ")", |
| 525 |
(case Nonterm.ty (Prod.lhs prod) |
| 526 |
of NONE => "" |
| 527 |
| SOME ty => " : " ^ ty), |
| 528 |
"\n"] |
| 529 |
| NONE => ()) |
| 530 |
fun args prod (itm as S.ITEM {sym = S.NONTERM (nt, SOME code), ...}) = |
| 531 |
output ["fun ", |
| 532 |
actionHeader ( |
| 533 |
"ARGS_" ^ Action.name code, |
| 534 |
Item.bindingsLeftOf (itm, prod), "", true, refcells, ""), |
| 535 |
" = \n (", Action.toString code, ")\n"] |
| 536 |
| args _ _ = () |
| 537 |
fun outCell (S.REFCELL {name, initCode, ty, ...}) = TextIO.output (strm, |
| 538 |
String.concat [ |
| 539 |
"fun mk", name ^ rcSuffix, "() : (", ty, ") ref = ref (", |
| 540 |
Action.toString initCode, ")\n"]) |
| 541 |
in |
| 542 |
app (actionLevel ("_ACT", Prod.action, false)) prods; |
| 543 |
app (actionLevel ("_PRED", Prod.pred, true)) prods; |
| 544 |
app (fn prod => app (args prod) (Prod.items prod)) prods; |
| 545 |
app outCell refcells |
| 546 |
end |
| 547 |
|
| 548 |
fun ehargsHook spec strm = let |
| 549 |
fun out s = TextIO.output (strm, s) |
| 550 |
val (S.Grammar {refcells, ...}, _) = spec |
| 551 |
val names = map (fn (S.REFCELL {name, ...}) => name) refcells |
| 552 |
fun prepend pre s = pre ^ s |
| 553 |
fun append post s = s ^ post |
| 554 |
fun mkc (S.REFCELL {name, ...}) = String.concat [ |
| 555 |
"val ", name, rcSuffix, " = UserCode.mk", name, rcSuffix, "()\n"] |
| 556 |
in |
| 557 |
app (out o mkc) refcells; |
| 558 |
out (String.concat ["fun getS() = {", |
| 559 |
String.concatWith ", " |
| 560 |
(map (fn nm => nm ^ " = !" ^ nm ^ rcSuffix) |
| 561 |
names), |
| 562 |
"}\n"]); |
| 563 |
out (String.concat ["fun putS{", |
| 564 |
String.concatWith ", " names, |
| 565 |
"} = (", |
| 566 |
String.concatWith |
| 567 |
"; " (map (fn nm => nm ^ rcSuffix ^ " := " ^ nm) |
| 568 |
names), |
| 569 |
")\n"]); |
| 570 |
out (String.concat ["fun unwrap (ret, strm, repairs) = ", |
| 571 |
"(ret, strm, repairs", |
| 572 |
if List.length names > 0 then ", getS()" else "", |
| 573 |
")\n"]) |
| 574 |
end |
| 575 |
|
| 576 |
fun output (grm, pm, fname) = |
| 577 |
ExpandFile.expandTemplate { |
| 578 |
src = SMLTemplate.template, |
| 579 |
dst = fname ^ ".sml", |
| 580 |
hooks = [ |
| 581 |
("parser", parserHook (grm, pm)), |
| 582 |
("token-struct", tokenStructHook (grm, pm)), |
| 583 |
("tokmod", tokmodHook (grm, pm)), |
| 584 |
("header", headerHook (grm, pm)), |
| 585 |
("usrdefs", defsHook (grm, pm)), |
| 586 |
("actions", actsHook (grm, pm)), |
| 587 |
("ehargs", ehargsHook (grm, pm)), |
| 588 |
("matchfns", matchfnsHook (grm, pm)) |
| 589 |
] |
| 590 |
} |
| 591 |
|
| 592 |
end |