(std11-check-enclosure): New function; changed from
[elisp/mu-cite.git] / std11-parse.el
1 ;;; std11-parse.el --- STD 11 parser for GNU Emacs
2
3 ;; Copyright (C) 1995,1996 Free Software Foundation, Inc.
4
5 ;; Author:   MORIOKA Tomohiko <morioka@jaist.ac.jp>
6 ;; Keywords: mail, news, RFC 822, STD 11
7 ;; Version: $Id: std11-parse.el,v 0.7 1996-08-28 18:07:03 morioka Exp $
8
9 ;; This file is part of tl (Tiny Library).
10
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.
15
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.
20
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.
25
26 ;;; Code:
27
28 (require 'std11)
29
30
31 ;;; @ lexical analyze
32 ;;;
33
34 (defconst std11-space-chars " \t\n")
35 (defconst std11-spaces-regexp (concat "^[" std11-space-chars "]+"))
36 (defconst std11-special-chars "][()<>@,;:\\<>.\"")
37 (defconst std11-atom-regexp
38   (concat "^[^" std11-special-chars std11-space-chars "]+"))
39
40 (defun std11-analyze-spaces (str)
41   (if (string-match std11-spaces-regexp str)
42       (let ((end (match-end 0)))
43         (cons (cons 'spaces (substring str 0 end))
44               (substring str end)
45               ))))
46
47 (defun std11-analyze-special (str)
48   (if (and (> (length str) 0)
49            (find (aref str 0) std11-special-chars)
50            )
51       (cons (cons 'specials (substring str 0 1))
52             (substring str 1)
53             )))
54
55 (defun std11-analyze-atom (str)
56   (if (string-match std11-atom-regexp str)
57       (let ((end (match-end 0)))
58         (cons (cons 'atom (substring str 0 end))
59               (substring str end)
60               ))))
61
62 (defun std11-check-enclosure (str open close &optional recursive from)
63   (let ((len (length str))
64         (i (or from 0))
65         )
66     (if (and (> len i)
67              (eq (aref str i) open))
68         (let (p chr dest)
69           (setq i (1+ i))
70           (catch 'tag
71             (while (< i len)
72               (setq chr (aref str i))
73               (cond ((eq chr ?\\)
74                      (setq i (1+ i))
75                      (if (>= i len)
76                          (throw 'tag nil)
77                        )
78                      (setq i (1+ i))
79                      )
80                     ((eq chr close)
81                      (throw 'tag (1+ i))
82                      )
83                     ((eq chr open)
84                      (if (and recursive
85                               (setq p (std11-check-enclosure
86                                        str open close recursive i))
87                               )
88                          (setq i p)
89                        (throw 'tag nil)
90                        ))
91                     (t
92                      (setq i (1+ i))
93                      ))
94               ))))))
95
96 (defun std11-analyze-quoted-string (str)
97   (let ((p (std11-check-enclosure str ?\" ?\")))
98     (if p
99         (cons (cons 'quoted-string (substring str 1 (1- p)))
100               (substring str p))
101       )))
102
103 (defun std11-analyze-domain-literal (str)
104   (let ((p (std11-check-enclosure str ?\[ ?\])))
105     (if p
106         (cons (cons 'domain-literal (substring str 1 (1- p)))
107               (substring str p))
108       )))
109
110 (defun std11-analyze-comment (str)
111   (let ((p (std11-check-enclosure str ?\( ?\) t)))
112     (if p
113         (cons (cons 'comment (substring str 1 (1- p)))
114               (substring str p))
115       )))
116
117
118 ;;; @ end
119 ;;;
120
121 (provide 'std11-parse)
122
123 ;;; std11-parse.el ends here