Skip to content

Commit

Permalink
grammar fixes and clippy cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
mdubinko committed Aug 23, 2022
1 parent 960a9b4 commit 8deae58
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 26 deletions.
12 changes: 6 additions & 6 deletions src/grammar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,14 @@ impl Grammar {
// 1) the main rule
let main_rule = Rule::new(rb.factors);
let branching_rule = self.definitions.entry(SmolStr::new(name))
.or_insert(BranchingRule::new(mark));
.or_insert_with(|| BranchingRule::new(mark));
branching_rule.add_alt_branch(main_rule);

// 2) synthesized rules
for (syn_name, builders) in rb.syn_rules {
for builder in builders {
let syn_branching_rule = self.definitions.entry(syn_name.clone())
.or_insert(BranchingRule::new(Mark::Mute));
.or_insert_with(|| BranchingRule::new(Mark::Mute));
syn_branching_rule.add_alt_branch(Rule::new(builder.factors));
}
}
Expand All @@ -76,15 +76,15 @@ impl Grammar {
&self.root_definition_name
}

pub fn get_root_definition<'a>(&'a self) -> &'a BranchingRule {
pub fn get_root_definition(&self) -> &BranchingRule {
self.get_definition(&self.root_definition_name)
}

pub fn get_definition_mark(&self, name: &str) -> Mark {
self.definitions[name].mark.clone()
}

pub fn get_definition<'a>(&'a self, name: &str) -> &'a BranchingRule {
pub fn get_definition(&self, name: &str) -> &BranchingRule {
&self.definitions[name]
}
}
Expand Down Expand Up @@ -225,7 +225,7 @@ impl Rule {
}

pub fn dot_notator(&self) -> crate::parser::DotNotation {
DotNotation::new(&self)
DotNotation::new(self)
}

pub fn add_term(&mut self, term: Factor) {
Expand Down Expand Up @@ -340,7 +340,7 @@ impl CharMatcher {
CharMatcher::Exact(ch) => *ch==test,
CharMatcher::OneOf(lst) => lst.contains(test),
CharMatcher::Range(bot, top) => test <= *top && test >= *bot,
CharMatcher::UnicodeRange(name) => UnicodeRange::new(&name).accept(test),
CharMatcher::UnicodeRange(name) => UnicodeRange::new(name).accept(test),
}
}
}
Expand Down
32 changes: 29 additions & 3 deletions src/ixml_grammar.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use crate::grammar::{Grammar, Rule, Mark, TMark};
use indextree::{Arena, NodeId};

use crate::{grammar::{Grammar, Rule, Mark, TMark}, parser::Content};

// TODO: -rules and @rules

