Struct Syntax

Source
pub struct Syntax { /* private fields */ }
Expand description

Onig Syntax Wrapper

Each syntax dfines a flavour of regex syntax. This type allows interaction with the built-in syntaxes through the static accessor functions (Syntax::emacs(), Syntax::default() etc.) and the creation of custom syntaxes.

For a demonstration of creating a custom syntax see examples/syntax.rs in the main onig crate.

Implementations§

Source§

impl Syntax

Source

pub fn python() -> &'static Syntax

Python syntax

Source

pub fn asis() -> &'static Syntax

Plain text syntax

Examples found in repository?
examples/syntax.rs (line 30)
20fn main() {
21    exec(
22        Syntax::perl(),
23        r"\p{XDigit}\P{XDigit}\p{^XDigit}\P{^XDigit}\p{XDigit}",
24        "bgh3a",
25    );
26
27    exec(Syntax::java(), r"\p{XDigit}\P{XDigit}[a-c&&b-g]", "bgc");
28
29    exec(
30        Syntax::asis(),
31        r"abc def* e+ g?ddd[a-rvvv] (vv){3,7}hv\dvv(?:aczui ss)\W\w$",
32        r"abc def* e+ g?ddd[a-rvvv] (vv){3,7}hv\dvv(?:aczui ss)\W\w$",
33    );
34}
Source

pub fn posix_basic() -> &'static Syntax

POSIX Basic RE syntax

Source

pub fn posix_extended() -> &'static Syntax

POSIX Extended RE syntax

Source

pub fn emacs() -> &'static Syntax

Emacs syntax

Examples found in repository?
examples/simple_grep.rs (line 14)
7fn main() {
8    let mut regexes = HashMap::new();
9    for arg in env::args().skip(1) {
10        println!("Compiling '{}'", arg);
11        let regex_compilation = Regex::with_options(
12            &arg,
13            onig::RegexOptions::REGEX_OPTION_SINGLELINE,
14            onig::Syntax::emacs(),
15        );
16        match regex_compilation {
17            Ok(regex) => {
18                regexes.insert(arg, regex);
19            }
20            Err(error) => {
21                panic!("{:?}", error);
22            }
23        }
24    }
25
26    let stdin = io::stdin();
27    for line in stdin.lock().lines() {
28        if let Ok(line) = line {
29            for (name, regex) in regexes.iter() {
30                let res = regex.search_with_options(
31                    &line,
32                    0,
33                    line.len(),
34                    onig::SearchOptions::SEARCH_OPTION_NONE,
35                    None,
36                );
37                match res {
38                    Some(pos) => println!("{} => matched @ {}", name, pos),
39                    None => println!("{} => did not match", name),
40                }
41            }
42        }
43    }
44    println!("done");
45}
Source

pub fn grep() -> &'static Syntax

Grep syntax

Source

pub fn gnu_regex() -> &'static Syntax

GNU regex syntax

Source

pub fn java() -> &'static Syntax

Java (Sun java.util.regex) syntax

Examples found in repository?
examples/syntax.rs (line 27)
20fn main() {
21    exec(
22        Syntax::perl(),
23        r"\p{XDigit}\P{XDigit}\p{^XDigit}\P{^XDigit}\p{XDigit}",
24        "bgh3a",
25    );
26
27    exec(Syntax::java(), r"\p{XDigit}\P{XDigit}[a-c&&b-g]", "bgc");
28
29    exec(
30        Syntax::asis(),
31        r"abc def* e+ g?ddd[a-rvvv] (vv){3,7}hv\dvv(?:aczui ss)\W\w$",
32        r"abc def* e+ g?ddd[a-rvvv] (vv){3,7}hv\dvv(?:aczui ss)\W\w$",
33    );
34}
Source

pub fn perl() -> &'static Syntax

Perl syntax

Examples found in repository?
examples/syntax.rs (line 22)
20fn main() {
21    exec(
22        Syntax::perl(),
23        r"\p{XDigit}\P{XDigit}\p{^XDigit}\P{^XDigit}\p{XDigit}",
24        "bgh3a",
25    );
26
27    exec(Syntax::java(), r"\p{XDigit}\P{XDigit}[a-c&&b-g]", "bgc");
28
29    exec(
30        Syntax::asis(),
31        r"abc def* e+ g?ddd[a-rvvv] (vv){3,7}hv\dvv(?:aczui ss)\W\w$",
32        r"abc def* e+ g?ddd[a-rvvv] (vv){3,7}hv\dvv(?:aczui ss)\W\w$",
33    );
34}
Source

