2020-09-05 05:47:54 +00:00
|
|
|
#lang racket/base
|
|
|
|
|
2020-09-09 07:35:17 +00:00
|
|
|
(require (for-syntax racket/base racket/list racket/match))
|
2020-09-06 05:35:32 +00:00
|
|
|
(provide (rename-out [kaitai:module-begin #%module-begin])
|
|
|
|
(except-out (all-from-out racket/base) #%module-begin))
|
2020-09-05 08:19:24 +00:00
|
|
|
|
2020-09-06 05:35:32 +00:00
|
|
|
;; meow
|
|
|
|
|
2020-09-07 05:31:24 +00:00
|
|
|
;; utility to concat symbol stuff
|
|
|
|
(define-for-syntax (sym+ . args)
|
|
|
|
(define (->string a) (if (symbol? a) (symbol->string a) a))
|
|
|
|
(string->symbol (apply string-append (map ->string args))))
|
|
|
|
|
2020-09-06 05:35:32 +00:00
|
|
|
;; makes a syntax error raiser
|
2020-09-07 05:31:24 +00:00
|
|
|
(define-for-syntax (make-rse msg)
|
2020-09-06 05:35:32 +00:00
|
|
|
(lambda () (raise-syntax-error #f msg)))
|
|
|
|
|
|
|
|
;; converts a ksy id to a racket id
|
|
|
|
;; maps _ to -
|
|
|
|
(define-for-syntax (kaitai-str->sym str)
|
|
|
|
(string->symbol (regexp-replace* #px"_" str "-")))
|
|
|
|
|
2020-09-07 05:31:24 +00:00
|
|
|
;; returns either the given id or meta.id
|
|
|
|
;; otherwise raises syntax error
|
|
|
|
(define-for-syntax (get-id top given-id)
|
|
|
|
(kaitai-str->sym
|
|
|
|
(or given-id (hash-ref (hash-ref top "meta" (hash)) "id"
|
|
|
|
(make-rse "type has no meta.id and none was provided")))))
|
|
|
|
|
|
|
|
;; helper for types meta lookup
|
|
|
|
(define-for-syntax (get-id-seq top given-id)
|
|
|
|
(define id-sym (get-id top given-id))
|
|
|
|
(define seq (hash-ref top "seq" (make-rse "form must have a seq element")))
|
|
|
|
(values id-sym seq))
|
|
|
|
|
2020-09-06 05:35:32 +00:00
|
|
|
;; generates struct definitions
|
|
|
|
(define-for-syntax (gen-struct top [given-id #f])
|
2020-09-07 05:31:24 +00:00
|
|
|
(define-values [id-sym seq] (get-id-seq top given-id))
|
2020-09-06 05:35:32 +00:00
|
|
|
(define instances (hash-ref top "instances" (hash)))
|
|
|
|
#`((provide [struct-out #,id-sym])
|
|
|
|
(struct #,id-sym
|
2020-09-07 05:31:24 +00:00
|
|
|
[#,@(map (lambda (a) (kaitai-str->sym (hash-ref a "id" (make-rse "seq element has no id"))))
|
2020-09-06 05:35:32 +00:00
|
|
|
seq)] #:transparent)))
|
|
|
|
|
2020-09-07 05:31:24 +00:00
|
|
|
;; runtime lib
|
|
|
|
|
|
|
|
(module runtime racket/base
|
|
|
|
(provide (all-defined-out))
|
|
|
|
(define (kaitai:->binaryio thing)
|
|
|
|
;; TODO
|
|
|
|
thing)
|
|
|
|
(define (kaitai:uwu)
|
|
|
|
(displayln "uwu")))
|
|
|
|
|
|
|
|
(require 'runtime)
|
|
|
|
(provide (all-from-out 'runtime))
|
|
|
|
|
2020-09-06 05:35:32 +00:00
|
|
|
;; language infrastructure
|
|
|
|
|
|
|
|
(define-syntax (kaitai:module-begin stx)
|
|
|
|
(define body (second (syntax-e stx)))
|
2020-09-09 07:35:17 +00:00
|
|
|
(displayln (syntax->datum body))
|
2020-09-06 05:35:32 +00:00
|
|
|
#`(#%module-begin
|
2020-09-07 05:31:24 +00:00
|
|
|
(module+ main
|
|
|
|
(kaitai:uwu))))
|
2020-09-05 08:19:24 +00:00
|
|
|
|
|
|
|
(module reader syntax/module-reader
|
|
|
|
#:read kaitai-read
|
|
|
|
#:read-syntax kaitai-read
|
2020-09-06 05:35:32 +00:00
|
|
|
#:language 'kaitai
|
2020-09-05 08:19:24 +00:00
|
|
|
|
|
|
|
(require yaml)
|
|
|
|
|
|
|
|
(define (kaitai-read path port)
|
|
|
|
(or (read-yaml port) eof))
|
|
|
|
|
|
|
|
(define (kaitai-read-syntax path port)
|
|
|
|
(datum->syntax #f (kaitai-read path port))))
|