Skip to content
Open
Show file tree
Hide file tree
Changes from 48 commits
Commits
Show all changes
50 commits
Select commit Hold shift + click to select a range
f523882
hackathon start
pietroalbini Oct 25, 2024
c01f1ab
add yet another token tree implementation
pietroalbini Oct 25, 2024
d3783d5
Meta variable type is resolved actually :sweat_smile:
scrabsha Oct 25, 2024
3fb5ac8
Oupsy doupsy
scrabsha Oct 25, 2024
c5dda81
add RepetitionId
pietroalbini Oct 25, 2024
68dd30b
silence missing docs lint
pietroalbini Oct 25, 2024
ae9e71f
Temporarily make the id inner field public
scrabsha Oct 25, 2024
c4bd7c2
The metavariable name is actually an identifier
scrabsha Oct 25, 2024
85fd5e7
Use the metavariable type in the tokenstream definition
scrabsha Oct 25, 2024
b680417
The repetition can actually be an ident, a punctuation or a literal
scrabsha Oct 25, 2024
fa7234c
Add the Repetition::to_token_tree method
scrabsha Oct 25, 2024
6cf4c7b
Add the proc-macro2::TokenStream -> expandable_impl::TokenStream impl
scrabsha Oct 25, 2024
2281e45
Fix the error enum
scrabsha Oct 25, 2024
ec31e32
Add transcriber parsing as well :sparkles:
scrabsha Oct 25, 2024
ad62900
Add tests for the TokenTree parsing, fix a (stupid) error
scrabsha Oct 25, 2024
b700112
Fix id ordering, add more tests
scrabsha Oct 25, 2024
29285be
Make sure expect test output is the same regardless of how the test s…
scrabsha Oct 25, 2024
bce4b17
add a better debug impl for repetition id
pietroalbini Oct 26, 2024
3c7a254
gather repetition groups
pietroalbini Oct 26, 2024
8c8e782
Add tokenstream -> generic tokenstream convertion function
scrabsha Oct 26, 2024
d8ee159
suppress warnings
pietroalbini Oct 26, 2024
3ad881d
Add tests
scrabsha Oct 26, 2024
98c0d3b
Remove warnings
scrabsha Oct 26, 2024
5966337
create our own Span type that renders the source text in the debug impl
pietroalbini Oct 26, 2024
6311e55
update tests
pietroalbini Oct 26, 2024
ca579e2
switch the default span in the error to our own
pietroalbini Oct 26, 2024
9e9f4ff
remove deref impl for our span
pietroalbini Oct 26, 2024
1d3c179
calculate proper span for metavariables
pietroalbini Oct 26, 2024
eb073af
slightly nicer debug implementation for span
pietroalbini Oct 26, 2024
6e87afd
better rendering for repetition group tests
pietroalbini Oct 26, 2024
732160c
fix span for repetitions
pietroalbini Oct 26, 2024
e90c025
Fancy debug impl :blush:
scrabsha Oct 26, 2024
f9a8553
document the repetition groups code
pietroalbini Oct 26, 2024
08cb4a2
Ditch the old architecture
scrabsha Oct 26, 2024
33ef9cc
pin a nightly and rewrap comments
pietroalbini Oct 26, 2024
293d9bb
Use our own span in the error
pietroalbini Oct 26, 2024
281b11f
add failing tests for the upcoming thing
pietroalbini Oct 26, 2024
45e0b4b
handle more edge cases with ghost metavars
pietroalbini Oct 26, 2024
6a2a692
Glue the new code together :sparkles:
scrabsha Oct 26, 2024
b663295
Actually `rust-grammar-dpdfa` and `grammar-gen` can be removed too!
scrabsha Oct 27, 2024
bd8d4ab
Move the expansion checking code to the right module, implement the m…
scrabsha Oct 27, 2024
7126b28
refactor to use an Analysis struct
pietroalbini Oct 27, 2024
02f1564
document ghost metavariables
pietroalbini Oct 27, 2024
411099f
remove compilation error
pietroalbini Oct 27, 2024
63df827
Fix warnings and lints
scrabsha Oct 27, 2024
c150537
better diagnostics
pietroalbini Oct 27, 2024
74debd9
fix conflicts
scrabsha Oct 27, 2024
5539b37
Missing docs
scrabsha Oct 27, 2024
f6bcabc
better rendering for ghost groups in tests
pietroalbini Nov 3, 2024
83e82dd
use correct spacing when converting our tokentree into generic
pietroalbini Nov 3, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
members = [
"",
"expandable-impl",
"grammar-gen",
"rust-grammar-dpdfa"
]

[workspace.package]
Expand All @@ -16,8 +14,6 @@ repository = "https://github.com/scrabsha/expandable"
[workspace.dependencies]
# MSRV: 1.70 (checked in CI)
expandable-impl = { path = "expandable-impl", version = "0.1.1" }
# MSRV: 1.70 (checked in CI)
rust-grammar-dpdfa = { path = "rust-grammar-dpdfa", version = "0.1.1" }

[package]
name = "expandable"
Expand Down
5 changes: 3 additions & 2 deletions expandable-impl/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ repository.workspace = true

[dependencies]
# MSRV: 1.36 (checked in CI)
smallvec = "1.11"
rust-grammar-dpdfa.workspace = true
proc-macro2 = { version = "1.0.89", features = ["span-locations"] }
syn = { version = "2.0", features = ["full", "parsing"] }

