-
Notifications
You must be signed in to change notification settings - Fork 0
/
ybot_parser.xtm
125 lines (102 loc) · 3.83 KB
/
ybot_parser.xtm
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
;; (if (and (defined? '*xtmlib-ybot-parser-loaded*) *xtmlib-ybot-parser-loaded*)
;; (sys:load-escape "ybot_parser library already loaded"))
;; (define *xtmlib-ybot-parser-loaded* #f)
;; (define *ybot-lib-dir* "/Users/s2805534/Dropbox/ybot/src/extempore_libs")
;; (if (not (defined? '*ybot-lib-dir*))
;; (sys:load-escape "Set the *ybot-lib-dir* variable before loading this library"))
;; (sys:load (string-append *ybot-lib-dir* "/ybot_adt.xtm"))
;; (sys:load (string-append *ybot-lib-dir* "/ybot_filesystem.xtm"))
;; (sys:load (string-append *ybot-lib-dir* "/ybot_dynamic_buffer.xtm"))
(sys:load-preload-check 'ybot_parser)
(define *xtmlib-ybot_parser-loaded* #f)
(impc:aot:suppress-aot-do
(sys:load "libs/contrib/ybot/ybot_adt.xtm")
(sys:load "libs/contrib/ybot/ybot_filesystem.xtm")
(sys:load "libs/contrib/ybot/ybot_dynamic_buffer.xtm"))
(impc:aot:insert-forms
(sys:load "libs/contrib/ybot/ybot_adt.xtm" 'quiet)
(sys:load "libs/contrib/ybot/ybot_filesystem.xtm" 'quiet)
(sys:load "libs/contrib/ybot/ybot_dynamic_buffer.xtm" 'quiet))
;; Character streams
(bind-type CharStream <apr_file_t*,bool*>)
(bind-func end_of_stream:[bool,CharStream*]*
(lambda (stream)
(@ (tref stream 1))))
(bind-func next:[i8,CharStream*]*
(lambda (stream)
(yfs_read_byte (tref stream 0) (tref stream 1))))
(bind-func peek:[i8,CharStream*]*
(lambda (stream)
(yfs_peek_byte (tref stream 0))))
(bind-func goto:[bool,CharStream*,i64]*
(lambda (stream index)
(let ((new_index:i64 (yfs_file_seek (tref stream 0) index)))
(= new_index index))))
(bind-func goto_start:[bool,CharStream*]*
(lambda (stream)
(goto stream 0)))
(bind-func playhead:[i64,CharStream*]*
(lambda (stream)
(yfs_current_playhead (tref stream 0))))
(bind-func step_back:[bool,CharStream*]*
(lambda (stream)
(goto stream (- (playhead stream) 1))))
(bind-func skip_whitespace:[bool,CharStream*]*
(lambda (stream)
(cond
((not (end_of_stream stream))
(let ((c:i8 (next stream)))
(cond ;; space tab CR LF
((or (= c 32:i8) (= c 9:i8) (= c 13:i8) (= c 10:i8))
(skip_whitespace stream))
(else
(step_back stream)
#t))))
(else #f))))
(bind-func look_ahead:[String*,CharStream*,i64]*
(lambda (stream:CharStream* n:i64)
(let* ((i:i64 0)
(ahead:i8* (salloc (+ n 1)))
(look_loop:[bool,i64]*
(lambda (cc)
(cond
((end_of_stream stream) #f)
((<= n cc) #t)
(else
(pset! ahead cc (next stream))
(look_loop (+ cc 1)))))))
(cond
((look_loop 0)
(pset! ahead n 0:i8)
(goto stream (- (playhead stream) n))
(String ahead))
(else
(goto stream (- (playhead stream) n))
null)))))
(bind-func look_ahead_match:[bool,CharStream*,String*]*
(lambda (stream:CharStream* str:String*)
(cond
((or (null? str) (null? stream)) #f)
(else
(let* ((i:i64 0) (n:i64 (length str))
(ahead:i8* (salloc (+ n 1)))
(look_loop:[bool,i64]*
(lambda (cc)
(cond
((end_of_stream stream) #f)
((<= n cc) #t)
(else
(pset! ahead cc (next stream))
(look_loop (+ cc 1)))))))
(cond
((look_loop 0)
(pset! ahead n 0:i8)
(goto stream (- (playhead stream) n))
(= 0:i32 (strcmp ahead (cstring str))))
(else
(goto stream (- (playhead stream) n))
#f)))))))
(bind-alias parser_t [bool,CharStream*]*)
;; Halting conditions for contextual parsers
(bind-alias halting_cond_t [bool,i8]*)
(define *xtmlib-ybot_parser-loaded* #t)