config-parser.lisp 2.2 KB

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