Struct pidgin::Grammar[][src]

pub struct Grammar {
    pub name: Option<String>,
    // some fields omitted
}

A compiled collection of rules ready for the building of a pidgin::Matcher or for use in the definition of a new rule.

You do not build new Grammars directly. Rather, the compile or grammar method of a Pidgin produces them for you.

Fields

Methods

impl Grammar
[src]

Assigns a name to the grammar.

This will have no effect on any uses of the grammar already used in rules.

Examples

let mut p = Pidgin::new();
let g = p.grammar(&vec!["foo", "bar", "baz"]);
p.rule("foo", &g);
let mut g = p.grammar(&vec!["foo cat", "foo dog"]);
print!("{}", g);
// TOP := {foo} (?:cat|dog)
// foo := foo|ba[rz]
g.name("Bartholemew");
print!("{}", g);
// Bartholemew := {foo} (?:cat|dog)
//         foo := foo|ba[rz]

Compiles a Matcher based on the Grammar's rules.

Errors

If the Grammar contains a "foreign rule" with a named capture, an error may be returned.

Sets the required number of repetitions of the grammar in the string matched against to exactly r.

This is chiefly useful in conjunction with Pidgin::build_rule.

Examples

let mut p = Pidgin::new();
let g = p.grammar(&vec!["foo", "bar", "baz"]);
p.build_rule("foo", vec![sf("xyzzy "), gf(g.reps(3)), sf(" plugh")]);
let g = p.grammar(&vec!["foo"]);
let m = g.matcher()?;
assert!(m.is_match("xyzzy foobarbaz plugh"));
assert!(!m.is_match("xyzzy foo plugh"));
assert!(!m.is_match("xyzzy foobarbazfoo plugh"));

Sets the minimum required number of repetitions of the grammar in the string matched against to r. If no maximum is set, this will be equivalent to the regex repetition suffix {r,} -- there will be no upper limit.

If you set the lower limit to 0 and set no upper limit, this will be equivalent to the regex repetition suffix *. Likewise, 1 and no upper limit will be equivalent to +.

This is chiefly useful in conjunction with Pidgin::build_rule.

Examples

let mut p = Pidgin::new();
let g = p.grammar(&vec!["foo", "bar", "baz"]);
p.build_rule("foo", vec![sf("xyzzy "), gf(g.reps_min(3)?), sf(" plugh")]);
let m = p.matcher()?;
assert!(m.is_match("xyzzy foobarbaz plugh"));
assert!(m.is_match("xyzzy foobarbazfoo plugh"));
assert!(!m.is_match("xyzzy foobar plugh"));

Errors

If an upper limit has been set and it is lower than the minimum, an error will be returned

Sets the maximum number of repetitions of the grammar in the string matched against to r. If no minimum is set, this will be equivalent to the regex repetition suffix {0,r} -- there will be no lower limit.

This is chiefly useful in conjunction with Pidgin::build_rule.

Examples

let mut p = Pidgin::new();
let g = p.grammar(&vec!["foo", "bar", "baz"]);
p.build_rule("foo", vec![sf("xyzzy "), gf(g.reps_max(3)?), sf(" plugh")]);
let m = p.matcher()?;
assert!(m.is_match("xyzzy foobarbaz plugh"));
assert!(m.is_match("xyzzy foobar plugh"));
assert!(!m.is_match("xyzzy foobarbazfoo plugh"));

Errors

If an lower limit has been set and it is higher than the maximum, an error will be returned. Also, if the maximum is set to 0 an error will be returned.

Sets the minimum number of repetitions of the grammar in the string matched against to min and the maximum to max. If no minimum is set, this will be equivalent to the regex repetition suffix {min,max}.

This is chiefly useful in conjunction with Pidgin::build_rule.

Examples

let mut p = Pidgin::new();
let g = p.grammar(&vec!["foo", "bar", "baz"]);
p.build_rule("foo", vec![sf("xyzzy "), gf(g.reps_min_max(1, 3)?), sf(" plugh")]);
let m = p.matcher()?;
assert!(m.is_match("xyzzy foo plugh"));
assert!(m.is_match("xyzzy foobar plugh"));
assert!(m.is_match("xyzzy foobarbaz plugh"));
assert!(!m.is_match("xyzzy  plugh"));
assert!(!m.is_match("xyzzy foobarbazfoo plugh"));

Errors

If the minimum is greater than the maximum or the maximum is 0, an errors will be returned.

Produces a quasi-BNF representation of the grammar.

This is how pidging::Grammar implements std::fmt::Display, so a grammar's description is what you get if you feed it to the {} pattern.

Examples

let mut p = Pidgin::new();
let g = p.grammar(&vec!["bar", "baz"]);
p.rule("foo", &g);
p = p.case_insensitive(true);
let g = p.grammar(&vec!["ping", "pang", "pong"]);
p = p.case_insensitive(false);
p.rule("xyzzy", &g);
let g = p.grammar(&vec!["xyzzy", "qux"]);
p.rule("plugh", &g);
let g = p.grammar(&vec!["foo", "plugh"]);
println!("{}", g.describe());
//   TOP :=      {foo}|{plugh}
//   foo :=      ba[rz]
// plugh :=      qux|{xyzzy}
// xyzzy := (?i) p[aio]ng
println!("{}", g);
//   TOP :=      {foo}|{plugh}
//   foo :=      ba[rz]
// plugh :=      qux|{xyzzy}
// xyzzy := (?i) p[aio]ng

Returns a quasi-regex representation of the grammar. This is intended mostly for debugging. Rules will be identifiable by named groups, but group names may repeat, in which case the stringification cannot be compiled into a regular expression.

Trait Implementations

impl Clone for Grammar
[src]

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

impl Debug for Grammar
[src]

Formats the value using the given formatter. Read more

impl PartialOrd for Grammar
[src]

This method returns an ordering between self and other values if one exists. Read more

This method tests less than (for self and other) and is used by the < operator. Read more

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more

This method tests greater than (for self and other) and is used by the > operator. Read more

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more

impl Ord for Grammar
[src]

This method returns an Ordering between self and other. Read more

Compares and returns the maximum of two values. Read more

Compares and returns the minimum of two values. Read more

impl PartialEq for Grammar
[src]

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

impl Eq for Grammar
[src]

impl Display for Grammar
[src]

Formats the value using the given formatter. Read more

Auto Trait Implementations

impl Send for Grammar

impl Sync for Grammar