# paste.myr -rw-r--r-- 3.5 KiB View raw
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
use bio
use date
use std

type parser = struct
	in : bio.file#
	out : bio.file#
	line : int
	col : int
;;

const main = {
	var p : parser = [
		.in = bio.mkfile(std.In, bio.Rd),
		.out = bio.mkfile(std.Out, bio.Wr),
		.line = 1, .col = 1,
	]

	preamble(&p)
	preamblepar(&p)
	documentpar(&p)

	bio.close(p.in)
	bio.close(p.out)
}

const roff = {p : parser#, cmd : byte[:], args : ...
	bio.put(p.out, ".{}", cmd)

	var ap = std.vastart(&args)
	while true
		var ty = std.vatype(&ap)
		match std.typedesc(ty)
		| `std.Tyslice desc:
			match std.typedesc(desc)
			| `std.Tybyte:
				var arg : byte[:] = std.vanext(&ap)
				bio.put(p.out, " \"{}\"")
			| _:
				std.suicide()
			;;
		| `std.Tyvoid:
			break
		| _:
			std.suicide()
		;;
	;;

	bio.put(p.out, "\n")
}

const preamble = {p : parser#
	bio.put(p.out, ".\\\" Generated by scdoc-myr\n")
	bio.put(p.out, ".\\\" Complete documentation for this program is not available as a GNU info page\n")
	// Fix weird quotation marks
	// http://bugs.debian.org/507673
	// http://lists.gnu.org/archive/html/groff/2009-02/msg00013.html
	bio.put(p.out, ".ie \\n(.g .ds Aq \\(aq\n");
	bio.put(p.out, ".el       .ds Aq '\n");
	// Disable hyphenation:
	roff(p, "nh", void);
	// Disable justification:
	roff(p, "ad l", void);
	bio.put(p.out, ".\\\" Begin generated content:\n");
}

const preamblepar = {p : parser#
	var sb = std.mksb()
	var sec = -1

	for ch : bio.bychar(p.in)
		if ch < 0x80 && std.isalnum(ch)
			std.sbputc(sb, ch)
		else
			match ch
			| '(':
				sec = sectionpar(p)
			| '\n':
				if std.sbpeek(sb).len == 0
					std.die("Expected section name")
				;;
				if sec == -1
					std.die("Expected section number")
				;;
				break
			| _:
				if std.sbpeek(sb).len == 0
					std.die("Expected section name")
				;;
			;;
		;;
	;;

	var name = std.sbfin(sb)
	bio.put(p.out, ".TH \"{}\" \"{}\" \"{f=%Y-%m-%d}\"\n",
		name, sec, date.utcnow())
	std.slfree(name)
}

const sectionpar = {p : parser# -> int
	var sb = std.mksb()

	while true
		match bio.peekc(p.in)
		| `std.Ok ch:
			if std.isdigit(ch)
				std.sbputc(sb, ch)
			else
				break
			;;
		| `std.Err err:
			std.die("Unexpected EOF")
		;;
		bio.getc(p.in)
	;;

	var sec
	var str = std.sbfin(sb)
	match std.intparse(str)
	| `std.Some s:
		sec = s
	| `std.None:
		std.die("Invalid section number")
	;;
	std.slfree(str)
	-> sec
}

const documentpar = {p : parser#
	var indent = 0

	while true
		indentpar(p, &indent, true)
		match bio.peekc(p.in)
		| `std.Ok ';':
			bio.getc(p.in)
			match bio.getc(p.in)
			| `std.Ok ' ':
			| _: std.die("Expected space after ;")
			;;
			match bio.readln(p.in)
			| `std.Ok ln: std.slfree(ln)
			| _: std.die("Unexpected EOF")
			;;
		| `std.Ok '#':
			// TODO: Heading
			bio.getc(p.in)
		| `std.Ok '-':
			// TODO: Unordered list
			bio.getc(p.in)
		| `std.Ok '.':
			// TODO: Ordered list
			bio.getc(p.in)
		| `std.Ok '`':
			// TODO: Literal
			bio.getc(p.in)
		| `std.Ok '\n':
			roff(p, "P", void)
			bio.getc(p.in)
		| `std.Ok ' ':
			std.die("Tabs required for indentation")
		| _:
			// TODO: Normal text
			bio.getc(p.in)
		;;
	;;
}

const indentpar = {p : parser#, indent : int#, write : bool -> int
	var i = 0
	while true
		match bio.peekc(p.in)
		| `std.Ok '\t':
			i++
		| `std.Ok ch:
			if ch == '\n' && indent# == 0
				-> indent#
			;;
			break
		| `std.Err err:
			std.die("Unexpected EOF")
		;;
		bio.getc(p.in)
	;;
	if write
		if i - indent# > 1
			std.die("Invalid indent")
		elif i < indent#
			var j
			for j = indent#; j < i; --j;
				roff(p, "RE", void)
			;;
		elif i == indent# + 1
			bio.put(p.out, ".RS 4\n")
		;;
	;;
	indent# = i
	-> i
}