Description

Templated types for string pattern matching and lexers.

Package Information

Version1.0.1 (2016-Apr-10)
Repositoryhttps://github.com/Mihail-K/pattern
LicenseMIT
CopyrightCopyright © 2016, Mihail K
AuthorsMihail-K
Registered byMihail K
Dependenciesnone

Installation

To use this package, put the following dependency into your project's dependencies section:

dub.json
dub.sdl

Readme

Pattern DUB DUB

Templated types for string pattern matching and lexers.

Pattern can be used to construct elaborate and complex pattern matchers that can operate at compile time (std.regex currently cannot), and are often faster than both regex and ctRegex matchers. Pattern was designed with lexers in mind.

Hello World

import pattern;

void main()
{
    // A pattern for lowercase letters.
    auto letters = repeat!(bracket!('a', 'z'));

    assert(letters("hello world") == "hello");
}

Patterns can also be defined from callback functions that accept either string or char and return bool.

import std.ascii : isWhite;

string whitespace(string input)
{
    // A pattern for whitespaces.
    enum pattern = repeat!isWhite;

    return pattern(input);
}

Pattern Types

Here's a short list of pattern types and concise examples of how they behave.

Primitive
void keyword()
{
    // A pattern that matches 'this' or 'that'
    enum p = primitive!("this", "that");

    assert(p("this") == "this");
    assert(p("them") is null);
}
Bracket
void numeric()
{
    // A pattern that behaves like /[0-9]/
    enum p = bracket!('0', '9');

    assert(p("123") == "1");
    assert(p("abc") is null);
}
Complement
void nonNumeric()
{
    // A pattern that behaves like /[^0-9]/
    enum p = complement!(bracket!('0', '9'));

    assert(p("123") is null);
    assert(p("abc") == "a");
}
Repeat
void integer()
{
    // A pattern that behaves like /[0-9]+/
    enum p = repeat!(bracket!('0', '9'));

    assert(p("123") == "123");
    assert(p("abc") is null);
}
Optional
void mightBeInt()
{
    // A pattern that behaves like /[0-9]?/
    enum p = optional!(bracket!('0', '9'));

    assert(p("123") == "123");
    assert(p("abc") == "");
}
Sequence
void decimal()
{
    // A pattern that behaves like /[0-9]\.[0-9]/
    enum p = sequence!(
        bracket!('0', '9'),
        primitive!("."),
        bracket!('0', '9')
    );

    assert(p("1.5") == "1.5");
    assert(p("1.b") is null);
    assert(p("5")   is null);
}
Selection
void intOrName()
{
    // A pattern that behaves like /[0-9]+|[a-z]+/
    enum p = selection!(
        repeat!(bracket!('0', '9')),
        repeat!(bracket!('a', 'z'))
    );

    assert(p("123") == "123");
    assert(p("abc") == "abc");
}

License

MIT

Available versions

1.0.1 1.0.0 ~master