Debian Codegraph @

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

raw
1 (* prod.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 functions for the prod datatype.
9 *)
10
11 structure Prod =
12 struct
13
14 datatype prod = datatype LLKSpec.prod
15
16 fun id (PROD{id, ...}) = id
17 fun lhs (PROD{lhs, ...}) = lhs
18 fun items (PROD{rhs, ...}) = !rhs
19 fun itemBindings (PROD{rhsBindings, ...}) =
20 (fn (x, _) => x) (ListPair.unzip rhsBindings)
21 fun itemYields (PROD{rhsBindings, ...}) =
22 (fn (_, y) => y) (ListPair.unzip rhsBindings)
23 fun action (PROD{action, ...}) = action
24 fun pred (PROD{pred, ...}) = pred
25
26 fun span (PROD{loc, ...}) = loc
27
28 fun name (PROD{name, ...}) = name
29 fun fullName p = name p
30 (* fun fullName p = (case Nonterm.parent (lhs p)
31 of SOME p' => String.concat [
32 fullName p', "_",
33 Nonterm.name (lhs p'), "_",
34 name p]
35 | NONE => name p)
36 *)
37
38 fun toString p = concat[
39 Nonterm.qualName (lhs p), " ::= ",
40 String.concatWith " " (List.map Item.toString (items p))
41 ]
42
43 fun compare (p1, p2) = Int.compare(id p1, id p2)
44
45 fun canTry (PROD{try = true, ...}) = true
46 | canTry _ = false
47 (*
48 | canTry (PROD{rhs, ...}) =
49 List.exists (fn (LLKSpec.SEM_PRED _) => true
50 | _ => false)
51 rhs
52 *)
53
54 fun bindingsAtAction p = let
55 val (leftBindings, formals) = case Nonterm.parent (lhs p)
56 of NONE => (AtomSet.empty,
57 AtomSet.addList (AtomSet.empty,
58 Nonterm.formals (lhs p)))
59 | SOME p' => let
60 fun isProdItm (itm) = (case Item.nt itm
61 of SOME nt => Nonterm.same (nt, lhs p)
62 | NONE => false)
63 val prodItm = valOf (List.find isProdItm (items p'))
64 in
65 Item.bindingsLeftOf (prodItm, p')
66 end
67 in
68 (AtomSet.addList (leftBindings, map Atom.atom (itemBindings p)), formals)
69 end
70
71 structure Set = RedBlackSetFn (
72 struct
73 type ord_key = prod
74 val compare = compare
75 end)
76
77 structure Map = RedBlackMapFn (
78 struct
79 type ord_key = prod
80 val compare = compare
81 end)
82
83 fun sortProds prods =
84 ListMergeSort.sort
85 (fn (x, y) => compare (x, y) = GREATER)
86 prods
87
88 end