| 1 |
(* lex-spec.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 |
* Input specification to ml-ulex |
| 9 |
*) |
| 10 |
|
| 11 |
structure LexSpec = |
| 12 |
struct |
| 13 |
|
| 14 |
datatype clamp = CLAMP127 | CLAMP255 | NO_CLAMP |
| 15 |
|
| 16 |
type action = string |
| 17 |
type rule_spec = AtomSet.set option * RegExp.re |
| 18 |
type rule = rule_spec * action |
| 19 |
type eof_rule = string * action |
| 20 |
|
| 21 |
datatype config = Conf of { |
| 22 |
structName : string, |
| 23 |
header : string, |
| 24 |
arg : string, |
| 25 |
startStates : AtomSet.set, |
| 26 |
clamp : clamp (* not used yet *) |
| 27 |
} |
| 28 |
|
| 29 |
datatype spec = Spec of { |
| 30 |
decls : string, |
| 31 |
conf : config, |
| 32 |
rules : rule list, |
| 33 |
eofRules : eof_rule list |
| 34 |
} |
| 35 |
|
| 36 |
fun mkConfig () = Conf { |
| 37 |
structName = "", header = "", arg = "", |
| 38 |
startStates = AtomSet.empty, |
| 39 |
clamp = CLAMP127 |
| 40 |
} |
| 41 |
|
| 42 |
fun updStartStates (conf, new) = let |
| 43 |
val Conf {structName, header, arg, startStates, clamp} = conf |
| 44 |
in Conf { |
| 45 |
structName = structName, |
| 46 |
header = header, |
| 47 |
arg = arg, |
| 48 |
startStates = new, |
| 49 |
clamp = clamp |
| 50 |
} |
| 51 |
end |
| 52 |
|
| 53 |
fun updHeader (conf, new) = let |
| 54 |
val Conf {structName, header, startStates, arg, clamp} = conf |
| 55 |
(* FIXME: we should be reporting an error instead of raising an exception here! *) |
| 56 |
val _ = if String.size structName > 0 |
| 57 |
then raise Fail "Cannot have both %structure and %header" |
| 58 |
else () |
| 59 |
in Conf { |
| 60 |
structName = structName, |
| 61 |
header = new, |
| 62 |
arg = arg, |
| 63 |
startStates = startStates, |
| 64 |
clamp = clamp |
| 65 |
} |
| 66 |
end |
| 67 |
|
| 68 |
fun updStructName (conf, new) = let |
| 69 |
val Conf {structName, header, startStates, arg, clamp} = conf |
| 70 |
(* FIXME: we should be reporting an error instead of raising an exception here! *) |
| 71 |
val _ = if String.size header > 0 |
| 72 |
then raise Fail "Cannot have both %structure and %header" |
| 73 |
else () |
| 74 |
in Conf { |
| 75 |
structName = new, |
| 76 |
header = header, |
| 77 |
arg = arg, |
| 78 |
startStates = startStates, |
| 79 |
clamp = clamp |
| 80 |
} |
| 81 |
end |
| 82 |
|
| 83 |
fun updArg (conf, new) = let |
| 84 |
val Conf {structName, header, startStates, arg, clamp} = conf |
| 85 |
in Conf { |
| 86 |
structName = structName, |
| 87 |
header = header, |
| 88 |
arg = new, |
| 89 |
startStates = startStates, |
| 90 |
clamp = clamp |
| 91 |
} |
| 92 |
end |
| 93 |
|
| 94 |
fun updClamp (conf, new) = let |
| 95 |
val Conf {structName, header, arg, startStates, clamp} = conf |
| 96 |
in Conf { |
| 97 |
structName = structName, |
| 98 |
header = header, |
| 99 |
arg = arg, |
| 100 |
startStates = startStates, |
| 101 |
clamp = new |
| 102 |
} |
| 103 |
end |
| 104 |
|
| 105 |
fun mkSpec() = Spec {decls = "", conf = mkConfig(), rules = [], eofRules = []} |
| 106 |
|
| 107 |
fun addRule (spec, new) = let |
| 108 |
val Spec {decls, conf, rules, eofRules} = spec |
| 109 |
in |
| 110 |
Spec {decls = decls, conf = conf, |
| 111 |
rules = rules @ [new], eofRules = eofRules} |
| 112 |
end |
| 113 |
|
| 114 |
fun addEOFRuleFront (spec, new) = let |
| 115 |
val Spec {decls, conf, rules, eofRules} = spec |
| 116 |
in |
| 117 |
Spec {decls = decls, conf = conf, |
| 118 |
rules = rules, eofRules = new::eofRules} |
| 119 |
end |
| 120 |
|
| 121 |
fun addEOFRule (spec, new) = let |
| 122 |
val Spec {decls, conf, rules, eofRules} = spec |
| 123 |
in |
| 124 |
Spec {decls = decls, conf = conf, |
| 125 |
rules = rules, eofRules = eofRules @ [new]} |
| 126 |
end |
| 127 |
|
| 128 |
fun getConf (Spec {conf, ...}) = conf |
| 129 |
fun updConf (spec, new) = let |
| 130 |
val Spec {decls, conf, rules, eofRules} = spec |
| 131 |
in |
| 132 |
Spec {decls = decls, conf = new, rules = rules, eofRules = eofRules} |
| 133 |
end |
| 134 |
|
| 135 |
fun updDecls (spec, new) = let |
| 136 |
val Spec {decls, conf, rules, eofRules} = spec |
| 137 |
in |
| 138 |
Spec {decls = new, conf = conf, rules = rules, eofRules = eofRules} |
| 139 |
end |
| 140 |
|
| 141 |
fun emptyActions (spec) = let |
| 142 |
val Spec {decls, conf, rules, eofRules} = spec |
| 143 |
val Conf {structName, header, arg, startStates, clamp} = conf |
| 144 |
val conf' = Conf { |
| 145 |
structName = "", header = "", arg = "", clamp = clamp, |
| 146 |
startStates = startStates |
| 147 |
} |
| 148 |
fun clearRule (rspec, action) = (rspec, "()") |
| 149 |
in Spec { |
| 150 |
decls = "fun eof() = ()\ntype lex_result = unit", |
| 151 |
conf = conf', |
| 152 |
rules = List.map clearRule rules, |
| 153 |
eofRules = List.map clearRule eofRules |
| 154 |
} |
| 155 |
end |
| 156 |
|
| 157 |
end |