Hash :
9f55a85f
Author :
Thomas de Grivel
Date :
2018-06-29T04:53:16
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340
;;
;; token-stream - Lexer classes for cl-stream
;;
;; Copyright 2017,2018 Thomas de Grivel <thoxdg@gmail.com>
;;
;; Permission to use, copy, modify, and distribute this software for any
;; purpose with or without fee is hereby granted, provided that the above
;; copyright notice and this permission notice appear in all copies.
;;
;; THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
;; WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
;; MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
;; ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
;; WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
;; ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
;; OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
;;
(in-package :token-stream)
(defclass token ()
((string :initarg :string
:initform ""
:accessor token-string
:type string)
(line :initarg :line
:reader token-line
:type (integer 0))
(character :initarg :character
:reader token-character
:type (integer 0))))
(defclass lexer-token (token)
((start :initarg :start
:accessor token-start)))
(defvar *buffer-size* 64)
(declaim (type fixnum *buffer-size*))
(defun make-buffer ()
(make-array `(,*buffer-size*)
:element-type 'character
:adjustable t
:fill-pointer 0))
(defclass lexer (super-stream input-stream)
((line :initarg :line
:initform 0
:accessor lexer-line
:type fixnum)
(character :initarg :character
:initform -1
:accessor lexer-character
:type fixnum)
(input-ended :initform nil
:accessor lexer-input-ended
:type boolean)
(buffer :initform (make-buffer)
:accessor lexer-buffer
:type string)
(match :initform (make-instance 'lexer-token
:line 0
:character 0
:start 0)
:accessor lexer-match
:type lexer-token)
(stack :initform ()
:accessor lexer-stack
:type list)
(eof-p :initform nil
:accessor lexer-eof-p
:type boolean)))
(defgeneric discard-token (lexer)
(:documentation "Discard token from lexer stack top."))
(defgeneric lexer-input (lexer)
(:documentation "Read a character from lexer in and put it into lexer
buffer."))
(defgeneric lexer-input-n (lexer n)
(:documentation "Ensure at least N characters are in lexer buffer."))
(defgeneric lexer-match-char (lexer index)
(:documentation "Ensure input and return the character from lexer
buffer for matching at index."))
(defgeneric lexer-match-start (lexer)
(:documentation "Return token start for lexer match."))
(defgeneric (setf lexer-match-start) (value lexer)
(:documentation "Set token start for lexer match."))
(defgeneric lexer-push-extend (lexer character)
(:documentation "Put a character into lexer buffer, extending it by
*BUFFER-SIZE* if necessary."))
(defgeneric lexer-token (lexer)
(:documentation "Consumes and returns a token."))
(defgeneric make-token (lexer symbol &rest initargs)
(:documentation "Create a token of given class from lexer stack
top."))
(defgeneric match (lexer thing)
(:documentation "Advance lexer match start if THING is matched in
lexer buffer."))
(defgeneric match-not (lexer thing))
(defgeneric match-option (lexer thing))
(defgeneric match-times (lexer thing min max))
(defgeneric match-until (lexer thing)
(:documentation "Advance lexer match start until THING is matched in
lexer buffer."))
(defgeneric pop-token (lexer)
(:documentation "Return the last token on lexer stack."))
(defgeneric push-token (lexer)
(:documentation "Push a copy of the lexer match onto the lexer
stack."))
;; Stream methods
(defmethod stream-element-type ((lx lexer))
'token)
;; Input
(defmethod lexer-push-extend ((lx lexer) item)
(let* ((buffer (lexer-buffer lx))
(fp (the fixnum (fill-pointer buffer)))
(new-fp (1+ fp)))
(if (= fp (array-dimension buffer 0))
(let ((new-buffer (adjust-array buffer
(the fixnum (+ fp *buffer-size*))
:fill-pointer new-fp)))
(setf (lexer-buffer lx) new-buffer))
(setf (fill-pointer buffer) new-fp))
(locally (declare (optimize (safety 0)))
(setf (aref buffer fp) item))
fp))
(defmethod lexer-input ((lx lexer))
(let ((in (stream-underlying-stream lx)))
(multiple-value-bind (item state) (stream-read in)
(ecase state
((nil) (let* ((pos (the fixnum (lexer-push-extend lx item)))
(buf (lexer-buffer lx)))
(declare (type vector buf))
(cond ((or (and (char= #\Newline item)
(or (not (< 0 pos))
(char/= #\Return
(char buf (1- pos)))))
(char= #\Return item))
(setf (lexer-character lx) 0)
(incf (the fixnum (lexer-line lx))))
(t
(incf (the fixnum (lexer-character lx)))))
(values item nil)))
((:eof) (setf (lexer-input-ended lx) t)
(values nil :eof))
((:non-blocking)
(signal (make-condition 'non-blocking :stream lx)))))))
(defmethod lexer-match-start ((lx lexer))
(token-start (lexer-match lx)))
(defmethod (setf lexer-match-start) ((value integer) (lx lexer))
(setf (token-start (lexer-match lx)) (the fixnum value)))
(defmethod lexer-input-n ((lx lexer) (n integer))
(declare (type fixnum n))
(loop
(let ((length (- (the fixnum (fill-pointer (lexer-buffer lx)))
(the fixnum (lexer-match-start lx)))))
(declare (type fixnum length))
(when (or (lexer-input-ended lx)
(<= n length))
(return))
(lexer-input lx))))
(defmethod lexer-match-char ((lx lexer) (index integer))
(declare (type fixnum index))
(lexer-input-n lx (the fixnum (1+ index)))
(let ((buf (lexer-buffer lx))
(match-index (+ (the fixnum (lexer-match-start lx))
index)))
(declare (type (vector character) buf)
(type fixnum match-index))
(char buf match-index)))
;; Tokenizer
(defmethod push-token ((lx lexer))
(let* ((match (lexer-match lx))
(line (token-line match))
(character (token-character match))
(start (token-start match))
(token (make-instance 'lexer-token
:line line
:character character
:start start)))
(push token (lexer-stack lx))))
(defun subseq* (sequence start &optional end)
(subseq sequence start end))
(defmethod pop-token ((lx lexer))
(assert (lexer-stack lx))
(let* ((buffer (lexer-buffer lx))
(fp (fill-pointer buffer))
(token (pop (lexer-stack lx)))
(match-start (lexer-match-start lx))
(string (subseq* buffer (token-start token) match-start)))
(setf (token-string token) string)
(when (endp (lexer-stack lx))
(replace buffer buffer :start2 match-start :end2 fp)
(setf (lexer-match-start lx) 0
(fill-pointer buffer) (- fp match-start)))
token))
(defmethod make-token ((lx lexer) (class symbol) &rest initargs)
(let ((lt (pop-token lx)))
(apply #'make-instance class
:string (token-string lt)
:line (token-line lt)
:character (token-character lt)
initargs)))
(defmethod discard-token ((lx lexer))
(pop (lexer-stack lx))
(when (endp (lexer-stack lx))
(let* ((buffer (lexer-buffer lx))
(fp (fill-pointer buffer))
(match-start (lexer-match-start lx)))
(replace buffer buffer :start2 match-start :end2 fp)
(setf (lexer-match-start lx) 0
(fill-pointer buffer) (- fp match-start))))
nil)
;; Matcher
(defmethod match ((lx lexer) (s string))
(let* ((length (length s))
(match-start (lexer-match-start lx))
(match-end (+ match-start length))
(buffer (lexer-buffer lx)))
(declare (type fixnum length match-start match-end)
(type (vector character) buffer))
(lexer-input-n lx length)
(when (and (<= match-end (length buffer))
(string= s buffer :start2 match-start :end2 match-end))
(incf (the fixnum (lexer-match-start lx)) length))))
(defmethod match ((lx lexer) (c character))
(when (char= c (lexer-match-char lx 0))
(incf (the fixnum (lexer-match-start lx)))))
(defmethod match-until ((lx lexer) (s string))
(lexer-input-n lx (length s))
(loop
(let ((match (match lx s)))
(when match
(return match)))
(when (lexer-input-ended lx)
(return))
(lexer-input lx)
(incf (the fixnum (lexer-match-start lx)))))
(defmethod match-until ((lx lexer) (f function))
(loop
(let ((match (funcall f lx)))
(when match
(return match)))
(when (lexer-input-ended lx)
(return))
(lexer-input lx)
(incf (the fixnum (lexer-match-start lx)))))
(defmethod match-option ((lx lexer) (f function))
(or (funcall f lx)
(lexer-match-start lx)))
(defmethod match-not ((lx lexer) (f function))
(let ((match-start (lexer-match-start lx)))
(cond ((or (funcall f lx)
(lexer-input-ended lx))
(setf (lexer-match-start lx) match-start)
nil)
(t
(incf (the fixnum (lexer-match-start lx)))))))
(defmacro match-sequence (lexer &body body)
(let ((lx (gensym "LX-"))
(match-start (gensym "MATCH-START-"))
(result (gensym "RESULT-")))
`(let* ((,lx ,lexer)
(,match-start (lexer-match-start ,lx))
(,result (progn ,@body)))
(cond (,result
,result)
(t
(setf (lexer-match-start ,lx) ,match-start)
nil)))))
(defmethod match-times ((lx lexer) (f function) (min integer) (max integer))
(declare (type fixnum min max))
(match-sequence lx
(let ((n 0))
(loop
(unless (< n max)
(return (lexer-match-start lx)))
(unless (funcall f lx)
(if (< n min)
(return nil)
(return (lexer-match-start lx))))
(incf n)))))
(defmethod match-times ((lx lexer) (f function) (min integer) (max null))
(declare (type fixnum min))
(match-sequence lx
(let ((n 0))
(declare (type fixnum n))
(loop
(unless (funcall f lx)
(if (< n min)
(return nil)
(return (lexer-match-start lx))))
(incf n)))))
(defmethod stream-read ((lx lexer))
(if (lexer-eof-p lx)
(values nil :eof)
(handler-case (values (lexer-token lx) nil)
(end-of-file () (values nil :eof))
(non-blocking () (values nil :non-blocking)))))