Skip to content

Commit

Permalink
grammar/parser: make the grammar use ParserFactory to create Parsers
Browse files Browse the repository at this point in the history
Signed-off-by: Tibor Benke <ihrwein@gmail.com>
  • Loading branch information
ihrwein committed Oct 3, 2015
1 parent ed9fb61 commit 9427b68
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 82 deletions.
25 changes: 22 additions & 3 deletions src/grammar/parser/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,24 @@
mod pattern_parser;
pub use self::pattern_parser::pattern;
pub use self::pattern_parser::ParseError;

pub type ConcreteParserFactory = ::matcher::trie::parser_factory::TrieParserFactory;
use matcher::compiled_pattern::CompiledPattern;
use parsers::ParserFactory;
use self::pattern_parser::ParseResult;

mod pattern_parser;

pub fn pattern_with_factory<F: ParserFactory>(input: &str) -> ParseResult<CompiledPattern> {
self::pattern_parser::pattern::<F>(input)
}

/*
When you regenerate the grammar, don't forget to insert the F generic type parameter with
the following command:
rm -f pattern_parser.rs; cat pattern_parser.rs.IN | sed "s/\(fn [a-zA-Z0-9_]*<'input\)/\1, F: ParserFactory/" | sed "s/\(parse[a-zA-Z0-9_]*\)(/\1::<F>(/" >> pattern_parser.rs
The first sed add the F: ParserFactory generic type parameter to every function definition.
The second sed threads this F parameter through the call sites as well.
*/
pub fn pattern(input: &str) -> ParseResult<CompiledPattern> {
use ::matcher::trie::parser_factory::TrieParserFactory;
self::pattern_parser::pattern::<TrieParserFactory>(input)
}
7 changes: 3 additions & 4 deletions src/grammar/parser/pattern.rustpeg
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use grammar;
use utils;

use parsers::ParserFactory;
use super::ConcreteParserFactory;
use std::str::FromStr;

#[pub]
Expand Down Expand Up @@ -36,15 +35,15 @@ parser -> Box<Parser>

parser_SET -> Box<Parser>
= SET PARSER_PARAMS_BEGIN set:string opt_params:parser_SET_optional_params? PARSER_PARAMS_END name:parser_name? {
ConcreteParserFactory::new_set(set, name, opt_params)
F::new_set(set, name, opt_params)
}

parser_SET_optional_params -> Vec<OptionalParameter<'input>>
= comma params:parser_BASE_optional_param ** comma { params }

parser_INT -> Box<Parser>
= INT opt_params:parser_INT_optional_params? name:parser_name? {
ConcreteParserFactory::new_int(name, opt_params)
F::new_int(name, opt_params)
}

parser_INT_optional_params -> Vec<OptionalParameter<'input>>
Expand All @@ -53,7 +52,7 @@ parser_INT_optional_params -> Vec<OptionalParameter<'input>>
parser_GREEDY -> Vec<TokenType>
= PARSER_BEGIN GREEDY name:parser_name? PARSER_END end_string:literal? {
let mut tokens = Vec::new();
let parser = ConcreteParserFactory::new_greedy(name, end_string);
let parser = F::new_greedy(name, end_string);
tokens.push(TokenType::Parser(parser));
let end_string = end_string.map(|string| string.to_string());
if let Some(end_string) = end_string {
Expand Down
Loading

0 comments on commit 9427b68

Please sign in to comment.