(define (parse-duckyscript input) (define blank '(+ ("\t\v "))) (define number '(+ numeric)) (define string '(+ nonl)) (define comment-cmd `(: (=> cmd "REM") ,blank (=> string ,string))) (define default-delay-name '(: (=> cmd "DEFAULT") (? "_") "DELAY")) (define default-delay-cmd `(: ,default-delay-name ,blank (=> number ,number))) (define delay-cmd `(: (=> cmd "DELAY") ,blank (=> number ,number))) (define string-cmd `(: (=> cmd "STRING") ,blank (=> string ,string))) (define repeat-cmd `(: (=> cmd "REPEAT") ,blank (=> number ,number))) (define mod '(or "WINDOWS" "GUI" "MENU" "APP" "SHIFT" "ALT" "CONTROL" "CTRL")) (define key '(+ (or upper numeric #\_))) (define char '(~ #\space)) (define shortcut-cmd `(: (=> strings (* (: ,mod (? ,blank)))) (=> string (? (or ,key ,char))))) (define command-rx `(: (or ,comment-cmd ,default-delay-cmd ,delay-cmd ,string-cmd ,repeat-cmd ,shortcut-cmd))) (define (to-lower s) (list->string (map char-downcase (string->list s)))) (define (non-blank-string s) (if (zero? (string-length s)) #f s)) (define (parse line) (define $ irregex-match-substring) (let ((m (irregex-match command-rx line))) (if m (let ((command ($ m 'cmd))) (case (and command (string->symbol (to-lower command))) ((rem) `(comment ,($ m 'string))) ((default) `(default-delay ,(string->number ($ m 'number)))) ((delay) `(delay ,(string->number ($ m 'number)))) ((string) `(string ,($ m 'string))) ((repeat) `(repeat ,(string->number ($ m 'number)))) (else `(shortcut ,(irregex-split blank ($ m 'strings)) ,(or (non-blank-string ($ m 'string)) (error "Missing key or character" line)))))) (error "Unrecognized line" line)))) (map parse (irregex-split "\r\n|\n" input))) (pp (parse-duckyscript "REM Hello Rick! DEFAULTDELAY 30 DELAY 3000 GUI r DELAY 200 STRING https://www.youtube.com/watch?v=dQw4w9WgXcQ ENTER DELAY 3000 STRING f REPEAT 3")) ;; ((comment "REM") ; why ;; (default-delay 30) ;; (delay 3000) ;; (shortcut ("GUI") "r") ;; (delay 200) ;; (string "STRING") ; no ;; (shortcut () "ENTER") ;; (delay 3000) ;; (string "STRING") ; plz ;; (repeat 3))