Debian Codegraph @

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

raw
1 (* token.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 token datatype.
9 *)
10
11 structure Token =
12 struct
13
14 datatype token = datatype LLKSpec.token
15
16 fun name (T{name, ...}) = Atom.toString name
17 fun ty (T{ty, ...}) = ty
18 fun hasTy (T{ty = SOME _, ...}) = true
19 | hasTy _ = false
20 fun isKW (T{keyword, ...}) = keyword
21 fun default (T{default, ...}) = default
22
23 fun toString (T{abbrev = SOME a, ...}) = Atom.toString a
24 | toString tok = name tok
25
26 fun quoted (T{abbrev = SOME a, ...}) = Atom.toString a
27 | quoted tok = String.concat["\"", name tok, "\""]
28
29 fun def tok = (case ty tok
30 of NONE => name tok
31 | SOME ty => String.concat[name tok, " of ", ty]
32 (* end case *))
33
34 fun compare (T{id=a, ...}, T{id=b, ...}) = Int.compare(a, b)
35 fun lexCompare (T{name=a, ...}, T{name=b, ...}) =
36 String.compare(Atom.toString a, Atom.toString b)
37 fun same (T{id=a, ...}, T{id=b, ...}) = (a = b)
38
39 structure Set = RedBlackSetFn (
40 struct
41 type ord_key = token
42 val compare = compare
43 end)
44
45 structure Map = RedBlackMapFn (
46 struct
47 type ord_key = token
48 val compare = compare
49 end)
50
51 fun setToString s = let
52 (* simple insertion sort to lexically order the set *)
53 fun ins (tok, []) = [tok]
54 | ins (tok, tok'::toks) = (case lexCompare(tok, tok')
55 of LESS => tok::tok'::toks
56 | _ => tok'::ins(tok, toks)
57 (* end case *))
58 val toks = Set.foldl ins [] s
59 in
60 String.concat[
61 "{", String.concatWith "," (List.map toString toks), "}"
62 ]
63 end
64
65 end