checkargs.py 17.5 KB
Newer Older
1
2
3
4
5
6
7
8
9
# Check @aarg{} and @rarg{} usage in Protocol-A.texi.

import sys
import types

class reader_eof(Exception):
    pass

class reader:
10
11
12
13
14
15
16
17
18
    parens = {'(': ')',
              '[': ']',
              '{': '}'}
    rev_parens = {}
    for k, v in parens.items():
        rev_parens[v] = k
    del v
    del k

19
20
21
22
23
24
    def __init__(self, file):
        self.__filename = file
        self.__file = open(file, "r")
        self.__line_no = 0
        self.__line = []
        self.__eof = 0
25
26
27
        self.__parenstack = []
        self.__errfound = 0
        self.__quoted = 0
28
29
30
31

    def filename(self):
        return self.__filename

32
33
34
35
36
37
38
    def error(self, line_no, errmsg):
        sys.stderr.write("%s:%d:%s\n" % (self.filename(), line_no, errmsg))
        self.__errfound = 1

    def errfound(self):
        return self.__errfound

39
    def ungetc(self, c):
40
41
42
43
44
45
46
47
        if self.rev_parens.has_key(c):
            self.__parenstack.append((self.rev_parens[c], 0))
        if self.parens.has_key(c):
            assert len(self.__parenstack) > 0
            assert self.__parenstack[-1][0] == c
            del self.__parenstack[-1]
        if c == '@':
            self.__quoted = not self.__quoted
48
49
50
51
52
53
54
55
56
57
58
59
60
61
        self.__line.insert(0, c)

    def getc_eofok(self):
        if self.__line == []:
            if self.__eof:
                return None
            line = self.__file.readline()
            if line == '':
                self.__eof = 1
                return None
            self.__line = list(line)
            self.__line_no += 1
        ret = self.__line[0]
        del self.__line[0]
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
        if not self.__quoted:
            if ret in '([{':
                self.__parenstack.append((ret, self.line_no()))
            if ret in '}])':
                if len(self.__parenstack) == 0:
                    self.error(self.line_no(),
                               "unmatched paren ``%s''" % ret)
                else:
                    if self.__parenstack[-1][0] != self.rev_parens[ret]:
                        self.error(self.line_no(),
                                   "badly matched parens ``%s'' ``%s''" %
                                   (self.__parenstack[-1][0], ret))
                    del self.__parenstack[-1]
        if ret == '@':
            self.__quoted = not self.__quoted
        else:
            self.__quoted = 0
79
80
81
82
83
84
85
86
87
88
89
        return ret

    def getc(self):
        c = self.getc_eofok()
        if c == None:
            raise reader_eof
        return c

    def line_no(self):
        return self.__line_no

90
    def check_paren_null(self):
91
92
        for par, line in self.__parenstack:
            self.error(line, "unclosed ``%s''" % par)
93
        self.__parenstack = []
94

95
96
97
98
class lexer:
    def __init__(self, file):
        self.__reader = reader(file)
        self.__findex = None
99
        self.__linkhere = None
100
101
102
103
104
105
106
107
        self.__builtin_types = ["BOOL", "INT8", "INT16", "INT32",
                                "HOLLERITH"]
        self.__builtin_aggregates = ["BITSTRING", "ENUMERATION",
                                     "ARRAY", "SELECTION", "RPC"]
        self.__builtin_typelike = (self.__builtin_types
                                   + self.__builtin_aggregates)
        self.__tindex_seen = {}
        self.__types_used = {}
108
109
110
111
112

    def run(self):
        while 1:
            c = self.__reader.getc_eofok()
            if c == None:
113
                self.__reader.check_paren_null()
