Skip to content

Commit

Permalink
Implements parsing validate statements
Browse files Browse the repository at this point in the history
  • Loading branch information
tailhook committed Sep 7, 2017
1 parent 2c28911 commit 8d7d8cc
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 3 deletions.
5 changes: 4 additions & 1 deletion src/grammar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -546,7 +546,10 @@ impl Parser {
skip_many(
st_start("syntax")
.skip(skip_many(satisfy(|x: Token| x.kind != Newline)))
.skip(kind(Newline)))
.skip(kind(Newline))
.or(st_start("validate")
.skip(skip_many(satisfy(|x: Token| x.kind != Newline)))
.skip(kind(Newline))))
.with(parser(body)).skip(kind(Kind::Eof));

let (body, _) = p.parse(s)?;
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ extern crate owning_ref;
extern crate regex;
#[macro_use] extern crate quick_error;
#[macro_use] extern crate matches;
#[cfg(test)] #[macro_use] extern crate difference;
#[cfg(feature="json")] extern crate serde_json;

mod compare;
Expand Down
5 changes: 5 additions & 0 deletions src/parse_error.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::fmt::Write;

use combine::primitives::{ParseError as CombineError, Error};
use regex;

use tokenizer::TokenStream;
use {Pos};
Expand Down Expand Up @@ -28,6 +29,10 @@ quick_error! {
description("Template must start with `## syntax: indent`")
display("Template must start with `## syntax: indent`")
}
BadRegexValidator(value: String, err: regex::Error) {
description("Validator regexp is invalid")
display("Validator regex {:?} is invalid: {}", value, err)
}
}
}

Expand Down
16 changes: 14 additions & 2 deletions src/preparser.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use regex::{Regex, RegexSet};

use parse_error::{ParseError, ParseErrorEnum};
use validators::Validator;
use {Options};


Expand Down Expand Up @@ -29,7 +30,7 @@ impl Preparser {

let list = &[
(r"^##\s*syntax:\s*(\w+)(?:\n|$)", Syntax),
(r"^##\s*validate\s+\w+:.*(?:\n|$)", Validate),
(r"^##\s*validate\s+(\w+):\s*(.*?)\s*(?:\n|$)", Validate),
(r"^#.*(?:\n|$)", Comment),
(r"^###.*(?:\n|$)", Comment),
(r"^\s*\n", Comment),
Expand Down Expand Up @@ -74,7 +75,18 @@ impl Preparser {
}
}
Token::Validate => {
unimplemented!();
let name = m.get(1).unwrap().as_str();
let regex = m.get(2).unwrap().as_str();
let regex = Regex::new(regex)
.map_err(|e| ParseErrorEnum::BadRegexValidator(
regex.to_string(), e))?;
if name == "default" {
options.default_validator =
Validator::Regex(regex);
} else {
options.validators.insert(
name.to_string(), Validator::Regex(regex));
}
}
Token::Comment => {
// Skip
Expand Down
1 change: 1 addition & 0 deletions src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ mod oneline;
mod vars;
mod boolean;
mod math;
mod validate;

pub use self::diff::assert_eq;
18 changes: 18 additions & 0 deletions src/tests/validate.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
use grammar::{Parser};
use {Context};

fn render_x(template: &str, x: &str) -> String {
let tpl = Parser::new().parse(template).unwrap();
let mut vars: Context = Context::new();
vars.set("x", &x);
tpl.render(&vars).unwrap()
}

#[test]
fn valid_default() {
assert_diff!(
&render_x(r#"## syntax: oneline
## validate default: [a-z]+
{{ x }}"#, "hello"),
"hello", "\n", 0);
}

0 comments on commit 8d7d8cc

Please sign in to comment.