Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Java serialization (pretty print) of strings does not escape special chars #449

Closed
marco-comini opened this issue Jul 18, 2023 · 3 comments
Assignees
Labels
C++ C Java printer Concerning the generated printer
Milestone

Comments

@marco-comini
Copy link

While predefined basic type String in Haskell backend serialization escapes special chars in strings (like "line1\nline2") in Java it does not (and output is "line1
line2").

@andreasabel andreasabel added C++ C Java printer Concerning the generated printer labels Jul 25, 2023
@andreasabel andreasabel added this to the 2.9.5 milestone Jul 25, 2023
@andreasabel
Copy link
Member

andreasabel commented Jul 25, 2023

This issue exists in all non-FP backends.

The Haskell backend produces the following printer for strings:

printString :: String -> Doc
printString s = doc (showChar '"' . concatS (map (mkEsc '"') s) . showChar '"')

mkEsc :: Char -> Char -> ShowS
mkEsc q = \case
  s | s == q -> showChar '\\' . showChar s
  '\\' -> showString "\\\\"
  '\n' -> showString "\\n"
  '\t' -> showString "\\t"
  s -> showChar s

The Ocaml backend produces:

let prtString (_:int) (s:string) : doc = render ("\"" ^ String.escaped s ^ "\"")

From the docs: https://v2.ocaml.org/api/String.html

escaped s is s with special characters represented by escape sequences, following the lexical conventions of OCaml.

All characters outside the US-ASCII printable range [0x20;0x7E] are escaped, as well as backslash (0x2F) and double-quote (0x22).

Java produces only this:

  private static void printQuoted(String s) { render("\"" + s + "\""); }

CPP this:

void PrintAbsyn::visitString(String s)
{
  bufAppend('\"');
  bufAppend(s);
  bufAppend('\"');
  bufAppend(' ');
}

@andreasabel andreasabel self-assigned this Jul 25, 2023
@andreasabel
Copy link
Member

andreasabel commented Jul 26, 2023

While working on this issue, I found that two backends do not lex escape characters properly:

andreasabel added a commit that referenced this issue Jul 26, 2023
This is a precaution, should not happen unless there are other bugs
(e.g. the one introduced in d5ba1ac).
andreasabel added a commit that referenced this issue Jul 26, 2023
This is a precaution, should not happen unless there are other bugs
(e.g. the one introduced in d5ba1ac).
@marco-comini
Copy link
Author

thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C++ C Java printer Concerning the generated printer
Projects
None yet
Development

No branches or pull requests

2 participants