Skip to content

Commit

Permalink
Merge pull request #17 from ihrwein/f/clone
Browse files Browse the repository at this point in the history
Matcher: implement Clone trait
  • Loading branch information
ihrwein committed Jul 5, 2015
2 parents b1b2437 + 9c730c1 commit 8ccc3c9
Show file tree
Hide file tree
Showing 12 changed files with 32 additions and 10 deletions.
1 change: 1 addition & 0 deletions src/matcher/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use grammar::parser;
use grammar::parser::ParseError;
use self::trie::ParserTrie;

#[derive(Clone)]
pub struct Matcher {
parser: ParserTrie
}
Expand Down
2 changes: 1 addition & 1 deletion src/matcher/trie/node/literal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use matcher::trie::node::{Node, ParserNode};
use matcher::trie::TrieOperations;
use parsers::Parser;

#[derive(Debug)]
#[derive(Debug, Clone)]
pub struct LiteralNode {
literal: String,
has_value: bool,
Expand Down
2 changes: 1 addition & 1 deletion src/matcher/trie/node/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub enum TokenType {
Literal(String)
}

#[derive(Debug)]
#[derive(Debug, Clone)]
pub struct Node {
literal_children: SortedVec<LiteralNode>,
parser_children: Vec<ParserNode>
Expand Down
9 changes: 9 additions & 0 deletions src/matcher/trie/node/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,12 @@ impl TrieOperations for ParserNode {
self.node.as_mut().unwrap().insert_parser(parser)
}
}

impl Clone for ParserNode {
fn clone(&self) -> ParserNode {
ParserNode{
parser: self.parser.boxed_clone(),
node: self.node.clone()
}
}
}
2 changes: 1 addition & 1 deletion src/matcher/trie/trie.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use matcher::trie::node::{Node, TokenType};
use matcher::trie::node::{CompiledPattern};
use matcher::trie::TrieOperations;

#[derive(Debug)]
#[derive(Debug, Clone)]
pub struct ParserTrie {
root: Node,
}
Expand Down
2 changes: 1 addition & 1 deletion src/parsers/base.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#[derive(Hash, Debug)]
#[derive(Clone, Hash, Debug)]
pub struct ParserBase {
name: String
}
Expand Down
6 changes: 5 additions & 1 deletion src/parsers/greedy.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::hash::{SipHasher, Hash, Hasher};
use super::{ParserBase, Parser, ObjectSafeHash};

#[derive(Debug, Hash)]
#[derive(Clone, Debug, Hash)]
pub struct GreedyParser {
base: ParserBase,
end_string: Option<String>
Expand Down Expand Up @@ -49,6 +49,10 @@ impl Parser for GreedyParser {
fn name(&self) -> &str {
self.base.name()
}

fn boxed_clone(&self) -> Box<Parser> {
Box::new(self.clone())
}
}

#[cfg(test)]
Expand Down
6 changes: 5 additions & 1 deletion src/parsers/int.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::hash::{SipHasher, Hash, Hasher};

use parsers::{Parser, ObjectSafeHash, SetParser, HasOptionalParameter, OptionalParameter};

#[derive(Debug, Hash)]
#[derive(Clone, Debug, Hash)]
pub struct IntParser {
delegate: SetParser
}
Expand Down Expand Up @@ -34,6 +34,10 @@ impl Parser for IntParser {
fn name(&self) -> &str {
self.delegate.name()
}

fn boxed_clone(&self) -> Box<Parser> {
Box::new(self.clone())
}
}

impl ObjectSafeHash for IntParser {
Expand Down
2 changes: 1 addition & 1 deletion src/parsers/length_checked.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use parsers::{HasOptionalParameter, OptionalParameter, ParserBase};

#[derive(Hash, Debug)]
#[derive(Clone, Hash, Debug)]
pub struct LengthCheckedParserBase {
base: ParserBase,
min_length: Option<usize>,
Expand Down
2 changes: 1 addition & 1 deletion src/parsers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ pub trait ObjectSafeHash {
pub trait Parser: Debug + ObjectSafeHash {
fn parse<'a, 'b>(&'a self, value: &'b str) -> Option<(&'a str, &'b str)>;
fn name(&self) -> &str;
fn boxed_clone(&self) -> Box<Parser>;
}


pub trait HasOptionalParameter {
fn set_optional_params<'a>(&mut self, params: &Vec<OptionalParameter<'a>>) -> bool;
}
Expand Down
6 changes: 5 additions & 1 deletion src/parsers/set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::hash::{SipHasher, Hash, Hasher};

use parsers::{Parser, ObjectSafeHash, LengthCheckedParserBase, HasOptionalParameter, OptionalParameter};

#[derive(Debug, Hash)]
#[derive(Clone, Debug, Hash)]
pub struct SetParser {
character_set: BTreeSet<u8>,
base: LengthCheckedParserBase
Expand Down Expand Up @@ -66,6 +66,10 @@ impl Parser for SetParser {
fn name(&self) -> &str {
self.base.name()
}

fn boxed_clone(&self) -> Box<Parser> {
Box::new(self.clone())
}
}

impl ObjectSafeHash for SetParser {
Expand Down
2 changes: 1 addition & 1 deletion src/utils/sortedvec.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::cmp::Ordering;

#[derive(Debug)]
#[derive(Clone, Debug)]
pub struct SortedVec<T> {
array: Vec<T>
}
Expand Down

0 comments on commit 8ccc3c9

Please sign in to comment.