aboutsummaryrefslogtreecommitdiff
path: root/src/parse.scm
blob: e5b3ae32ae379e9b40df8a9db0c1e7c27674f50b (plain)
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
201
202
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

(define-module (parse)
  :use-module (rnrs io ports)
  :use-module (rnrs bytevectors)
  :use-module (srfi srfi-9)

  )


(define-record-type <vcomponent>
  (make-vcomponent% type children parent attributes)
  vcomponent?
  (type component-type)
  (children get-component-children set-component-children!)
  (parent get-component-parent set-component-parent!)
  (attributes get-component-attributes))

(define* (make-vcomponent #:optional (type 'VIRTUAL))
  (make-vcomponent% type '() #f (make-hash-table #x10)))

(define (add-child! parent child)
  (set-component-children! parent (cons child (get-component-children parent)))
  (set-component-parent! child parent))

(define (set-attribute! component key value)
  (let ((ht (get-component-attributes component)))
   (cond [(hashq-ref ht key #f)
          => (lambda (pair) (set-cdr! pair value))]
         [else (hashq-set! ht key (cons (make-hash-table) value))])))




(define contexts '(key value param-name param-value escape))


(define-record-type <parse-ctx>
  (make-parse-ctx% row col ctx line-key param-key param-table)
  parse-ctx?
  (row get-row set-row!)
  (col get-col set-col!)
  (ctx get-ctx set-ctx!)
  (line-key get-line-key set-line-key!)
  (param-key get-param-key set-param-key!)
  (param-table get-param-table set-param-table!)
  )

(define (make-parse-ctx)
  (make-parse-ctx% 0 0 'key
                   #f #f (make-hash-table)))

(define (increment-column! ctx)
  (set-col! ctx (1+ (get-col ctx))))

(define (increment-row! ctx)
  (set-col! ctx 0)
  (set-row! ctx (1+ (get-row ctx))))


(define-record-type <strbuf>
  (make-strbuf% len bytes)
  strbuf?
  (len get-length set-length!)
  (bytes get-bytes)
  )

(define (make-strbuf)
  (make-strbuf% 0 (make-u8vector #x1000))
  )

(define (strbuf->string strbuf)
  (let ((bv (make-u8vector (get-length strbuf))))
    (bytevector-copy! (get-bytes strbuf) 0
                      bv 0
                      (get-length strbuf))
    (bytevector->string bv (native-transcoder))))  ; TODO charset

(define (strbuf-reset! strbuf)
  (set-length! strbuf 0))

(define (strbuf-append! strbuf u8)
  (u8vector-set! (get-bytes strbuf)
                 (get-length strbuf)
                 u8)
  (set-length! strbuf (1+ (get-length strbuf))))

(define (fold-proc ctx c)
  (let ((pair (cons (if (= c (char->integer #\newline))
                        c (get-u8 (current-input-port)))
                    (get-u8 (current-input-port)))))
    (increment-row! ctx)
    (cond [(not (= (char->integer #\newline)
                   (car pair)))
           ;; ERROR expected newline after CR
            'error
           ]

          [(memv (integer->char (cdr pair)) '(#\space #\tab))
           (increment-column! ctx)
           'fold
           ]

          #;
          [ungetc...
          'writeback-error
          ]

          [else
           ;; ...
           'end-of-line
           ]

          )))

(define (parse-file filename file root)
  (set-current-input-port file)
  (let ((component root)
        (ctx (make-parse-ctx))
        (strbuf (make-strbuf)))
    (catch #t
      (lambda ()
       (while #t
         (let ((c (get-u8 (current-input-port))))
           (cond

            [(eof-object? c)
             (break)]

            [(memv (integer->char c) '(#\return #\newline))
             (case (fold-proc ctx c)
               [(error writeback-error) => (lambda (t) (throw t))]
               [(end-of-line)
                (let ((str (strbuf->string strbuf)))
                  (cond [(string=? (get-line-key ctx) "BEGIN")
                         (let ((child (make-vcomponent (string->symbol str))))
                           (add-child! component child)
                           (set! component child))]

                        [(string=? (get-line-key ctx) "END")
                         (set! component (get-component-parent component))]

                        [else
                         (let ((ht (get-component-attributes component)))
                           ;; TODO repeated keys
                           (hashq-set! ht (string->symbol (get-line-key ctx))
                                       (cons (get-param-table ctx)
                                             str))
                           (set-param-table! ctx (make-hash-table)))])

                  (strbuf-reset! strbuf)
                  (set-ctx! ctx 'key))])]

            [(char=? (integer->char c) #\\)
             (let ((cc (get-u8 (current-input-port))))
               (case cc
                 [(#\return #\newline) ;; TODO fold?
                  (fold-proc ctx cc)
                  ]
                 [(#\n #\N)
                  (strbuf-append! strbuf (char->integer #\newline))]
                 [(#\; #\, #\\) => (lambda (c) (strbuf-append! strbuf c))]
                 [else 'err]
                 )
               (increment-column! ctx))]

            [(and (eq? (get-ctx ctx) 'panam-name) (char=? (integer->char c) #\=))
             (set-param-key! ctx (strbuf->string strbuf))
             (strbuf-reset! strbuf)
             (set-ctx! ctx 'param-value)]

            [(memv (integer->char c) '(#\: #\;))
             (case (get-ctx ctx)
               [(param-value)
                (hashq-set! (get-param-table ctx)
                            (get-param-key ctx)
                            (strbuf->string strbuf))
                (strbuf-reset! strbuf)]
               [(key)
                (set-line-key! ctx (strbuf->string strbuf))
                (strbuf-reset! strbuf)])
             (set-ctx! ctx (case c
                             [(#\:) 'value]
                             [(#\;) 'param-name]))]

            [else
             (strbuf-append! strbuf c)
             (increment-column! ctx)
             ]))))
     (lambda (err . args)
       (format #t "err = ~a~%ctx = ~a~%args = ~a~%"
               err ctx args)
       ))))


;;; TODO

(define (open-ics path cal)
  (define f (open-input-file path))
  (parse-file path f cal))

(define (handle-dir cal path)
  'TODO
  ;; TODO
  )

(define (handle-file cal path)
  (set-attribute! cal 'X-HNH-SOURCETYPE "file")
  (open-ics path cal)
  )


(define (read-vcalendar root path)
  (define st (stat path))
  (case (stat:type st)
    [(regular) (handle-file root path)]
    [(directory) (handle-dir root path)]
    [(block-special char-special fifo socket unknown symlink)
     => (lambda (t) (throw t))])
  )

(define (parse-cal-path path)
  (define root (make-vcomponent))
  (read-vcalendar root path)
  root)


(define root (parse-cal-path "/home/hugo/.local/var/cal/STABEN/599ca4a2f8eda362aaac598c999321dcc8004780a1d5cef36019c7e421b70b08.ics"))

(format #t "root = ~a~%" root)