Skip to main content

Crate justlex

Crate justlex 

Source
Expand description

§justlex.rs

Configurable lexer with no dependencies.

The entire implementation lives in a single .rs file. This means that you can copy-paste it in your project and modify it as you like, keeping your dependency tree clean. You can easily add custom Punctuation or Keywords by modifying the file directly.

Check out the examples in the examples directory.

§Quickstart

use justlex::*;

// Some input string
let example_input = String::from(
    "#include <stdio.h>
    if (a == 17*2 + 35) { // single line comment
        /* multi
         * line
         * comment
         */
        return b;
    }\n");

// Setup the lexer
let mut lexer = JustLex::default();
lexer.set_input(example_input);

// Iterate over the tokens
for token in lexer {
    let token = token;
    println!("Received token {:?}", token);
    if token == Token::None {
      break;
    }
}

§Credits

  • Author: Giovanni Santini
  • Mail: giovanni.santini@proton.me
  • Github: @San7o
  • License: MIT

Structs§

JustLex
You can set the keywords, punctuation and input to the lexer via the functions new and set_input. Then, you can keep calling next_token or next to receive all the tokens.

Enums§

Keyword
Punct
Token

Functions§

alphanumeric_len
Returns the number of alphanumeric characters that occur from the start of input
is_alpha
Returns true if c is a letter of the alphabet (ASCII)
is_alphanumeric
Returns true if c is a letter or a digit
is_numeric
Returns true if c is a digit (0 to 9)
is_space
Returns true if c is considered a white-space character
numeric_len
Returns the number of numeric characters that occur from the start of input