Crate rsedn

Crate rsedn 

Source
Expand description

§rsedn - Rust EDN parser

rsedn is a parser for the EDN data format written in Rust.

§Example


fn main() {
    use std::collections::LinkedList;
    
    use rsedn::{
        lexer::{source::Source, token::Token},
        parser::{self, form::FormKind},
    };
    // A Source can be created from a &str
    let mut source: Source = "(defn add [a b] (+ a b))".into();
    // Lex the source into Vec<Lexeme>
    let lexemes = source.lex();
    // Parse the lexemes into a LinkedList<Token>
    let tokens = lexemes
        .into_iter()
        .map(|lexeme| Token::parse(&source, lexeme)) // Parse the lexeme into a Token
        .map(|token| token.unwrap()) // Unwrap the Result<Token, ParsingError>
        .collect::<LinkedList<_>>();
    let mut token_stream = tokens.iter(); // Create a TokenStream from the LinkedList
    let form = parser::parse_form(&mut token_stream).unwrap().unwrap(); // Parse the tokens into a Form

    assert!(matches!(form.kind, FormKind::List(_)));
}

§Usage

  1. Take your source code as a &str
  2. Create a Source from the &str using rsedn::Source::from
  3. Lex the Source using Source::lex this produces a Vec<Lexeme>
  4. Parse each Lexeme into a Token using Token::parse
  5. Collect the Tokens into a LinkedList<Token>
  6. Create a TokenStream from the LinkedList<Token> using LinkedList::iter
  7. Consume the TokenStream using [parse_tokens] to produce a Result<Option<Form>, ParsingError>
  8. Use the Source and the Lexeme to get the span of a given Lexeme

Modules§

lexer
This is the lexer module. Source code is read as a Chars iterator and and it is tokenized into Token.
parser
Takes tokens and builds an AST of Forms

Functions§

consume_token_stream
Consumes a TokenStream to produce a Result<Option<Form>, ParsingError> The final step of the parsing process
lex_source
Lexes a Source into a Vec<Lexeme> The second step of the parsing process
parse_lexeme
Parses a Lexeme into a Token using the Source to get the span The third step of the parsing process
source_from_str
Produces a Source from a &str The first step of the parsing process