X-Git-Url: https://jxself.org/git/?p=mudsync.git;a=blobdiff_plain;f=mudsync%2Fparser.scm;h=5da6bc9f8c6c8c0ee8dd0f38a68891abfa2eca67;hp=51e07c899b888d1bb89e9cd9036489d30002a3cc;hb=b8279be0a2ca8f5e235166acd5cc1bde3854dd5b;hpb=846235afb4cdf32d98c7db8ebdea5bdfcbc72f45 diff --git a/mudsync/parser.scm b/mudsync/parser.scm index 51e07c8..5da6bc9 100644 --- a/mudsync/parser.scm +++ b/mudsync/parser.scm @@ -16,29 +16,43 @@ ;;; You should have received a copy of the GNU General Public License ;;; along with Mudsync. If not, see . -(use-modules (rx irregex) - (ice-9 match)) +(define-module (mudsync parser) + #:use-module (rx irregex) + #:use-module (ice-9 match) + #:use-module (srfi srfi-1) + #:use-module (srfi srfi-9) + #:export (match-to-kwargs + split-verb-and-rest + article preposition + + cmatch-indir-obj + cmatch-direct-obj + cmatch-direct-obj-greedy + cmatch-empty + cmatch-greedy)) (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)) + (fold + (match-lambda* + (((match-part . idx) prev) + (cons (symbol->keyword match-part) + (cons (irregex-match-substring + rx-match idx) + prev)))) + '() + (irregex-match-names rx-match)) #f))) (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 . ""))))) + (cons (substring trimmed 0 first-space) + (substring trimmed (+ 1 first-space))) + (cons trimmed "")))) ;; @@: Not currently used ;; Borrowed from irregex.scm @@ -53,21 +67,42 @@ (sre->irregex `(: (? (: ,preposition (+ space))) ; possibly a preposition (ignored) (? (: ,article (+ space))) ; possibly an article (ignored) - (=> direct-object (* any)) ; direct object (kept) + (=> direct-obj (* any)) ; direct object (kept) (+ space) (=> preposition ,preposition) ; main preposition (kept) (+ space) (? (: ,article (+ space))) ; possibly an article (ignored) - (=> indirect-object (+ any))))) ; indirect object (kept) + (=> indir-obj (+ any))))) ; indirect object (kept) + +(define (cmatch-indir-obj phrase) + (match-to-kwargs indirect-irx phrase)) (define direct-irx (sre->irregex `(: (? (: ,preposition (+ space))) ; possibly a preposition (ignored) (? (: ,article (+ space))) ; possibly an article (ignored) - (=> direct-object (* any))))) ; direct object (kept) + (=> direct-obj (+ any))))) ; direct object (kept) + +(define (cmatch-direct-obj phrase) + (match-to-kwargs direct-irx phrase)) + +(define (cmatch-empty phrase) + (if (equal? (string-trim phrase) "") + '() + #f)) + +(define (cmatch-direct-obj-greedy phrase) + ;; Turns out this uses the same semantics as splitting verb/rest + (match (split-verb-and-rest phrase) + ((direct-obj . rest) + (list #:direct-obj direct-obj + #:rest rest)) + (#f #f))) +(define (cmatch-greedy phrase) + `(#:phrase ,phrase)) -(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") +;; (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")