Skip to content

Commit

Permalink
Re #449: C/C++/Java printer: escape newlines etc. in Char
Browse files Browse the repository at this point in the history
  • Loading branch information
andreasabel committed Jul 26, 2023
1 parent 18fdec1 commit 9cb7658
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 10 deletions.
3 changes: 2 additions & 1 deletion source/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

Unreleased

* C/C++/Java: escape newline etc. when printing `String` literals [[#449](https://github.com/BNFC/bnfc/issues/449)]
* C/C++/Java: escape newline etc. when printing `String` and `Char` literals
[[#449](https://github.com/BNFC/bnfc/issues/449)]
* Haskell/GADT: generated `ComposOp.hs` no longer needs `mtl` library [[#438](https://github.com/BNFC/bnfc/pull/438)]
* Ocaml: fixed a crash in printer with unbalanced `}` [[#439](https://github.com/BNFC/bnfc/issues/439)]
* Ocaml: lex escape sequences in `Char` [[#452](https://github.com/BNFC/bnfc/issues/452)]
Expand Down
7 changes: 4 additions & 3 deletions source/src/BNFC/Backend/C/CFtoCPrinter.hs
Original file line number Diff line number Diff line change
Expand Up @@ -194,9 +194,10 @@ mkCFile cf groups = concat
"}",
"void ppChar(Char c, int i)",
"{",
" char tmp[4];",
" sprintf(tmp, \"'%c'\", c);",
" renderS(tmp);",
" bufAppendC('\\'');",
" bufEscapeC(c);",
" bufAppendC('\\'');",
" bufAppendC(' ');",
"}",
"void ppString(String s, int i)",
"{",
Expand Down
7 changes: 4 additions & 3 deletions source/src/BNFC/Backend/CPP/PrettyPrinter.hs
Original file line number Diff line number Diff line change
Expand Up @@ -317,9 +317,10 @@ mkCFile useStl inPackage cf groups = concat
"",
"void PrintAbsyn::visitChar(Char c)",
"{",
" char tmp[4];",
" sprintf(tmp, \"'%c'\", c);",
" render(tmp);",
" bufAppend('\\'');",
" bufEscape(c);",
" bufAppend('\\'');",
" bufAppend(' ');",
"}",
"",
"void PrintAbsyn::visitString(String s)",
Expand Down
4 changes: 2 additions & 2 deletions source/src/BNFC/Backend/Java/CFtoJavaPrinter15.hs
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,10 @@ cf2JavaPrinter packageBase packageAbsyn cf =
" private static void pp(Integer n, int _i_) { buf_.append(n); buf_.append(\" \"); }",
" private static void pp(Double d, int _i_) { buf_.append(String.format(java.util.Locale.ROOT, \"%.15g \", d)); }",
" private static void pp(String s, int _i_) { buf_.append(s); buf_.append(\" \"); }",
" private static void pp(Character c, int _i_) { buf_.append(\"'\" + c.toString() + \"'\"); buf_.append(\" \"); }",
" private static void pp(Character c, int _i_) { buf_.append(\"'\" + escape(c.toString()) + \"'\"); buf_.append(\" \"); }",
" private static void sh(Integer n) { render(n.toString()); }",
" private static void sh(Double d) { render(String.format(java.util.Locale.ROOT, \"%.15g\", d)); }",
" private static void sh(Character c) { render(\"'\" + c.toString() + \"'\"); }",
" private static void sh(Character c) { render(\"'\" + escape(c.toString()) + \"'\"); }",
" private static void sh(String s) { printQuoted(s); }",
"",
" private static void printQuoted(String s) { render(\"\\\"\" + escape(s) + \"\\\"\"); }",
Expand Down
2 changes: 1 addition & 1 deletion testing/regression-tests/358_MixFixLists/test.cf
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
-- Issue #361, C++ list parsing.
-- Issue #366, render } on new line.
-- Issue #359, Haskell: print Char lists.
-- Issue #449 C/C++/Java: print escape characters in String.
-- Issue #449 C/C++/Java: print escape characters in String and Char.
-- Issue #452 Ocaml: lex escape characters in Char.

Top. S ::= Char
Expand Down

0 comments on commit 9cb7658

Please sign in to comment.