parser.scm 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. ;;; Mudsync --- Live hackable MUD
  2. ;;; Copyright © 2016 Christine Lemmer-Webber <cwebber@dustycloud.org>
  3. ;;;
  4. ;;; This file is part of Mudsync.
  5. ;;;
  6. ;;; Mudsync is free software; you can redistribute it and/or modify it
  7. ;;; under the terms of the GNU General Public License as published by
  8. ;;; the Free Software Foundation; either version 3 of the License, or
  9. ;;; (at your option) any later version.
  10. ;;;
  11. ;;; Mudsync is distributed in the hope that it will be useful, but
  12. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. ;;; General Public License for more details.
  15. ;;;
  16. ;;; You should have received a copy of the GNU General Public License
  17. ;;; along with Mudsync. If not, see <http://www.gnu.org/licenses/>.
  18. (define-module (mudsync parser)
  19. #:use-module (rx irregex)
  20. #:use-module (ice-9 match)
  21. #:use-module (srfi srfi-1)
  22. #:use-module (srfi srfi-9)
  23. #:export (match-to-kwargs
  24. split-verb-and-rest
  25. article preposition
  26. cmatch-indir-obj
  27. cmatch-direct-obj
  28. cmatch-direct-obj-greedy
  29. cmatch-empty
  30. cmatch-greedy))
  31. (define (match-to-kwargs irx string)
  32. (let ((rx-match (irregex-match irx string)))
  33. (if rx-match
  34. (fold
  35. (match-lambda*
  36. (((match-part . idx) prev)
  37. (cons (symbol->keyword match-part)
  38. (cons (irregex-match-substring
  39. rx-match idx)
  40. prev))))
  41. '()
  42. (irregex-match-names rx-match))
  43. #f)))
  44. (define (split-verb-and-rest string)
  45. (let* ((trimmed (string-trim-both string))
  46. (first-space (string-index trimmed #\space)))
  47. (if first-space
  48. (cons (substring trimmed 0 first-space)
  49. (substring trimmed (+ 1 first-space)))
  50. (cons trimmed ""))))
  51. ;; @@: Not currently used
  52. ;; Borrowed from irregex.scm
  53. (define match-string
  54. '(seq #\" (* (or (~ #\\ #\") (seq #\\ any))) #\"))
  55. ;; definite and indefinite, but not partitive articles
  56. (define article '(or "the" "a" "an"))
  57. (define preposition '(or "with" "in" "inside" "into" "on" "out" "out of"
  58. "at" "as" "to" "about" "from"))
  59. (define indirect-irx
  60. (sre->irregex
  61. `(: (? (: ,preposition (+ space))) ; possibly a preposition
  62. (? (: ,article (+ space))) ; possibly an article (ignored)
  63. (=> direct-obj (* any)) ; direct object (kept)
  64. (+ space)
  65. (=> preposition ,preposition) ; main preposition (kept)
  66. (+ space)
  67. (? (: ,article (+ space))) ; possibly an article (ignored)
  68. (=> indir-obj (+ any))))) ; indirect object (kept)
  69. (define (cmatch-indir-obj phrase)
  70. (match-to-kwargs indirect-irx phrase))
  71. (define direct-irx
  72. (sre->irregex
  73. `(: (? (: ,preposition (+ space))) ; possibly a preposition (ignored)
  74. (? (: ,article (+ space))) ; possibly an article (ignored)
  75. (=> direct-obj (+ any))))) ; direct object (kept)
  76. (define (cmatch-direct-obj phrase)
  77. (match-to-kwargs direct-irx phrase))
  78. (define (cmatch-empty phrase)
  79. (if (equal? (string-trim phrase) "")
  80. '()
  81. #f))
  82. (define (cmatch-direct-obj-greedy phrase)
  83. ;; Turns out this uses the same semantics as splitting verb/rest
  84. (match (split-verb-and-rest phrase)
  85. ((direct-obj . rest)
  86. (list #:direct-obj direct-obj
  87. #:rest rest))
  88. (#f #f)))
  89. (define (cmatch-greedy phrase)
  90. `(#:phrase ,phrase))
  91. ;; (define say-example "say I really need to get going.")
  92. ;; (define attack-sword-example "hit goblin with sword")
  93. ;; (define attack-simple-example "hit goblin")
  94. ;; (define put-book-on-desk "put the book on the desk")