我正在尝试在我的语法函数中加入双引号。我希望我可以使用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中一样)?
如有任何建议或参考其他文档,我们将不胜感激!
发布于 2021-01-13 15:06:00
据我所知,目前还没有处理报价的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的内联代码。这只适用于一个“引用”的部分,如你所愿调整更多。
$ 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")https://stackoverflow.com/questions/65666080
复制相似问题