pub fn perl_ng() -> &'static Syntax

Perl + named group syntax

Source

pub fn ruby() -> &'static Syntax

Ruby syntax

Source

pub fn oniguruma() -> &'static Syntax

Oniguruma Syntax

Source

pub fn default() -> &'static Syntax

Default syntax (Ruby syntax)

Examples found in repository?
examples/listcap.rs (line 36)
35fn main() {
36    let mut syn = Syntax::default().clone();
37    syn.enable_operators(SyntaxOperator::SYNTAX_OPERATOR_ATMARK_CAPTURE_HISTORY);
38
39    ex(
40        "((())())",
41        "\\g<p>(?@<p>\\(\\g<s>\\)){0}(?@<s>(?:\\g<p>)*|){0}",
42        &syn,
43    );
44    ex("x00x00x00", "(?@x(?@\\d+))+", &syn);
45    ex("0123", "(?@.)(?@.)(?@.)(?@.)", &syn);
46}
More examples
Hide additional examples
examples/sql.rs (line 4)
3fn main() {
4    let mut syntax = Syntax::default().clone();
5
6    syntax.set_operators(SyntaxOperator::SYNTAX_OPERATOR_VARIABLE_META_CHARACTERS);
7    syntax.set_behavior(SyntaxBehavior::empty());
8    syntax.set_options(RegexOptions::REGEX_OPTION_MULTILINE);
9
10    syntax.set_meta_char(MetaCharType::META_CHAR_ESCAPE, MetaChar::Character('\\'));
11    syntax.set_meta_char(MetaCharType::META_CHAR_ANYCHAR, MetaChar::Character('_'));
12    syntax.set_meta_char(MetaCharType::META_CHAR_ANYTIME, MetaChar::Ineffective);
13    syntax.set_meta_char(
14        MetaCharType::META_CHAR_ZERO_OR_ONE_TIME,
15        MetaChar::Ineffective,
16    );
17    syntax.set_meta_char(
18        MetaCharType::META_CHAR_ONE_OR_MORE_TIME,
19        MetaChar::Ineffective,
20    );
21    syntax.set_meta_char(
22        MetaCharType::META_CHAR_ANYCHAR_ANYTIME,
23        MetaChar::Character('%'),
24    );
25
26    let reg =
27        Regex::with_options("\\_%\\\\__zz", RegexOptions::REGEX_OPTION_NONE, &syntax).unwrap();
28
29    match reg.captures("a_abcabcabc\\ppzz") {
30        Some(caps) => {
31            println!("match at {}", caps.offset());
32            for (i, cap) in caps.iter_pos().enumerate() {
33                match cap {
34                    Some(pos) => println!("{}: {:?}", i, pos),
35                    None => println!("{}: did not capture", i),
36                }
37            }
38        }
39        None => println!("search fail"),
40    }
41}
Source

pub fn operators(&self) -> SyntaxOperator

Retrieve the operators for this syntax

Source

pub fn set_operators(&mut self, operators: SyntaxOperator)

Replace the operators for this syntax

Examples found in repository?
examples/sql.rs (line 6)
3fn main() {
4    let mut syntax = Syntax::default().clone();
5
6    syntax.set_operators(SyntaxOperator::SYNTAX_OPERATOR_VARIABLE_META_CHARACTERS);
7    syntax.set_behavior(SyntaxBehavior::empty());
8    syntax.set_options(RegexOptions::REGEX_OPTION_MULTILINE);
9
10    syntax.set_meta_char(MetaCharType::META_CHAR_ESCAPE, MetaChar::Character('\\'));
11    syntax.set_meta_char(MetaCharType::META_CHAR_ANYCHAR, MetaChar::Character('_'));
12    syntax.set_meta_char(MetaCharType::META_CHAR_ANYTIME, MetaChar::Ineffective);
13    syntax.set_meta_char(
14        MetaCharType::META_CHAR_ZERO_OR_ONE_TIME,
15        MetaChar::Ineffective,
16    );
17    syntax.set_meta_char(
18        MetaCharType::META_CHAR_ONE_OR_MORE_TIME,
19        MetaChar::Ineffective,
20    );
21    syntax.set_meta_char(
22        MetaCharType::META_CHAR_ANYCHAR_ANYTIME,
23        MetaChar::Character('%'),
24    );
25
26    let reg =
27        Regex::with_options("\\_%\\\\__zz", RegexOptions::REGEX_OPTION_NONE, &syntax).unwrap();
28
29    match reg.captures("a_abcabcabc\\ppzz") {
30        Some(caps) => {
31            println!("match at {}", caps.offset());
32            for (i, cap) in caps.iter_pos().enumerate() {
33                match cap {
34                    Some(pos) => println!("{}: {:?}", i, pos),
35                    None => println!("{}: did not capture", i),
36                }
37            }
38        }
39        None => println!("search fail"),
40    }
41}
Source

