From 495c839e7f26e86521fba24a0402c5e0bf4dd579 Mon Sep 17 00:00:00 2001 From: Christopher Allan Webber Date: Mon, 2 May 2016 08:47:35 -0500 Subject: [PATCH] prototype sentence parsing code --- mudsync/parser.scm | 58 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 mudsync/parser.scm diff --git a/mudsync/parser.scm b/mudsync/parser.scm new file mode 100644 index 0000000..fb88210 --- /dev/null +++ b/mudsync/parser.scm @@ -0,0 +1,58 @@ +(use-modules (rx irregex) + (ice-9 match)) + + +(define (match-to-kwargs irx string) + (let ((rx-match (irregex-match irx string))) + (if rx-match + (map (match-lambda + ((match-part . idx) + (cons match-part + (irregex-match-substring + rx-match idx)))) + (irregex-match-names rx-match)) + #f))) + + +;; definite and indefinite, but not partitive articles +(define article '(or "the" "a" "an")) +(define preposition '(or "with" "in" "on" "out of" "at")) + +(define split-verb-and-rest-irx + (sre->irregex + `(: (* space) (=> verb (+ alphanum)) + (? (+ space) (=> rest (* any)))))) + +;; TODO: we could make this MUCH FASTER if we didn't use irregex +;; for this step! +(define (split-verb-and-rest string) + (let* ((trimmed (string-trim-both string)) + (first-space (string-index trimmed #\space))) + (if first-space + `((verb . ,(substring trimmed 0 first-space)) + (rest . ,(substring trimmed (+ 1 first-space)))) + `((verb . ,trimmed) + (rest . ""))))) + +;; @@: Not currently used +;; Borrowed from irregex.scm +(define match-string + '(seq #\" (* (or (~ #\\ #\") (seq #\\ any))) #\")) + +(define indirect-irx + (sre->irregex + `(: (? (: ,article (+ space))) ; possibly an article (ignored) + (=> direct-object (* any)) ; direct object + (+ space) (=> preposition ,preposition) (+ space) + (? (: ,article (+ space))) + (=> indirect-object (+ any))))) + +(define direct-irx + `(: (? (: ,article (+ space))) ; possibly an article (ignored) + (=> direct-object (* any)))) ; direct object + + +(define say-example "say I really need to get going.") +(define attack-sword-example "hit goblin with sword") +(define attack-simple-example "hit goblin") +(define put-book-on-desk "put the book on the desk") -- 2.31.1