hammer/src/bindings/ruby
2014-01-08 20:18:07 +01:00
..
ext/hammer_ext Add first version of ruby bindings. 2014-01-08 20:18:07 +01:00
lib Duplicate string argument to token parser. 2014-01-08 20:18:07 +01:00
.gitignore Add first version of ruby bindings. 2014-01-08 20:18:07 +01:00
Gemfile Need rake-compiler gem. 2014-01-08 20:18:07 +01:00
hammer-parser.gemspec Add first version of ruby bindings. 2014-01-08 20:18:07 +01:00
Rakefile Add first version of ruby bindings. 2014-01-08 20:18:07 +01:00
README.md Remove Hammer::Parser subclasses. 2014-01-08 20:18:07 +01:00

hammer-parser

Ruby bindings for hammer, a parsing library.

Notes

  • I called the gem hammer-parser, since there already is a gem named hammer.

  • C extension not really needed at the moment, if we don't mind hardcoding the token types in the ruby code.

Development

  1. cd src/bindings/ruby.

  2. Run bundle install to install dependencies.

  3. Run rake compile to compile the C extension.

  4. Run irb -I ./lib -r hammer to open irb with hammer loaded.

Installation

TODO

Examples

Building a parser

parser = Hammer::Parser.build {
  token 'Hello '
  choice {
    token 'Mom'
    token 'Dad'
  }
  token '!'
}

Also possible:

parser = Hammer::ParserBuilder.new
  .token('Hello ')
  .choice(Hammer::Parser.token('Mom'), Hammer::Parser.token('Dad'))
  .token('!')
  .build

More like hammer in C:

h = Hammer::Parser
parser = h.sequence(h.token('Hello '), h.choice(h.token('Mom'), h.token('Dad')), h.token('!'))

Parsing

parser.parse 'Hello Mom!'
=> true
parser.parse 'Hello Someone!'
=> false

Currently you only get true or false depending on whether the parse succeeded or failed. There's no way to access the parsed data yet.