| 1 | #| |
|---|
| 2 | Copyright (C) 2007 David Owen <dsowen@fugue88.ws> |
|---|
| 3 | |
|---|
| 4 | This library is free software; you can redistribute it and/or |
|---|
| 5 | modify it under the terms of the GNU Lesser General Public |
|---|
| 6 | License as published by the Free Software Foundation; either |
|---|
| 7 | version 2.1 of the License, or (at your option) any later version. |
|---|
| 8 | |
|---|
| 9 | This library is distributed in the hope that it will be useful, |
|---|
| 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|---|
| 12 | Lesser General Public License for more details. |
|---|
| 13 | |
|---|
| 14 | You should have received a copy of the GNU Lesser General Public |
|---|
| 15 | License along with this library; if not, write to the Free Software |
|---|
| 16 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
|---|
| 17 | |# |
|---|
| 18 | |
|---|
| 19 | |
|---|
| 20 | |
|---|
| 21 | (require '#:cl-ppcre) |
|---|
| 22 | (require '#:dso-lex) |
|---|
| 23 | |
|---|
| 24 | (use-package '(#:cl-ppcre #:dso-lex)) |
|---|
| 25 | |
|---|
| 26 | |
|---|
| 27 | |
|---|
| 28 | (defun snip (s) (subseq s 1 (1- (length s)))) |
|---|
| 29 | |
|---|
| 30 | (defun un-squote (s) (regex-replace-all "''" (snip s) "'")) |
|---|
| 31 | |
|---|
| 32 | (defun un-dquote (s) (regex-replace-all "\"\"" (snip s) "\"")) |
|---|
| 33 | |
|---|
| 34 | (deflexer scan-csv |
|---|
| 35 | ("," comma) |
|---|
| 36 | ("[^\"',]+" value) |
|---|
| 37 | ("'(?:[^']|'')*'" value un-squote) |
|---|
| 38 | ("\"(?:[^\"]|\"\")*\"" value un-dquote)) |
|---|
| 39 | |
|---|
| 40 | (defun scan-all (input) |
|---|
| 41 | (labels ((scan (start tokens) |
|---|
| 42 | (if (> (length input) start) |
|---|
| 43 | (multiple-value-bind (class image remainder) |
|---|
| 44 | (scan-csv input start) |
|---|
| 45 | (when class |
|---|
| 46 | (scan remainder (cons (cons class image) tokens)))) |
|---|
| 47 | (nreverse tokens)))) |
|---|
| 48 | (scan 0 '()))) |
|---|
| 49 | |
|---|
| 50 | (scan-all "no quotes,'a ''quote''',\"another \"\"quote\"\"\"") |
|---|