jgm / doctemplates

Pandoc-compatible templating system
BSD 3-Clause "New" or "Revised" License
52 stars 9 forks source link

Misleading error message for unmatched template conditionals #3

Open tstenner opened 5 years ago

tstenner commented 5 years ago

In pandoc 2.5, a template with an unmatched template conditional causes pandoc to fail with an error:

$ cat test.md 
Foo.
$endif$

$ echo 'Test' | pandoc --template=test.md
[WARNING] This document format requires a nonempty [...]
"template" (line 2, column 7):
unexpected "$"

Any non-keyword (e.g. $endi$) is replaced by an empty string and a document is created. While the error message indicates the correct position, it should also include the keyword, i.e. "template" (line 2, column 7): unexpected "$else$"

jgm commented 5 years ago

jgm/doctemplates is the library used for parsing templates, so any improvements would need to go there.

tstenner commented 5 years ago

Right, I see. Could you move this issue to jgm/doctemplates or should I open a new issue?

jgm commented 5 years ago

I can move it...

jgm commented 5 years ago

Note: no document is created. Try -o test.out; you'll see there is no test.out.

So you're getting an error message about invalid syntax in the template, which points to the line and column where the error is. All this is just right. The only thing that could be improved, as far as I can see, is that it could say unexpected "$endif$" instead of unexpected "$". Am I missing something?

tstenner commented 5 years ago

The only thing that could be improved, as far as I can see, is that it could say unexpected "$endif$" instead of unexpected "$"

That's correct. It's great the correct location is indicated, but if the error message indicated that a $ would be allowed, just not a lone $endif$ it'd be easier to correct the mistake in the template.

jgm commented 5 years ago

Tristan Stenner notifications@github.com writes:

The only thing that could be improved, as far as I can see, is that it could say unexpected "$endif$" instead of unexpected "$"

That's correct. It's great the correct location is indicated, but if the error message indicated that a $ would be allowed, just not a lone $endif$ it'd be easier to correct the mistake in the template.

Well, actually a lone $ would not be allowed. Literal $ must be escaped as $$.

tstenner commented 5 years ago

Well, actually a lone $ would not be allowed.

You're right, but a single $ also has a good error message:


$endif$
      ^    -> error: unexpected "$"
      ^       maybe "error: $endif$ without $if$"?

$endi$        Ok, evaluates to empty string

Test $foo without closing dollar sign
         ^    -> error: unexpected " ", expecting "." or "$"
jgm commented 5 years ago

Yes, I agree that the messages could be somewhat better. This is a low priority for me, though, compared to other things. I'll move this to doctemplates and perhaps someone will take it up.