Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Lexing

This section is a work-in-progress experiment about making the book executable.

#![allow(unused)]
fn main() {
#[derive(Clone, Debug, PartialEq, Eq, Logos)]
#[logos(skip r"[ \t\n\r]+")]
pub enum Token {
    #[token("fn")]
    Fn,
    #[token("const")]
    Const,
    #[token("async")]
    Async,
    #[token("safe")]
    Safe,
    #[token("unsafe")]
    Unsafe,
    #[token("extern")]
    Extern,
    #[token("let")]
    Let,
    #[token("pub")]
    Pub,
    #[token("crate")]
    Crate,
    #[token("super")]
    Super,
    #[token("in")]
    In,
    #[token("if")]
    If,
    #[token("else")]
    Else,
    #[token("mut")]
    Mut,
    #[token("self")]
    Self_,
    #[token("Self")]
    TraitSelf,
    #[token("bool")]
    Bool,
    #[token("str")]
    Str,
    #[token("true")]
    True,
    #[token("false")]
    False,
    #[token("->")]
    Arrow,
    #[token("=")]
    Eq,
    #[token("+")]
    Plus,
    #[token("&")]
    Amp,
    #[token(":")]
    Colon,
    #[token(",")]
    Comma,
    #[token("(")]
    LParen,
    #[token(")")]
    RParen,
    #[token("{")]
    LBrace,
    #[token("}")]
    RBrace,
    #[token(";")]
    Semicolon,
    #[token("...")]
    Ellipsis,
    #[token("_")]
    Underscore,
    #[regex(r"[0-9]+", |lex| lex.slice().parse::<u128>().unwrap())]
    IntegerLiteral(u128),
    #[regex(r"[A-Za-z_][A-Za-z0-9_]*", |lex| lex.slice().to_owned(), priority = 1)]
    Identifier(String),
    #[regex(r#""[^"]*""#, string_literal)]
    StringLiteral(String),
    #[regex(r"'[A-Za-z_][A-Za-z0-9_]*", |lex| lex.slice().to_owned())]
    Lifetime(String),
    #[token("__unsupported__")]
    Unsupported,
}

fn string_literal(lex: &mut logos::Lexer<'_, Token>) -> String {
    let slice = lex.slice();
    slice[1..slice.len() - 1].to_owned()
}

}
%tokentype Token;
%start Program;

`fn` Fn;
`const` Const;
`async` Async;
`safe` Safe;
`unsafe` Unsafe;
`extern` Extern;
`if` If;
`else` Else;
`let` Let;
`pub` Pub;
`crate` Crate;
`super` Super;
`in` In;
`mut` Mut;
`self` Self_;
`Self` TraitSelf;
`bool` Bool;
`str` Str;
`true` True;
`false` False;
`->` Arrow;
`=` Eq;
`+` Plus;
`&` Amp;
`:` Colon;
`,` Comma;
`(` LParen;
`)` RParen;
`{` LBrace;
`}` RBrace;
`;` Semicolon;
`...` Ellipsis;
`_` Underscore;
INTEGER_LITERAL IntegerLiteral(u128);
IDENTIFIER Identifier(String);
STRING_LITERAL StringLiteral(String);
LIFETIME Lifetime(String);
UNSUPPORTED Unsupported;

%precedence `self`;
%precedence `:`;

%allow unit_production_eliminated(Identifier);
%allow unit_production_eliminated(GenericParams);
%allow unit_production_eliminated(WhereClauses);
%allow unit_production_eliminated(OuterAttribute);
%allow unit_production_eliminated(Lifetime);