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.
brag/yaragg-lib/yaragg/codegen/codegen.rkt

275 lines
11 KiB
Racket

8 years ago
#lang racket/base
7 years ago
(require racket/list
8 years ago
racket/syntax
yaragg/rules/stx-types
8 years ago
syntax/id-table
(prefix-in sat: yaragg/codegen/satisfaction)
7 years ago
(for-template racket/base
yaragg/codegen/runtime
yaragg/private/internal-support))
8 years ago
7 years ago
(provide (all-defined-out)
(for-template (all-from-out yaragg/codegen/runtime
yaragg/private/internal-support)))
8 years ago
;; Given a flattened rule, returns a syntax for the code that
;; preserves as much source location as possible.
;;
;; Each rule is defined to return a list with the following structure:
;;
;; stx :== (name (U tokens rule-stx) ...)
;;
(define (flat-rule->yacc-rule a-flat-rule)
(syntax-case a-flat-rule ()
7 years ago
[(rule-type origin name . clauses)
7 years ago
(with-syntax ([translated-clauses (for/list ([clause-stx (in-list (syntax->list #'clauses))])
7 years ago
(translate-clause clause-stx #'name #'origin))])
#'[name . translated-clauses])]))
8 years ago
;; translates a single primitive rule clause.
;; A clause is a simple list of ids, lit, vals, and inferred-id elements.
;; The action taken depends on the pattern type.
(define (translate-clause a-clause rule-name/false origin)
(define translated-patterns
(let loop ([primitive-patterns (syntax->list a-clause)])
(cond
7 years ago
[(empty? primitive-patterns) null]
8 years ago
[else
(cons (syntax-case (first primitive-patterns) (id lit token inferred-id)
[(id val)
#'val]
[(lit val)
(datum->syntax #f (string->symbol (syntax-e #'val)) #'val)]
[(token val)
#'val]
[(inferred-id val reason)
#'val])
(loop (rest primitive-patterns)))])))
(define translated-actions
(for/list ([translated-pattern (in-list translated-patterns)]
7 years ago
[primitive-pattern (in-list (syntax->list a-clause))]
8 years ago
[pos (in-naturals 1)])
(if (eq? (syntax-property primitive-pattern 'hide) 'hide)
#'null
7 years ago
(with-syntax ([$X (format-id translated-pattern "$~a" pos)]
[$X-start-pos (format-id translated-pattern "$~a-start-pos" pos)]
[$X-end-pos (format-id translated-pattern "$~a-end-pos" pos)])
(syntax-case primitive-pattern (id lit token inferred-id)
8 years ago
;; When a rule usage is inferred, the value of $X is a syntax object
7 years ago
;; whose head is the name of the inferred rule. We strip that out,
;; leaving the residue to be absorbed.
[(inferred-id val reason)
#'(syntax-case $X ()
[(inferred-rule-name . rest)
(syntax->list #'rest)])]
[(id val)
;; at this point, the 'hide property is either #f or "splice"
7 years ago
;; ('hide value is handled at the top of this conditional)
;; we need to use boolean because a symbol is treated as an identifier.
;; also we'll separate it into its own property for clarity and test for it in "runtime.rkt"
#`(list (syntax-property $X 'splice-rh-id #,(and (syntax-property primitive-pattern 'hide) #t)))]
[(lit val)
#'(list (atomic-datum->syntax $X $X-start-pos $X-end-pos))]
[(token val)
#'(list (atomic-datum->syntax $X $X-start-pos $X-end-pos))])))))
8 years ago
(define whole-rule-loc
(if (empty? translated-patterns)
#'(list (current-source) #f #f #f #f)
(with-syntax ([$1-start-pos (datum->syntax (first translated-patterns) '$1-start-pos)]
[$n-end-pos (format-id (last translated-patterns) "$~a-end-pos" (length translated-patterns))])
#`(positions->srcloc $1-start-pos $n-end-pos))))
;; move 'hide-or-splice-lhs-id property into function because name is datum-ized
(with-syntax ([(translated-pattern ...) translated-patterns]
[(translated-action ...) translated-actions])
#`[(translated-pattern ...)
(rule-components->syntax '#,rule-name/false translated-action ...
#:srcloc #,whole-rule-loc
#:hide-or-splice? #,(syntax-property rule-name/false 'hide-or-splice-lhs-id))]))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; collect-token-types: (listof rule-syntax) -> (values (listof identifier) (listof identifier))
;;
;; Given a rule, automatically derive the list of implicit and
;; explicit token types we need to generate.
;;
;; Note: EOF is reserved, and will always be included in the list
;; of explicit token types, though the user is not allow to express it themselves.
(define (rules-collect-token-types rules)
(define-values (implicit explicit)
7 years ago
(for/fold ([implicit null]
8 years ago
[explicit (list (datum->syntax (first rules) 'EOF))])
7 years ago
([a-rule (in-list rules)])
(syntax-case a-rule (rule)
[(rule _ a-pattern)
(let loop ([a-pattern #'a-pattern]
[implicit implicit]
[explicit explicit])
(syntax-case a-pattern (id lit token choice repeat maybe seq EOF)
[(id val)
(values implicit explicit)]
[(lit val)
(values (cons #'val implicit) explicit)]
[(token EOF)
(raise-syntax-error #f "Token EOF is reserved and can not be used in a grammar" #'val)]
[(token val)
(values implicit (cons #'val explicit))]
[(choice . vals)
(for/fold ([implicit implicit]
[explicit explicit])
([v (in-list (syntax->list #'vals))])
(loop v implicit explicit))]
[(repeat min max val)
(loop #'val implicit explicit)]
[(maybe val)
(loop #'val implicit explicit)]
[(seq . vals)
(for/fold ([implicit implicit]
[explicit explicit])
([v (in-list (syntax->list #'vals))])
(loop v implicit explicit))]))])))
8 years ago
(values (reverse implicit) (reverse explicit)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; rule-id: rule -> identifier-stx
;; Get the binding id of a rule.
(define (rule-id a-rule)
(syntax-case a-rule (rule)
[(rule id a-pattern)
#'id]))
(define (rule-pattern a-rule)
(syntax-case a-rule (rule)
[(rule id a-pattern)
#'a-pattern]))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; check-all-rules-defined!: (listof rule-stx) -> void
(define (check-all-rules-defined! rules)
(define table (make-free-id-table))
;; Pass one: collect all the defined rule names.
(for ([a-rule (in-list rules)])
(free-id-table-set! table (rule-id a-rule) #t))
8 years ago
;; Pass two: check each referenced id, and make sure it's been defined.
7 years ago
(for* ([a-rule (in-list rules)]
[referenced-id (in-list (rule-collect-used-ids a-rule))]
#:unless (free-id-table-ref table referenced-id (λ () #f)))
(raise-syntax-error #f (format "Rule ~a has no definition" (syntax-e referenced-id))
referenced-id)))
8 years ago
;; check-all-rules-no-duplicates!: (listof rule-stx) -> void
(define (check-all-rules-no-duplicates! rules)
(define table (make-free-id-table))
;; Pass one: collect all the defined rule names.
(for ([a-rule (in-list rules)])
7 years ago
(define maybe-other-rule-id (free-id-table-ref table (rule-id a-rule) (λ () #f)))
(when maybe-other-rule-id
(raise-syntax-error #f (format "Rule ~a has a duplicate definition" (syntax-e (rule-id a-rule)))
(rule-id a-rule)
#f
(list (rule-id a-rule) maybe-other-rule-id)))
(free-id-table-set! table (rule-id a-rule) (rule-id a-rule))))
8 years ago
;; rule-collect-used-ids: rule-stx -> (listof identifier)
;; Given a rule, extracts a list of identifiers
(define (rule-collect-used-ids a-rule)
(syntax-case a-rule (rule)
[(rule id a-pattern)
7 years ago
(pattern-collect-used-ids #'a-pattern null)]))
8 years ago
;; pattern-collect-used-ids: pattern-stx (listof identifier) -> (listof identifier)
;; Returns a flat list of rule identifiers referenced in the pattern.
(define (pattern-collect-used-ids a-pattern acc)
(let loop ([a-pattern a-pattern]
[acc acc])
(syntax-case a-pattern (id lit token choice repeat maybe seq)
[(id val)
(cons #'val acc)]
[(lit val)
acc]
[(token val)
acc]
7 years ago
[(choice . vals)
8 years ago
(for/fold ([acc acc])
7 years ago
([v (in-list (syntax->list #'vals))])
8 years ago
(loop v acc))]
[(repeat min max val)
8 years ago
(loop #'val acc)]
[(maybe val)
(loop #'val acc)]
7 years ago
[(seq . vals)
8 years ago
(for/fold ([acc acc])
7 years ago
([v (in-list (syntax->list #'vals))])
8 years ago
(loop v acc))])))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; check-all-rules-satisfiable: (listof rule-stx) -> void
;; Does a simple graph traversal / topological sort-like thing to make sure that, for
;; any rule, there's some finite sequence of tokens that
;; satisfies it. If this is not the case, then something horrible
;; has happened, and we need to tell the user about it.
;;
;; NOTE: Assumes all referenced rules have definitions.
(define (check-all-rules-satisfiable! rules)
7 years ago
(define toplevel-rule-table
(make-free-id-table (for/list ([a-rule (in-list rules)])
(cons (rule-id a-rule) (sat:make-and)))))
7 years ago
(define leaves null)
8 years ago
(define (make-leaf)
(define a-leaf (sat:make-and))
(set! leaves (cons a-leaf leaves))
a-leaf)
(define (process-pattern a-pattern)
(syntax-case a-pattern (id lit token choice repeat maybe seq)
[(id val)
(free-id-table-ref toplevel-rule-table #'val)]
[(lit val)
(make-leaf)]
[(token val)
(make-leaf)]
7 years ago
[(choice . vals)
(let ([an-or-node (sat:make-or)])
(for* ([v (in-list (syntax->list #'vals))]
[a-child (in-value (process-pattern v))])
(sat:add-child! an-or-node a-child))
8 years ago
an-or-node)]
[(repeat min max val)
8 years ago
(syntax-case #'min ()
7 years ago
[0 (make-leaf)]
[_ (process-pattern #'val)])]
[(maybe val) (make-leaf)]
[(seq . vals)
(let ([an-and-node (sat:make-and)])
(for* ([v (in-list (syntax->list #'vals))]
[a-child (in-value (process-pattern v))])
(sat:add-child! an-and-node a-child))
8 years ago
an-and-node)]))
7 years ago
(for* ([a-rule (in-list rules)]
[rule-node (in-value (free-id-table-ref toplevel-rule-table (rule-id a-rule)))])
(sat:add-child! rule-node (process-pattern (rule-pattern a-rule))))
8 years ago
7 years ago
(for-each sat:visit! leaves)
8 years ago
7 years ago
(for* ([a-rule (in-list rules)]
[rule-node (in-value (free-id-table-ref toplevel-rule-table (rule-id a-rule)))]
#:unless (sat:node-yes? rule-node))
(raise-syntax-error #f
(format "Rule ~a has no finite derivation" (syntax-e (rule-id a-rule)))
7 years ago
(rule-id a-rule))))