我想通过一个测试语法异常的宏来扩展 srfi-78。我想要这样的东西:
#! /usr/bin/env scheme-script
#!r6rs
(import (rnrs) (srfi :78 lightweight-testing))
; the macros I want to test
(define-syntax some-macros
(syntax-rules ()
[(_) 'ok]))
; the extension to srfi-78
(define-syntax check-exception
(syntax-rules ()
; ... some code ...
))
; tests
; prints "correct" or someting like that
(check (some-macros) => 'ok)
; should print "correct" (i. e. the test passed)
(check-exception (some-macros 'arg))
; should print "error"
; (i. e. the exception was not thrown as expected)
(check-exception (some-macros))
有可能吗?如果没有,您将如何为宏编写测试?
我知道
test-read-eval-string
来自 srfi-64。它接受一个字符串,将它转换成一个形式并在初始环境中评估这个形式。我想要一个宏来评估当前环境中的给定形式并捕获异常。
最佳答案
唯一可移植的方法是通过 eval 调用代码,并将其包装在一个守卫中。
例如:
(define (safe-eval code env)
(guard [e [(syntax-violation? e) (display e)]]
(eval code env)))
用法:
> (safe-eval '(let a v) (environment '(rnrs)))
&who: let
&message: "invalid syntax"
&syntax:
form: (let a v)
subform: #f
关于scheme - 如何捕获语法异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13701605/