114
                return self.__reader.errfound()
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
201
            if c == '@':
                self.__toplevel_at()

    def __toplevel_at(self):
        line_no = self.__reader.line_no()
        c = self.__reader.getc()
        if c in '{}@-"*':
            return
        cmd = ""
        while 1:
            if c.isalpha() or c in '_"':
                cmd = cmd + c
                c = self.__reader.getc()
            elif c in ' \t\n{@=-':
                assert cmd != ''
                if c == '{':
                    arg = self.__read_arg()
                elif c == ' ' and cmd not in ['tab']:
                    arg = self.__read_line()
                else:
                    arg = None
                if c == '@':
                    self.__reader.ungetc(c)

                if hasattr(self, 'toplevel_' + cmd):
                    getattr(self, 'toplevel_' + cmd)(arg, line_no)
                else:
                    self.error(line_no, "unknown command @%s{}" % cmd)
                return
            else:
                self.error(line_no, "bad command ``@%s%s''" % (cmd, c))
                return

    def __read_line(self):
        line = ""
        while 1:
            c = self.__reader.getc()
            if c == '\n':
                return line
            line = line + c

    def ignore(self, arg, line_no):
        pass

    toplevel_setfilename = ignore
    toplevel_settitle = ignore
    toplevel_setchapternewpage = ignore
    toplevel_set = ignore
    toplevel_macro = ignore
    toplevel_code = ignore
    toplevel_end = ignore
    toplevel_ifinfo = ignore
    toplevel_value = ignore
    toplevel_copyright = ignore
    toplevel_iftex = ignore
    toplevel_parindent = ignore
    toplevel_ifinfo = ignore
    toplevel_begin = ignore
    toplevel_titlepage = ignore
    toplevel_title = ignore
    toplevel_subtitle = ignore
    toplevel_author = ignore
    toplevel_font = ignore
    toplevel_ignore = ignore
    toplevel_tensltt = ignore
    toplevel_page = ignore
    toplevel_vskip = ignore
    toplevel_ifnothtml = ignore
    toplevel_contents = ignore
    toplevel_dircategory = ignore
    toplevel_direntry = ignore
    toplevel_ifhtml = ignore
    toplevel_html = ignore
    toplevel_ifnottex = ignore
    toplevel_top = ignore
    toplevel_uref = ignore
    toplevel_menu = ignore
    toplevel_chapter = ignore
    toplevel_uref = ignore
    toplevel_footnote = ignore
    toplevel_email = ignore
    toplevel_penalty = ignore
    toplevel_section = ignore
    toplevel_table = ignore
    toplevel_asis = ignore
    toplevel_item = ignore
    toplevel_req = ignore
202
    toplevel_reqlink = ignore
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
    toplevel_subsection = ignore
    toplevel_itemize = ignore
    toplevel_bullet = ignore
    toplevel_multitable = ignore
    toplevel_tab = ignore
    toplevel_aux = ignore
    toplevel_samp = ignore
    toplevel_pxref = ignore
    toplevel_errorcode = ignore
    toplevel_misc = ignore
    toplevel_type = ignore
    toplevel_field = ignore
    toplevel_dfn = ignore
    toplevel_conftype = ignore
    toplevel_command = ignore
    toplevel_ref = ignore
    toplevel_var = ignore
    toplevel_subsubsection = ignore
    toplevel_priv = ignore
    toplevel_emph = ignore
    toplevel_enumerate = ignore
    toplevel_xref = ignore
    toplevel_itemx = ignore
    toplevel_ae = ignore
    toplevel_i = ignore
    toplevel_subheading = ignore
    toplevel_c = ignore
    toplevel_t = ignore
    toplevel_aa = ignore
    toplevel_async = ignore
    toplevel_unnumbered = ignore
    toplevel_printindex = ignore
    toplevel_example = ignore
236
    toplevel_unmacro = ignore
237
238
239
240
241
242
243
244
245
246
247

    def toplevel_node(self, arg, line_no):
        if self.__findex != None:
            self.__findex = None
            for (argname, [lineno, usage]) in self.__args.items():
                if usage == 0:
                    self.error(lineno,
                               "Undocumented argument ``%s''" % (argname, ))
            self.__findex = None
        self.__node_name = arg
        self.__node_start = line_no
248
249
        self.__assert_no_linkhere()
        self.__reader.check_paren_null()
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281

    def toplevel_findex(self, arg, line_no):
        if self.__node_name != arg:
            self.error(line_no, "@node/@findex mismatch: %s..." % arg)
            self.error(line_no, "...inside node %s" % self.__node_name)
            return
        if self.__findex != None:
            self.error(line_no, "multiple @findex in single @node")
            return
        self.__findex = arg
        self.__args = {}
        if self.__get_token() != '@example':
            self.error(self.__reader.line_no(), "missing @example")
            return
        self.__parse_request()

    def toplevel_rarg(self, arg, line_no):
        if self.__findex == None:
            self.error(line_no, "@rarg outside @findex node")
            return
        if not self.__args.has_key(arg):
            self.error(line_no, "undefined argument ``%s''" % (arg, ))
            return
        self.__args[arg][1] += 1

    def toplevel_aarg(self, arg, line_no):
        pass
        # FIXME

    def toplevel_bye(self, arg, line_no):
        if self.__findex != None:
            self.error(self.__reader.line_no(), "unterminated @findex node")
