source: tags/dso-lex-0.2.1/example.lisp

Last change on this file was 14, checked in by dsowen, 19 years ago

Fixed to work with new lexers.

File size: 1.5 KB
Line 
1#|
2Copyright (C) 2007  David Owen <dsowen@fugue88.ws>
3
4This library is free software; you can redistribute it and/or
5modify it under the terms of the GNU Lesser General Public
6License as published by the Free Software Foundation; either
7version 2.1 of the License, or (at your option) any later version.
8
9This library is distributed in the hope that it will be useful,
10but WITHOUT ANY WARRANTY; without even the implied warranty of
11MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12Lesser General Public License for more details.
13
14You should have received a copy of the GNU Lesser General Public
15License along with this library; if not, write to the Free Software
16Foundation, 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\"\"\"")
Note: See TracBrowser for help on using the repository browser.