Debian Codegraph @

lib/mllpt-lib/ml-lpt/ml-antlr/item.sml

raw
1 (* item.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 * Utility code for the item datatype.
9 *)
10
11 structure Item =
12 struct
13
14 structure S = LLKSpec
15
16 fun sym (S.ITEM {sym = s, ...}) = s
17 val name = Preitem.name o sym
18 val toString = Preitem.toString o sym
19 val listToString = Preitem.listToString o (map sym)
20
21 fun id (S.ITEM {id, ...}) = id
22 fun compare (item1, item2) = Int.compare(id item1, id item2)
23 fun same pair = (case compare pair
24 of EQUAL => true
25 | _ => false)
26
27 fun nt itm = (case sym itm
28 of S.NONTERM (nt, _) => SOME nt
29 | S.POSCLOS nt => SOME nt
30 | S.CLOS nt => SOME nt
31 | S.OPT nt => SOME nt
32 | S.TOK _ => NONE
33 (* end case *))
34
35 (* return the SET of bound names to the left of an item in a production,
36 * and the SET of the formal parameters available to the item.
37 * we use sets rather than lists because names might be
38 * repeated in the bindings.
39 *)
40 fun bindingsLeftOf (item, prod) = let
41 fun lhs (LLKSpec.PROD {lhs, ...}) = lhs
42 fun items (LLKSpec.PROD {rhs, ...}) = !rhs
43 fun itemBindings (LLKSpec.PROD {rhsBindings, ...}) =
44 (fn (x,_) => x) (ListPair.unzip rhsBindings)
45 fun leftOf ([], accum) = raise Fail "BUG: leftOf on empty list"
46 | leftOf ((i, name)::is, accum) =
47 if same (i, item) then accum
48 else leftOf (is, AtomSet.add(accum, Atom.atom name))
49 val (parentBindings, formals) = case Nonterm.parent (lhs prod)
50 of NONE => (AtomSet.empty,
51 AtomSet.addList (AtomSet.empty, Nonterm.formals (lhs prod)))
52 | SOME prod' => let
53 fun isProdItm (itm) = (case nt itm
54 of SOME nt => Nonterm.same (nt, lhs prod)
55 | NONE => false)
56 val prodItm = valOf (List.find isProdItm (items prod'))
57 in
58 bindingsLeftOf (prodItm, prod')
59 end
60 in
61 (leftOf (ListPair.zip (items prod, itemBindings prod),
62 parentBindings),
63 formals)
64 end
65
66 structure Set = RedBlackSetFn (
67 struct
68 type ord_key = S.item
69 val compare = compare
70 end)
71
72 structure Map = RedBlackMapFn (
73 struct
74 type ord_key = S.item
75 val compare = compare
76 end)
77
78 end