Add copyright, clean up parser.scm
[mudsync.git] / mudsync / parser.scm
1 ;;; Mudsync --- Live hackable MUD
2 ;;; Copyright © 2016 Christopher Allan 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
19 (use-modules (rx irregex)
20              (ice-9 match))
21
22
23 (define (match-to-kwargs irx string)
24   (let ((rx-match (irregex-match irx string)))
25     (if rx-match
26         (map (match-lambda
27                ((match-part . idx)
28                 (cons match-part
29                       (irregex-match-substring
30                        rx-match idx))))
31              (irregex-match-names rx-match))
32         #f)))
33
34 (define (split-verb-and-rest string)
35   (let* ((trimmed (string-trim-both string))
36          (first-space (string-index trimmed #\space)))
37     (if first-space
38         `((verb . ,(substring trimmed 0 first-space))
39           (rest . ,(substring trimmed (+ 1 first-space))))
40         `((verb . ,trimmed)
41           (rest . "")))))
42
43 ;; @@: Not currently used
44 ;; Borrowed from irregex.scm
45 (define match-string
46   '(seq #\" (* (or (~ #\\ #\") (seq #\\ any))) #\"))
47
48 ;; definite and indefinite, but not partitive articles
49 (define article '(or "the" "a" "an"))
50 (define preposition '(or "with" "in" "on" "out of" "at"))
51
52 (define indirect-irx
53   (sre->irregex
54    `(: (? (: ,preposition (+ space)))  ; possibly a preposition (ignored)
55        (? (: ,article (+ space)))      ; possibly an article (ignored)
56        (=> direct-object (* any))      ; direct object (kept)
57        (+ space)
58        (=> preposition ,preposition)   ; main preposition (kept)
59        (+ space)
60        (? (: ,article (+ space)))      ; possibly an article (ignored)
61        (=> indirect-object (+ any))))) ; indirect object (kept)
62
63 (define direct-irx
64   (sre->irregex
65    `(: (? (: ,preposition (+ space)))  ; possibly a preposition (ignored)
66        (? (: ,article (+ space)))     ; possibly an article (ignored)
67        (=> direct-object (* any)))))  ; direct object (kept)
68
69
70 (define say-example "say I really need to get going.")
71 (define attack-sword-example "hit goblin with sword")
72 (define attack-simple-example "hit goblin")
73 (define put-book-on-desk "put the book on the desk")