-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse-bsd.scm
64 lines (50 loc) · 1.74 KB
/
parse-bsd.scm
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
;;; Parse syscalls.master
(import (scheme base) (scheme write))
;;
(define (read-eof-object)
(let ((char (peek-char)))
(and (eof-object? char) char)))
(define (read-the-char goal)
(let ((char (peek-char)))
(and (not (eof-object? char))
(char=? goal char)
(read-char))))
(define (read-matching-char match?)
(let ((char (peek-char)))
(and (not (eof-object? char))
(match? char)
(read-char))))
(define (read-matching-chars match?)
(let loop ((chars #f))
(let ((char (read-matching-char match?)))
(if (not char) (and chars (list->string (reverse chars)))
(loop (cons char (or chars '())))))))
;;
(define (char-horizontal? char)
(not (char=? char #\newline)))
(define (char-punctuation? char)
(case char ((#\< #\> #\{ #\} #\( #\) #\[ #\] #\. #\, #\; #\* #\- #\|) #t) (else #f)))
(define (char-identifier? char)
(or (char<=? #\0 char #\9)
(char<=? #\A char #\Z)
(char<=? #\a char #\z)
(case char ((#\_ #\# #\$ #\. #\/) #t) (else #f))))
(define (skip-whitespace-and-comments)
(cond ((read-matching-char char-whitespace?)
(skip-whitespace-and-comments))
((read-the-char #\;)
(let loop ()
(if (read-matching-char char-horizontal?) (loop)
(skip-whitespace-and-comments))))))
(define (read-token)
(skip-whitespace-and-comments)
(or (read-eof-object)
(read-matching-char char-punctuation?)
(read-matching-chars char-identifier?)
(error "Huh?" (read-char))))
(define (read-all-tokens)
(let loop ((tokens '()))
(let ((token (read-token)))
(if (eof-object? token) (reverse tokens) (loop (cons token tokens))))))
(define (writeln x) (write x) (newline))
(for-each writeln (read-all-tokens))