scheme - 如何捕获语法异常

标签 scheme r6rs

我想通过一个测试语法异常的宏来扩展 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/

相关文章:

scheme - 编码一个什么都不做的延续

scheme - SICP练习1.5和1.6

linux - 在 Chicken Scheme 中运行系统命令的最佳方式

recursion - 方案 - foo(x y) 没有调用递归调用

Scheme:define 和 let 与 continuation 一起使用时有什么区别

scheme - 截至 2016 年,是否有支持 100% R7RS(小型)且没有偏差的方案实现?

debugging - 将方案对象序列化为字符串

function - 方案基本循环

scheme - Guile 方案和标准方案之间的区别(在 Racket IDE 中)?