Debian Codegraph @

lib/mlrisc-lib/MLRISC/Tools/PP/pp.sml

raw
1 structure PP :> PP =
2 struct
3
4 val TABSPACE = 3
5
6 datatype tok = STRING | NUM | SYM | TOK | SPACE | NEWLINE
7 datatype pps = PP of { buf : string list ref,
8 tabs : indent list ref,
9 modes : mode list ref,
10 col : col ref,
11 tok : tok ref,
12 width : col ref
13 }
14 withtype indent = int
15 and col = int
16 and mode = string
17 and pp = pps -> unit
18
19 infix ++
20
21 fun (f ++ g) S = (f S; g S)
22
23 fun nop S = ()
24
25 fun emit(PP{buf, col, tok, ...},s,t) =
26 (buf := s :: !buf; col := !col + size s; tok := t)
27
28 fun spaceIf p (pps as PP{tok, ...}) =
29 if p (!tok) then emit(pps," ",SPACE) else ()
30
31 val sp = spaceIf(fn (SPACE | NEWLINE) => false | _ => true)
32 val space = spaceIf(fn (SPACE | NEWLINE | SYM) => false | _ => true)
33 fun $ s pps = (space pps; emit(pps, s, TOK))
34 fun $$ s pps = emit(pps, s, SYM)
35 val bool = $ o Bool.toString
36 fun string s pps = emit(pps,"\""^String.toString s^"\"",STRING)
37 fun char c pps = emit(pps,"#\""^Char.toString c^"\"",STRING)
38 fun num n pps = (space pps; emit(pps,n,NUM))
39 val int = num o Int.toString
40 val int32 = num o Int32.toString
41 val real = num o Real.toString
42 val intinf = num o IntInf.toString
43 val word = num o (fn w => "0wx"^Word.toString w)
44 val word32 = num o (fn w => "0wx"^Word32.toString w)
45 fun tab' offset (pps as PP{tabs, col, ...}) =
46 let val at = (case !tabs of i::_ => i | _ => 0) + offset
47 val n = at - !col
48 in if n <= 0 then () else emit(pps,StringCvt.padLeft #" " n "",SPACE)
49 end
50 val tab = tab' 0
51 fun indent (PP{tabs, ...}) =
52 case !tabs of
53 [] => tabs := [TABSPACE]
54 | t::_ => tabs := (t+TABSPACE) :: !tabs
55 fun settab (PP{tabs, col, ...}) = tabs := !col :: !tabs
56 fun unindent (PP{tabs as ref(_::t), ...}) = tabs := t
57 | unindent _ = raise Fail "unindent"
58 fun setmode m (PP{modes, ...}) = modes := m :: !modes
59 fun unsetmode (PP{modes as ref(_::m), ...}) = modes := m
60 | unsetmode _ = raise Fail "unsetmode"
61 fun select f (pps as PP{modes=ref(m::_), ...}) = f m pps
62 | select _ _ = raise Fail "select"
63 fun nl (PP{buf, col, tok, ...}) =
64 (buf := "\n" :: !buf; col := 0; tok := NEWLINE)
65 fun nl' (offset,indent) (pps as PP{col, width, ...}) =
66 if !col >= !width - offset
67 then (nl pps; tab' indent pps)
68 else ()
69 fun textWidth w (PP{width, ...}) = width := w
70
71 fun seq (l,sep,r) pps =
72 let fun f [] = nop
73 | f [a] = a
74 | f(a::b) = a ++ sep ++ f b
75 in l ++ f pps ++ r end
76 fun concat pps = foldr op++ nop pps
77 fun block pp = indent ++ pp ++ unindent
78 fun line pp = tab ++ pp ++ nl
79 fun paren pp = $$ "(" ++ pp ++ $$ ")"
80 fun group(l,r) pp = settab ++ $$ l ++ settab ++ pp ++
81 unindent ++ tab ++ $$ r ++ unindent
82 fun text pp =
83 let val buf = ref []
84 val pps = PP{buf=buf, tabs=ref [], modes=ref ["pretty"],
85 col=ref 0, tok=ref NEWLINE, width=ref 80}
86 in pp pps;
87 String.concat(rev(! buf))
88 end
89
90 val ! = $
91 val !! = $$
92
93 end