Pegasus (pegasus v0.1.1)
converts peg
files into NimbleParsec
parsers.
For documentation on this peg format: https://www.piumarta.com/software/peg/peg.1.html
To use, drop this in your model:
defmodule MyModule
require Pegasus
Pegasus.parser_from_string("""
foo <- "foo" "bar"
""")
end
Now each included parser identifier is turned into a public function.
See NimbleParsec
for the description of the output.
MyModule.foo("foobar")
Note: for capitalized identifiers, you will have to use Kernel.apply/2 to call the function.
You may also load a parser from a file using parser_from_file/2
.
post-traversals
Post-Traversals
You may supply a post_traversal for any parser. See NimbleParsec
for how to
implement post-traversal functions. These are defined by passing a keyword list
to the parser_from_file/2
or parser_from_string/2
function.
Example
Pegasus.parser_from_string("""
foo <- "foo" "bar"
""", foo: {:some_function, []})
defp foo(rest, ["bar", "foo"], context, {_line, _col}, _bytes) do
{rest, [:parsed], context}
end
not-implemented
Not implemented
Actions, which imply the use of C code, are not implemented.
Link to this section Summary
Link to this section Functions
parse(binary, opts \\ [])
@spec parse(binary(), keyword()) :: {:ok, [term()], rest, context, line, byte_offset} | {:error, reason, rest, context, line, byte_offset} when line: {pos_integer(), byte_offset}, byte_offset: pos_integer(), rest: binary(), reason: String.t(), context: map()
Parses the given binary
as parse.
Returns {:ok, [token], rest, context, position, byte_offset}
or
{:error, reason, rest, context, line, byte_offset}
where position
describes the location of the parse (start position) as {line, offset_to_start_of_line}
.
To column where the error occurred can be inferred from byte_offset - offset_to_start_of_line
.
options
Options
:byte_offset
- the byte offset for the whole binary, defaults to 0:line
- the line and the byte offset into that line, defaults to{1, byte_offset}
:context
- the initial context value. It will be converted to a map