Skip to content

Commit

Permalink
Fix #451: Java/ANTLR: unescape Char and String literals in parser
Browse files Browse the repository at this point in the history
  • Loading branch information
andreasabel committed Jul 26, 2023
1 parent 9cb7658 commit d5ba1ac
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 14 deletions.
4 changes: 2 additions & 2 deletions source/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
21 changes: 9 additions & 12 deletions source/src/BNFC/Backend/Java/CFtoAntlr4Parser.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit d5ba1ac

Please sign in to comment.