From 484fffc6c5946b8d90decb803bfc9e318ba3a2e0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hugo=20H=C3=B6rnquist?= Date: Fri, 23 Sep 2022 21:42:31 +0200 Subject: Document and parameterize peg-to-graph. --- module/scripts/peg-to-graph.scm | 46 +++++++++++++++++++++++++++-------------- 1 file changed, 30 insertions(+), 16 deletions(-) diff --git a/module/scripts/peg-to-graph.scm b/module/scripts/peg-to-graph.scm index 0472fd02..e2a829f9 100755 --- a/module/scripts/peg-to-graph.scm +++ b/module/scripts/peg-to-graph.scm @@ -4,10 +4,20 @@ :use-module ((hnh module-introspection static-util) :select (get-forms)) :use-module (srfi srfi-1) :use-module (ice-9 match) + :use-module (hnh util options) + :use-module (ice-9 getopt-long) :export (main)) +(define option-spec + `((engine (value #t) + (description "Graphviz rendering engine to use. Defaults to DOT")) + (output (single-char #\o) + (value #t) + (description "Name of output pdf")))) + (define %summary "Output peg-pattern relations as a graphviz graph.") -(define %include-in-list #t) +(define %synopsis "peg-to-graph [options] ") +(define %help (format-arg-help option-spec)) (define peg-primitives '(and or * + ? followed-by not-followed-by peg-any range @@ -30,20 +40,24 @@ (unique-symbols (list body))))))) (define (main . args) - (when (< 1 (length args)) - (format #t "Usage: guild peg-to-graph ~%") - (exit 1)) + (define options (getopt-long (cons "peg-to-graph" argrs) + (getopt-opt option-spec))) + (define engine (option-ref options 'engine "dot")) + (define output-file (option-ref options 'output "lex2.pdf")) + (define input-file (let ((filenames (option-ref options '() '()))) + (when (null? filenames) + (format #t "Usage: ~a~%" %summary) + (exit 1)) + (car filenames))) + (let ((graph (gv:digraph "G"))) - (let ((input-file (car args))) - (for-each (lambda (form) handle-peg-form! graph form) - (filter (lambda (x) - (and (list? x) - (not (null? x)) - (eq? 'define-peg-pattern (car x)))) - (call-with-input-file input-file get-forms)))) - - (gv:layout graph "dot") - (gv:render graph "pdf" "lex2.pdf") - - (display "done\n"))) + (for-each (lambda (form) handle-peg-form! graph form) + (filter (lambda (x) + (and (list? x) + (not (null? x)) + (eq? 'define-peg-pattern (car x)))) + (call-with-input-file input-file get-forms))) + + (gv:layout graph engine) + (gv:render graph "pdf" output-file))) -- cgit v1.2.3