/[climacs]/climacs/html-syntax.lisp
ViewVC logotype

Contents of /climacs/html-syntax.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.4 - (show annotations)
Mon Feb 28 08:51:34 2005 UTC (9 years, 1 month ago) by rstrandh
Branch: MAIN
Changes since 1.3: +214 -160 lines
Improvements to HTML syntax.  This syntax module now uses an
incremental lexer, and and incremental parser based on the existing
Earley parser in syntax.lisp.

Removed backward-to-error and forward-to-error, since I am not sure
that these are what we want.
1 ;;; -*- Mode: Lisp; Package: COMMON-LISP-USER -*-
2
3 ;;; (c) copyright 2005 by
4 ;;; Robert Strandh (strandh@labri.fr)
5
6 ;;; This library is free software; you can redistribute it and/or
7 ;;; modify it under the terms of the GNU Library General Public
8 ;;; License as published by the Free Software Foundation; either
9 ;;; version 2 of the License, or (at your option) any later version.
10 ;;;
11 ;;; This library is distributed in the hope that it will be useful,
12 ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
13 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 ;;; Library General Public License for more details.
15 ;;;
16 ;;; You should have received a copy of the GNU Library General Public
17 ;;; License along with this library; if not, write to the
18 ;;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 ;;; Boston, MA 02111-1307 USA.
20
21 ;;; Syntax for analysing HTML
22
23 (in-package :climacs-syntax) ;;; Put this in a separate package once it works
24
25 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
26 ;;;
27 ;;; grammar classes
28
29 (defclass html-sym ()
30 ((badness :initform 0 :initarg :badness :reader badness)
31 (message :initform "" :initarg :message :reader message)))
32
33 (defmethod parse-tree-better ((t1 html-sym) (t2 html-sym))
34 (and (eq (class-of t1) (class-of t2))
35 (< (badness t1) (badness t2))))
36
37 (defclass words (html-sym) ())
38
39 (defclass empty-words (words) ())
40
41 (defclass nonempty-words (words)
42 ((words :initarg :words)
43 (word :initarg :word)))
44
45 (defclass html-balanced (html-sym)
46 ((start :initarg :start)
47 (end :initarg :end)))
48
49 (defclass html (html-balanced)
50 ((head :initarg :head)
51 (body :initarg :body)))
52
53 (defclass head (html-balanced)
54 ((title :initarg :title)))
55
56 (defclass html-words (html-balanced)
57 ((words :initarg :words)))
58
59 (defclass title (html-words) ())
60 (defclass body (html-words) ())
61 (defclass h1 (html-words) ())
62 (defclass h2 (html-words) ())
63 (defclass h3 (html-words) ())
64 (defclass para (html-words) ())
65
66 (defclass html-token (html-sym)
67 ((start-mark :initarg :start-mark :reader start-mark)
68 (size :initarg :size)))
69
70 (defgeneric end-offset (html-token))
71
72 (defmethod end-offset ((token html-token))
73 (with-slots (start-mark size) token
74 (+ (offset start-mark) size)))
75
76 (defgeneric start-offset (html-token))
77
78 (defmethod start-offset ((token html-token))
79 (offset (start-mark token)))
80
81 (defclass <html> (html-token) () (:default-initargs :size 6))
82 (defclass </html> (html-token) ()(:default-initargs :size 7))
83 (defclass <head> (html-token) () (:default-initargs :size 6))
84 (defclass </head> (html-token) () (:default-initargs :size 7))
85 (defclass <title> (html-token) () (:default-initargs :size 7))
86 (defclass </title> (html-token) () (:default-initargs :size 8))
87 (defclass <body> (html-token) () (:default-initargs :size 6))
88 (defclass </body> (html-token) () (:default-initargs :size 7))
89 (defclass <h1> (html-token) () (:default-initargs :size 4))
90 (defclass </h1> (html-token) () (:default-initargs :size 5))
91 (defclass <h2> (html-token) () (:default-initargs :size 4))
92 (defclass </h2> (html-token) () (:default-initargs :size 5))
93 (defclass <h3> (html-token) () (:default-initargs :size 4))
94 (defclass </h3> (html-token) () (:default-initargs :size 5))
95 (defclass <p> (html-token) () (:default-initargs :size 3))
96 (defclass </p> (html-token) () (:default-initargs :size 4))
97 (defclass <ul> (html-token) () (:default-initargs :size 4))
98 (defclass </ul> (html-token) () (:default-initargs :size 5))
99 (defclass <li> (html-token) () (:default-initargs :size 4))
100 (defclass </li> (html-token) () (:default-initargs :size 5))
101
102 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
103 ;;;
104 ;;; lexer
105
106 (defclass html-element (html-token)
107 ((state :initarg :state)))
108
109 (defclass start-element (html-element) ())
110 (defclass tag-start (html-element) ())
111 (defclass tag-end (html-element) ())
112 (defclass slash (html-element) ())
113 (defclass word (html-element) ())
114 (defclass delimiter (html-element) ())
115
116 (defun next-token (scan)
117 (let ((start-mark (clone-mark scan)))
118 (flet ((fo () (forward-object scan)))
119 (macrolet ((make-entry (type)
120 `(return-from next-token
121 (make-instance ,type :start-mark start-mark
122 :size (- (offset scan) (offset start-mark))))))
123 (loop with object = (object-after scan)
124 until (end-of-buffer-p scan)
125 do (case object
126 (#\< (fo) (make-entry 'tag-start))
127 (#\> (fo) (make-entry 'tag-end))
128 (#\/ (fo) (make-entry 'slash))
129 (t (cond ((alphanumericp object)
130 (loop until (end-of-buffer-p scan)
131 while (alphanumericp (object-after scan))
132 do (fo))
133 (make-entry 'word))
134 (t
135 (fo) (make-entry 'delimiter))))))))))
136
137 (define-syntax html-syntax ("HTML" (basic-syntax))
138 ((tokens :initform (make-instance 'standard-flexichain))
139 (guess-pos :initform 1)
140 (valid-parse :initform 1)
141 (parser)))
142
143 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
144 ;;;
145 ;;; parser
146
147 (defun word-is (word string)
148 (string-equal (coerce (region-to-sequence (start-mark word) (end-offset word)) 'string)
149 string))
150
151 (defparameter *html-grammar*
152 (grammar
153 (<html> -> (tag-start
154 (word (and (= (end-offset tag-start) (start-offset word))
155 (word-is word "html")))
156 (tag-end (= (end-offset word) (start-offset tag-end))))
157 :start-mark (start-mark tag-start))
158 (</html> -> (tag-start
159 (slash (= (end-offset tag-start) (start-offset slash)))
160 (word (and (= (end-offset slash) (start-offset word))
161 (word-is word "html")))
162 (tag-end (= (end-offset word) (start-offset tag-end))))
163 :start-mark (start-mark tag-start))
164 (<head> -> (tag-start
165 (word (and (= (end-offset tag-start) (start-offset word))
166 (word-is word "head")))
167 (tag-end (= (end-offset word) (start-offset tag-end))))
168 :start-mark (start-mark tag-start))
169 (</head> -> (tag-start
170 (slash (= (end-offset tag-start) (start-offset slash)))
171 (word (and (= (end-offset slash) (start-offset word))
172 (word-is word "head")))
173 (tag-end (= (end-offset word) (start-offset tag-end))))
174 :start-mark (start-mark tag-start))
175 (<title> -> (tag-start
176 (word (and (= (end-offset tag-start) (start-offset word))
177 (word-is word "title")))
178 (tag-end (= (end-offset word) (start-offset tag-end))))
179 :start-mark (start-mark tag-start))
180 (</title> -> (tag-start
181 (slash (= (end-offset tag-start) (start-offset slash)))
182 (word (and (= (end-offset slash) (start-offset word))
183 (word-is word "title")))
184 (tag-end (= (end-offset word) (start-offset tag-end))))
185 :start-mark (start-mark tag-start))
186 (<body> -> (tag-start
187 (word (and (= (end-offset tag-start) (start-offset word))
188 (word-is word "body")))
189 (tag-end (= (end-offset word) (start-offset tag-end))))
190 :start-mark (start-mark tag-start))
191 (</body> -> (tag-start
192 (slash (= (end-offset tag-start) (start-offset slash)))
193 (word (and (= (end-offset slash) (start-offset word))
194 (word-is word "body")))
195 (tag-end (= (end-offset word) (start-offset tag-end))))
196 :start-mark (start-mark tag-start))
197 (html -> (<html> head body </html>)
198 :start <html> :head head :body body :end </html>)
199 (head -> (<head> title </head>)
200 :start <head> :title title :end </head>)
201 (title -> (<title> words </title>)
202 :start <title> :words words :end </title>)
203 (body -> (<body> words </body>)
204 :start <body> :words words :end </body>)
205 (words -> ()
206 (make-instance 'empty-words))
207 (words -> (words word)
208 (make-instance 'nonempty-words :words words :word word))))
209
210 (defmethod initialize-instance :after ((syntax html-syntax) &rest args)
211 (declare (ignore args))
212 (with-slots (parser tokens buffer) syntax
213 (setf parser (make-instance 'parser
214 :grammar *html-grammar*
215 :target 'html))
216 (insert* tokens 0 (make-instance 'start-element
217 :start-mark (make-instance 'standard-left-sticky-mark
218 :buffer buffer
219 :offset 0)
220 :size 0
221 :state (initial-state parser)))))
222
223 (defmethod update-syntax-for-display (buffer (syntax html-syntax) top bot)
224 (with-slots (parser tokens valid-parse) syntax
225 (loop until (= valid-parse (nb-elements tokens))
226 while (mark< (end-offset (element* tokens valid-parse)) bot)
227 do (let ((current-token (element* tokens (1- valid-parse)))
228 (next-token (element* tokens valid-parse)))
229 (setf (slot-value next-token 'state)
230 (advance-parse parser (list next-token) (slot-value current-token 'state))))
231 (incf valid-parse))))
232
233 (defmethod update-syntax (buffer (syntax html-syntax))
234 (let ((low-mark (low-mark buffer))
235 (high-mark (high-mark buffer))
236 (scan))
237 (with-slots (tokens guess-pos valid-parse) syntax
238 (when (mark<= low-mark high-mark)
239 ;; go back to a position before low-mark
240 (loop until (or (= guess-pos 1)
241 (mark< (end-offset (element* tokens (1- guess-pos))) low-mark))
242 do (decf guess-pos))
243 ;; go forward to the last position before low-mark
244 (loop with nb-elements = (nb-elements tokens)
245 until (or (= guess-pos nb-elements)
246 (mark>= (end-offset (element* tokens guess-pos)) low-mark))
247 do (incf guess-pos))
248 ;; mark valid parse
249 (setf valid-parse guess-pos)
250 ;; delete entries that must be reparsed
251 (loop until (or (= guess-pos (nb-elements tokens))
252 (mark> (start-mark (element* tokens guess-pos)) high-mark))
253 do (delete* tokens guess-pos))
254 (setf scan (make-instance 'standard-left-sticky-mark
255 :buffer buffer
256 :offset (if (zerop guess-pos)
257 0
258 (end-offset (element* tokens (1- guess-pos))))))
259 ;; scan
260 (loop with start-mark = nil
261 do (loop until (end-of-buffer-p scan)
262 while (whitespacep (object-after scan))
263 do (forward-object scan))
264 until (if (end-of-buffer-p high-mark)
265 (end-of-buffer-p scan)
266 (mark> scan high-mark))
267 do (setf start-mark (clone-mark scan))
268 (insert* tokens guess-pos (next-token scan))
269 (incf guess-pos))))))
270

  ViewVC Help
Powered by ViewVC 1.1.5