config-parser.lisp 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. ;;;; config-parser.lisp
  2. (in-package #:config-parser)
  3. ;;; "config-parser" goes here. Hacks and glory await!
  4. (defrule space
  5. (+ (or #\Space #\Tab #\Newline))
  6. (:constant nil))
  7. (defrule identifier
  8. (* (or (alphanumericp character) "_" "-" "."))
  9. (:text t)
  10. (:lambda (identifier)
  11. (intern (string-upcase identifier) "KEYWORD")))
  12. (defun not-doublequote (character)
  13. (not (eql #\" character)))
  14. (defrule string-chars
  15. (or (not-doublequote character)
  16. (and #\\ #\")))
  17. (defrule string
  18. (and #\" (* string-chars) #\")
  19. (:destructure (sq string eq)
  20. (declare (ignore sq eq))
  21. (text string)))
  22. (defrule number
  23. (and (? (or "-" "+"))
  24. (+ (or "0" "1" "2" "3" "4" "5" "6" "7" "8" "9"))
  25. (? (and "."
  26. (+ (or "0" "1" "2" "3" "4" "5" "6" "7" "8" "9"))))
  27. (? (and (or "e" "E")
  28. (? "-")
  29. (+ (or "0" "1" "2" "3" "4" "5" "6" "7" "8" "9")))))
  30. (:text t)
  31. (:lambda (num-string)
  32. (parse-number num-string)))
  33. (defrule block-title
  34. (and (? #\Newline) "[" (? space) identifier (? space) "]" (? #\Newline))
  35. (:destructure (nl1 lb sp1 ident sp2 rb nl)
  36. (declare (ignore nl1 lb sp1 sp2 rb nl))
  37. ident))
  38. (defrule ident-path
  39. (and identifier (? space) "/" (? space) identifier)
  40. (:destructure (identa sp1 sep sp2 identb)
  41. (declare (ignore sp1 sep sp2))
  42. (list identa identb)))
  43. (defrule variable-expression
  44. (and identifier (? space) "=" (? space) (or string number ident-path) (? #\Newline))
  45. (:destructure (identifier sp1 eq sp2 value nl)
  46. (declare (ignore sp1 eq sp2 nl))
  47. (cons identifier value)))
  48. (defrule block-contents
  49. (* variable-expression)
  50. (:lambda (expressions)
  51. (loop for expression in expressions
  52. collect expression)))
  53. (defrule block
  54. (and block-title block-contents)
  55. (:destructure (title contents)
  56. (cons title contents)))
  57. (defrule file
  58. (* block)
  59. (:lambda (blocks)
  60. (loop for block in blocks
  61. collect block)))
  62. (defun open-configuration-file (filename)
  63. (parse 'file
  64. (concatenate 'string
  65. (with-open-file (input-file filename)
  66. (loop for char = (read-char input-file nil 'foo)
  67. until (eq char 'foo)
  68. collect char)))))
  69. (defun get-config-drive (section ident config)
  70. (cdr (assoc ident (cdr (assoc section config)))))