Home My Page Projects Code Snippets Project Openings SML/NJ
Summary Activity Forums Tracker Lists Tasks Docs Surveys News SCM Files

SCM Repository

[smlnj] Annotation of /smlnj-lib/trunk/PP/devices/char-buffer-pp.sml
ViewVC logotype

Annotation of /smlnj-lib/trunk/PP/devices/char-buffer-pp.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 4898 - (view) (download)

1 : jhr 4898 (* char-buffer-pp.sml
2 :     *
3 :     * COPYRIGHT (c) 2018 The Fellowship of SML/NJ (http://www.smlnj.org)
4 :     * All rights reserved.
5 :     *
6 :     * A pretty printer that puts its output in a CharBuffer.buf object. There
7 :     * are no styles and tokens are strings. You can use this module to pretty-print
8 :     * into a string as follows:
9 :     *
10 :     * val buf = CharBuffer.new 1024
11 :     * val ppStrm = CharBufferPP.openOut {dst = buf, wid = 80}
12 :     * .... pretty printing ....
13 :     * val result = CharBuffer.contents buf
14 :     *)
15 :    
16 :     structure CharBufferPP : sig
17 :    
18 :     include PP_STREAM
19 :     where type token = string
20 :    
21 :     val openOut : {dst : CharBuffer.buf, wid : int} -> stream
22 :    
23 :     end = struct
24 :    
25 :     structure Device = struct
26 :    
27 :     datatype device = DEV of {
28 :     dst : CharBuffer.buf,
29 :     wid : int
30 :     }
31 :    
32 :     (* no style support *)
33 :     type style = unit
34 :     fun sameStyle _ = true
35 :     fun pushStyle _ = ()
36 :     fun popStyle _ = ()
37 :     fun defaultStyle _ = ()
38 :    
39 :     val openDev = DEV
40 :    
41 :     (* maximum printing depth (in terms of boxes) *)
42 :     fun depth _ = NONE
43 :    
44 :     (* the width of the device *)
45 :     fun lineWidth (DEV{wid, ...}) = SOME wid
46 :     (* the suggested maximum width of text on a line *)
47 :     fun textWidth _ = NONE
48 :    
49 :     (* output some number of spaces to the device *)
50 :     fun space (DEV{dst, ...}, n) = CharBuffer.addVec (dst, StringCvt.padLeft #" " n "")
51 :    
52 :     (* output a new-line to the device *)
53 :     fun newline (DEV{dst, ...}) = CharBuffer.add1 (dst, #"\n")
54 :    
55 :     (* output a string/character in the current style to the device *)
56 :     fun string (DEV{dst, ...}, s) = CharBuffer.addVec (dst, s)
57 :     fun char (DEV{dst, ...}, c) = CharBuffer.add1 (dst, c)
58 :    
59 :     (* nothing to flush *)
60 :     fun flush _ = ()
61 :     end
62 :    
63 :     structure PP = PPStreamFn (
64 :     structure Token = StringToken
65 :     structure Device = Device)
66 :    
67 :     open PP
68 :    
69 :     fun openOut arg = openStream(Device.openDev arg)
70 :    
71 :     end;
72 :    

root@smlnj-gforge.cs.uchicago.edu
ViewVC Help
Powered by ViewVC 1.0.0