Changeset trunk,33
- Timestamp:
- 07/15/2009 03:13:57 AM (17 years ago)
- branch-nick:
- parse2
- revision id:
- dsowen@fugue88.ws-20090715031357-6yrs8cdyx2filngn
- Location:
- trunk
- Files:
-
- 1 added
- 2 edited
-
THANKS.txt (added)
-
csv.lisp (modified) (4 diffs)
-
parsing.lisp (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/csv.lisp
r31 r33 2 2 3 3 #| 4 Copyright (C) 2007, 2008 David Owen <dsowen@fugue88.ws>4 Copyright (C) 2007, 2008, 2009 David Owen <dsowen@fugue88.ws> 5 5 6 6 This program is free software: you can redistribute it and/or modify … … 26 26 27 27 28 (defparameter *input* "7,'now is ''the'' time' 29 13,\"for all good \"\"men\"\"\" 30 ") 31 28 32 (flet ((trim (s) (substring s :start 1 :length (- (length s) 2)))) 29 33 (defun un-squote (s) … … 32 36 (cl-ppcre:regex-replace-all "\"\"" (trim s) "\""))) 33 37 34 (deflexer lex-csv 38 (deflexer lex-csv (:priority-only t) 35 39 ("," comma) 36 40 ("\\r\\n?|\\n" newline) … … 70 74 (file (+ row)) 71 75 (row (t-value (* row-rest) (= t-newline)) 72 (lambda (row) (cons (caar row) (mapcar #'second (second row)))))73 (row-rest ((= t-comma) t-value) car))76 :filter (lambda (row) (cons (caar row) (mapcar #'second (second row))))) 77 (row-rest ((= t-comma) t-value) :filter 'car)) 74 78 75 ;;; (defparser test (* (#\a #\b))) doesn't grab the match. 79 80 81 ;; Tokenize the input first... 82 (let ((tokens (lex-all-csv *input*))) 83 ;; Then parse. 84 (format t "~S~%" (nth-value 2 (file tokens)))) 85 86 87 88 ;; Or, do the tokenizing in the parser: 89 (defgrammar () 90 (file2 (+ row2)) 91 (row2 (value (* row-rest2) (= newline)) 92 :filter (lambda (row) (cons (car row) (mapcar #'second (second row))))) 93 (row-rest2 ((= comma) value) :filter 'identity) 94 (squoted-value ((= #\') (^ "([^']|'')+") (= #\')) 95 :cclass t 96 :filter 97 (lambda (s) (cl-ppcre:regex-replace-all "''" (car s) "'"))) 98 (dquoted-value ((= #\") (^ "([^\"]|\"\")+") (= #\")) 99 :cclass t 100 :filter 101 (lambda (s) (cl-ppcre:regex-replace-all "\"\"" (car s) "\""))) 102 (unquoted-value (^ "[^,'\"\\n\\r]+") :cclass t) 103 (value (/ squoted-value dquoted-value unquoted-value) :cclass t) 104 (newline (^ "\\r\\n?|\\n")) 105 (comma #\,)) 106 107 ;; Then parse directly. 108 (format t "~S~%" (nth-value 2 (file2 *input*))) -
trunk/parsing.lisp
r32 r33 2 2 3 3 #| 4 Copyright (C) 2007, 2008 David Owen <dsowen@fugue88.ws>4 Copyright (C) 2007, 2008, 2009 David Owen <dsowen@fugue88.ws> 5 5 6 6 This program is free software: you can redistribute it and/or modify … … 37 37 "Defines a grammar, being a collection of parsers. The syntax is: 38 38 39 (DEFGRAMMAR (options*) definitions*)39 \(DEFGRAMMAR (options*) definitions*) 40 40 definitions: (name rule &key filter cclass)" 41 41 (flet ((x (definition) `(defparser ,@definition))) 42 `(progv () ,@(mapcar #'x definitions)))) 42 `(progn 43 ,@(mapcar #'x definitions) 44 '(,@(mapcar 'first definitions)))))
Note: See TracChangeset
for help on using the changeset viewer.
