Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/lalloni/seared
Lightweight PEG-based text parsing in Golang without code generation.
https://github.com/lalloni/seared
go golang parser parser-library parsing parsing-expression-grammar peg text
Last synced: 4 days ago
JSON representation
Lightweight PEG-based text parsing in Golang without code generation.
- Host: GitHub
- URL: https://github.com/lalloni/seared
- Owner: lalloni
- License: bsd-2-clause
- Created: 2017-02-14T22:42:18.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2017-03-21T11:00:21.000Z (over 7 years ago)
- Last Synced: 2024-06-21T19:54:14.761Z (5 months ago)
- Topics: go, golang, parser, parser-library, parsing, parsing-expression-grammar, peg, text
- Language: Go
- Size: 43 KB
- Stars: 4
- Watchers: 5
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
Seared
======*Seared* is a Go library targeted at allowing easy implementation of text parsers based on powerful [Parsing Expression Grammars](https://en.wikipedia.org/wiki/Parsing_expression_grammar) without the hassle of parser generation steps, while trying to be easy to use, lightweight and appropriate for high-performance parsing needs.
So, how does a grammar definition look in practice? The well-known example "calculator" grammar can be defined like this:
```go
func Number(r *seared.Rules) seared.Rule {
return r.Rule(func() seared.Rule {
return r.OneOrMore(r.Range('0', '9'))
})
}func Factor(r *seared.Rules) seared.Rule {
return r.Rule(func() seared.Rule {
return r.Choice(Number(r), r.Sequence(r.Rune('('), Sum(r), r.Rune(')')))
})
}func Term(r *seared.Rules) seared.Rule {
return r.Rule(func() seared.Rule {
return r.Sequence(Factor(r), r.ZeroOrMore(r.Any("*/"), Factor(r)))
})
}func Sum(r *seared.Rules) seared.Rule {
return r.Rule(func() seared.Rule {
return r.Sequence(Term(r), r.ZeroOrMore(r.Any("+-"), Term(r)))
})
}func Operation(r *seared.Rules) seared.Rule {
return r.Rule(func() seared.Rule {
return r.Sequence(Sum(r), r.End())
})
}func Calculator() *seared.Parser {
return seared.NewParser(Operation)
}
```And this is how that parser could be directly used for syntax recognizing:
```go
parser := Calculator()
success := parser.Recognize("2+1*3+4*(2-1)")
```License
=======*Seared* is released under the [Simplified BSD License](./LICENSE) which can be found at the root of this project.