Skip to content

Instantly share code, notes, and snippets.

@ravinggenius
Created July 25, 2016 19:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ravinggenius/c0ed1ea9a7535c8b78b8198f442d743b to your computer and use it in GitHub Desktop.
Save ravinggenius/c0ed1ea9a7535c8b78b8198f442d743b to your computer and use it in GitHub Desktop.
require 'parslet'
module Rip::Parser::Atoms
class Pattern < Parslet::Atoms::Base
attr_reader :pattern
def initialize(pattern)
super()
@pattern = pattern
end
def try(source, context, consume_all)
match_count = source.match(pattern)
case
when match_count
succ(source.consume(match_count))
when source.chars_left.zero?
context.err(self, source, 'Premature end of input')
else
context.err(self, source, "Failed to match #{pattern}")
end
end
end
end
module Parslet
def pattern(regular_expression)
Rip::Parser::Atoms::Pattern.new(regular_expression)
end
module_function :pattern
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment