Psst.. new poll here.
Psst.. new forums here.
Microsoft is blocking us again (TY IP Reputation!) so just use oauth login instead. :)
Paste
Pasted as Lisp by phryk ( 15 years ago )
;;TODO: Add functions to evaluate different types of value: a) generic b) key-dependant
(ql:quickload "cl-ppcre")
(load "config.lisp")
(defpackage :lizardsnot-metafile
(:use :cl :cl-ppcre :lizardsnot-config)
(:export file-parse)
)
(in-package :lizardsnot-metafile)
(defvar *basetypes*
(list
;; Schema Typename Evaluation
(list "^\\\"(.*)\\\"$" "string" (lambda (x) x))
(list "^\\(d+)$" "integer" (lambda (x) (parse-integer x)))
(list "^\\(d*\\.\\d+)$" "float" (lambda (x) (read-from-string x)))
))
(defun file-parse (pathspec)
(let
((current-key "")
(structure NIL)
(values NIL)
(handle (open pathspec :if-does-not-exist NIL)))
(when handle
(loop for line = (read-line handle NIL)
:while line do
(let
((key (match-key line))
(value (match-value line)))
(cond
(key
;;Before applying the key of the new section,
;;add the current section to the structure
(push (cons (intern (string-upcase current-key)) values) structure)
(setf values NIL)
(setf current-key key))
(value (push (cons (intern (string-upcase (car value))) (value-parse (cdr value))) values)))))
(push (cons (intern (string-upcase current-key)) values) structure)
(close handle)
structure)))
;;TODO structure to string
(defun match-key (line)
(register-groups-bind (key) ("\\[(.*)\\]" line) key))
(defun match-value (line)
(register-groups-bind (key value) ("([^\\s]*)\\s*=\\s*(.*)" line) (list key value)))
(defun value-parse (value)
(loop for typedesc in *basetypes* do
(let ((match (register-groups-bind (x) ("(.*)" value) x)))
(if match (return (funcall (caddr typedesc) match)) NIL))))
(print (file-parse "info"))
Revise this Paste