Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

. refactor: postfix -> consume_all

  • Loading branch information...
commit 3ad24aab001b63af07415a52efe45d113f3c45c3 1 parent affbe7e
@kschiess authored
View
2  Gemfile.lock
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
- parslet (1.4.0)
+ parslet (1.4.1)
blankslate (~> 2.0)
GEM
View
4 example/ignore.rb
@@ -10,8 +10,8 @@ def initialize(parslet)
def to_s_inner(prec)
@parslet.to_s(prec)
end
- def try(source, context, postfix)
- success, value = result = @parslet.try(source, context, postfix)
+ def try(source, context, consume_all)
+ success, value = result = @parslet.try(source, context, consume_all)
return succ(nil) if success
return result
View
4 lib/parslet/atoms/alternative.rb
@@ -30,9 +30,9 @@ def |(parslet)
self.class.new(*@alternatives + [parslet])
end
- def try(source, context, postfix)
+ def try(source, context, consume_all)
errors = alternatives.map { |a|
- success, value = result = a.apply(source, context, postfix)
+ success, value = result = a.apply(source, context, consume_all)
return result if success
# Aggregate all errors
View
44 lib/parslet/atoms/base.rb
@@ -65,9 +65,9 @@ def parse(io, options={})
#
# @return [<Boolean, Object>] Result of the parse. If the first member is
# true, the parse has succeeded.
- def setup_and_apply(source, error_reporter, demand_postfix)
+ def setup_and_apply(source, error_reporter, consume_all)
context = Parslet::Atoms::Context.new(error_reporter)
- apply(source, context, demand_postfix)
+ apply(source, context, consume_all)
end
# Calls the #try method of this parslet. Success consumes input, error will
@@ -75,31 +75,31 @@ def setup_and_apply(source, error_reporter, demand_postfix)
#
# @param source [Parslet::Source] source to read input from
# @param context [Parslet::Atoms::Context] context to use for the parsing
- # @param postfix [Boolean] true if this atom is in postfix position
- # for the current parse.
- def apply(source, context, postfix=false)
+ # @param consume_all [Boolean] true if the current parse must consume
+ # all input by itself.
+ def apply(source, context, consume_all=false)
old_pos = source.pos
- success, value = result = context.try_with_cache(self, source, postfix)
+ success, value = result = context.try_with_cache(self, source, consume_all)
if success
- # If a postfix parse was made and doesn't result in the consumption of
- # all the input, that is considered an error.
- # old_pos = source.pos
- # Parslet::Cause.format(
- # source, old_pos,
- # "Don't know what to do with #{source.consume(10).to_s.inspect}").
- # raise(Parslet::UnconsumedInput)
+ # If a consume_all parse was made and doesn't result in the consumption
+ # of all the input, that is considered an error.
- offending_pos = source.pos
- offending_input = source.consume(10)
- source.pos = offending_pos
- return context.err(
- self,
- source,
- "Don't know what to do with #{offending_input.to_s.inspect}"
- ) if postfix && source.chars_left>0
+ if consume_all && source.chars_left>0
+ # Read 10 characters ahead. Why ten? I don't know.
+ offending_pos = source.pos
+ offending_input = source.consume(10)
+ source.pos = offending_pos
+
+ return context.err(
+ self,
+ source,
+ "Don't know what to do with #{offending_input.to_s.inspect}"
+ )
+ end
+ # Looks like the parse was successful after all. Don't rewind the input.
return result
end
@@ -111,7 +111,7 @@ def apply(source, context, postfix=false)
# Override this in your Atoms::Base subclasses to implement parsing
# behaviour.
#
- def try(source, context, postfix)
+ def try(source, context, consume_all)
raise NotImplementedError, \
"Atoms::Base doesn't have behaviour, please implement #try(source, context)."
end
View
4 lib/parslet/atoms/context.rb
@@ -22,12 +22,12 @@ def initialize(reporter=Parslet::ErrorReporter::Tree.new)
# were consumed by a successful parse. Imitation of such a parse must
# advance the input pos by the same amount of bytes.
#
- def try_with_cache(obj, source, postfix)
+ def try_with_cache(obj, source, consume_all)
beg = source.pos
# Not in cache yet? Return early.
unless entry = lookup(obj, beg)
- result = obj.try(source, self, postfix)
+ result = obj.try(source, self, consume_all)
set obj, beg, [result, source.pos-beg]
return result
View
4 lib/parslet/atoms/entity.rb
@@ -17,8 +17,8 @@ def initialize(name, &block)
@block = block
end
- def try(source, context, postfix)
- parslet.apply(source, context, postfix)
+ def try(source, context, consume_all)
+ parslet.apply(source, context, consume_all)
end
def parslet
View
4 lib/parslet/atoms/lookahead.rb
@@ -21,10 +21,10 @@ def initialize(bound_parslet, positive=true)
}
end
- def try(source, context, postfix)
+ def try(source, context, consume_all)
pos = source.pos
- success, value = bound_parslet.apply(source, context, postfix)
+ success, value = bound_parslet.apply(source, context, consume_all)
if positive
return succ(nil) if success
View
4 lib/parslet/atoms/named.rb
@@ -13,8 +13,8 @@ def initialize(parslet, name)
@parslet, @name = parslet, name
end
- def apply(source, context, postfix)
- success, value = result = parslet.apply(source, context, postfix)
+ def apply(source, context, consume_all)
+ success, value = result = parslet.apply(source, context, consume_all)
return result unless success
succ(
View
2  lib/parslet/atoms/re.rb
@@ -20,7 +20,7 @@ def initialize(match)
}
end
- def try(source, context, postfix)
+ def try(source, context, consume_all)
return succ(source.consume(1)) if source.matches?(re)
# No string could be read
View
14 lib/parslet/atoms/repetition.rb
@@ -20,7 +20,7 @@ def initialize(parslet, min, max, tag=:repetition)
}
end
- def try(source, context, postfix)
+ def try(source, context, consume_all)
occ = 0
accum = [@tag] # initialize the result array with the tag (for flattening)
start_pos = source.pos
@@ -50,17 +50,19 @@ def try(source, context, postfix)
start_pos,
[break_on]) if occ < min
- # Postfix is true, that means that we're inside the part of the parser that
- # should consume the input completely. Repetition failing here means
+ # consume_all is true, that means that we're inside the part of the parser
+ # that should consume the input completely. Repetition failing here means
# probably that we didn't.
- # We have a special clause to create an error here because otherwise
- # break_on would get thrown away. It turns out, that contains very
+ #
+ # We have a special clause to create an error here because otherwise
+ # break_on would get thrown away. It turns out, that contains very
# interesting information in a lot of cases.
+ #
return context.err(
self,
source,
@error_msgs[:unconsumed],
- [break_on]) if postfix && source.chars_left>0
+ [break_on]) if consume_all && source.chars_left>0
return succ(accum)
end
View
6 lib/parslet/atoms/sequence.rb
@@ -19,10 +19,10 @@ def >>(parslet)
self.class.new(* @parslets+[parslet])
end
- def try(source, context, postfix)
+ def try(source, context, consume_all)
succ([:sequence]+parslets.map.each_with_index { |p, idx|
- child_postfix = postfix && (idx == parslets.size-1)
- success, value = p.apply(source, context, child_postfix)
+ child_consume_all = consume_all && (idx == parslets.size-1)
+ success, value = p.apply(source, context, child_consume_all)
unless success
return context.err(self, source, @error_msgs[:failed], [value])
View
2  lib/parslet/atoms/str.rb
@@ -17,7 +17,7 @@ def initialize(str)
}
end
- def try(source, context, postfix)
+ def try(source, context, consume_all)
return succ(source.consume(@len)) if source.matches?(str)
# Input ending early:
View
4 lib/parslet/parser.rb
@@ -57,8 +57,8 @@ def root(name)
end
end
- def try(source, context, postfix)
- root.try(source, context, postfix)
+ def try(source, context, consume_all)
+ root.try(source, context, consume_all)
end
def to_s_inner(prec)
Please sign in to comment.
Something went wrong with that request. Please try again.