Skip to content

Commit

Permalink
Add logos(crate = "...") attribute (#268)
Browse files Browse the repository at this point in the history
* Add logos(crate = "...") attribute

* Use logos-derive per path
  • Loading branch information
jannik4 committed Feb 26, 2023
1 parent 1ecd6a4 commit 56afbaa
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 6 deletions.
13 changes: 9 additions & 4 deletions logos-codegen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ use util::MaybeVoid;
use proc_macro2::Span;
use proc_macro2::TokenStream;
use quote::quote;
use syn::parse_quote;
use syn::spanned::Spanned;
use syn::{Fields, ItemEnum};

Expand Down Expand Up @@ -211,6 +212,10 @@ pub fn generate(input: TokenStream) -> TokenStream {
Mode::Utf8 => quote!(str),
Mode::Binary => quote!([u8]),
};
let logos_path = parser
.logos_path
.take()
.unwrap_or_else(|| parse_quote!(::logos));

let error_def = match error {
Some(error) => Some(quote!(const ERROR: Self = #name::#error;)),
Expand All @@ -225,14 +230,14 @@ pub fn generate(input: TokenStream) -> TokenStream {

let impl_logos = |body| {
quote! {
impl<'s> ::logos::Logos<'s> for #this {
impl<'s> #logos_path::Logos<'s> for #this {
type Extras = #extras;

type Source = #source;

#error_def

fn lex(lex: &mut ::logos::Lexer<'s, Self>) {
fn lex(lex: &mut #logos_path::Lexer<'s, Self>) {
#body
}
}
Expand Down Expand Up @@ -297,9 +302,9 @@ pub fn generate(input: TokenStream) -> TokenStream {

let body = generator.generate();
let tokens = impl_logos(quote! {
use ::logos::internal::{LexerInternal, CallbackResult};
use #logos_path::internal::{LexerInternal, CallbackResult};

type Lexer<'s> = ::logos::Lexer<'s, #this>;
type Lexer<'s> = #logos_path::Lexer<'s, #this>;

fn _end<'s>(lex: &mut Lexer<'s>) {
lex.end()
Expand Down
18 changes: 17 additions & 1 deletion logos-codegen/src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use beef::lean::Cow;
use proc_macro2::{Span, TokenStream, TokenTree};
use quote::quote;
use syn::spanned::Spanned;
use syn::{Attribute, GenericParam, Lit, Type};
use syn::{Attribute, GenericParam, Lit, Path, Type};

use crate::error::Errors;
use crate::leaf::{Callback, InlineCallback};
Expand All @@ -27,6 +27,7 @@ pub struct Parser {
pub mode: Mode,
pub extras: MaybeVoid,
pub subpatterns: Subpatterns,
pub logos_path: Option<Path>,
types: TypeParams,
}

Expand Down Expand Up @@ -129,6 +130,21 @@ impl Parser {
("subpattern", _) => {
self.err(r#"Expected: subpattern name = r"regex""#, name.span());
}
("crate", NestedValue::Assign(value)) => {
let value = syn::parse2::<Lit>(value).ok().and_then(|lit| match lit {
Lit::Str(string) => syn::parse_str(&string.value()).ok(),
_ => None,
});
match value {
Some(logos_path) => self.logos_path = Some(logos_path),
None => {
self.err(r#"Expected: crate = "some::path::to::logos""#, name.span());
}
}
}
("crate", _) => {
self.err(r#"Expected: crate = "some::path::to::logos""#, name.span());
}
(unknown, _) => {
self.err(
format!("Unknown nested attribute: {}", unknown),
Expand Down
2 changes: 1 addition & 1 deletion logos/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ readme = "../README.md"
edition = "2018"

[dependencies]
logos-derive = { version = "0.12.1", optional = true }
logos-derive = { path = "../logos-derive", version = "0.12.1", optional = true }

[features]
default = ["export_derive", "std"]
Expand Down
36 changes: 36 additions & 0 deletions tests/tests/crate_.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
use logos_derive::Logos;
use tests::assert_lex;

mod some {
pub mod path {
pub use logos as _logos;
}
}

#[derive(Logos, Debug, Clone, Copy, PartialEq)]
#[logos(crate = "some::path::_logos")]
enum Token {
#[regex(r"[ \t\n\f]+", logos::skip)]
#[error]
Error,

#[regex("-?[0-9]+")]
LiteralInteger,

#[token("'")]
SingleQuote,
}

#[test]
fn simple() {
assert_lex(
"' -1'2 '",
&[
(Token::SingleQuote, "'", 0..1),
(Token::LiteralInteger, "-1", 2..4),
(Token::SingleQuote, "'", 4..5),
(Token::LiteralInteger, "2", 5..6),
(Token::SingleQuote, "'", 8..9),
],
);
}

0 comments on commit 56afbaa

Please sign in to comment.