1 ;;; std11-parse.el --- STD 11 parser for GNU Emacs
3 ;; Copyright (C) 1995,1996 Free Software Foundation, Inc.
5 ;; Author: MORIOKA Tomohiko <morioka@jaist.ac.jp>
6 ;; Keywords: mail, news, RFC 822, STD 11
7 ;; Version: $Id: std11-parse.el,v 0.11 1996-08-28 20:54:03 morioka Exp $
9 ;; This file is part of tl (Tiny Library).
11 ;; This program is free software; you can redistribute it and/or
12 ;; modify it under the terms of the GNU General Public License as
13 ;; published by the Free Software Foundation; either version 2, or (at
14 ;; your option) any later version.
16 ;; This program is distributed in the hope that it will be useful, but
17 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 ;; General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with This program; see the file COPYING. If not, write to
23 ;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
30 (autoload 'find-charset-string "emu")
36 (defconst std11-space-chars " \t\n")
37 (defconst std11-spaces-regexp (concat "^[" std11-space-chars "]+"))
38 (defconst std11-special-chars "][()<>@,;:\\<>.\"")
39 (defconst std11-atom-regexp
40 (concat "^[^" std11-special-chars std11-space-chars "]+"))
42 (defun std11-analyze-spaces (str)
43 (if (string-match std11-spaces-regexp str)
44 (let ((end (match-end 0)))
45 (cons (cons 'spaces (substring str 0 end))
49 (defun std11-analyze-special (str)
50 (if (and (> (length str) 0)
51 (find (aref str 0) std11-special-chars)
53 (cons (cons 'specials (substring str 0 1))
57 (defun std11-analyze-atom (str)
58 (if (string-match std11-atom-regexp str)
59 (let ((end (match-end 0)))
60 (cons (cons 'atom (substring str 0 end))
64 (defun std11-check-enclosure (str open close &optional recursive from)
65 (let ((len (length str))
69 (eq (aref str i) open))
74 (setq chr (aref str i))
87 (setq p (std11-check-enclosure
88 str open close recursive i))
98 (defun std11-analyze-quoted-string (str)
99 (let ((p (std11-check-enclosure str ?\" ?\")))
101 (cons (cons 'quoted-string (substring str 1 (1- p)))
105 (defun std11-analyze-domain-literal (str)
106 (let ((p (std11-check-enclosure str ?\[ ?\])))
108 (cons (cons 'domain-literal (substring str 1 (1- p)))
112 (defun std11-analyze-comment (str)
113 (let ((p (std11-check-enclosure str ?\( ?\) t)))
115 (cons (cons 'comment (substring str 1 (1- p)))
119 (defun std11-lexical-analyze (str)
121 (while (not (string-equal str ""))
123 (or (std11-analyze-quoted-string str)
124 (std11-analyze-domain-literal str)
125 (std11-analyze-comment str)
126 (std11-analyze-spaces str)
127 (std11-analyze-special str)
128 (std11-analyze-atom str)
131 (setq dest (cons (car ret) dest))
141 (defun std11-ignored-token-p (token)
142 (let ((type (car token)))
143 (or (eq type 'spaces)(eq type 'comment))
146 (defun std11-parse-token (lal)
150 (setq token (car lal))
151 (std11-ignored-token-p token)
154 (setq itl (cons token itl))
156 (cons (nreverse (cons token itl))
160 (defun std11-parse-ascii-token (lal)
161 (let (token itl parsed token-value)
163 (setq token (car lal))
164 (if (and (setq token-value (cdr token))
165 (find-charset-string token-value)
168 (std11-ignored-token-p token)
171 (setq itl (cons token itl))
174 (setq parsed (nreverse (cons token itl)))
176 (cons parsed (cdr lal))
179 (defun std11-parse-token-or-comment (lal)
183 (setq token (car lal))
184 (eq (car token) 'spaces)
187 (setq itl (cons token itl))
189 (cons (nreverse (cons token itl))
193 (defun std11-parse-word (lal)
194 (let ((ret (std11-parse-ascii-token lal)))
196 (let ((elt (car ret))
199 (if (or (assq 'atom elt)
200 (assq 'quoted-string elt))
201 (cons (cons 'word elt) rest)
204 (defun std11-parse-word-or-comment (lal)
205 (let ((ret (std11-parse-token-or-comment lal)))
207 (let ((elt (car ret))
210 (cond ((or (assq 'atom elt)
211 (assq 'quoted-string elt))
212 (cons (cons 'word elt) rest)
215 (cons (cons 'comment-word elt) rest)
219 (defun std11-parse-phrase (lal)
221 (while (setq ret (std11-parse-word-or-comment lal))
222 (setq phrase (append phrase (cdr (car ret))))
226 (cons (cons 'phrase phrase) lal)
229 (defun std11-parse-local-part (lal)
230 (let ((ret (std11-parse-word lal)))
232 (let ((local-part (cdr (car ret))) dot)
234 (while (and (setq ret (std11-parse-ascii-token lal))
236 (string-equal (cdr (assq 'specials dot)) ".")
237 (setq ret (std11-parse-word (cdr ret)))
239 (append local-part dot (cdr (car ret)))
243 (cons (cons 'local-part local-part) lal)
246 (defun std11-parse-sub-domain (lal)
247 (let ((ret (std11-parse-ascii-token lal)))
249 (let ((sub-domain (car ret)))
250 (if (or (assq 'atom sub-domain)
251 (assq 'domain-literal sub-domain)
253 (cons (cons 'sub-domain sub-domain)
258 (defun std11-parse-domain (lal)
259 (let ((ret (std11-parse-sub-domain lal)))
261 (let ((domain (cdr (car ret))) dot)
263 (while (and (setq ret (std11-parse-ascii-token lal))
265 (string-equal (cdr (assq 'specials dot)) ".")
266 (setq ret (std11-parse-sub-domain (cdr ret)))
268 (append domain dot (cdr (car ret)))
272 (cons (cons 'domain domain) lal)
275 (defun std11-parse-at-domain (lal)
276 (let ((ret (std11-parse-ascii-token lal)) at-sign)
278 (setq at-sign (car ret))
279 (string-equal (cdr (assq 'specials at-sign)) "@")
280 (setq ret (std11-parse-domain (cdr ret)))
282 (cons (cons 'at-domain (append at-sign (cdr (car ret))))
286 (defun std11-parse-addr-spec (lal)
287 (let ((ret (std11-parse-local-part lal))
291 (setq addr (cdr (car ret)))
293 (and (setq ret (std11-parse-at-domain lal))
294 (setq addr (append addr (cdr (car ret))))
297 (cons (cons 'addr-spec addr) lal)
300 (defun std11-parse-route (lal)
301 (let ((ret (std11-parse-at-domain lal))
305 (setq route (cdr (car ret)))
307 (while (and (setq ret (std11-parse-ascii-token lal))
308 (setq comma (car ret))
309 (string-equal (cdr (assq 'specials comma)) ",")
310 (setq ret (std11-parse-at-domain (cdr ret)))
312 (setq route (append route comma (cdr (car ret))))
315 (and (setq ret (std11-parse-ascii-token lal))
316 (setq colon (car ret))
317 (string-equal (cdr (assq 'specials colon)) ":")
318 (setq route (append route colon))
321 (cons (cons 'route route)
326 (defun std11-parse-route-addr (lal)
327 (let ((ret (std11-parse-ascii-token lal))
331 (string-equal (cdr (assq 'specials <)) "<")
333 (progn (and (setq ret (std11-parse-route lal))
334 (setq route (cdr (car ret)))
337 (setq ret (std11-parse-addr-spec lal))
339 (setq addr-spec (cdr (car ret)))
341 (setq ret (std11-parse-ascii-token lal))
343 (string-equal (cdr (assq 'specials >)) ">")
345 (cons (cons 'route-addr (append route addr-spec))
350 (defun std11-parse-phrase-route-addr (lal)
351 (let ((ret (std11-parse-phrase lal)) phrase)
354 (setq phrase (cdr (car ret)))
357 (if (setq ret (std11-parse-route-addr lal))
358 (cons (list 'phrase-route-addr
364 (defun std11-parse-mailbox (lal)
365 (let ((ret (or (std11-parse-phrase-route-addr lal)
366 (std11-parse-addr-spec lal)))
370 (setq mbox (car ret))
372 (if (and (setq ret (std11-parse-token-or-comment lal))
373 (setq comment (cdr (assq 'comment (car ret))))
377 (cons (list 'mailbox mbox comment)
381 (defun std11-parse-group (lal)
382 (let ((ret (std11-parse-phrase lal))
383 phrase colon comma mbox semicolon)
385 (setq phrase (cdr (car ret)))
387 (setq ret (std11-parse-ascii-token lal))
388 (setq colon (car ret))
389 (string-equal (cdr (assq 'specials colon)) ":")
392 (and (setq ret (std11-parse-mailbox lal))
393 (setq mbox (list (car ret)))
396 (while (and (setq ret (std11-parse-ascii-token lal))
397 (setq comma (car ret))
399 (cdr (assq 'specials comma)) ",")
401 (setq ret (std11-parse-mailbox lal))
402 (setq mbox (cons (car ret) mbox))
406 (and (setq ret (std11-parse-ascii-token lal))
407 (setq semicolon (car ret))
408 (string-equal (cdr (assq 'specials semicolon)) ";")
410 (cons (list 'group phrase (nreverse mbox))
415 (defun std11-parse-address (lal)
416 (or (std11-parse-group lal)
417 (std11-parse-mailbox lal)
420 (defun std11-parse-addresses (lal)
421 (let ((ret (std11-parse-address lal)))
423 (let ((dest (list (car ret))))
425 (while (and (setq ret (std11-parse-ascii-token lal))
426 (string-equal (cdr (assq 'specials (car ret))) ",")
427 (setq ret (std11-parse-address (cdr ret)))
429 (setq dest (cons (car ret) dest))
439 (provide 'std11-parse)
441 ;;; std11-parse.el ends here