双引号

问题描述

我试图在我的语法函数中使用双引号。我希望我可以使用 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")