vickenty / lang-c

Lightweight C parser for Rust
Apache License 2.0
202 stars 30 forks source link

Syntax Error using MinGW/GCC #19

Closed robbym closed 4 years ago

robbym commented 4 years ago

With the following program:

use std::path::Path;
use lang_c::driver as c;

fn lint_file<P: AsRef<Path>>(config: &c::Config, file: P) {
    let result = c::parse(config, file);

    match result {
        Ok(parsed) => {
            println!("SUCCESS");
            println!("{:#?}", parsed.source);
        }

        Err(c::Error::PreprocessorError(error)) => {
            println!("PREPROCESSOR");
            println!("{:#?}", error.into_inner().unwrap());
        }

        Err(c::Error::SyntaxError(error)) => {
            println!("SYNTAX");
            println!("{}", error.source);
            println!("{:#?}", error);
        }
    }
}

fn main() {
    let config = c::Config::with_gcc();
    lint_file(&config, "main.c");
}

And a minimal main.c file:

int main()
{
    return 0;
}

Prints this output:

SYNTAX
# 1 "main.c"
# 1 "<built-in>"
# 1 "<command-line>"
# 1 "main.c"
int main()
{
    return 0;
}

SyntaxError {
    source: "# 1 \"main.c\"\r\n# 1 \"<built-in>\"\r\n# 1 \"<command-line>\"\r\n# 1 \"main.c\"\r\nint main()\r\n{\r\n    return 0;\r\n}\r\n",
    line: 5,
    column: 11,
    offset: 78,
    expected: {
        ";",
        "asm",
        "<typedef_name>",
        "{",
        "=",
        "[",
        ",",
        "(",
    },
}

I am using:

> gcc --version
gcc.exe (MinGW.org GCC-8.2.0-3) 8.2.0
Copyright (C) 2018 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS 

On Windows 10.

Am I not using this correctly?

vickenty commented 4 years ago

Thank you for a detailed report.

The problem seems to be that the parser does not support windows-style line endings. The error is pointing at \r character in the source. Should be easy to fix, I think.