Expand Down Expand Up @@ -39,7 +41,7 @@ pub fn grammar() -> Grammar {
// -namestart: ["_"; L].
// -namefollower: namestart; ["-.·‿⁀"; Nd; Mn].
// TODO: fixme
g.define("name", Rule::seq().repeat1( Rule::seq().ch_in("_abcdefghijklmnopqrstuvwxyzRS")));
g.mark_define(Mark::Attr, "name", Rule::seq().repeat1( Rule::seq().ch_in("_abcdefghijklmnopqrstuvwxyzRS")));

// alts: alt++(-[";|"], s).
g.define("alts", Rule::seq().repeat1_sep(
Expand Down Expand Up @@ -119,7 +121,7 @@ fn parse_ixml() {
let mut parser = crate::parser::Parser::new(g);
let _trace = parser.parse(ixml);
let result = crate::parser::Parser::tree_to_testfmt( &parser.unpack_parse_tree("ixml") );
let expected = r#"<ixml><rule><name>doc</name><alt><literal string="A"></literal><literal string="B"></literal></alt></rule></ixml>"#;
let expected = r#"<ixml><rule name="doc"><alt><literal string="A"></literal><literal string="B"></literal></alt></rule></ixml>"#;
assert_eq!(result, expected);

// now do a second pass, with the just-generated grammar
Expand Down Expand Up @@ -201,3 +203,27 @@ member: string;
-letter: ["a"-"z"].
insertion: -"+", s, (string; -"#", hex), s.
*/

/// Accepts the Arena<Content> resulting from the parse of a valid ixml grammar
/// Produces a new Grammar as output
pub fn ixml_tree_to_grammar(arena: &Arena<Content>) -> Grammar {
let g = Grammar::new("ixml");

let root_node = arena.iter().next().unwrap(); // first item == root
let root_id = arena.get_node_id(root_node).unwrap();

// first a pass over everything, making some indexes as we go
let mut all_rules: Vec<NodeId> = Vec::new();
for nid in root_id.descendants(arena) {
let content = arena.get(nid).unwrap().get();
match content {
Content::Element(name) if name=="rule" => all_rules.push(nid),
_ => {}
}
}


for child in all_rules {
}
g
}
28 changes: 11 additions & 17 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use log::{info, debug, trace};
// TODO: logs ^^^
// TODO: just make parser.parse return indextree::Arena

const DOTSEP: &'static str = "•";
const DOTSEP: &str = "•";

#[derive(Debug, Clone, Eq, PartialEq)]
/// A sort of iterator for a Rule.
Expand Down Expand Up @@ -85,8 +85,8 @@ enum MatchRec {
impl MatchRec {
fn pos(&self) -> usize {
match self {
MatchRec::Term(_, pos, _) => pos.clone(),
MatchRec::NonTerm(_, pos, _) => pos.clone(),
MatchRec::Term(_, pos, _) => *pos,
MatchRec::NonTerm(_, pos, _) => *pos,
}
}
}
Expand Down Expand Up @@ -283,16 +283,10 @@ pub enum Content {

impl Content {
pub fn is_attr(&self) -> bool {
match self {
Content::Attribute(_) => true,
_ => false,
}
matches!(self, Content::Attribute(_))
}
pub fn is_elem(&self) -> bool {
match self {
Content::Element(_) => true,
_=> false,
}
matches!(self, Content::Element(_))
}
}

Expand Down Expand Up @@ -428,13 +422,13 @@ impl Parser {
}

fn queue_front(&mut self, maybe_id: Option<TraceId>) {
for id in maybe_id {
if let Some(id) = maybe_id {
self.traces.queue.push_front(id)
}
}

fn queue_back(&mut self, maybe_id: Option<TraceId>) {
for id in maybe_id {
if let Some(id) = maybe_id {
self.traces.queue.push_back(id)
}
}
Expand Down Expand Up @@ -476,14 +470,14 @@ impl Parser {
arena
}

fn unpack_parse_tree_internal(&self, arena: &mut Arena<Content>, name: &str, mark: Mark, origin: usize, end: usize, root: NodeId) -> () {
fn unpack_parse_tree_internal(&self, arena: &mut Arena<Content>, name: &str, mark: Mark, origin: usize, end: usize, root: NodeId) {
let matching_trace = self.filter_completed_trace(name, origin, end);
let mut new_root = root;
match matching_trace {
Some(task) => {
let match_name = &task.name;

if task.mark==Mark::Mute || match_name.starts_with("-") {
if task.mark==Mark::Mute || match_name.starts_with('-') {
// Skip
println!("trace found {mark} {task} -- SKIPPING");
} else {
Expand Down Expand Up @@ -567,7 +561,7 @@ impl Parser {
builder.append(name.to_string());

// handle attributes before closing start tag...
for attr_child in nid.children(arena).filter(|n| arena.get(n.clone()).unwrap().get().is_attr() ) {
for attr_child in nid.children(arena).filter(|n| arena.get(*n).unwrap().get().is_attr() ) {
builder.append(" ");
let attr_desc = arena.get(attr_child).unwrap().get();
let attr_name = match attr_desc {
Expand All @@ -584,7 +578,7 @@ impl Parser {
Content::Text(txt) => attr_builder.append(txt.as_str()),
_ => {}
}
builder.append(attr_builder.string().unwrap().replace("\"", "&quot;"));
builder.append(attr_builder.string().unwrap().replace('\"', "&quot;"));
}

builder.append("\"");
Expand Down

0 comments on commit 8deae58

Please sign in to comment.