mirror of
https://github.com/rust-lang/rust-analyzer
synced 2024-11-10 07:04:22 +00:00
move macros to a separate crate
This commit is contained in:
parent
9a043a163c
commit
ce3636798b
7 changed files with 50 additions and 23 deletions
8
Cargo.lock
generated
8
Cargo.lock
generated
|
@ -940,6 +940,7 @@ dependencies = [
|
||||||
"parking_lot 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
"parking_lot 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"ra_arena 0.1.0",
|
"ra_arena 0.1.0",
|
||||||
"ra_db 0.1.0",
|
"ra_db 0.1.0",
|
||||||
|
"ra_macros 0.1.0",
|
||||||
"ra_syntax 0.1.0",
|
"ra_syntax 0.1.0",
|
||||||
"relative-path 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
"relative-path 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"rustc-hash 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
"rustc-hash 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
|
@ -1017,6 +1018,13 @@ dependencies = [
|
||||||
"walkdir 2.2.7 (registry+https://github.com/rust-lang/crates.io-index)",
|
"walkdir 2.2.7 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "ra_macros"
|
||||||
|
version = "0.1.0"
|
||||||
|
dependencies = [
|
||||||
|
"smol_str 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "ra_syntax"
|
name = "ra_syntax"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
|
|
|
@ -16,6 +16,7 @@ join_to_string = "0.1.3"
|
||||||
ra_syntax = { path = "../ra_syntax" }
|
ra_syntax = { path = "../ra_syntax" }
|
||||||
ra_arena = { path = "../ra_arena" }
|
ra_arena = { path = "../ra_arena" }
|
||||||
ra_db = { path = "../ra_db" }
|
ra_db = { path = "../ra_db" }
|
||||||
|
ra_macros = { path = "../ra_macros" }
|
||||||
test_utils = { path = "../test_utils" }
|
test_utils = { path = "../test_utils" }
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
|
|
|
@ -1,8 +1,3 @@
|
||||||
#[allow(unused)]
|
|
||||||
mod tt;
|
|
||||||
#[allow(unused)]
|
|
||||||
mod mbe;
|
|
||||||
|
|
||||||
/// Machinery for macro expansion.
|
/// Machinery for macro expansion.
|
||||||
///
|
///
|
||||||
/// One of the more complicated things about macros is managing the source code
|
/// One of the more complicated things about macros is managing the source code
|
||||||
|
@ -19,6 +14,7 @@ use ra_syntax::{
|
||||||
SyntaxKind::*,
|
SyntaxKind::*,
|
||||||
ast::{self, NameOwner},
|
ast::{self, NameOwner},
|
||||||
};
|
};
|
||||||
|
use ra_macros::{tt, mbe};
|
||||||
|
|
||||||
use crate::{HirDatabase, MacroCallId};
|
use crate::{HirDatabase, MacroCallId};
|
||||||
|
|
||||||
|
|
8
crates/ra_macros/Cargo.toml
Normal file
8
crates/ra_macros/Cargo.toml
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
[package]
|
||||||
|
edition = "2018"
|
||||||
|
name = "ra_macros"
|
||||||
|
version = "0.1.0"
|
||||||
|
authors = ["Aleksey Kladov <aleksey.kladov@gmail.com>"]
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
smol_str = "0.1.9"
|
14
crates/ra_macros/src/lib.rs
Normal file
14
crates/ra_macros/src/lib.rs
Normal file
|
@ -0,0 +1,14 @@
|
||||||
|
macro_rules! impl_froms {
|
||||||
|
($e:ident: $($v:ident), *) => {
|
||||||
|
$(
|
||||||
|
impl From<$v> for $e {
|
||||||
|
fn from(it: $v) -> $e {
|
||||||
|
$e::$v(it)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
)*
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub mod tt;
|
||||||
|
pub mod mbe;
|
|
@ -1,9 +1,9 @@
|
||||||
use ra_syntax::SmolStr;
|
use smol_str::SmolStr;
|
||||||
|
|
||||||
use crate::macros::tt::{self, Delimiter};
|
use crate::tt::{self, Delimiter};
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub(crate) struct MacroRules {
|
pub struct MacroRules {
|
||||||
rules: Vec<Rule>,
|
rules: Vec<Rule>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -71,7 +71,7 @@ struct Var {
|
||||||
kind: Option<SmolStr>,
|
kind: Option<SmolStr>,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn parse(tt: &tt::Subtree) -> Option<MacroRules> {
|
pub fn parse(tt: &tt::Subtree) -> Option<MacroRules> {
|
||||||
let mut parser = RulesParser::new(tt);
|
let mut parser = RulesParser::new(tt);
|
||||||
let mut rules = Vec::new();
|
let mut rules = Vec::new();
|
||||||
while !parser.is_eof() {
|
while !parser.is_eof() {
|
||||||
|
@ -140,7 +140,7 @@ fn parse_var(p: &mut RulesParser) -> Option<Var> {
|
||||||
fn parse_repeat(p: &mut RulesParser) -> Option<Repeat> {
|
fn parse_repeat(p: &mut RulesParser) -> Option<Repeat> {
|
||||||
let subtree = p.eat_subtree().unwrap();
|
let subtree = p.eat_subtree().unwrap();
|
||||||
let subtree = parse_subtree(subtree)?;
|
let subtree = parse_subtree(subtree)?;
|
||||||
let mut sep = p.eat_punct()?;
|
let sep = p.eat_punct()?;
|
||||||
let (separator, rep) = match sep.char {
|
let (separator, rep) = match sep.char {
|
||||||
'*' | '+' | '?' => (None, sep.char),
|
'*' | '+' | '?' => (None, sep.char),
|
||||||
char => (Some(Punct { char }), p.eat_punct()?.char),
|
char => (Some(Punct { char }), p.eat_punct()?.char),
|
|
@ -1,14 +1,14 @@
|
||||||
use ra_syntax::SmolStr;
|
use smol_str::SmolStr;
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub(crate) enum TokenTree {
|
pub enum TokenTree {
|
||||||
Leaf(Leaf),
|
Leaf(Leaf),
|
||||||
Subtree(Subtree),
|
Subtree(Subtree),
|
||||||
}
|
}
|
||||||
impl_froms!(TokenTree: Leaf, Subtree);
|
impl_froms!(TokenTree: Leaf, Subtree);
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub(crate) enum Leaf {
|
pub enum Leaf {
|
||||||
Literal(Literal),
|
Literal(Literal),
|
||||||
Punct(Punct),
|
Punct(Punct),
|
||||||
Ident(Ident),
|
Ident(Ident),
|
||||||
|
@ -16,13 +16,13 @@ pub(crate) enum Leaf {
|
||||||
impl_froms!(Leaf: Literal, Punct, Ident);
|
impl_froms!(Leaf: Literal, Punct, Ident);
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub(crate) struct Subtree {
|
pub struct Subtree {
|
||||||
pub(crate) delimiter: Delimiter,
|
pub delimiter: Delimiter,
|
||||||
pub(crate) token_trees: Vec<TokenTree>,
|
pub token_trees: Vec<TokenTree>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Copy, Debug)]
|
#[derive(Clone, Copy, Debug)]
|
||||||
pub(crate) enum Delimiter {
|
pub enum Delimiter {
|
||||||
Parenthesis,
|
Parenthesis,
|
||||||
Brace,
|
Brace,
|
||||||
Bracket,
|
Bracket,
|
||||||
|
@ -30,16 +30,16 @@ pub(crate) enum Delimiter {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub(crate) struct Literal {
|
pub struct Literal {
|
||||||
pub(crate) text: SmolStr,
|
pub text: SmolStr,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub(crate) struct Punct {
|
pub struct Punct {
|
||||||
pub(crate) char: char,
|
pub char: char,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub(crate) struct Ident {
|
pub struct Ident {
|
||||||
pub(crate) text: SmolStr,
|
pub text: SmolStr,
|
||||||
}
|
}
|
Loading…
Reference in a new issue