1

I am trying to allow double quotation marks into my grammar's functions. I was hoping that I could use Haskell conventions to generate something like:

> mkSentence "This is \"just\" a sentence"
> This is "just" a sentence

However, when I try this in my grammar, I am faced with errors like in the example below using the English 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

I can see that src/common/ExtendFunctor.gf in the RGL has an implementation of quoted:

oper
  quoted : Str -> Str = \s -> "\"" ++ s ++ "\"" ; ---- TODO bind ; move to Prelude?

I have tried to implement something similar, but " may be used in different parts of my grammar, so ideally the double quotation marks could be escaped without special binds. I am considering defaulting to to avoid the issues with ", but maybe there is a way to escape double quotation marks "everywhere" (like in these docs)?

Any tips or reference to other docs would be very appreciated!

ppski
  • 41
  • 4

1 Answers1

1

As far as I know, there is no API function for handling quotes. You can just do something like this yourself:

oper
  qmark : Str = "\"" ;
  quote : Str -> Str = \s -> qmark + s + qmark ;

And call it like this:

> cc -one ss ("This is" ++ quote "just" ++ "a sentence")
This is "just" a sentence

As long as you're only handling strings that are not runtime tokens, it works fine.

It's of course a bit clumsy to have to write it like that, but you can always write a sed oneliner from your preferred syntax. This works for just one "quoted" part, adjust as you wish for more.

$ 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")
inariksit
  • 1,232
  • 7
  • 13