Changeset trunk,33


Ignore:
Timestamp:
07/14/2009 09:13:57 PM (3 years ago)
Author:
David Owen <dsowen@fugue88.ws>
branch-nick:
parse2
revision id:
dsowen@fugue88.ws-20090715031357-6yrs8cdyx2filngn
Message:
  • Replaced PROGV with PROGN
  • Enhanced the CSV example
Location:
trunk
Files:
1 added
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/csv.lisp

    r31 r33  
    22 
    33#| 
    4 Copyright (C) 2007, 2008  David Owen <dsowen@fugue88.ws> 
     4Copyright (C) 2007, 2008, 2009  David Owen <dsowen@fugue88.ws> 
    55 
    66This program is free software: you can redistribute it and/or modify 
     
    2626 
    2727 
     28(defparameter *input* "7,'now is ''the'' time' 
     2913,\"for all good \"\"men\"\"\" 
     30") 
     31 
    2832(flet ((trim (s) (substring s :start 1 :length (- (length s) 2)))) 
    2933  (defun un-squote (s) 
     
    3236    (cl-ppcre:regex-replace-all "\"\"" (trim s) "\""))) 
    3337 
    34 (deflexer lex-csv 
     38(deflexer lex-csv (:priority-only t) 
    3539  ("," comma) 
    3640  ("\\r\\n?|\\n" newline) 
     
    7074  (file (+ row)) 
    7175  (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)) 
    7478 
    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  
    22 
    33#| 
    4 Copyright (C) 2007, 2008  David Owen <dsowen@fugue88.ws> 
     4Copyright (C) 2007, 2008, 2009  David Owen <dsowen@fugue88.ws> 
    55 
    66This program is free software: you can redistribute it and/or modify 
     
    3737  "Defines a grammar, being a collection of parsers.  The syntax is: 
    3838 
    39 (DEFGRAMMAR (options*) definitions*) 
     39\(DEFGRAMMAR (options*) definitions*) 
    4040definitions: (name rule &key filter cclass)" 
    4141  (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.