如何解决双引号
我试图在我的语法函数中使用双引号。我希望我可以使用 Haskell 约定来生成如下内容:
> mkSentence "This is \"just\" a sentence"
> This is "just" a sentence
但是,当我在语法中尝试此操作时,我遇到了如下示例中使用英语 RGL 的错误:
> cc -table ss "This is \"just\" a sentence"
constant not found: just
given Predef,Predef,CatEng,ResEng,MorphoEng,Prelude,ParadigmsEng
A function type is expected for ss "This is " instead of type {s : Str}
0 msec
> cc -table ss "This is \"just a sentence"
lexical error
0 msec
我可以看到 RGL 中的 src/common/ExtendFunctor.gf
具有 quoted
的实现:
oper
quoted : Str -> Str = \s -> "\"" ++ s ++ "\"" ; ---- Todo bind ; move to Prelude?
我尝试实现类似的东西,但是 "
可能用于我的语法的不同部分,所以理想情况下双引号可以在没有特殊绑定的情况下转义。我正在考虑默认为 ”
以避免 "
的问题,但也许有一种方法可以在“无处不在”(如 these docs 中)转义双引号?
任何提示或对其他文档的参考将不胜感激!
解决方法
据我所知,没有处理报价的 API 函数。你可以自己做这样的事情:
oper
qmark : Str = "\"" ;
quote : Str -> Str = \s -> qmark + s + qmark ;
然后这样称呼它:
> cc -one ss ("This is" ++ quote "just" ++ "a sentence")
This is "just" a sentence
只要您只处理 not runtime tokens 的字符串,它就可以正常工作。
不得不这样写当然有点笨拙,但你总是可以用你喜欢的语法写一个 sed oneliner。这仅适用于一个“引用”的部分,根据您的需要进行调整。
$ sed -E 's/(.*) \\"(.*)\\" (.*)/("\1" ++ quote "\2" ++ "\3")/'
this is \"just\" a sentence
("this is" ++ quote "just" ++ "a sentence")
this is \"just\" a sentence with \"two\" quoted words
("this is \"just\" a sentence with" ++ quote "two" ++ "quoted words")
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。