GithubHelp home page GithubHelp logo

paco's Introduction

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.

paco's People

Contributors

palkan avatar skryukov avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.