Isoraqathedh

parsing funny notation

Aug 5th, 2014
300
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lisp 1.12 KB | None | 0 0
  1. (define-matcher uppercase (in #\A #\Z))
  2. (define-matcher numerals (in #\0 #\9))
  3.  
  4. (defun read-bracket ()
  5.   "Reads fragments of funny notation inside brackets"
  6.   (consume)
  7.   (loop for next = (peek)
  8.         while (and next (char/= next #\]))
  9.         collect (read-piece)
  10.         finally (consume)))
  11.  
  12. (defun read-piece ()
  13.   "Reads a fragment of funny notation up to a landmark (rN rNN cpR4, up to a capital letter + opt. digit)"
  14.   (let ((tag (consume-until (make-matcher (or :uppercase (any #\[ #\])))))
  15.         (next (peek)))
  16.     (format t "~a & ~s, " tag next)
  17.     (case next
  18.       (#\[ (cons tag (read-bracket)))
  19.       (#\] tag)
  20.       ;; Issue: reader consumes too many repeated letters ("WWW" doesn't get parsed into "WW" "W")
  21.       (T (format NIL "~a~:[~;~:*~a~]" tag (consume-until
  22.                                            (make-matcher
  23.                                             (not
  24.                                              (or (is (string next)) :numerals)))))))))
  25.  
  26. (defun funny-notation->parsed-fragments (string)
  27.   (with-lexer-environment (string)
  28.     (loop while (peek)
  29.           collect (read-piece))))
Advertisement
Add Comment
Please, Sign In to add comment