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
|
;;; This source file is part of the Meta-CVS program,
;;; which is distributed under the GNU license.
;;; Copyright 2002 Kaz Kylheku
(require "slot-refs")
(provide "rcs-utils")
(defstruct rcs-token
(type)
(lexeme))
(defstruct rcs-token-stream
(stream)
(pushback-stack))
(defgeneric rcs-read-token (stream))
(defgeneric rcs-peek-token (stream tok))
(defstruct rcs-admin
(head)
(branch)
(access-list)
(symbols)
(locks)
(locks-strict)
(comment)
(expand)
(newphrases))
(defstruct rcs-delta
(version)
(date)
(author)
(state)
(branches)
(next)
(newphrases))
(defstruct rcs-file
(admin)
(deltas)
(delta-hash))
(defun rcs-special-p (ch)
(or (char= ch #\$) (char= ch #\,) (char= ch #\.)
(char= ch #\;) (char= ch #\:) (char= ch #\@)))
(defun rcs-extract-id-sym-or-num (stream)
(let (contains-dot contains-idchar)
(let ((lexeme (with-output-to-string (ss)
(loop
(let ((ch (peek-char nil stream)))
(cond
((char= ch #\.)
(setf contains-dot t)
(write-char ch ss))
((digit-char-p ch)
(write-char ch ss))
((or (rcs-special-p ch) (char= ch #\space)
(not (graphic-char-p ch)))
(return))
(t (setf contains-idchar t)
(write-char ch ss)))
(read-char stream))))))
(make-rcs-token :type (cond
((and contains-dot contains-idchar) :id)
(contains-idchar :sym)
(t :num))
:lexeme lexeme))))
(defun rcs-extract-string (stream)
(read-char stream)
(make-rcs-token
:type :string
:lexeme
(with-output-to-string (ss)
(let ((state :initial))
(loop
(let ((ch (peek-char nil stream)))
(case state
((:initial)
(if (char= ch #\@)
(setf state :atsign)
(write-char ch ss)))
(otherwise
(if (char= ch #\@)
(progn
(write-char ch ss)
(setf state :initial))
(return))))
(read-char stream)))))))
(defmethod rcs-read-token ((stream stream))
(handler-bind ((end-of-file #'(lambda (condition)
(declare (ignore condition))
(return-from rcs-read-token
(load-time-value
(make-rcs-token :type :eof
:lexeme ""))))))
(loop
(let ((ch (peek-char nil stream)))
(cond
((digit-char-p ch)
(return (rcs-extract-id-sym-or-num stream)))
((char= ch #\@)
(return (rcs-extract-string stream)))
((rcs-special-p ch)
(read-char stream)
(return (make-rcs-token :type :special :lexeme ch)))
((and (graphic-char-p ch) (not (char= ch #\space)))
(return (rcs-extract-id-sym-or-num stream)))
(t (read-char stream)))))))
(defmethod rcs-read-token ((stream t))
(rcs-read-token *standard-input*))
(defmethod rcs-read-token ((rts rcs-token-stream))
(with-slots (stream pushback-stack) rts
(cond
((pop pushback-stack))
(t (rcs-read-token stream)))))
(defmethod rcs-pushback-token ((rts rcs-token-stream) (tok rcs-token))
(push tok (slot-value rts 'pushback-stack)))
(defun rcs-match-optional (stream type-match &optional lexeme-match)
(let ((token (rcs-read-token stream)))
(with-slots (type lexeme) token
(cond
((and lexeme-match
(not (and (eq type type-match) (string= lexeme lexeme-match))))
(rcs-pushback-token stream token)
nil)
((not (eq type type-match))
(rcs-pushback-token stream token)
nil)
(t token)))))
(defun rcs-match-token (stream type-match &optional lexeme-match)
(let ((token (rcs-read-token stream)))
(with-slots (type lexeme) token
(if lexeme-match
(when (not (and (eq type type-match) (string= lexeme lexeme-match)))
(error "rcs-parse: expecting token \"~a\" of type ~a, not \"~a\" of type ~a."
lexeme-match type-match lexeme type))
(when (not (eq type type-match))
(error "rcs-parse: expecting token of type ~a, not ~a." type-match type)))
token)))
(defun rcs-parse-newphrases (stream)
(let (token newphrases)
(with-slot-refs (lexeme) token
(loop
(if (setf token (or (rcs-match-optional stream :sym)
(rcs-match-optional stream :id)))
(let ((phrase (list lexeme)))
(loop
(cond
((setf token (or (rcs-match-optional stream :sym)
(rcs-match-optional stream :id)
(rcs-match-optional stream :num)
(rcs-match-optional stream :string)))
(push lexeme phrase))
((setf token (rcs-match-optional stream :special))
(if (char= lexeme #\:)
(push lexeme phrase)
(progn
(rcs-pushback-token stream token)
(push (nreverse phrase) newphrases)
(return))))
(t (push (nreverse phrase) newphrases)
(return))))
(rcs-match-token stream :special #\;))
(return))))
(nreverse newphrases)))
(defun rcs-parse-admin (stream)
(let ((admin (make-rcs-admin))
token)
(with-multi-slot-refs ((head branch access-list symbols locks locks-strict
comment expand newphrases) admin
(lexeme) token)
;; head { num } ;
(rcs-match-token stream :sym "head")
(setf token (rcs-match-optional stream :num))
(when token
(setf head lexeme))
(rcs-match-token stream :special #\;)
;; { branch { num } ; }
(when (rcs-match-optional stream :sym "branch")
(setf token (rcs-match-optional stream :num))
(when token
(setf branch lexeme))
(rcs-match-token stream :special #\;))
;; access { id } * ;
(rcs-match-token stream :sym "access")
(loop
(setf token (or (rcs-match-optional stream :sym)
(rcs-match-optional stream :id)))
(if token
(push lexeme access-list)
(return)))
(nreverse access-list)
(rcs-match-token stream :special #\;)
;; symbols { sym : num }* ;
(rcs-match-token stream :sym "symbols")
(loop
(setf token (rcs-match-optional stream :sym))
(cond
(token
(let ((symbol lexeme))
(rcs-match-token stream :special #\:)
(setf token (rcs-match-token stream :num))
(push (list symbol lexeme) symbols)))
(t (return))))
(nreverse symbols)
(rcs-match-token stream :special #\;)
;; locks { id : num }* ; { strict ; }
(rcs-match-token stream :sym "locks")
(loop
(setf token (or (rcs-match-optional stream :sym)
(rcs-match-optional stream :id)))
(cond
(token
(let ((symbol lexeme))
(rcs-match-token stream :special #\:)
(setf token (rcs-match-token stream :num))
(push (list symbol lexeme) locks)))
(t (return))))
(nreverse locks)
(rcs-match-token stream :special #\;)
(when (rcs-match-optional stream :sym "strict")
(setf locks-strict t)
(rcs-match-token stream :special #\;))
;; { comment { string } ; }
(when (rcs-match-optional stream :sym "comment")
(setf token (rcs-match-optional stream :string))
(when token
(setf comment lexeme))
(rcs-match-token stream :special #\;))
;; { expand { string } ; }
(when (rcs-match-optional stream :sym "expand")
(setf token (rcs-match-optional stream :string))
(when token
(setf expand lexeme))
(rcs-match-token stream :special #\;))
;; { newphrase }*
(setf newphrases (rcs-parse-newphrases stream))
admin)))
(defun rcs-parse-delta (stream)
(let ((delta (make-rcs-delta))
token)
(with-multi-slot-refs ((version date author state branches
next newphrases) delta
(lexeme) token)
;; num
(setf token (rcs-match-optional stream :num))
(if (not token)
(return-from rcs-parse-delta nil))
(setf version lexeme)
;; date num ;
(rcs-match-token stream :sym "date")
(setf token (rcs-match-token stream :num))
(setf date lexeme)
(rcs-match-token stream :special #\;)
;; author id ;
(rcs-match-token stream :sym "author")
(setf token (or (rcs-match-optional stream :sym)
(rcs-match-token stream :id)))
(setf author lexeme)
(rcs-match-token stream :special #\;)
;; state { id } ;
(rcs-match-token stream :sym "state")
(setf token (or (rcs-match-optional stream :sym)
(rcs-match-optional stream :id)))
(when token
(setf state lexeme))
(rcs-match-token stream :special #\;)
;; branches { num } * ;
(rcs-match-token stream :sym "branches")
(loop
(let ((token (rcs-match-optional stream :num)))
(if token
(push lexeme branches)
(return (nreverse branches)))))
(rcs-match-token stream :special #\;)
;; next { num } ;
(rcs-match-token stream :sym "next")
(setf token (rcs-match-optional stream :num))
(when token
(setf next lexeme))
(rcs-match-token stream :special #\;)
;; { newphrase }*
(when (not (rcs-match-optional stream :sym "desc"))
(setf newphrases (rcs-parse-newphrases stream)))
delta)))
(defun rcs-parse-deltas (stream)
(let (deltas)
(loop
(let ((delta (rcs-parse-delta stream)))
(if delta
(push delta deltas)
(return (nreverse deltas)))))))
(defun rcs-make-delta-hash (deltas)
(let ((hash (make-hash-table :test #'equal)))
(mapc #'(lambda (delta)
(setf (gethash (slot-value delta 'next) hash) delta))
deltas)
hash))
(defun rcs-parse (stream)
"Parse RCS file."
(let ((token-stream (make-rcs-token-stream :stream stream)))
;; We currently just need the admin and delta sections.
(let ((file (make-rcs-file :admin (rcs-parse-admin token-stream)
:deltas (rcs-parse-deltas token-stream))))
(setf (slot-value file 'delta-hash)
(rcs-make-delta-hash (slot-value file 'deltas)))
file)))
|