pub fn lex<'a, 'o>(source: &'a str, options: &'o Options) -> Lexer<'a, 'o> ⓘExpand description
Lex source under options, yielding a token stream that tiles the input.
source must be at most u32::MAX bytes (Span stores u32 offsets).
Unlike the tree, the token stream keeps trivia (whitespace, comments) — the layer a formatter or parinfer backend wants:
use lispexp::{lex, Options, TokenKind};
let comments = lex("(a ; note\n b)", &Options::scheme())
.filter(|t| matches!(t.kind, TokenKind::LineComment))
.count();
assert_eq!(comments, 1);Examples found in repository?
examples/lex_tokens.rs (line 15)
12fn main() {
13 let source = "(define x ; the answer\n 42)";
14
15 for token in lex(source, &Options::scheme()) {
16 let text = token.span.text(source);
17 // Show newlines/spaces legibly.
18 let shown = text.replace('\n', "\\n");
19 println!(
20 "{:>2}..{:<2} {:<16} {:?}",
21 token.span.start,
22 token.span.end,
23 format!("{:?}", token.kind),
24 shown,
25 );
26 }
27}