Skip to content

Instantly share code, notes, and snippets.

#lang racket
(module test-parsing ragg/codegen/sexp-based-lang
(rules
;; ones ::= ["1" ones]
(rule ones (maybe (seq (lit "1") (id ones))))))
(require (submod "." test-parsing))
(parse (list "1" "1" "1"))
@dyoo
dyoo / gist:4589601
Created January 21, 2013 21:28
Example of ragg output being parsed with syntax-parse and literal sets
#lang racket/base
(require ragg/examples/simple-line-drawing/lexer
ragg/examples/simple-line-drawing/grammar
syntax/strip-context
syntax/parse
(for-syntax racket/base))
(define-syntax (drawing stx) (raise-syntax-error #f "Don't look at me; I'm shy" stx))
#lang racket
(require syntax/parse)
(syntax-parse #'("foo" "bar")
[((~and "foo" x) y)
(list #'x #'y)])
(syntax-parse #'("food" "bar")
[((~and "foo" x) y)
(list #'x #'y)])
#lang racket/base
(require (for-syntax racket/base syntax/parse))
(module+ test (require rackunit))
(define-syntax (test+ stx)
(syntax-parse stx
#:literals (=>)
[(_ (fn arg ...) => v)
(with-syntax ([fn-name (symbol->string (syntax->datum #'fn))])
#lang racket
(module foo ragg/sexp/lang
rule-1: "0"+ || rule-2
rule-2: "1"
)
(require (prefix-in test: (submod "." foo)))
@dyoo
dyoo / gist:4659253
Created January 28, 2013 21:33
external test with submodule
#lang racket
(define (f x)
(* x x))
(provide f)
(module* foo racket/base
;; External test of the function
(require rackunit
@dyoo
dyoo / str-expr.rkt
Last active December 11, 2015 23:48
String expression evaluation example
#lang racket
;; A str-expr is either:
;;
;; 1. a plain string, or
;; 2. (list '+ str-expr str-expr)
;; 3. (list '* number str-expr)
;; evaluate: str-expr -> string
@dyoo
dyoo / find-duplicates.rkt
Last active December 11, 2015 23:48
Finding duplicates
#lang racket
(define (find-duplicates elts)
(define ht (make-hash))
(for/fold ([dups '()]) ([x (in-list elts)])
(hash-set! ht x (add1 (hash-ref ht x 0)))
(cond
[(= (hash-ref ht x) 2)
(cons x dups)]
@dyoo
dyoo / for-string.rkt
Last active December 12, 2015 00:39
for/string
#lang racket
(define-syntax (for/string stx)
(syntax-case stx ()
[(_ clauses . defs+exprs)
(with-syntax ([original stx])
(syntax/loc stx
(apply string-append
(reverse
(for/fold/derived original
@dyoo
dyoo / gist:4695459
Created February 2, 2013 01:20
An example of letting Racket tell you where things are defined.
#lang racket
(require syntax/modresolve)
;; An example of letting Racket tell you where things are defined.
(resolve-module-path-index (first (identifier-binding #'for/hash))
(current-directory))