Implement h_attr_bool.

This commit is contained in:
Jakob Rath 2013-12-17 00:06:29 +01:00 committed by Dan Hirsch
parent d343b0c8f0
commit 905183cddc
3 changed files with 17 additions and 1 deletions

View file

@ -46,3 +46,8 @@ $r = parser.parse 'abcdefgh'
p $r[:ast][:data][:seq].elements.map {|e| e[:data][:uint]} p $r[:ast][:data][:seq].elements.map {|e| e[:data][:uint]}
# or: # or:
p $r.ast.data.map(&:data) p $r.ast.data.map(&:data)
h = Hammer::Parser
parser = h.many(h.attr_bool(h.uint8) { |r| r.ast.data <= 100 })
p parser.parse('abcdefgh').ast.data.map(&:data)

View file

@ -169,7 +169,9 @@ module Hammer
callback :HAction, [HParseResult.by_ref], HParsedToken.by_ref callback :HAction, [HParseResult.by_ref], HParsedToken.by_ref
attach_function :h_action, [:h_parser, :HAction], :h_parser attach_function :h_action, [:h_parser, :HAction], :h_parser
#attach_function :h_attr_bool, [:h_parser, ...], :h_parser
callback :HPredicate, [HParseResult.by_ref], :bool
attach_function :h_attr_bool, [:h_parser, :HPredicate], :h_parser
# free the parse result # free the parse result
attach_function :h_parse_result_free, [HParseResult.by_ref], :void attach_function :h_parse_result_free, [HParseResult.by_ref], :void

View file

@ -54,6 +54,15 @@ module Hammer
return Hammer::Parser.new(:action, h_parser, [parser, action]) return Hammer::Parser.new(:action, h_parser, [parser, action])
end end
# Can pass the predicate either as a Proc in second parameter, or as block.
def self.attr_bool(parser, predicate=nil, &block)
predicate = block if predicate.nil?
raise ArgumentError, 'no predicate' if predicate.nil?
h_parser = Hammer::Internal.h_attr_bool(parser.h_parser, predicate)
return Hammer::Parser.new(:attr_bool, h_parser, [parser, predicate])
end
def self.token(string) def self.token(string)
# Need to copy string to a memory buffer (not just string.dup) # Need to copy string to a memory buffer (not just string.dup)
# * Original string might be modified, this must not affect existing tokens # * Original string might be modified, this must not affect existing tokens