fb88210a41ed67052ceba5818b7048287169ef01
[mudsync.git] / mudsync / parser.scm
1 (use-modules (rx irregex)
2              (ice-9 match))
3
4
5 (define (match-to-kwargs irx string)
6   (let ((rx-match (irregex-match irx string)))
7     (if rx-match
8         (map (match-lambda
9                ((match-part . idx)
10                 (cons match-part
11                       (irregex-match-substring
12                        rx-match idx))))
13              (irregex-match-names rx-match))
14         #f)))
15
16
17 ;; definite and indefinite, but not partitive articles
18 (define article '(or "the" "a" "an"))
19 (define preposition '(or "with" "in" "on" "out of" "at"))
20
21 (define split-verb-and-rest-irx
22   (sre->irregex
23    `(: (* space) (=> verb (+ alphanum))
24        (? (+ space) (=> rest (* any))))))
25
26 ;; TODO: we could make this MUCH FASTER if we didn't use irregex
27 ;;   for this step!
28 (define (split-verb-and-rest string)
29   (let* ((trimmed (string-trim-both string))
30          (first-space (string-index trimmed #\space)))
31     (if first-space
32         `((verb . ,(substring trimmed 0 first-space))
33           (rest . ,(substring trimmed (+ 1 first-space))))
34         `((verb . ,trimmed)
35           (rest . "")))))
36
37 ;; @@: Not currently used
38 ;; Borrowed from irregex.scm
39 (define match-string
40   '(seq #\" (* (or (~ #\\ #\") (seq #\\ any))) #\"))
41
42 (define indirect-irx
43   (sre->irregex
44    `(: (? (: ,article (+ space)))  ; possibly an article (ignored)
45        (=> direct-object (* any))  ; direct object
46        (+ space) (=> preposition ,preposition) (+ space)
47        (? (: ,article (+ space)))
48        (=> indirect-object (+ any)))))
49
50 (define direct-irx
51   `(: (? (: ,article (+ space)))    ; possibly an article (ignored)
52       (=> direct-object (* any))))  ; direct object
53
54
55 (define say-example "say I really need to get going.")
56 (define attack-sword-example "hit goblin with sword")
57 (define attack-simple-example "hit goblin")
58 (define put-book-on-desk "put the book on the desk")