From 7b9b8a32e728cd9254c4b207392946f8371da564 Mon Sep 17 00:00:00 2001 From: Dimitri Fontaine Date: Mon, 5 Oct 2015 11:39:44 +0200 Subject: [PATCH] Move sexp parsing into its own file. After all, it's shared between the CSV command parsing and the Cast Rules parsing. src/parsers/command-csv.lisp still contains lots of facilities shared between the file based sources, will need another series of splits. --- pgloader.asd | 1 + src/parsers/command-csv.lisp | 42 ------------------------------ src/parsers/command-sexp.lisp | 48 +++++++++++++++++++++++++++++++++++ 3 files changed, 49 insertions(+), 42 deletions(-) create mode 100644 src/parsers/command-sexp.lisp diff --git a/pgloader.asd b/pgloader.asd index c9da027..537ffea 100644 --- a/pgloader.asd +++ b/pgloader.asd @@ -98,6 +98,7 @@ (:file "command-source") (:file "command-options") (:file "command-sql-block") + (:file "command-sexp") (:file "command-csv") (:file "command-ixf") (:file "command-fixed") diff --git a/src/parsers/command-csv.lisp b/src/parsers/command-csv.lisp index 564412f..6ca6fed 100644 --- a/src/parsers/command-csv.lisp +++ b/src/parsers/command-csv.lisp @@ -253,48 +253,6 @@ (defrule column-name csv-field-name) ; same rules here (defrule column-type csv-field-name) ; again, same rules, names only -(defun not-doublequote (char) - (not (eql #\" char))) - -(defun symbol-character-p (character) - (not (member character '(#\Space #\( #\))))) - -(defun symbol-first-character-p (character) - (and (symbol-character-p character) - (not (member character '(#\+ #\-))))) - -(defrule sexp-symbol (and (symbol-first-character-p character) - (* (symbol-character-p character))) - (:lambda (schars) - (pgloader.transforms:intern-symbol - (text schars) - '(("nil" . cl:nil) - ("precision" . pgloader.transforms::precision) - ("scale" . pgloader.transforms::scale))))) - -(defrule sexp-string-char (or (not-doublequote character) (and #\\ #\"))) - -(defrule sexp-string (and #\" (* sexp-string-char) #\") - (:destructure (q1 string q2) - (declare (ignore q1 q2)) - (text string))) - -(defrule sexp-integer (+ (or "0" "1" "2" "3" "4" "5" "6" "7" "8" "9")) - (:lambda (list) - (parse-integer (text list) :radix 10))) - -(defrule sexp-list (and open-paren sexp (* sexp) close-paren) - (:destructure (open car cdr close) - (declare (ignore open close)) - (cons car cdr))) - -(defrule sexp-atom (and ignore-whitespace - (or sexp-string sexp-integer sexp-symbol)) - (:lambda (atom) - (bind (((_ a) atom)) a))) - -(defrule sexp (or sexp-atom sexp-list)) - (defrule column-expression (and kw-using sexp) (:lambda (expr) (bind (((_ sexp) expr)) sexp))) diff --git a/src/parsers/command-sexp.lisp b/src/parsers/command-sexp.lisp new file mode 100644 index 0000000..7115ef3 --- /dev/null +++ b/src/parsers/command-sexp.lisp @@ -0,0 +1,48 @@ +;;; +;;; Parse user given s-expressions in commands +;;; + +(in-package #:pgloader.parser) + +(defun not-doublequote (char) + (not (eql #\" char))) + +(defun symbol-character-p (character) + (not (member character '(#\Space #\( #\))))) + +(defun symbol-first-character-p (character) + (and (symbol-character-p character) + (not (member character '(#\+ #\-))))) + +(defrule sexp-symbol (and (symbol-first-character-p character) + (* (symbol-character-p character))) + (:lambda (schars) + (pgloader.transforms:intern-symbol + (text schars) + '(("nil" . cl:nil) + ("precision" . pgloader.transforms::precision) + ("scale" . pgloader.transforms::scale))))) + +(defrule sexp-string-char (or (not-doublequote character) (and #\\ #\"))) + +(defrule sexp-string (and #\" (* sexp-string-char) #\") + (:destructure (q1 string q2) + (declare (ignore q1 q2)) + (text string))) + +(defrule sexp-integer (+ (or "0" "1" "2" "3" "4" "5" "6" "7" "8" "9")) + (:lambda (list) + (parse-integer (text list) :radix 10))) + +(defrule sexp-list (and open-paren sexp (* sexp) close-paren) + (:destructure (open car cdr close) + (declare (ignore open close)) + (cons car cdr))) + +(defrule sexp-atom (and ignore-whitespace + (or sexp-string sexp-integer sexp-symbol)) + (:lambda (atom) + (bind (((_ a) atom)) a))) + +(defrule sexp (or sexp-atom sexp-list)) +