cpp_rs 0.1.0

[Not currently ready] A C Preprocessor library and associated binary.
Documentation
/// This is an implementation of a very simple (read very-bad) C pre-processor
/// it doesn't do true tokenization and it doesn't support many features.
///
/// It does support the following:
///    * Logical line concatenation
///    * Comments (Line and block)
///    * `define` for constant strings
///    * `undef`
///    * `ifdef` and `ifndef`
///
/// It does not support the following:
///    * Macro functions
///    * Symbol concatenation
///    * Generalized `if`
pub mod lexer;

use lexer::OwningTokenizer;
use regex::Regex;
use std::path::PathBuf;

// Reference:
//     * https://www.lysator.liu.se/c/ANSI-C-grammar-l.html
//     * https://github.com/bagder/fcpp
//     * https://github.com/danmar/simplecpp

pub fn splice_lines<S: AsRef<str>>(input: S) -> String {
    let line_regex = Regex::new("\r\n|\n\r|\r").unwrap();

    line_regex.replace_all(input.as_ref(), "\n").into()
}

#[cfg(test)]
mod splice_tests {
    use crate::splice_lines;
    #[test]
    fn nothing_to_do() {
        assert_eq!(splice_lines("a\nb"), "a\nb")
    }

    #[test]
    fn windows_line_ending() {
        assert_eq!(splice_lines("a\r\nb"), "a\nb")
    }

    #[test]
    fn reverse_windows_line_ending() {
        assert_eq!(splice_lines("a\n\rb"), "a\nb")
    }

    #[test]
    fn longest_sequence_first() {
        assert_eq!(splice_lines("a\r\n\rb"), "a\n\nb")
    }

    #[test]
    fn non_greedy() {
        assert_eq!(splice_lines("a\n\r\nb"), "a\n\nb")
    }
}

pub fn preprocess(_input: String) -> String {
    // let stream = TokenStream {
    //     path: "/".into(),
    //     tokenizer: OwningTokenizer::new(input),
    // };
    // let mut file_stack = vec![Tokenizer::new(&input)];
    // let mut current_token_stream = file_stack.last_mut().unwrap();

    // //let ouput_token_stream = Vec::new();
    // let current_row = Vec::new();

    // let mut in_directive = false;

    // while file_stack.len() > 0 {

    //     // Grab a single line of nput off the currently active token stream
    //     loop {
    //         match current_token_stream.get_next_token() {
    //             Ok(tok) => {
    //                 match tok.token_type {
    //                     TokenType::Punctuator if tok.text == "#" => {
    //                         in_directive = true;
    //                     }
    //                     TokenType::EOL => {
    //                         current_row.push(tok);
    //                         break;
    //                     }
    //                     TokenType::EOF => break,
    //                     _ => current_row.push(tok),
    //                 }
    //             }
    //             Err(err) => {
    //                 unimplemented!();
    //             }
    //         }
    //     }

    //     //Process the current line
    // }

    "".into()
}

struct TokenStream {
    path: PathBuf,
    tokenizer: OwningTokenizer,
}