diff --git a/source/CHANGELOG.md b/source/CHANGELOG.md index 72936ee3..203fd7a2 100644 --- a/source/CHANGELOG.md +++ b/source/CHANGELOG.md @@ -2,8 +2,8 @@ Unreleased -* C/C++/Java: escape newline etc. when printing `String` and `Char` 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)] +* Java/ANTLR: unescape `String` and `Char` literals in parser [[#451](https://github.com/BNFC/bnfc/issues/451)] * 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)] diff --git a/source/src/BNFC/Backend/Java/CFtoAntlr4Parser.hs b/source/src/BNFC/Backend/Java/CFtoAntlr4Parser.hs index 12c3a677..59b1ddb5 100644 --- a/source/src/BNFC/Backend/Java/CFtoAntlr4Parser.hs +++ b/source/src/BNFC/Backend/Java/CFtoAntlr4Parser.hs @@ -138,19 +138,16 @@ generateAction packageAbsyn nt f ms rev p_1 = resultvalue $ ms!!0 p_2 = resultvalue $ ms!!1 add = if rev then "addLast" else "addFirst" - gettext = "getText()" - removeQuotes x = "substring(1, "++ x +.+ gettext +.+ "length()-1)" - parseint x = "Integer.parseInt("++x++")" - parsedouble x = "Double.parseDouble("++x++")" - charat = "charAt(1)" + unescape x = concat [ "org.antlr.v4.misc.CharSupport.getStringFromGrammarStringLiteral(", x, ")" ] resultvalue (n,c) = case c of - TokenCat "Ident" -> n'+.+gettext - TokenCat "Integer" -> parseint $ n'+.+gettext - TokenCat "Char" -> n'+.+gettext+.+charat - TokenCat "Double" -> parsedouble $ n'+.+gettext - TokenCat "String" -> n'+.+gettext+.+removeQuotes n' - _ -> (+.+) n' (if isTokenCat c then gettext else "result") - where n' = '$':n + TokenCat "Double" -> concat [ "Double.parseDouble(", txt, ")" ] + TokenCat "Integer" -> concat [ "Integer.parseInt(" , txt, ")" ] + TokenCat "Char" -> unescape txt +.+ "charAt(0)" + TokenCat "String" -> unescape txt + TokenCat "Ident" -> txt + c | isTokenCat c -> txt + | otherwise -> concat [ "$", n, ".result" ] + where txt = '$':n +.+ "getText()" -- | Generate patterns and a set of metavariables indicating -- where in the pattern the non-terminal