282
283
        # FIXME: check __tindex_seen and __tindex_used.  They should
        # contain the same keys.  Report any discrepancies.
284

285
286
287
288
289
290
291
292
293
294
295
296
297
298
    def toplevel_reqdlink(self, arg, line_no):
        self.__assert_no_linkhere()
        self.__linkhere = ("@reqdlink{%s}" % arg, line_no)

    def __assert_no_linkhere(self):
        if self.__linkhere != None:
            self.error(self.__linkhere[1],
                       "@linkhere{} after %s missing" % (self.__linkhere[0]))

    def toplevel_linkhere(self, arg, line_no):
        if self.__linkhere == None:
            self.error(line_no, "spurious @linkhere{}")
        self.__linkhere = None

299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
    def __seen_type(self, arg, line_no):
        if self.__tindex_seen.has_key(arg):
            self.error(line_no, "multiple @tindex entries for %s" % arg)
            self.error(self.__tindex_seen[arg], "previous location")
        else:
            self.__tindex_seen[arg] = line_no

    def toplevel_tindex(self, arg, line_no):
        self.__seen_type(arg, line_no)
        if arg in self.__builtin_typelike:
            return

        self.__bad_type(arg)

        self.__tokens = []
        t = self.__get_token()
        while t == '@tindex':
            t = self.__get_token()
            self.__seen_type(t, self.__reader.line_no())
            self.__bad_type(t)
            t = self.__get_token()
        if t != '@example':
            self.error(self.__reader.line_no(),
                       "missing @example after @tindex for defined type")
            return
        self.__parse_userdefined_types()

    def __parse_userdefined_types(self):
        # FIXME
        # FIXME: Check that the defined type has a @tindex above.
        # FIXME: Parse the type.
        pass

332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
    def __parse_request(self):
        self.__tokens = []
        req = self.__get_token()
        if req != self.__findex:
            self.error(self.__reader.line_no(),
                       "wrong request name ``%s''" % req)
            return

        if self.__get_token() != '[':
            self.error(self.__reader.line_no(), "missing ``[''")
            return
            
        nr = self.__get_token()
        if type(nr) != types.IntType:
            self.error(self.__reader.line_no(), "bad request number")

        if self.__get_token() != ']':
            self.error(self.__reader.line_no(), "missing ``]''")
            return

        paren = self.__get_token()
        if paren == '(':
            next = self.__get_token()
            if next != ')':
                self.__unget_token(next)
                self.__parse_request_arg()
                next = self.__get_token()
            if next != ')':
                self.error(self.__reader.line_no(),
                           "missing close parenthesis after arguments")
                return
        elif paren == '((':
            self.__parse_request_arg()
            next = self.__get_token()
            while next == ';':
                self.__parse_request_arg()
                next = self.__get_token()
            if next != '))':
                self.error(self.__reader.line_no(),
                           "missing double close parenthesis after arguments")
                return
        else:
            self.error(self.__reader.line_no(),
                       "missing argument list")
            return

        if self.__get_token() != '->':
            self.error(self.__reader.line_no(), "missing ``->''")
            return

        if self.__get_token() != '(':
            self.error(self.__reader.line_no(), "missing ``('' for result")
            return

        next = self.__get_token()
        if next != ')':
            self.__unget_token(next)
            ret_type = self.__parse_type()
            next = self.__get_token()
        if next != ')':
            self.error(self.__reader.line_no(), "missing ``)'' for result")
            return

        if self.__get_token() != ';':
            self.error(self.__reader.line_no(), "missing final ``;''")
            return

        if self.__get_token() != '@end':
            self.error(self.__reader.line_no(), "extra garbage found")
            return

        return

    def __parse_type(self):
        token = self.__get_token()
        if token == 'ARRAY':
            tp = self.__get_token()
            if self.__bad_type(tp):
                return None
            return token + " " + tp
        else:
            if self.__bad_type(token):
                return None
            return token

    def __bad_type(self, tp):
