1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
//! Very work-in-progress C parser, ported from Haskell's language-c.
//!
//! ```rust,no_run
//! extern crate parser_c;
//! 
//! use parser_c::parse;
//! 
//! const INPUT: &'static str = r#"
//! 
//! int main() {
//!     return 0;
//! }
//! 
//! "#;
//! 
//! fn main() {
//!     match parse(INPUT, "simple.c") {
//!         Err(err) => {
//!             panic!("error: {:?}", err);
//!         }
//!         Ok(ast) => {
//!             println!("success: {:?}", ast);
//!         }
//!     }
//! }
//! ```

// Original file: "C.hs"

#![feature(proc_macro)]
#![feature(slice_patterns, box_syntax, box_patterns, fnbox)]
#![allow(unused_parens)]
#![recursion_limit="500"]

extern crate num;
#[macro_use] extern crate matches;
#[macro_use] extern crate num_derive;
extern crate parser_c_macro;
#[macro_use] extern crate lazy_static;

// pub mod analysis;
#[macro_use] pub mod support;
pub mod data;
pub mod parser;
pub mod syntax;

use support as corollary_support;
use corollary_support::*;
use syntax::preprocess::*;
use syntax::ast::*;
use data::input_stream::readInputStream;
use data::position::initPos;
use parser::parser_monad::ParseError;
use parser::parser::parseC;
use data::input_stream::inputStreamFromString;

// NOTE: These imports are advisory. You probably need to change them to support Rust.
// use Language::C::Data;
// use Language::C::Syntax;
// use Language::C::Pretty;
// use Language::C::Parser;
// use Language::C::System::Preprocess;

fn parseCFile<C: Preprocessor>(cpp: C,
                                   tmp_dir_opt: Option<FilePath>,
                                   args: Vec<String>,
                                   input_file: FilePath)
                                   -> Either<ParseError, CTranslUnit> {

    let handleCppError = |_0| match (_0) {
        Left(exitCode) => __error!(__op_addadd("Preprocessor failed with ".to_string(), show(exitCode))),
        Right(ok) => ok,
    };

    /*do*/
    {
        let input_stream = if !isPreprocessed(input_file.clone().into()) {
            {
                let cpp_args = __assign!((rawCppArgs(args, input_file.clone())), {
                    cppTmpDir: tmp_dir_opt
                });

                handleCppError(runPreprocessor(cpp, cpp_args))
            }
        } else {
            readInputStream(input_file.clone())
        };

        parseC(input_stream, (initPos(input_file)))
    }
}

fn parseCFilePre(file: FilePath) -> Either<ParseError, CTranslUnit> {
    /*do*/
    {
        let input_stream = readInputStream(file.clone());

        parseC(input_stream, (initPos(file)))
    }
}

/// Basic public API. Accepts C source and a filename.
pub fn parse(input: &str, filename: &str) -> Result<CTranslUnit, ParseError> {
    // This doesn't represent possible final functionality of the crate,
    // but makes it usable at this early stage.

    use std::thread;

    let input = input.to_string();
    let filename = filename.to_string();

    // TODO which stack size is necessary? Can we eliminate this?
    thread::Builder::new().stack_size(32 * 1024 * 1024).spawn(move || {
        let input_stream = inputStreamFromString(input);

        let todo = parseC(input_stream, (initPos(FilePath::from(filename))));

        match todo {
            Left(err) => {
                Err(err)
            }
            Right(ast) => {
                Ok(ast)
            }
        }
    }).unwrap().join().unwrap()
}