You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
beautiful-racket/beautiful-racket-demo/regexcellent-demo/main.rkt

66 lines
1.9 KiB
Racket

#lang br/quicklang
(require brag/support "grammar.rkt")
(provide (all-from-out br/quicklang) (all-defined-out))
(module+ reader
(provide read-syntax))
7 years ago
(define-lex-abbrev reserved-chars (char-set "()*+?.^$|<!=[]"))
7 years ago
(define tokenize
(lexer
[(:+ "\n") (token 'NEWLINE lexeme)]
[(from/stop-before ";" "\n") (token 'COMMENT #:skip? #t)]
[(:+ whitespace) (token 'SP lexeme #:skip? #t)]
7 years ago
[reserved-chars lexeme]
[alphabetic (token 'LITERAL lexeme)]))
(define (read-syntax src ip)
7 years ago
(define parse-tree (parse (λ () (tokenize ip))))
(strip-context
(with-syntax ([PT parse-tree])
7 years ago
#'(module mod-name regexcellent-demo
(for-each displayln PT)))))
(define-macro (top . LINES) #'(list . LINES))
(define (line . pieces)
(format "This pattern matches ~a." (string-join pieces ", followed by ")))
(define (pat . xs)
(if (= (length xs) 1)
(car xs)
7 years ago
(format "a sequence (~a)" (string-join xs ", followed by "))))
(define (lookbehind pat)
(format "a lookbehind assertion of ~a" pat))
7 years ago
(define (lookahead pat)
(format "a lookahead assertion of ~a" pat))
(define (choice . pats)
7 years ago
(format "a choice of ~a" (string-join pats " or ")))
(define (repeat thing [quantifier #f] [maybe-non-greedy? #f])
(string-join
(filter values
(list thing
(case quantifier
[("*") "repeated zero or more times"]
[("+") "repeated one or more times"]
[("?") "zero or once"]
[else #f])
(and maybe-non-greedy? "non-greedily")))
" "))
(define (group pat)
(format "the group containing ~a" pat))
(define (any) "any character")
(define (start) "the start of the input")
7 years ago
(define (end) "the end of the input")
(define (literal str) (~v str))
(define (chars . lits)
7 years ago
(format "any member of the character set {~a}" (string-join (map ~a lits) " ")))