418
        if tp in self.__builtin_types:
419
            return 0
420
421
422
423
424
425

        # This is an ugly special case, that is a good type name.
        # More or less.
        if tp == "UConference":
            return 0

426
427
428
429
430
        ok = 1
        if len(tp) < 0:
            ok = 0
        if ok and not tp[0].isupper():
            ok = 0
431
        upper = 0
432
        for c in tp[1:]:
433
434
435
436
437
438
439
440
441
            if upper:
                if c == '-' or not c.isupper():
                    ok = 0
                upper = 0
            else:
                if c == '-':
                    upper = 1
                elif not c.islower():
                    ok = 0
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
        if ok and tp[-1] == '-':
            ok = 0
        if not ok:
            self.error(self.__reader.line_no(), "bad type name ``%s''" % (tp,))
        return not ok

    def __bad_arg(self, arg):
        ok = 1
        if len(arg) < 0:
            ok = 0
        if ok and not arg[0].islower():
            ok = 0
        for c in arg[1:]:
            if not c.islower() and c != '-':
                ok = 0
        if ok and arg[-1] == '-':
            ok = 0
        if not ok:
            if arg == '))':
461
                self.__unget_token(arg)
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
                self.error(self.__reader.line_no(),
                           "extra semicolon after last arg")
            else:
                self.error(self.__reader.line_no(),
                           "bad argument ``%s''" % (arg,))
        return not ok

    def __parse_request_arg(self):
        argname = self.__get_token()
        if self.__bad_arg(argname):
            return
        if self.__get_token() != ':':
            self.error(self.__reader.line_no(), "missing ``:'' after argument")
            return
        tp = self.__parse_type()
        if tp == None:
            return
        if self.__args.has_key(argname):
            self.error(self.__reader.line_no(),
                       "argument name ``%s'' used twice" % (argname, ))
            return
        self.__args[argname] = [self.__reader.line_no(), 0]

    def __unget_token(self, token):
        self.__tokens.insert(0, token)

    def __get_token(self):
        if len(self.__tokens) > 0:
            res = self.__tokens[0]
            del self.__tokens[0]
            return res
        c = self.__reader.getc()
        while c.isspace():
            c = self.__reader.getc()
        if c.isalpha():
            res = c
            while 1:
                c = self.__reader.getc()
                if not c.isalpha() and c not in "-" and not c.isdigit():
                    if c.isdigit():
                        self.error(self.__reader.line_no(),
                                   "bad token ``%s''" % (res + c))
                    self.__reader.ungetc(c)
                    return res
                res = res + c
        elif c in '[];:':
            return c
        elif c in '()':
            d = self.__reader.getc()
            if c != d:
                self.__reader.ungetc(d)
                return c
            else:
                return c + d
        elif c.isdigit():
            res = c
            while 1:
                c = self.__reader.getc()
                if not c.isdigit():
                    if c.isalpha():
                        self.error(self.__reader.line_no(),
                                   "bad token ``%s''" % (res + c))
                    self.__reader.ungetc(c)
                    if res[0] == '0' and len(res) > 1:
                        self.error(self.__reader.line_no(),
                                   "bad number ``%s''" % (res,))
                    return int(res)
                res = res + c
        elif c == '-':
            d = self.__reader.getc()
            if d == '>':
                return '->'
            else:
                self.error(self.__reader.line_no(),
                           "bad token ``%s%s''" % (c, d))
                self.__reader.ungetc(d)
                return c
        elif c == '@':
            res = c
            while 1:
                c = self.__reader.getc()
                if not c.isalpha():
                    self.__reader.ungetc(c)
                    return res
                res = res + c
        else:
            self.error(self.__reader.line_no(),
                       "bad character ``%s''" % (c,))
            return c
        
                
    def __read_arg(self):
        arg = ""
        while 1:
            c = self.__reader.getc()
            if c == '}':
                return arg
            else:
                arg = arg + c

    def error(self, line_no, errmsg):
563
        self.__reader.error(line_no, errmsg)
564
565
566
567

if __name__ == '__main__':
    l = lexer(sys.argv[1])
    sys.exit(l.run())