lib/ebnf/peg/parser.rb in ebnf-2.1.3 vs lib/ebnf/peg/parser.rb in ebnf-2.2.0

- old
+ new

@@ -53,10 +53,11 @@ def start_handlers; (@start_handlers ||= {}); end def start_options; (@start_hoptions ||= {}); end def production_handlers; (@production_handlers ||= {}); end def terminal_handlers; (@terminal_handlers ||= {}); end def terminal_regexps; (@terminal_regexps ||= {}); end + def terminal_options; (@terminal_options ||= {}); end ## # Defines the pattern for a terminal node and a block to be invoked # when ther terminal is encountered. If the block is missing, the # value of the terminal will be placed on the input hash to be returned @@ -70,13 +71,12 @@ # @param [Regexp] regexp (nil) # Pattern used to scan for this terminal, # defaults to the expression defined in the associated rule. # If unset, the terminal rule is used for matching. # @param [Hash] options - # @option options [Hash{String => String}] :map ({}) - # A mapping from terminals, in lower-case form, to - # their canonical value + # @option options [Boolean] :unescape + # Cause strings and codepoints to be unescaped. # @yield [value, prod] # @yieldparam [String] value # The scanned terminal value. # @yieldparam [Symbol] prod # A symbol indicating the production which referenced this terminal @@ -84,10 +84,11 @@ # Block passed to initialization for yielding to calling parser. # Should conform to the yield specs for #initialize def terminal(term, regexp = nil, **options, &block) terminal_regexps[term] = regexp if regexp terminal_handlers[term] = block if block_given? + terminal_options[term] = options.freeze end ## # Defines a production called at the beggining of a particular production # with data from previous production along with data defined for the @@ -98,10 +99,12 @@ # The rule name # @param [Hash{Symbol => Object}] options # Options which are returned from {Parser#onStart}. # @option options [Boolean] :as_hash (false) # If the production is a `seq`, causes the value to be represented as a single hash, rather than an array of individual hashes for each sub-production. Note that this is not always advisable due to the possibility of repeated productions within the sequence. + # @option options[:upper, :lower] :insensitive_strings + # Perform case-insensitive match of strings not defined as terminals, and map to either upper or lower case. # @yield [data, block] # @yieldparam [Hash] data # A Hash defined for the current production, during :start # may be initialized with data to pass to further productions, # during :finish, it contains data placed by earlier productions @@ -180,10 +183,12 @@ # Identify the symbol of the starting rule with `start`. # @param [Hash{Symbol => Object}] options # @option options[Integer] :high_water passed to lexer # @option options [Logger] :logger for errors/progress/debug. # @option options[Integer] :low_water passed to lexer + # @option options[Boolean] :seq_hash (false) + # If `true`, sets the default for the value sent to a production handler that is for a `seq` to a hash composed of the flattened consitutent hashes that are otherwise provided. # @option options [Symbol, Regexp] :whitespace # Symbol of whitespace rule (defaults to `@pass`), or a regular expression # for eating whitespace between non-terminal rules (strongly encouraged). # @yield [context, *data] # Yields to return data to parser @@ -193,10 +198,11 @@ # Data specific to the call # @return [Object] AST resulting from parse # @raise [Exception] Raises exceptions for parsing errors # or errors raised during processing callbacks. Internal # errors are raised using {Error}. + # @todo FIXME implement seq_hash def parse(input = nil, start = nil, rules = nil, **options, &block) start ||= options[:start] rules ||= options[:rules] || [] @rules = rules.inject({}) {|memo, rule| memo.merge(rule.sym => rule)} @packrat = {} @@ -465,11 +471,20 @@ ## # Find a regular expression defined for a terminal # # @param [Symbol] sym # @return [Regexp] - def find_terminal_regexp(sym) + def terminal_regexp(sym) self.class.terminal_regexps[sym] + end + + ## + # Find a regular expression defined for a terminal + # + # @param [Symbol] sym + # @return [Regexp] + def terminal_options(sym) + self.class.terminal_options[sym] end ## # Record furthest failure. #