1 ;;; std11-parse.el --- STD 11 parser for GNU Emacs
3 ;; Copyright (C) 1995,1996,1997 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.17 1997-09-25 15:01:24 morioka Exp $
9 ;; This file is part of MU (Message Utilities).
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 GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
35 (defconst std11-space-chars " \t\n")
36 (defconst std11-spaces-regexp (` (, (concat "[" std11-space-chars "]+"))))
37 (defconst std11-special-char-list '(?\( ?\) ?< ?> ?@
40 (defconst std11-atom-regexp
41 (` (, (concat "^[^" std11-special-char-list std11-space-chars "]+"))))
43 (defun std11-analyze-spaces (string)
44 (if (and (string-match std11-spaces-regexp string)
45 (= (match-beginning 0) 0))
46 (let ((end (match-end 0)))
47 (cons (cons 'spaces (substring string 0 end))
48 (substring string end)
51 (defun std11-analyze-special (str)
52 (if (and (> (length str) 0)
53 (memq (aref str 0) std11-special-char-list))
54 (cons (cons 'specials (substring str 0 1))
58 (defun std11-analyze-atom (str)
59 (if (string-match std11-atom-regexp str)
60 (let ((end (match-end 0)))
61 (cons (cons 'atom (substring str 0 end))
65 (defun std11-check-enclosure (str open close &optional recursive from)
66 (let ((len (length str))
70 (eq (aref str i) open))
75 (setq chr (aref str i))
88 (setq p (std11-check-enclosure
89 str open close recursive i))
99 (defun std11-analyze-quoted-string (str)
100 (let ((p (std11-check-enclosure str ?\" ?\")))
102 (cons (cons 'quoted-string (substring str 1 (1- p)))
106 (defun std11-analyze-domain-literal (str)
107 (let ((p (std11-check-enclosure str ?\[ ?\])))
109 (cons (cons 'domain-literal (substring str 1 (1- p)))
113 (defun std11-analyze-comment (str)
114 (let ((p (std11-check-enclosure str ?\( ?\) t)))
116 (cons (cons 'comment (substring str 1 (1- p)))
120 (defun std11-lexical-analyze (str)
122 (while (not (string-equal str ""))
124 (or (std11-analyze-quoted-string str)
125 (std11-analyze-domain-literal str)
126 (std11-analyze-comment str)
127 (std11-analyze-spaces str)
128 (std11-analyze-special str)
129 (std11-analyze-atom str)
132 (setq dest (cons (car ret) dest))
142 (defun std11-ignored-token-p (token)
143 (let ((type (car token)))
144 (or (eq type 'spaces)(eq type 'comment))
147 (defun std11-parse-token (lal)
151 (setq token (car lal))
152 (std11-ignored-token-p token)
155 (setq itl (cons token itl))
157 (cons (nreverse (cons token itl))
161 (defun std11-parse-ascii-token (lal)
162 (let (token itl parsed token-value)
164 (setq token (car lal))
165 (or (std11-ignored-token-p token)
166 (if (and (setq token-value (cdr token))
167 (find-non-ascii-charset-string token-value)
172 (setq itl (cons token itl))
175 (setq parsed (nreverse (cons token itl)))
177 (cons parsed (cdr lal))
180 (defun std11-parse-token-or-comment (lal)
184 (setq token (car lal))
185 (eq (car token) 'spaces)
188 (setq itl (cons token itl))
190 (cons (nreverse (cons token itl))
194 (defun std11-parse-word (lal)
195 (let ((ret (std11-parse-ascii-token lal)))
197 (let ((elt (car ret))
200 (if (or (assq 'atom elt)
201 (assq 'quoted-string elt))
202 (cons (cons 'word elt) rest)
205 (defun std11-parse-word-or-comment (lal)
206 (let ((ret (std11-parse-token-or-comment lal)))
208 (let ((elt (car ret))
211 (cond ((or (assq 'atom elt)
212 (assq 'quoted-string elt))
213 (cons (cons 'word elt) rest)
216 (cons (cons 'comment-word elt) rest)
220 (defun std11-parse-phrase (lal)
222 (while (setq ret (std11-parse-word-or-comment lal))
223 (setq phrase (append phrase (cdr (car ret))))
227 (cons (cons 'phrase phrase) lal)
230 (defun std11-parse-local-part (lal)
231 (let ((ret (std11-parse-word lal)))
233 (let ((local-part (cdr (car ret))) dot)
235 (while (and (setq ret (std11-parse-ascii-token lal))
237 (string-equal (cdr (assq 'specials dot)) ".")
238 (setq ret (std11-parse-word (cdr ret)))
240 (append local-part dot (cdr (car ret)))
244 (cons (cons 'local-part local-part) lal)
247 (defun std11-parse-sub-domain (lal)
248 (let ((ret (std11-parse-ascii-token lal)))
250 (let ((sub-domain (car ret)))
251 (if (or (assq 'atom sub-domain)
252 (assq 'domain-literal sub-domain)
254 (cons (cons 'sub-domain sub-domain)
259 (defun std11-parse-domain (lal)
260 (let ((ret (std11-parse-sub-domain lal)))
262 (let ((domain (cdr (car ret))) dot)
264 (while (and (setq ret (std11-parse-ascii-token lal))
266 (string-equal (cdr (assq 'specials dot)) ".")
267 (setq ret (std11-parse-sub-domain (cdr ret)))
269 (append domain dot (cdr (car ret)))
273 (cons (cons 'domain domain) lal)
276 (defun std11-parse-at-domain (lal)
277 (let ((ret (std11-parse-ascii-token lal)) at-sign)
279 (setq at-sign (car ret))
280 (string-equal (cdr (assq 'specials at-sign)) "@")
281 (setq ret (std11-parse-domain (cdr ret)))
283 (cons (cons 'at-domain (append at-sign (cdr (car ret))))
287 (defun std11-parse-addr-spec (lal)
288 (let ((ret (std11-parse-local-part lal))
292 (setq addr (cdr (car ret)))
294 (and (setq ret (std11-parse-at-domain lal))
295 (setq addr (append addr (cdr (car ret))))
298 (cons (cons 'addr-spec addr) lal)
301 (defun std11-parse-route (lal)
302 (let ((ret (std11-parse-at-domain lal))
306 (setq route (cdr (car ret)))
308 (while (and (setq ret (std11-parse-ascii-token lal))
309 (setq comma (car ret))
310 (string-equal (cdr (assq 'specials comma)) ",")
311 (setq ret (std11-parse-at-domain (cdr ret)))
313 (setq route (append route comma (cdr (car ret))))
316 (and (setq ret (std11-parse-ascii-token lal))
317 (setq colon (car ret))
318 (string-equal (cdr (assq 'specials colon)) ":")
319 (setq route (append route colon))
322 (cons (cons 'route route)
327 (defun std11-parse-route-addr (lal)
328 (let ((ret (std11-parse-ascii-token lal))
332 (string-equal (cdr (assq 'specials <)) "<")
334 (progn (and (setq ret (std11-parse-route lal))
335 (setq route (cdr (car ret)))
338 (setq ret (std11-parse-addr-spec lal))
340 (setq addr-spec (cdr (car ret)))
342 (setq ret (std11-parse-ascii-token lal))
344 (string-equal (cdr (assq 'specials >)) ">")
346 (cons (cons 'route-addr (append route addr-spec))
351 (defun std11-parse-phrase-route-addr (lal)
352 (let ((ret (std11-parse-phrase lal)) phrase)
355 (setq phrase (cdr (car ret)))
358 (if (setq ret (std11-parse-route-addr lal))
359 (cons (list 'phrase-route-addr
365 (defun std11-parse-mailbox (lal)
366 (let ((ret (or (std11-parse-phrase-route-addr lal)
367 (std11-parse-addr-spec lal)))
371 (setq mbox (car ret))
373 (if (and (setq ret (std11-parse-token-or-comment lal))
374 (setq comment (cdr (assq 'comment (car ret))))
378 (cons (list 'mailbox mbox comment)
382 (defun std11-parse-group (lal)
383 (let ((ret (std11-parse-phrase lal))
384 phrase colon comma mbox semicolon)
386 (setq phrase (cdr (car ret)))
388 (setq ret (std11-parse-ascii-token lal))
389 (setq colon (car ret))
390 (string-equal (cdr (assq 'specials colon)) ":")
393 (and (setq ret (std11-parse-mailbox lal))
394 (setq mbox (list (car ret)))
397 (while (and (setq ret (std11-parse-ascii-token lal))
398 (setq comma (car ret))
400 (cdr (assq 'specials comma)) ",")
402 (setq ret (std11-parse-mailbox lal))
403 (setq mbox (cons (car ret) mbox))
407 (and (setq ret (std11-parse-ascii-token lal))
408 (setq semicolon (car ret))
409 (string-equal (cdr (assq 'specials semicolon)) ";")
411 (cons (list 'group phrase (nreverse mbox))
416 (defun std11-parse-address (lal)
417 (or (std11-parse-group lal)
418 (std11-parse-mailbox lal)
421 (defun std11-parse-addresses (lal)
422 (let ((ret (std11-parse-address lal)))
424 (let ((dest (list (car ret))))
426 (while (and (setq ret (std11-parse-ascii-token lal))
427 (string-equal (cdr (assq 'specials (car ret))) ",")
428 (setq ret (std11-parse-address (cdr ret)))
430 (setq dest (cons (car ret) dest))
440 (provide 'std11-parse)
442 ;;; std11-parse.el ends here