June 2018 Update

This commit is contained in:
Ingy döt Net 2018-06-22 20:57:24 +00:00
parent ba8067c3b7
commit 22f33d4004
5278 changed files with 84726 additions and 14379 deletions

View file

@ -1,10 +1,40 @@
;config-file.txt
;lisp comments works normally as it would in lisp
#S(config-file
:fullname "Foo Barber"
:favoritefruit "banana"
:needspeeling t
:seedsremoved nil
:otherfamily '("Rhu Barber" "Harry Barber")
;:will "not be read"
)
(ql:quickload :parser-combinators)
(defpackage :read-config
(:use :cl :parser-combinators))
(in-package :read-config)
(defun trim-space (string)
(string-trim '(#\space #\tab) string))
(defun any-but1? (except)
(named-seq? (<- res (many1? (except? (item) except)))
(coerce res 'string)))
(defun values? ()
(named-seq? (<- values (sepby? (any-but1? #\,) #\,))
(mapcar 'trim-space values)))
(defun key-values? ()
(named-seq? (<- key (word?))
(opt? (many? (whitespace?)))
(opt? #\=)
(<- values (values?))
(cons key (or (if (cdr values) values (car values)) t))))
(defun parse-line (line)
(setf line (trim-space line))
(if (or (string= line "") (member (char line 0) '(#\# #\;)))
:comment
(parse-string* (key-values?) line)))
(defun parse-config (stream)
(let ((hash (make-hash-table :test 'equal)))
(loop for line = (read-line stream nil nil)
while line
do (let ((parsed (parse-line line)))
(cond ((eq parsed :comment))
((eq parsed nil) (error "config parser error: ~a" line))
(t (setf (gethash (car parsed) hash) (cdr parsed))))))
hash))

View file

@ -1,9 +1,12 @@
;config-file.lisp
(defstruct config-file :fullname :favoritefruit :needspeeling :seedsremoved :otherfamily)
(with-open-file (in "config-file.txt")
(defvar contents (read in))
(format t "~a~%" contents)
;reading the config-file into a structure gives us
;some helper functions to access individualy each element
(format t "Fullname: ~a~%" (config-file-fullname contents))
(format t "Contents is a config-file? ~a~%" (config-file-p contents)))
READ-CONFIG> (with-open-file (s "test.cfg") (parse-config s))
#<HASH-TABLE :TEST EQUAL :COUNT 4 {100BD25B43}>
READ-CONFIG> (maphash (lambda (k v) (print (list k v))) *)
("FULLNAME" "Foo Barber")
("FAVOURITEFRUIT" "banana")
("NEEDSPEELING" T)
("OTHERFAMILY" ("Rhu Barber" "Harry Barber"))
NIL
READ-CONFIG> (gethash "SEEDSREMOVED" **)
NIL
NIL