如何检查Racket语法中是否存在关键字?
我想检查语法对象中是否存在关键字。 当关键字后面跟一个表达式时很容易:如何检查Racket语法中是否存在关键字?,racket,Racket,我想检查语法对象中是否存在关键字。 当关键字后面跟一个表达式时很容易: (syntax-parse #'(hello #:world "sunny") [(_ (~optional (~seq #:world <adjective>:str))) #'(string-append "Hello world! Today's weather is " (~? <adjective> "unknown"))]) 如何将#:w
(syntax-parse #'(hello #:world "sunny")
[(_ (~optional (~seq #:world <adjective>:str)))
#'(string-append "Hello world! Today's weather is "
(~? <adjective> "unknown"))])
如何将
#:world
的存在绑定到语法属性?您可以使用~和
将语法绑定到属性:
(syntax-parse #'(hello #:world)
[(_ (~optional (~and world? #:world)))
#:with maybe-world (if (attribute world?) #'" world" #'"")
#'(string-append "Hello" maybe-world "!")])
从文档: 另见。
(syntax-parse #'(hello #:world)
[(_ (~optional (~and world? #:world)))
#:with maybe-world (if (attribute world?) #'" world" #'"")
#'(string-append "Hello" maybe-world "!")])
One use for ~and-patterns is preserving a whole term (including its lexical
context, source location, etc) while also examining its structure.
Syntax classes are useful for the same purpose, but ~and can be lighter weight.