Skip to content
/ seared Public

Lightweight PEG-based text parsing in Golang without code generation.

License

Notifications You must be signed in to change notification settings

lalloni/seared

Repository files navigation

Seared

Seared is a Go library targeted at allowing easy implementation of text parsers based on powerful Parsing Expression Grammars 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:

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:

parser := Calculator()
success := parser.Recognize("2+1*3+4*(2-1)")

License

Seared is released under the Simplified BSD License which can be found at the root of this project.

Releases

No releases published

Packages

No packages published