pub fn enable_operators(&mut self, operators: SyntaxOperator)

Enable Operators for this Syntax

Updates the operators for this syntax to enable the chosen ones.

Examples found in repository?
examples/listcap.rs (line 37)
35fn main() {
36    let mut syn = Syntax::default().clone();
37    syn.enable_operators(SyntaxOperator::SYNTAX_OPERATOR_ATMARK_CAPTURE_HISTORY);
38
39    ex(
40        "((())())",
41        "\\g<p>(?@<p>\\(\\g<s>\\)){0}(?@<s>(?:\\g<p>)*|){0}",
42        &syn,
43    );
44    ex("x00x00x00", "(?@x(?@\\d+))+", &syn);
45    ex("0123", "(?@.)(?@.)(?@.)(?@.)", &syn);
46}
Source

pub fn disable_operators(&mut self, operators: SyntaxOperator)

Disable Operators for this Syntax

Updates the operators for this syntax to remove the specified operators.

Source

pub fn behavior(&self) -> SyntaxBehavior

Retrieves the syntax behaviours

Source

pub fn set_behavior(&mut self, behavior: SyntaxBehavior)

Overwrite the syntax behaviour for this syntax.

Examples found in repository?
examples/sql.rs (line 7)
3fn main() {
4    let mut syntax = Syntax::default().clone();
5
6    syntax.set_operators(SyntaxOperator::SYNTAX_OPERATOR_VARIABLE_META_CHARACTERS);
7    syntax.set_behavior(SyntaxBehavior::empty());
8    syntax.set_options(RegexOptions::REGEX_OPTION_MULTILINE);
9
10    syntax.set_meta_char(MetaCharType::META_CHAR_ESCAPE, MetaChar::Character('\\'));
11    syntax.set_meta_char(MetaCharType::META_CHAR_ANYCHAR, MetaChar::Character('_'));
12    syntax.set_meta_char(MetaCharType::META_CHAR_ANYTIME, MetaChar::Ineffective);
13    syntax.set_meta_char(
14        MetaCharType::META_CHAR_ZERO_OR_ONE_TIME,
15        MetaChar::Ineffective,
16    );
17    syntax.set_meta_char(
18        MetaCharType::META_CHAR_ONE_OR_MORE_TIME,
19        MetaChar::Ineffective,
20    );
21    syntax.set_meta_char(
22        MetaCharType::META_CHAR_ANYCHAR_ANYTIME,
23        MetaChar::Character('%'),
24    );
25
26    let reg =
27        Regex::with_options("\\_%\\\\__zz", RegexOptions::REGEX_OPTION_NONE, &syntax).unwrap();
28
29    match reg.captures("a_abcabcabc\\ppzz") {
30        Some(caps) => {
31            println!("match at {}", caps.offset());
32            for (i, cap) in caps.iter_pos().enumerate() {
33                match cap {
34                    Some(pos) => println!("{}: {:?}", i, pos),
35                    None => println!("{}: did not capture", i),
36                }
37            }
38        }
39        None => println!("search fail"),
40    }
41}
Source

pub fn enable_behavior(&mut self, behavior: SyntaxBehavior)

Enable a given behaviour for this syntax

Source

pub fn disable_behavior(&mut self, behavior: SyntaxBehavior)

Disable a given behaviour for this syntax

Source

pub fn options(&self) -> RegexOptions

Retireve the syntax options for this syntax

Source

pub fn set_options(&mut self, options: RegexOptions)

Replace the syntax options for this syntax

