Skip to content
/ paco Public

Paco is a parser combinator library inspired by Haskell's Parsec and Parsimmon.

License

Notifications You must be signed in to change notification settings

ruby-next/paco

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ecdb0d2 Â· Dec 27, 2023

History

26 Commits
Aug 23, 2023
Dec 18, 2021
Dec 21, 2021
Dec 12, 2021
Apr 9, 2022
Dec 27, 2023
Dec 27, 2023
Apr 9, 2022
Aug 29, 2023
Dec 12, 2021
Dec 19, 2021
Aug 29, 2023
Apr 9, 2022
Dec 12, 2021
Aug 23, 2023
Dec 20, 2021
Apr 9, 2022
Aug 29, 2023

Repository files navigation

Paco

Gem Version Build

Paco is a parser combinator library inspired by Haskell's Parsec and Parsimmon.

"But I don't need to write another JSON parser or a new language, why do I need your library then?"

Well, most probably you don't. But I can think of rare cases when you do. Say, you need to write a validation for git branch names.

You can go with easy-peasy regex:

branch_name_regex = /^(?!\/|.*(?:[\/.]\.|\/\/|@{|\\|\.lock$|[\/.]$))[^\040\177 ~^:?*\[]+$/

branch_name_regex.match?("feature/branch-validation")

With Paco, you can go with a little more verbose version of that rule:

module BranchNameParser
  extend Paco

  class << self
    def parse(input)
      parser.parse(input)
    end

    def parser
      lookahead(none_of("/")).next(valid_chars.join)
    end

    def valid_chars
      any_char.not_followed_by(invalid_sequences).at_least(1)
    end
    
    def invalid_sequences
      alt(invalid_chars, invalid_endings)
    end

    def invalid_chars
      alt(
        string("/."),
        string(".."),
        string("//"),
        string("@{"),
        string("\\\\"),
        one_of("\040\177 ~^:?*\\[")
      )
    end

    def invalid_endings
      seq(
        alt(string(".lock"), one_of("/.")),
        eof
      )
    end
  end
end

BranchNameParser.parse("feature/branch-validation")

Easy? Not really, but there is a chance you can read it. 😅

See API documentation, examples and specs for more info on usage.

Sponsored by Evil Martians

Installation

Add to your Gemfile:

gem "paco"

And then run bundle install.

Development

After checking out the repo, run bin/setup to install dependencies. Then, run bundle exec rspec to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run bundle exec rake install.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/skryukov/paco.

Alternatives

  • parslet - A small (but featureful) PEG based parser library.
  • parsby — Parser combinator library for Ruby inspired by Haskell's Parsec.

License

The gem is available as open source under the terms of the MIT License.