4 releases (2 breaking)

Uses old Rust 2015

0.3.0 Aug 2, 2017
0.2.1 Jul 5, 2017
0.2.0 Jul 5, 2017
0.0.1 Jun 6, 2017

#2366 in Rust patterns

23 downloads per month

MIT license

4MB
134K SLoC

Haskell 61K SLoC // 0.1% comments Rust 60K SLoC // 0.0% comments Happy 7.5K SLoC Alex 3.5K SLoC C 788 SLoC // 0.1% comments Cabal 496 SLoC // 0.0% comments M4 258 SLoC // 0.2% comments Shell 245 SLoC // 0.2% comments RPM Specfile 96 SLoC Ruby 33 SLoC SWIG 2 SLoC

Contains (obscure autoconf code, 1KB) deps/happy-rust/doc/configure.ac, (obscure autoconf code, 1KB) deps/alex-rust/doc/configure.ac

parser-c

Gitter https://img.shields.io/crates/v/parser-c.svg

Rust module for parsing C code. Port of Haskell's language-c, semi-automatically translated using Corollary.

This port is a work in progress. A lot of work remains to parse anything but very simple C files; while most source code has been translated from Haskell, errors in translation prevent it from matching language-c's functionality yet. Here are the next steps for achieving parity, in order:

  1. Building up an equivalent test bed to language-c's, then automatically cross-check
  2. Fix errors in the ported code to support those test cases
  3. Converting portions of the code into Rust idioms without breaking tests
  4. Figure out a porting story for the alex/happy generated parser output

parser-c requires nightly (for now). See tests/ for some working examples, or try this example:

extern crate parser_c;

use parser_c::parse;

const INPUT: &'static str = r#"

int main() {
    printf("hello world!\n");
    return 0;
}

"#;

fn main() {
    match parse(INPUT, "simple.c") {
        Err(err) => {
            panic!("error: {:?}", err);
        }
        Ok(ast) => {
            println!("success: {:#?}", ast);
        }
    }
}

Result is:

success: Right(
    CTranslationUnit(
        [
            CFDefExt(
                CFunctionDef(
                    [
                        CTypeSpec(
                            CIntType(
                                ..
                            )
                        )
                    ],
                    CDeclarator(
                        Some(
                            Ident(
                                "main",
                                124382170,
                                ..
                            )
                        ),
                        ...

Development

Clone this crate and install its Haskell dependencies:

git clone https://github.com/tcr/parser-c --init --recursive
stack install alex happy

License

MIT

Dependencies

~5.5MB
~127K SLoC