Skip to content
/ parser-c Public
forked from tcr/parser-c

Haskell's language-c ported to Rust.

Notifications You must be signed in to change notification settings

esovm/parser-c

This branch is up to date with tcr/parser-c:master.

Folders and files

NameName
Last commit message
Last commit date

Latest commit

1c51c38 · Apr 4, 2018
Dec 3, 2017
Nov 24, 2017
Dec 3, 2017
Apr 4, 2018
Dec 3, 2017
Apr 4, 2018
Dec 3, 2017
Jul 11, 2017
Nov 21, 2017
Apr 4, 2018
Apr 4, 2018
Nov 21, 2017
Jul 11, 2017
Nov 21, 2017

Repository files navigation

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:

git clone https://github.com/tcr/parser-c --init --recursive

Hacking on the lexer and parser requires to build and run the Haskell dependencies using:

./regen.sh

The test suite is being ported at the moment. It is in a separate crate, because it requires a build script with prerequisites, so to run it, use this script:

./test.sh

License

MIT

About

Haskell's language-c ported to Rust.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Rust 49.5%
  • Haskell 34.7%
  • C 10.1%
  • Yacc 4.0%
  • Logos 1.1%
  • Shell 0.4%
  • Other 0.2%