Created
July 25, 2016 19:28
-
-
Save ravinggenius/c0ed1ea9a7535c8b78b8198f442d743b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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