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 1.1 1998-02-04 07:21:11 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 '(?\] ?\[
41 (defconst std11-atom-regexp
42 (` (, (concat "^[^" std11-special-char-list std11-space-chars "]+"))))
44 (defun std11-analyze-spaces (string)
45 (if (and (string-match std11-spaces-regexp string)
46 (= (match-beginning 0) 0))
47 (let ((end (match-end 0)))
48 (cons (cons 'spaces (substring string 0 end))
49 (substring string end)
52 (defun std11-analyze-special (str)
53 (if (and (> (length str) 0)
54 (memq (aref str 0) std11-special-char-list))
55 (cons (cons 'specials (substring str 0 1))
59 (defun std11-analyze-atom (str)
60 (if (string-match std11-atom-regexp str)
61 (let ((end (match-end 0)))
62 (cons (cons 'atom (substring str 0 end))
66 (defun std11-check-enclosure (str open close &optional recursive from)
67 (let ((len (length str))
71 (eq (aref str i) open))
76 (setq chr (aref str i))
89 (setq p (std11-check-enclosure
90 str open close recursive i))
100 (defun std11-analyze-quoted-string (str)
101 (let ((p (std11-check-enclosure str ?\" ?\")))
103 (cons (cons 'quoted-string (substring str 1 (1- p)))
107 (defun std11-analyze-domain-literal (str)
108 (let ((p (std11-check-enclosure str ?\[ ?\])))
110 (cons (cons 'domain-literal (substring str 1 (1- p)))
114 (defun std11-analyze-comment (str)
115 (let ((p (std11-check-enclosure str ?\( ?\) t)))
117 (cons (cons 'comment (substring str 1 (1- p)))
121 (defun std11-lexical-analyze (str)
123 (while (not (string-equal str ""))
125 (or (std11-analyze-quoted-string str)
126 (std11-analyze-domain-literal str)
127 (std11-analyze-comment str)
128 (std11-analyze-spaces str)
129 (std11-analyze-special str)
130 (std11-analyze-atom str)
133 (setq dest (cons (car ret) dest))
143 (defun std11-ignored-token-p (token)
144 (let ((type (car token)))
145 (or (eq type 'spaces)(eq type 'comment))
148 (defun std11-parse-token (lal)
152 (setq token (car lal))
153 (std11-ignored-token-p token)
156 (setq itl (cons token itl))
158 (cons (nreverse (cons token itl))
162 (defun std11-parse-ascii-token (lal)
163 (let (token itl parsed token-value)
165 (setq token (car lal))
166 (or (std11-ignored-token-p token)
167 (if (and (setq token-value (cdr token))
168 (find-non-ascii-charset-string token-value)
173 (setq itl (cons token itl))
176 (setq parsed (nreverse (cons token itl)))
178 (cons parsed (cdr lal))
181 (defun std11-parse-token-or-comment (lal)
185 (setq token (car lal))
186 (eq (car token) 'spaces)
189 (setq itl (cons token itl))
191 (cons (nreverse (cons token itl))
195 (defun std11-parse-word (lal)
196 (let ((ret (std11-parse-ascii-token lal)))
198 (let ((elt (car ret))
201 (if (or (assq 'atom elt)
202 (assq 'quoted-string elt))
203 (cons (cons 'word elt) rest)
206 (defun std11-parse-word-or-comment (lal)
207 (let ((ret (std11-parse-token-or-comment lal)))
209 (let ((elt (car ret))
212 (cond ((or (assq 'atom elt)
213 (assq 'quoted-string elt))
214 (cons (cons 'word elt) rest)
217 (cons (cons 'comment-word elt) rest)
221 (defun std11-parse-phrase (lal)
223 (while (setq ret (std11-parse-word-or-comment lal))
224 (setq phrase (append phrase (cdr (car ret))))
228 (cons (cons 'phrase phrase) lal)
231 (defun std11-parse-local-part (lal)
232 (let ((ret (std11-parse-word lal)))
234 (let ((local-part (cdr (car ret))) dot)
236 (while (and (setq ret (std11-parse-ascii-token lal))
238 (string-equal (cdr (assq 'specials dot)) ".")
239 (setq ret (std11-parse-word (cdr ret)))
241 (append local-part dot (cdr (car ret)))
245 (cons (cons 'local-part local-part) lal)
248 (defun std11-parse-sub-domain (lal)
249 (let ((ret (std11-parse-ascii-token lal)))
251 (let ((sub-domain (car ret)))
252 (if (or (assq 'atom sub-domain)
253 (assq 'domain-literal sub-domain)
255 (cons (cons 'sub-domain sub-domain)
260 (defun std11-parse-domain (lal)
261 (let ((ret (std11-parse-sub-domain lal)))
263 (let ((domain (cdr (car ret))) dot)
265 (while (and (setq ret (std11-parse-ascii-token lal))
267 (string-equal (cdr (assq 'specials dot)) ".")
268 (setq ret (std11-parse-sub-domain (cdr ret)))
270 (append domain dot (cdr (car ret)))
274 (cons (cons 'domain domain) lal)
277 (defun std11-parse-at-domain (lal)
278 (let ((ret (std11-parse-ascii-token lal)) at-sign)
280 (setq at-sign (car ret))
281 (string-equal (cdr (assq 'specials at-sign)) "@")
282 (setq ret (std11-parse-domain (cdr ret)))
284 (cons (cons 'at-domain (append at-sign (cdr (car ret))))
288 (defun std11-parse-addr-spec (lal)
289 (let ((ret (std11-parse-local-part lal))
293 (setq addr (cdr (car ret)))
295 (and (setq ret (std11-parse-at-domain lal))
296 (setq addr (append addr (cdr (car ret))))
299 (cons (cons 'addr-spec addr) lal)
302 (defun std11-parse-route (lal)
303 (let ((ret (std11-parse-at-domain lal))
307 (setq route (cdr (car ret)))
309 (while (and (setq ret (std11-parse-ascii-token lal))
310 (setq comma (car ret))
311 (string-equal (cdr (assq 'specials comma)) ",")
312 (setq ret (std11-parse-at-domain (cdr ret)))
314 (setq route (append route comma (cdr (car ret))))
317 (and (setq ret (std11-parse-ascii-token lal))
318 (setq colon (car ret))
319 (string-equal (cdr (assq 'specials colon)) ":")
320 (setq route (append route colon))
323 (cons (cons 'route route)
328 (defun std11-parse-route-addr (lal)
329 (let ((ret (std11-parse-ascii-token lal))
333 (string-equal (cdr (assq 'specials <)) "<")
335 (progn (and (setq ret (std11-parse-route lal))
336 (setq route (cdr (car ret)))
339 (setq ret (std11-parse-addr-spec lal))
341 (setq addr-spec (cdr (car ret)))
343 (setq ret (std11-parse-ascii-token lal))
345 (string-equal (cdr (assq 'specials >)) ">")
347 (cons (cons 'route-addr (append route addr-spec))
352 (defun std11-parse-phrase-route-addr (lal)
353 (let ((ret (std11-parse-phrase lal)) phrase)
356 (setq phrase (cdr (car ret)))
359 (if (setq ret (std11-parse-route-addr lal))
360 (cons (list 'phrase-route-addr
366 (defun std11-parse-mailbox (lal)
367 (let ((ret (or (std11-parse-phrase-route-addr lal)
368 (std11-parse-addr-spec lal)))
372 (setq mbox (car ret))
374 (if (and (setq ret (std11-parse-token-or-comment lal))
375 (setq comment (cdr (assq 'comment (car ret))))
379 (cons (list 'mailbox mbox comment)
383 (defun std11-parse-group (lal)
384 (let ((ret (std11-parse-phrase lal))
385 phrase colon comma mbox semicolon)
387 (setq phrase (cdr (car ret)))
389 (setq ret (std11-parse-ascii-token lal))
390 (setq colon (car ret))
391 (string-equal (cdr (assq 'specials colon)) ":")
394 (and (setq ret (std11-parse-mailbox lal))
395 (setq mbox (list (car ret)))
398 (while (and (setq ret (std11-parse-ascii-token lal))
399 (setq comma (car ret))
401 (cdr (assq 'specials comma)) ",")
403 (setq ret (std11-parse-mailbox lal))
404 (setq mbox (cons (car ret) mbox))
408 (and (setq ret (std11-parse-ascii-token lal))
409 (setq semicolon (car ret))
410 (string-equal (cdr (assq 'specials semicolon)) ";")
412 (cons (list 'group phrase (nreverse mbox))
417 (defun std11-parse-address (lal)
418 (or (std11-parse-group lal)
419 (std11-parse-mailbox lal)
422 (defun std11-parse-addresses (lal)
423 (let ((ret (std11-parse-address lal)))
425 (let ((dest (list (car ret))))
427 (while (and (setq ret (std11-parse-ascii-token lal))
428 (string-equal (cdr (assq 'specials (car ret))) ",")
429 (setq ret (std11-parse-address (cdr ret)))
431 (setq dest (cons (car ret) dest))
437 (defun std11-parse-msg-id (lal)
438 (let ((ret (std11-parse-ascii-token lal))
442 (string-equal (cdr (assq 'specials <)) "<")
444 (setq ret (std11-parse-addr-spec lal))
445 (setq addr-spec (car ret))
447 (setq ret (std11-parse-ascii-token lal))
449 (string-equal (cdr (assq 'specials >)) ">")
451 (cons (cons 'msg-id (cdr addr-spec))
459 (provide 'std11-parse)
461 ;;; std11-parse.el ends here