Skip to content

Commit

Permalink
can access other rules through inventory
Browse files Browse the repository at this point in the history
  • Loading branch information
JoelCourtney committed Jun 18, 2021
1 parent d8de1df commit 7e058be
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 18 deletions.
10 changes: 8 additions & 2 deletions src/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use check_keyword::CheckKeyword;
use std::collections::HashMap;
use crate::common::*;


#[derive(Debug)]
struct VariantInfo {
modes: Vec<String>,
Expand All @@ -28,6 +27,8 @@ pub(crate) fn lexer(lexer_ident: Ident, mut input: syn::ItemEnum) -> Result<Toke
vec![String::from("Default")]
};

let visibility = input.vis.clone();

let mut variant_info = vec![];
let mut current_modes = vec![modes.first().unwrap().clone()];
for variant in &mut input.variants {
Expand Down Expand Up @@ -145,13 +146,18 @@ pub(crate) fn lexer(lexer_ident: Ident, mut input: syn::ItemEnum) -> Result<Toke

let default_mode = format_ident!("{}", modes.first().unwrap().clone());

let submission = format_ident!("{}ParserSubmission", lexer_ident);

Ok(quote! {
#[derive(parce_macros::RemoveLexerAttributes, Debug, Eq, PartialEq, Copy, Clone, Hash)]
#[allow(dead_code)]
#input

#visibility struct #submission(pub core::any::TypeId, pub fn(u32, u32, parce::reexports::Lexeme<#ident>) -> parce::reexports::ArrayVec<[parce::reexports::AutomatonCommand; 3]>);
parce::reexports::inventory::collect!(#submission);

#[derive(Debug, Eq, PartialEq, Copy, Clone)]
enum #lexer_ident {
#visibility enum #lexer_ident {
#(#mode_idents),*
}

Expand Down
43 changes: 27 additions & 16 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ pub(crate) fn parser(lexer: syn::Path, mut input: syn::ItemEnum) -> Result<Token
let enum_ident = input.ident.clone();
let num_productions = variants.len();
let num_prod_index = syn::Index::from(num_productions);
let other_rule_paths: Vec<syn::Path> = other_rules.iter().map(|rule| syn::parse_str(rule).unwrap()).collect();

let mut route_matchers = vec![];
let mut route_assemblers = vec![];
Expand Down Expand Up @@ -128,11 +127,27 @@ pub(crate) fn parser(lexer: syn::Path, mut input: syn::ItemEnum) -> Result<Token
});
}

let mut parser_submission = lexer.clone();
let last_ident = parser_submission.segments.last().unwrap().ident.clone();
parser_submission.segments.last_mut().unwrap().ident = format_ident!("{}ParserSubmission", last_ident);

Ok(quote! {
#[derive(Debug, Eq, PartialEq)]
#[allow(dead_code)]
#input

parce::reexports::inventory::submit! {
#parser_submission(core::any::TypeId::of::<#enum_ident>(), |route: u32, state: u32, lexeme: parce::reexports::Lexeme<<#lexer as Lexer>::Lexemes>| -> parce::reexports::ArrayVec<[parce::reexports::AutomatonCommand; 3]> {
use parce::reexports::*;
use AutomatonCommand::*;

match route {
#(#route_matchers)*
other => panic!("route {} out of bounds", other)
}
})
}

impl parce::reexports::Parser for #enum_ident {
type Lexemes = <#lexer as Lexer>::Lexemes;
const PRODUCTIONS: u32 = #num_prod_index;
Expand All @@ -144,21 +159,19 @@ pub(crate) fn parser(lexer: syn::Path, mut input: syn::ItemEnum) -> Result<Token
use parce::reexports::*;
use AutomatonCommand::*;

println!("");
dbg!(route);
dbg!(state);
dbg!(&lexeme);
let result = if rule == Rule::of::<#enum_ident>() {
match route {
if rule == Rule::of::<#enum_ident>() {
return match route {
#(#route_matchers)*
other => panic!("route {} out of bounds", other)
}
} #(else if rule == Rule::of::<#other_rule_paths>() {
<#other_rule_paths as Parser>::commands(rule, route, state, lexeme)
})* else {
panic!("shouldn't be able to get here")
};
dbg!(result)
} else {
for submission in inventory::iter::<#parser_submission> {
if rule == submission.0 {
return submission.1(route, state, lexeme);
}
}
panic!("rule number {:?} not found", rule);
}
}
fn assemble(auto: parce::reexports::Rawtomaton, lexemes: &[parce::reexports::Lexeme<Self::Lexemes>], text: &str) -> (usize, Self) {
use parce::reexports::*;
Expand All @@ -173,9 +186,7 @@ pub(crate) fn parser(lexer: syn::Path, mut input: syn::ItemEnum) -> Result<Token
other => panic!("route {} out of bounds, shouldn't be possible", other)
};
(consumed, result)
} #(else if rule == Rule::of::<#other_rule_paths>() {
<#other_rule_paths as Parser>::assemble(auto, lexemes, text)
})* else {
} else {
panic!("shouldn't be able to get here")
}
}
Expand Down

0 comments on commit 7e058be

Please sign in to comment.