pelecanus v0.3.0 Pelecanus.Operator

Basic operators of PEG.

What you can do with regex should be done with regex.

Link to this section Summary

Functions

And-predicate operator which succeeds when given parser succeeds

Ordered choice operator which is given a list of parsers

Return a parser which always succeeds with {:ok, given_state}

Not-predicate operator which succeeds when given parser fails

Optional operator which succeeds regardless given parser succeeds or fails

Zero-or-more operator which invoke given parser until it fails

One-or-more operator which invoke given parser until it fails

Sequence operator which is given a list of parsers

Link to this section Functions

Link to this function and_predicate(e)
and_predicate(Pelecanus.parser()) :: Pelecanus.parser()

And-predicate operator which succeeds when given parser succeeds.

The parser return given state and no value even if given parser succeeds. This is used in sequence for lookahead.

Ordered choice operator which is given a list of parsers.

The parser invoke given parsers in order. Each parser are given the state the parser given.

When one of parsers succeeds, the parser succeeds and return it’s result.

If no parser succeeds, the parser fails.

If given list is [], the parser fails.

The parser is memoized. it will expires in 60 sec.

Example

iex> use Pelecanus, sigil_p: true
iex> state = State.init("baker")
iex> {:ok, _state, value} = choice([~p(a.*), ~p(b.+)]).(state)
iex> value
"baker"

Return a parser which always succeeds with {:ok, given_state}.

Link to this function not_predicate(e)
not_predicate(Pelecanus.parser()) :: Pelecanus.parser()

Not-predicate operator which succeeds when given parser fails.

This function is counterpart of and_predicate/1.

Optional operator which succeeds regardless given parser succeeds or fails.

If given parser fails, the result is same to empty()

Zero-or-more operator which invoke given parser until it fails.

The parser returns {:ok, state, value}. state is returned given parser last succeeding. value is a list of given parser returned.

When given parser succeeds 0 times, the parser return given state and [].

When given parser return {ok, state}, no value is append to the result.

The parser is memoized. it will expires in 60 sec.

One-or-more operator which invoke given parser until it fails.

It is differ from repeat/1 that this one fails unless given parser succeeds at least 1 time.

Link to this function sequence(e_list)
sequence([Pelecanus.parser()]) :: Pelecanus.parser()

Sequence operator which is given a list of parsers.

The parser invoke each parser in order. Each parser are passed state which previous parser return.

When one of parsers fails, the parser breaks sequence and fails.

If all parsers succeeds, the parser return {:ok, state, value}. state is last parser returned. value is a list of values each parser returned.

If given parser return {:ok, state}, no value is appended to the result.

The parser is memoized. it will expires in 60 sec.

Example

iex> use Pelecanus, sigil_p: true
iex> state = State.init("abc 123")
iex> {:ok, _state, value} = sequence([~p([a-z]+), ignore(~p( )), ~p(\d+)]).(state)
iex> value
["abc", "123"]