knrafto / language-bash

Parse and pretty-print Bash shell scripts
BSD 3-Clause "New" or "Revised" License
35 stars 9 forks source link

Pretty printing of here documents without trailing newline #23

Open mmhat opened 4 years ago

mmhat commented 4 years ago

Consider the following here document:

Heredoc Here "EOF" False (stringToWord "here document")

If pretty printed it looks like that:

<<EOF
here documentEOF

This is clearly not the intended result. Maybe we should add a trailing newline if it is not present in the Word being rendered.

mmhat commented 4 years ago

I could implement that if we agree on a solution.

knrafto commented 4 years ago

Hmm right now parse(prettyPrint(ast)) == ast for all possible trees (or at least that's the goal) but adding a newline would break that. Is it ever possible for a heredoc to not end in a newline in bash? Maybe we should comment that on the Heredoc constructor instead. What do you think?

mmhat commented 4 years ago

I'm not yet sure about that. I stumbled upon that issue while writing a package for shell script generation in the spirit of shell-monad. I do think it would be nice to force the EOF marker on a separate line because otherwise bash will extend the end of file which could lead to some really unintended results. Indeed it is IMHO not possible in bash to produce a heredoc without trailing newline. I'm not sure why a newline list terminator would break the assertion you mentioned above; can you elaborate a bit more on that?

mmhat commented 4 years ago

I implemented a solution here. Didn't test it though.

knrafto commented 4 years ago

You're right, it won't break that assertion. The solution looks good to me, feel free to turn it into a pull request.

mmhat commented 4 years ago

Ok, I'll do that. I'll wait until #24 gets merged.