clex

Crates.ioclex
lib.rsclex
version0.1.0
sourcesrc
created_at2022-07-03 19:08:58.900631
updated_at2022-07-03 19:08:58.900631
descriptionFast C-lang lexer (library)
homepagehttps://github.com/katyo/clex
repositoryhttps://github.com/katyo/clex
max_upload_size
id618512
size44,450
Kayo Phoenix (katyo)

documentation

README

C source lexer in Rust

github crate docs MIT CI

This is a fast and robust C source lexer in Rust. For example it can be used to extract some metadata from sources like comments or strings.

Library usage

use clex::{Lexer, Token};

let src = r#"
static const char *s = "world";

int main() {
  // Hello world
  printf("Hello %s\n", s);

  return 0;
}
"#;

for lexeme in Lexer::from(src) {
  match lexeme.token {
    Token::Comment => {
      println!("comment: {:?}", lexeme.comment().unwrap());
    }
    Token::String => {
      println!("string: {:?}", lexeme.string().unwrap());
    }
    _ => {}
  }
}

This example prints the following:

string: "world"
comment: "Hello world"
string: "Hello %s\n"

Command-line usage

Currently command-line tool is used to test this library. You can use it to analyze variuos C-sources and extract data.

Commit count: 4

cargo fmt