[dev-dependencies]
# MSRV: 1.45 (crate-level documentation)
expect-test = "1.4"
quote = "1.0"
91 changes: 84 additions & 7 deletions expandable-impl/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Architectural invariant: this module contains types that are useful for error
// reporting and nothing else.

use crate::{RepetitionQuantifierKind, Terminal, grammar::TokenDescription};
use crate::token_tree::{Span, TokenTree};

/// An error that is generated when checking an incorrect macro.
///
Expand All @@ -14,8 +14,8 @@ use crate::{RepetitionQuantifierKind, Terminal, grammar::TokenDescription};
/// [`check_macro`]: crate::check_macro
#[derive(Debug)]
#[non_exhaustive]
pub enum Error<Span> {
/// Generated when the macro definition itself doesn't parse correctly.
pub enum Error {
/// Generated when one of the matcher or transcriber does not parse.
///
/// The Rust compiler is likely to emit an error anyway. Below is an
/// example of code that triggers this error:
Expand All @@ -37,6 +37,26 @@ pub enum Error<Span> {
where_: Span,
},

/// Generated when the macro arms don't parse.
///
/// The Rust compiler is likely to emit an error anyway. Below is an
/// example of code that triggers this error:
///
/// ```rust,compile_fail
/// macro_rules! blatant_error {
/// () => =>;
/// // |
/// // error: macro rhs must be delimited.
/// }
/// ```
///
/// This prevents us from doing any analyses.
#[non_exhaustive]
SynError {
/// The exact error that `syn` returned to us.
error: syn::Error,
},

/// An EOF was reached when it was not expected.
#[non_exhaustive]
UnexpectedEnd {
Expand All @@ -54,11 +74,11 @@ pub enum Error<Span> {
/// Where the error happens.
span: Span,
/// What tokens are expected here.
expected: Vec<TokenDescription>,
expected: Vec<TokenTree>,
/// A possible expansion of the macro that exhibits a parsing error.
///
/// The expansion may contain fragments.
counter_example: Vec<(TokenDescription, Span)>,
counter_example: Vec<TokenTree>,
},

/// A macro expansion refers to a metavariable that is not defined in the
Expand All @@ -74,6 +94,34 @@ pub enum Error<Span> {
where_: Span,
},

/// A metavariable is defined at a lower depth than it is used at. At any
/// given repetition depth, it is only possible to use metavariables
/// defined at the same or higher depth.
#[non_exhaustive]
MetavariableDefinedAtLowerDepth {
/// The name of the metavariable that was used.
name: String,
/// Where it was defined.
definition_span: Span,
/// Where it was used.
usage_span: Span,
/// The depth at which the metavariable was defined.
definition_depth: usize,
/// The depth at which the metavariable was used.
usage_depth: usize,
},

// TODO: explain
#[non_exhaustive]
NoRepeatingMetavariables {
/// Where the repetition is defined.
span: Span,
/// The depth of the repetition.
depth: usize,
static_metavariables: Vec<StaticMetavariable>,
},

/*
/// A variable is being repeated with a sequence of operator that does not
/// match the one used when the variable was declared.
///
Expand Down Expand Up @@ -109,10 +157,39 @@ pub enum Error<Span> {
/// The nesting encountered when the metavariable was used.
got_nesting: Vec<RepetitionQuantifierKind>,
},
*/
/// When an invalid repetition separator is being used.
///
/// This corresponds to the following situation:
///
/// ```rust,compile_fail
/// $( /* tokens */ )()*
/// ```
///
/// The only valid repetition separators are punctuations, idents and
/// literals.
InvalidSeparator {
/// The tree that is being wrongly repeated.
tree: TokenTree,
},
}

impl From<syn::Error> for Error {
fn from(error: syn::Error) -> Error {
Error::SynError { error }
}
}

#[derive(Debug)]
#[non_exhaustive]
pub struct StaticMetavariable {
pub name: String,
pub span: Span,
pub repeats_at_depth: usize,
}

/// Various nodes that can be expected in a `macro_rules!` invocation.
#[derive(Clone, Debug, PartialEq)]
#[derive(Clone, Debug)]
#[non_exhaustive]
pub enum MacroRuleNode {
/// A matcher (everything that comes _before_ the `=>` of a macro rule.
Expand All @@ -132,5 +209,5 @@ pub enum MacroRuleNode {
/// A repetition separator (the `,` in `$( $expr ),*`).
RepetitionSeparator,
/// Any terminal.
Terminal(Terminal),
Terminal(TokenTree),
}
18 changes: 18 additions & 0 deletions expandable-impl/src/expand/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
mod repetition_groups;

use crate::{Error, expand::repetition_groups::RepetitionGroups, token_tree::TokenTree};

#[expect(dead_code)]
pub(crate) fn check_macro_arm(
matcher: Vec<TokenTree>,
transcriber: Vec<TokenTree>,
) -> Result<(), Error> {
let _relevant_expansions = expand(matcher, transcriber);

Ok(())
}

fn expand(matcher: Vec<TokenTree>, transcriber: Vec<TokenTree>) -> Vec<Vec<TokenTree>> {
let _ = RepetitionGroups::new(&matcher, &transcriber);
todo!();
}
Loading
Loading