hammer/src/bindings/ruby/README.md

73 lines
1.3 KiB
Markdown
Raw Normal View History

2013-11-10 15:13:15 +01:00
# hammer-parser
Ruby bindings for [hammer](https://github.com/UpstandingHackers/hammer), a parsing library.
## Notes
* I called the gem `hammer-parser`, since there already is a [gem named `hammer`](https://rubygems.org/gems/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
```ruby
parser = Hammer::Parser.build {
token 'Hello '
choice {
token 'Mom'
token 'Dad'
}
token '!'
}
```
Also possible:
```ruby
parser = Hammer::ParserBuilder.new
.token('Hello ')
2013-11-15 14:21:20 +01:00
.choice(Hammer::Parser.token('Mom'), Hammer::Parser.token('Dad'))
2013-11-10 15:13:15 +01:00
.token('!')
.build
```
More like hammer in C:
```ruby
h = Hammer::Parser
2013-11-15 14:21:20 +01:00
parser = h.sequence(h.token('Hello '), h.choice(h.token('Mom'), h.token('Dad')), h.token('!'))
2013-11-10 15:13:15 +01:00
```
### Parsing
```ruby
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.