Examples found in repository?
examples/sql.rs (line 8)
3fn main() {
4    let mut syntax = Syntax::default().clone();
5
6    syntax.set_operators(SyntaxOperator::SYNTAX_OPERATOR_VARIABLE_META_CHARACTERS);
7    syntax.set_behavior(SyntaxBehavior::empty());
8    syntax.set_options(RegexOptions::REGEX_OPTION_MULTILINE);
9
10    syntax.set_meta_char(MetaCharType::META_CHAR_ESCAPE, MetaChar::Character('\\'));
11    syntax.set_meta_char(MetaCharType::META_CHAR_ANYCHAR, MetaChar::Character('_'));
12    syntax.set_meta_char(MetaCharType::META_CHAR_ANYTIME, MetaChar::Ineffective);
13    syntax.set_meta_char(
14        MetaCharType::META_CHAR_ZERO_OR_ONE_TIME,
15        MetaChar::Ineffective,
16    );
17    syntax.set_meta_char(
18        MetaCharType::META_CHAR_ONE_OR_MORE_TIME,
19        MetaChar::Ineffective,
20    );
21    syntax.set_meta_char(
22        MetaCharType::META_CHAR_ANYCHAR_ANYTIME,
23        MetaChar::Character('%'),
24    );
25
26    let reg =
27        Regex::with_options("\\_%\\\\__zz", RegexOptions::REGEX_OPTION_NONE, &syntax).unwrap();
28
29    match reg.captures("a_abcabcabc\\ppzz") {
30        Some(caps) => {
31            println!("match at {}", caps.offset());
32            for (i, cap) in caps.iter_pos().enumerate() {
33                match cap {
34                    Some(pos) => println!("{}: {:?}", i, pos),
35                    None => println!("{}: did not capture", i),
36                }
37            }
38        }
39        None => println!("search fail"),
40    }
41}
Source

pub fn set_meta_char(&mut self, what: MetaCharType, meta: MetaChar)

Set a given meta character’s state

Arguments:

  • what: The meta character to update
  • meta: The value to set the meta character to
Examples found in repository?
examples/sql.rs (line 10)
3fn main() {
4    let mut syntax = Syntax::default().clone();
5
6    syntax.set_operators(SyntaxOperator::SYNTAX_OPERATOR_VARIABLE_META_CHARACTERS);
7    syntax.set_behavior(SyntaxBehavior::empty());
8    syntax.set_options(RegexOptions::REGEX_OPTION_MULTILINE);
9
10    syntax.set_meta_char(MetaCharType::META_CHAR_ESCAPE, MetaChar::Character('\\'));
11    syntax.set_meta_char(MetaCharType::META_CHAR_ANYCHAR, MetaChar::Character('_'));
12    syntax.set_meta_char(MetaCharType::META_CHAR_ANYTIME, MetaChar::Ineffective);
13    syntax.set_meta_char(
14        MetaCharType::META_CHAR_ZERO_OR_ONE_TIME,
15        MetaChar::Ineffective,
16    );
17    syntax.set_meta_char(
18        MetaCharType::META_CHAR_ONE_OR_MORE_TIME,
19        MetaChar::Ineffective,
20    );
21    syntax.set_meta_char(
22        MetaCharType::META_CHAR_ANYCHAR_ANYTIME,
23        MetaChar::Character('%'),
24    );
25
26    let reg =
27        Regex::with_options("\\_%\\\\__zz", RegexOptions::REGEX_OPTION_NONE, &syntax).unwrap();
28
29    match reg.captures("a_abcabcabc\\ppzz") {
30        Some(caps) => {
31            println!("match at {}", caps.offset());
32            for (i, cap) in caps.iter_pos().enumerate() {
33                match cap {
34                    Some(pos) => println!("{}: {:?}", i, pos),
35                    None => println!("{}: did not capture", i),
36                }
37            }
38        }
39        None => println!("search fail"),
40    }
41}

Trait Implementations§

Source§

impl Clone for Syntax

Source§

fn clone(&self) -> Syntax

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Syntax

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Copy for Syntax

Auto Trait Implementations§

§

impl Freeze for Syntax

§

impl RefUnwindSafe for Syntax

§

impl Send for Syntax

§

impl Sync for Syntax

§

impl Unpin for Syntax

§

impl UnwindSafe for Syntax

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.