Update macro
This commit is contained in:
parent
4784921bb8
commit
fcc401f203
23 changed files with 63 additions and 120 deletions
|
@ -76,6 +76,12 @@ impl Parse for AutoRegistryArgs {
|
|||
/// - registry: (String) Name of the registry to collect the struct into
|
||||
/// - path: (Optional String) The crate path in which the struct is located
|
||||
/// If left empty, the path will be try to be automatically-deduced
|
||||
///
|
||||
/// # Note
|
||||
///
|
||||
/// Due to a lacking implementation of `proc_macro_span` in rust-analyzer,
|
||||
/// it is highly advised the set the `path` attribute when using this macro.
|
||||
/// See https://github.com/rust-lang/rust-analyzer/issues/15950
|
||||
#[proc_macro_attribute]
|
||||
pub fn auto_registry(attr: TokenStream, input: TokenStream) -> TokenStream {
|
||||
let args = parse_macro_input!(attr as AutoRegistryArgs);
|
||||
|
@ -84,7 +90,12 @@ pub fn auto_registry(attr: TokenStream, input: TokenStream) -> TokenStream {
|
|||
let ident = &input.ident;
|
||||
|
||||
let path = if let Some(path) = args.path {
|
||||
format!("{}::{}", path.value(), ident.to_string().as_str())
|
||||
let value = path.value();
|
||||
if value.is_empty() {
|
||||
value
|
||||
} else {
|
||||
format!("{}::{}", value, ident.to_string().as_str())
|
||||
}
|
||||
} else {
|
||||
// Attempt to get the path in a hacky way in case the path wasn't
|
||||
// specified as an attribute to the macro
|
||||
|
@ -229,6 +240,25 @@ impl Parse for GenerateRegistryArgs {
|
|||
/// comma-separated and create the resulting expression
|
||||
/// - return_type: (Type) The return type of the generated function.
|
||||
/// Must match the type of the macro invocation
|
||||
///
|
||||
/// # Example
|
||||
/// ```
|
||||
/// macro_rules! create_listeners {
|
||||
/// ( $($construct:expr),+ $(,)? ) => {{
|
||||
/// vec![$(Box::new($construct) as Box<dyn Listener>,)+]
|
||||
/// }};
|
||||
/// }
|
||||
/// #[generate_registry(
|
||||
/// registry = "listeners",
|
||||
/// target = build_listeners,
|
||||
/// return_type = Vec<Box<dyn Listener>>,
|
||||
/// maker = create_listeners)]
|
||||
///
|
||||
/// fn main()
|
||||
/// {
|
||||
/// let all_listeners : Vec<Box<dyn Listener>> = build_listeners();
|
||||
/// }
|
||||
/// ```
|
||||
#[proc_macro_attribute]
|
||||
pub fn generate_registry(attr: TokenStream, input: TokenStream) -> TokenStream {
|
||||
let args = parse_macro_input!(attr as GenerateRegistryArgs);
|
||||
|
@ -250,7 +280,6 @@ pub fn generate_registry(attr: TokenStream, input: TokenStream) -> TokenStream {
|
|||
}
|
||||
|
||||
let function = args.target;
|
||||
//proc_macro2::Ident::new(args.target.value().as_str(), proc_macro2::Span::call_site());
|
||||
let return_type = args.return_type;
|
||||
let maker = args.maker;
|
||||
|
||||
|
|
|
@ -13,6 +13,7 @@ use crate::document::variable::Variable;
|
|||
#[derive(Clone, Copy)]
|
||||
pub enum Target {
|
||||
HTML,
|
||||
#[allow(unused)]
|
||||
LATEX,
|
||||
}
|
||||
|
||||
|
@ -21,7 +22,6 @@ pub struct Compiler {
|
|||
cache: Option<RefCell<Connection>>,
|
||||
reference_count: RefCell<HashMap<String, HashMap<String, usize>>>,
|
||||
// TODO: External references, i.e resolved later
|
||||
|
||||
sections_counter: RefCell<Vec<usize>>,
|
||||
}
|
||||
|
||||
|
@ -44,11 +44,12 @@ impl Compiler {
|
|||
|
||||
/// Gets the section counter for a given depth
|
||||
/// This function modifies the section counter
|
||||
pub fn section_counter(&self, depth: usize) -> Ref<'_, Vec<usize>>
|
||||
{
|
||||
pub fn section_counter(&self, depth: usize) -> Ref<'_, Vec<usize>> {
|
||||
// Increment current counter
|
||||
if self.sections_counter.borrow().len() == depth {
|
||||
self.sections_counter.borrow_mut().last_mut()
|
||||
self.sections_counter
|
||||
.borrow_mut()
|
||||
.last_mut()
|
||||
.map(|id| *id += 1);
|
||||
return Ref::map(self.sections_counter.borrow(), |b| &*b);
|
||||
}
|
||||
|
|
|
@ -296,7 +296,7 @@ impl Element for Code {
|
|||
}
|
||||
}
|
||||
|
||||
#[auto_registry::auto_registry(registry = "rules")]
|
||||
#[auto_registry::auto_registry(registry = "rules", path = "crate::elements::code")]
|
||||
pub struct CodeRule {
|
||||
re: [Regex; 2],
|
||||
properties: PropertyParser,
|
||||
|
|
|
@ -16,17 +16,9 @@ use std::rc::Rc;
|
|||
|
||||
#[derive(Debug)]
|
||||
pub struct Comment {
|
||||
location: Token,
|
||||
content: String,
|
||||
}
|
||||
|
||||
impl Comment {
|
||||
pub fn new(location: Token, content: String) -> Self {
|
||||
Self {
|
||||
location: location,
|
||||
content,
|
||||
}
|
||||
}
|
||||
pub location: Token,
|
||||
#[allow(unused)]
|
||||
pub content: String,
|
||||
}
|
||||
|
||||
impl Element for Comment {
|
||||
|
@ -38,7 +30,7 @@ impl Element for Comment {
|
|||
}
|
||||
}
|
||||
|
||||
#[auto_registry::auto_registry(registry = "rules")]
|
||||
#[auto_registry::auto_registry(registry = "rules", path = "crate::elements::comment")]
|
||||
pub struct CommentRule {
|
||||
re: [Regex; 1],
|
||||
}
|
||||
|
|
|
@ -178,7 +178,7 @@ impl RuleState for CustomStyleState {
|
|||
|
||||
static STATE_NAME: &'static str = "elements.custom_style";
|
||||
|
||||
#[auto_registry::auto_registry(registry = "rules")]
|
||||
#[auto_registry::auto_registry(registry = "rules", path = "crate::elements::customstyle")]
|
||||
pub struct CustomStyleRule;
|
||||
|
||||
impl CustomStyleRule {
|
||||
|
|
|
@ -21,7 +21,7 @@ use crate::parser::rule::Rule;
|
|||
use crate::parser::source::Cursor;
|
||||
use crate::parser::source::Source;
|
||||
|
||||
#[auto_registry::auto_registry(registry = "rules")]
|
||||
#[auto_registry::auto_registry(registry = "rules", path = "crate::elements::elemstyle")]
|
||||
pub struct ElemStyleRule {
|
||||
start_re: Regex,
|
||||
}
|
||||
|
|
|
@ -146,7 +146,7 @@ impl Element for Graphviz {
|
|||
}
|
||||
}
|
||||
|
||||
#[auto_registry::auto_registry(registry = "rules")]
|
||||
#[auto_registry::auto_registry(registry = "rules", path = "crate::elements::graphviz")]
|
||||
pub struct GraphRule {
|
||||
re: [Regex; 1],
|
||||
properties: PropertyParser,
|
||||
|
|
|
@ -17,7 +17,7 @@ use std::rc::Rc;
|
|||
|
||||
use super::paragraph::Paragraph;
|
||||
|
||||
#[auto_registry::auto_registry(registry = "rules")]
|
||||
#[auto_registry::auto_registry(registry = "rules", path = "crate::elements::import")]
|
||||
pub struct ImportRule {
|
||||
re: [Regex; 1],
|
||||
}
|
||||
|
|
|
@ -284,7 +284,7 @@ impl RuleState for LayoutState {
|
|||
}
|
||||
}
|
||||
|
||||
#[auto_registry::auto_registry(registry = "rules")]
|
||||
#[auto_registry::auto_registry(registry = "rules", path = "crate::elements::layout")]
|
||||
pub struct LayoutRule {
|
||||
re: [Regex; 3],
|
||||
}
|
||||
|
|
|
@ -71,7 +71,7 @@ impl ContainerElement for Link {
|
|||
}
|
||||
}
|
||||
|
||||
#[auto_registry::auto_registry(registry = "rules")]
|
||||
#[auto_registry::auto_registry(registry = "rules", path = "crate::elements::link")]
|
||||
pub struct LinkRule {
|
||||
re: [Regex; 1],
|
||||
}
|
||||
|
|
|
@ -106,7 +106,7 @@ impl ContainerElement for ListEntry {
|
|||
}
|
||||
}
|
||||
|
||||
#[auto_registry::auto_registry(registry = "rules")]
|
||||
#[auto_registry::auto_registry(registry = "rules", path = "crate::elements::list")]
|
||||
pub struct ListRule {
|
||||
start_re: Regex,
|
||||
continue_re: Regex,
|
||||
|
|
|
@ -221,7 +221,7 @@ impl ReferenceableElement for Medium {
|
|||
}
|
||||
}
|
||||
|
||||
#[auto_registry::auto_registry(registry = "rules")]
|
||||
#[auto_registry::auto_registry(registry = "rules", path = "crate::elements::media")]
|
||||
pub struct MediaRule {
|
||||
re: [Regex; 1],
|
||||
properties: PropertyParser,
|
||||
|
|
|
@ -91,7 +91,7 @@ impl ContainerElement for Paragraph {
|
|||
}
|
||||
}
|
||||
|
||||
#[auto_registry::auto_registry(registry = "rules")]
|
||||
#[auto_registry::auto_registry(registry = "rules", path = "crate::elements::paragraph")]
|
||||
pub struct ParagraphRule {
|
||||
re: Regex,
|
||||
}
|
||||
|
|
|
@ -44,7 +44,7 @@ impl Element for Raw {
|
|||
}
|
||||
}
|
||||
|
||||
#[auto_registry::auto_registry(registry = "rules")]
|
||||
#[auto_registry::auto_registry(registry = "rules", path = "crate::elements::raw")]
|
||||
pub struct RawRule {
|
||||
re: [Regex; 1],
|
||||
properties: PropertyParser,
|
||||
|
|
|
@ -62,7 +62,7 @@ impl Element for Reference {
|
|||
}
|
||||
}
|
||||
|
||||
#[auto_registry::auto_registry(registry = "rules")]
|
||||
#[auto_registry::auto_registry(registry = "rules", path = "crate::elements::reference")]
|
||||
pub struct ReferenceRule {
|
||||
re: [Regex; 1],
|
||||
properties: PropertyParser,
|
||||
|
|
|
@ -20,7 +20,7 @@ use std::rc::Rc;
|
|||
|
||||
use super::text::Text;
|
||||
|
||||
#[auto_registry::auto_registry(registry = "rules")]
|
||||
#[auto_registry::auto_registry(registry = "rules", path = "crate::elements::script")]
|
||||
pub struct ScriptRule {
|
||||
re: [Regex; 2],
|
||||
eval_kinds: [(&'static str, &'static str); 3],
|
||||
|
|
|
@ -135,7 +135,7 @@ impl ReferenceableElement for Section {
|
|||
}
|
||||
}
|
||||
|
||||
#[auto_registry::auto_registry(registry = "rules")]
|
||||
#[auto_registry::auto_registry(registry = "rules", path = "crate::elements::section")]
|
||||
pub struct SectionRule {
|
||||
re: [Regex; 1],
|
||||
}
|
||||
|
|
|
@ -132,7 +132,7 @@ impl RuleState for StyleState {
|
|||
}
|
||||
}
|
||||
|
||||
#[auto_registry::auto_registry(registry = "rules")]
|
||||
#[auto_registry::auto_registry(registry = "rules", path = "crate::elements::style")]
|
||||
pub struct StyleRule {
|
||||
re: [Regex; 4],
|
||||
}
|
||||
|
|
|
@ -219,7 +219,7 @@ impl Element for Tex {
|
|||
}
|
||||
}
|
||||
|
||||
#[auto_registry::auto_registry(registry = "rules")]
|
||||
#[auto_registry::auto_registry(registry = "rules", path = "crate::elements::tex")]
|
||||
pub struct TexRule {
|
||||
re: [Regex; 2],
|
||||
properties: PropertyParser,
|
||||
|
|
|
@ -42,7 +42,7 @@ impl Element for Text {
|
|||
}
|
||||
}
|
||||
|
||||
#[auto_registry::auto_registry(registry = "rules")]
|
||||
#[auto_registry::auto_registry(registry = "rules", path = "crate::elements::text")]
|
||||
pub struct TextRule;
|
||||
|
||||
impl TextRule {
|
||||
|
|
|
@ -37,7 +37,7 @@ impl FromStr for VariableKind {
|
|||
}
|
||||
}
|
||||
|
||||
#[auto_registry::auto_registry(registry = "rules")]
|
||||
#[auto_registry::auto_registry(registry = "rules", path = "crate::elements::variable")]
|
||||
pub struct VariableRule {
|
||||
re: [Regex; 1],
|
||||
kinds: Vec<(String, String)>,
|
||||
|
@ -297,7 +297,7 @@ impl RegexRule for VariableRule {
|
|||
}
|
||||
}
|
||||
|
||||
#[auto_registry::auto_registry(registry = "rules")]
|
||||
#[auto_registry::auto_registry(registry = "rules", path = "crate::elements::variable")]
|
||||
pub struct VariableSubstitutionRule {
|
||||
re: [Regex; 1],
|
||||
}
|
||||
|
|
|
@ -94,85 +94,3 @@ impl From<&LineCursor> for Cursor
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct LsParser
|
||||
{
|
||||
rules: Vec<Box<dyn Rule>>,
|
||||
colors: ReportColors,
|
||||
|
||||
// Parser state
|
||||
pub state: RefCell<StateHolder>,
|
||||
pub kernels: RefCell<HashMap<String, Kernel>>,
|
||||
}
|
||||
|
||||
impl Parser for LsParser
|
||||
{
|
||||
fn colors(&self) -> &ReportColors { &self.colors }
|
||||
fn rules(&self) -> &Vec<Box<dyn Rule>> { &self.rules }
|
||||
fn rules_mut(&mut self) -> &mut Vec<Box<dyn Rule>> { &mut self.rules }
|
||||
|
||||
fn state(&self) -> Ref<'_, StateHolder> { self.state.borrow() }
|
||||
fn state_mut(&self) -> std::cell::RefMut<'_, StateHolder> { self.state.borrow_mut() }
|
||||
|
||||
fn has_error(&self) -> bool { true }
|
||||
|
||||
fn push<'a>(&self, doc: &dyn Document, elem: Box<dyn Element>) {
|
||||
todo!()
|
||||
}
|
||||
|
||||
fn parse<'a>(&self, source: Rc<dyn Source>, parent: Option<&'a dyn Document<'a>>) -> Box<dyn Document<'a>+'a> {
|
||||
todo!()
|
||||
}
|
||||
|
||||
fn parse_into<'a>(&self, source: Rc<dyn Source>, document: &'a dyn Document<'a>) {
|
||||
todo!()
|
||||
}
|
||||
}
|
||||
|
||||
impl KernelHolder for LsParser
|
||||
{
|
||||
fn get_kernel(&self, name: &str)
|
||||
-> Option<RefMut<'_, Kernel>> {
|
||||
RefMut::filter_map(self.kernels.borrow_mut(),
|
||||
|map| map.get_mut(name)).ok()
|
||||
}
|
||||
|
||||
fn insert_kernel(&self, name: String, kernel: Kernel)
|
||||
-> RefMut<'_, Kernel> {
|
||||
//TODO do not get
|
||||
self.kernels.borrow_mut()
|
||||
.insert(name.clone(), kernel);
|
||||
self.get_kernel(name.as_str()).unwrap()
|
||||
}
|
||||
}
|
||||
|
||||
impl StyleHolder for LsParser {
|
||||
fn element_styles(&self) -> Ref<'_, HashMap<String, Rc<dyn ElementStyle>>> {
|
||||
todo!()
|
||||
}
|
||||
|
||||
fn element_styles_mut(&self) -> RefMut<'_, HashMap<String, Rc<dyn ElementStyle>>> {
|
||||
todo!()
|
||||
}
|
||||
}
|
||||
|
||||
impl LayoutHolder for LsParser {
|
||||
fn layouts(&self) -> Ref<'_, HashMap<String, Rc<dyn LayoutType>>> {
|
||||
todo!()
|
||||
}
|
||||
|
||||
fn layouts_mut(&self) -> RefMut<'_, HashMap<String, Rc<dyn LayoutType>>> {
|
||||
todo!()
|
||||
}
|
||||
}
|
||||
|
||||
impl CustomStyleHolder for LsParser {
|
||||
fn custom_styles(&self) -> Ref<'_, HashMap<String, Rc<dyn CustomStyle>>> {
|
||||
todo!()
|
||||
}
|
||||
|
||||
fn custom_styles_mut(&self) -> RefMut<'_, HashMap<String, Rc<dyn CustomStyle>>> {
|
||||
todo!()
|
||||
}
|
||||
}
|
||||
|
|
|
@ -423,7 +423,10 @@ mod tests {
|
|||
(&doc as &dyn Document)
|
||||
.last_element_mut::<Paragraph>()
|
||||
.unwrap()
|
||||
.push(Box::new(Comment::new(tok.clone(), "COMMENT".to_string())))
|
||||
.push(Box::new(Comment {
|
||||
location: tok.clone(),
|
||||
content: "COMMENT".into(),
|
||||
}))
|
||||
.unwrap();
|
||||
assert_eq!(process_text(&doc, "\na"), "a");
|
||||
|
||||
|
|
Loading…
Reference in a new issue