sailfish icon indicating copy to clipboard operation
sailfish copied to clipboard

Lexer error does not provide line number

Open levkk opened this issue 3 years ago • 3 comments

Hi,

Thank you for making an awesome crate. This is my favorite Rust template library by a mile.

When writing relatively large templates, I occasionally make mistakes in the template syntax and I get a lexer error like this one:

error: Failed to compile template.
       caused by: Rust Syntax Error (lex error)

file: /home/lev/Projects/dashboard/templates/model.html

#[derive(TemplateOnce)]

note: this error originates in the derive macro `TemplateOnce` (in Nightly builds, run with -Z macro-backtrace for more info)

Unfortunately, it doesn't tell me which line number the error is on, so it's pretty difficult to debug. Is it possible to count line numbers when parsing and report the line number accordingly?

Thank you.

levkk avatar Nov 28 '22 19:11 levkk

@levkk Could you provide contents of the template file? (i.e., model.html)

Kogia-sima avatar Mar 05 '23 10:03 Kogia-sima

main.rs

use sailfish::TemplateOnce;

#[derive(TemplateOnce)]
#[template(path = "example.html")]
pub struct Example {
    title: String,
}

templates/example.html

<html>
	<head><%= title %></head>
</html>

<% if { non_ %>
$ cargo build
   Compiling example v0.1.0 (/home/lev/code/example)
error: Failed to compile template.
       caused by: Rust Syntax Error (lex error)
       
       file: /home/lev/code/example/templates/example.html
 --> src/main.rs:3:10
  |
3 | #[derive(TemplateOnce)]
  |          ^^^^^^^^^^^^
  |
  = note: this error originates in the derive macro `TemplateOnce` (in Nightly builds, run with -Z macro-backtrace for more info)

error: could not compile `example` due to previous error

levkk avatar Mar 06 '23 05:03 levkk

It seems that the extra brace causes proc_macro2::LexError with unpredictable span.

{
    "feed": "{\nif { non_\n\n}",
    "error": "lex error",
    "span": {
        "start": {
            "line": 1,
            "column": 0
        },
        "end": {
            "line": 1,
            "column": 0
        }
    }
}

Maybe we need to implement Rust lexer from scratch in order to solve this error...

Kogia-sima avatar Mar 06 '23 08:03 Kogia-sima