#! /usr/bin/env mzscheme #lang scheme ;tested in PLT Scheme 4.1 ;; ;; py.scm -- Python(ish) parser ;; (require mzlib/defmacro) (require mzlib/compat) ;1+ 1- (require mzlib/string) ;read-from-string (include "om-core.scm") ;; ----------------------------------------------- ;; Python-ish grammar definition (include "py-core.scm") (include "py-grammar.scm") ; Generated from py-grammar.bnf (define grammar top) ;alias ;; ----------------------------------------------- ;; This is basically I/O plumbing.... (define (transform-grammar input-file output-file) ;(when (>= *LOGLEVEL* 2) ; (printf ";GIVEN:~%~a~%" *s*) ; (printf ";PARSING...~%") ) (let* ((output (parse-from-file input-file)) (outfile (open-output-file output-file #:exists 'replace))) (pretty-print-columns 80) (for-each (lambda (expr) (pretty-print expr outfile) (newline outfile)) output) ) (let ((remaining (substring *s* *i*))) (if (equal? remaining "") (when (>= *LOGLEVEL* 1) (printf "~%All input was parsed.~%")) (printf "~%WARNING: UNPARSED INPUT REMAINING:~%~a~%" remaining) )) ) ;; ----------------------------------------------- ;; Command line interface ;; (This is PLT-Scheme-specific; also, R6RS offers a command-line parser) ;; (define verbosity (make-parameter 0)) (define includes (make-parameter null)) (command-line #:multi [("-v" "--verbose") "Verbose messages (-vv, -vvv for voluminous debug messages)" (verbosity (1+ (verbosity)))] ; I don't think this is really necessary... and besides, I don't think ; (include) works at runtime; we'd have to (load) all the interdependent ; parser parts, or deal with namespaces... [("-i" "--include") scheme-file "Include ..." (includes (cons scheme-file (includes)))] #:args (python-input-file output-scheme-file) (begin (set! *LOGLEVEL* (verbosity)) (transform-grammar python-input-file output-scheme-file) ))