raabro
A very dumb PEG parser library.
Son to aabro, grandson to neg, grand-grandson to parslet. There is also a javascript version jaabro.
a sample parser/rewriter
You use raabro by providing the parsing rules, then some rewrite rules.
The parsing rules make use of the raabro basic parsers seq
, alt
, str
, rex
, eseq
, …
The rewrite rules match names passed as first argument to the basic parsers to rewrite the resulting parse trees.
require 'raabro' module Fun include Raabro # parse # # Last function is the root, "i" stands for "input". def pa(i); rex(nil, i, /\(\s*/); end def pz(i); rex(nil, i, /\)\s*/); end def com(i); rex(nil, i, /,\s*/); end def num(i); rex(:num, i, /-?[0-9]+\s*/); end def args(i); eseq(nil, i, :pa, :exp, :com, :pz); end def funame(i); rex(nil, i, /[a-z][a-z0-9]*/); end def fun(i); seq(:fun, i, :funame, :args); end def exp(i); alt(nil, i, :fun, :num); end # rewrite # # Names above (:num, :fun, ...) get a rewrite_xxx function. # "t" stands for "tree". # # The trees with a nil name are handled by rewrite_(tree) a default # rewrite function def rewrite_num(t); t.string.to_i; end def rewrite_fun(t) [ t.children[0].string ] + t.children[1].odd_children.collect { |a| rewrite(a) } end end p Fun.parse('mul(1, 2)') # => ["mul", 1, 2] p Fun.parse('mul(1, add(-2, 3))') # => ["mul", 1, ["add", -2, 3]] p Fun.parse('mul (1, 2)') # => nil (doesn't accept a space after the function name)
This sample is available at: doc/readme0.rb.
custom rewrite()
By default, a parser gets a rewrite(t)
that looks at the parse tree node names and calls the corresponding rewrite_{node_name}()
.
It’s OK to provide a custom rewrite(t)
function.
module Hello include Raabro def hello(i); str(:hello, i, 'hello'); end def rewrite(t) [ :ok, t.string ] end end
basic parsers
One makes a parser by composing basic parsers, for example:
def args(i); eseq(:args, i, :pa, :exp, :com, :pz); end def funame(i); rex(:funame, i, /[a-z][a-z0-9]*/); end def fun(i); seq(:fun, i, :funame, :args); end
where the fun
parser is a sequence combining the funame
parser then the args
one. :fun
(the first argument to the basic parser seq
) will be the name of the resulting (local) parse tree.
Below is a list of the basic parsers provided by Raabro.
The first parameter to the basic parser is the name used by rewrite rules.
The second parameter is a Raabro::Input
instance, mostly a wrapped string.
def str(name, input, string) # matching a string def rex(name, input, regex_or_string) # matching a regexp # no need for ^ or \A, checks the match occurs at current offset def seq(name, input, *parsers) # a sequence of parsers def alt(name, input, *parsers) # tries the parsers returns as soon as one succeeds def altg(name, input, *parsers) # tries all the parsers, returns with the longest match def rep(name, input, parser, min, max=0) # repeats the the wrapped parser def ren(name, input, parser) # renames the output of the wrapped parser def jseq(name, input, eltpa, seppa) # seq(name, input, eltpa, seppa, eltpa, seppa, eltpaa, seppa, ...) def eseq(name, input, startpa, eltpa, seppa, endpa) # seq(name, input, startpa, eltpa, seppa, eltpa, seppa, ..., endpa)
LICENSE
MIT, see LICENSE.txt