- Timestamp:
- 06/18/2007 07:51:03 PM (19 years ago)
- revision id:
- svn-v3-trunk0:2948df59-2b31-0410-8e06-c40c0b09d5b6:trunk:11
- Location:
- trunk
- Files:
-
- 2 edited
-
csv.lisp (modified) (1 diff)
-
parse3.lisp (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/csv.lisp
r9 r11 1 (require '#:dso-lex) 1 2 (require '#:parse) 2 3 (require '#:cl-ppcre) 4 (use-package '#:dso-lex) 3 5 (import 'parse::substring) 4 6 (import 'parse::with-gensyms) 5 7 (import 'cl-ppcre:scan) 6 (load " parse/parse3")8 (load "lisp/parse/parse3") 7 9 8 (defmacro defparser (name rule) 10 11 12 (flet ((trim (s) (substring s :start 1 :length (- (length s) 2)))) 13 (defun un-squote (s) 14 (cl-ppcre:regex-replace-all "''" (trim s) "'")) 15 (defun un-dquote (s) 16 (cl-ppcre:regex-replace-all "\"\"" (trim s) "\""))) 17 18 (deflexer lex-csv 19 ("," comma) 20 ("\\n|\\r|\\r\\n" newline) 21 ("'(?:[^']|'')*'" value un-squote) 22 ("\"(?:[^\"]|\"\")*\"" value un-dquote) 23 ("[^,'\"\\n\\r]+" value)) 24 25 (defun lex-all-csv (input &optional tokens) 26 (multiple-value-bind (class image left) (lex-csv input) 27 (if class 28 (lex-all-csv left (cons (list class image) tokens)) 29 (nreverse tokens)))) 30 31 (defmacro defmatcher (class) 32 (let ((fn-sym (intern (concatenate 'string "T-" (symbol-name class))))) 33 `(defun ,fn-sym (token-list) 34 (when token-list 35 (destructuring-bind (class image) (first token-list) 36 (when (eq class ',class) 37 (values t (rest token-list) (list class image)))))))) 38 39 (defmatcher comma) 40 (defmatcher newline) 41 (defmatcher value) 42 43 (defmacro defparser (name rule &optional filter) 9 44 (with-gensyms (input next match) 10 `(defun ,name (,input)11 (if-matches ,input ,rule (,next ,match)12 (values t ,next (list ',name ,match))))))45 `(defun ,name (,input) 46 (if-matches ,input ,rule (,next ,match) 47 (values t ,next (cons ',name ,(if filter `(,filter ,match) match))))))) 13 48 14 49 (defmacro defgrammar (&body definitions) 15 50 (flet ((x (definition) 16 (destructuring-bind (name rule ) definition17 `(defparser ,name ,rule ))))51 (destructuring-bind (name rule &optional filter) definition 52 `(defparser ,name ,rule ,filter)))) 18 53 `(progv () ,@(mapcar #'x definitions)))) 19 54 20 55 (defgrammar 21 (file (+ row)) 22 (row (value (* ((= comma) value)) (= #\Newline))) 23 (comma #\,) 24 (value (? (/ dquoted-value squoted-value raw-value))) 25 (dquoted-value (#\" (^ "(?:[^\"]|\"\")*") #\")) 26 (squoted-value (#\' (^ "(?:[^']|'')*") #\')) 27 (raw-value (^ "[^,'\"\\n]*"))) 56 (file (+ row)) 57 (row (t-value (* row-rest) (= t-newline)) 58 (lambda (row) 59 (princ row) 60 (mapcar #'second (cons (first row) (mapcar #'second (second row)))))) 61 (row-rest ((= t-comma) t-value) cdr)) 28 62 29 ;;; (de parser test (* (#\a #\b))) doesn't grab the match.63 ;;; (defparser test (* (#\a #\b))) doesn't grab the match. -
trunk/parse3.lisp
r10 r11 4 4 ,@body)) 5 5 6 (defmacro if-matche r-parser (input parser (next match)6 (defmacro if-matches-parser (input parser (next match) 7 7 &body (&optional (then t) else)) 8 8 (declare (symbol parser next match)) … … 66 66 `(if-matches ,input ,head (,head-next ,head-match) 67 67 (if-matches-sequence ,head-next ,tail (,next ,tail-match) 68 (let ((,match (cons ,head-match (list ,tail-match))))68 (let ((,match (cons ,head-match ,tail-match))) 69 69 ,then) 70 70 ,else) 71 71 ,else)) 72 `(if-matches ,input ,head (,next ,match) ,then ,else)))) 72 (with-gensyms (tail-match) 73 `(if-matches ,input ,head (,next ,tail-match) 74 (let ((,match (list ,tail-match))) 75 ,then) 76 ,else))))) 73 77 74 78 (defmacro if-matches-eqcount (input count rule (next match) … … 149 153 (defmacro if-matches (input rule (next match) &body (&optional (then t) else)) 150 154 (etypecase rule 151 (symbol `(if-matche r-parser ,input ,rule (,next ,match) ,then ,else))155 (symbol `(if-matches-parser ,input ,rule (,next ,match) ,then ,else)) 152 156 (character `(if-matches-char ,input ,rule (,next ,match) ,then ,else)) 153 157 (string `(if-matches-string ,input ,rule (,next ,match) ,then ,else))
Note: See TracChangeset
for help on using the changeset viewer.
