Sha256: 0f818aa5f4cfc4a63850b80475c29d0fdd314837bfeb4a18a7ac000a632c2af1

Contents?: true

Size: 1.02 KB

Versions: 3

Compression:

Stored size: 1.02 KB

Contents

# frozen_string_literal: true

require 'mustermann'
require 'mustermann/ast/pattern'

module Mustermann
  # Grape style pattern implementation.
  #
  # @example
  #   Mustermann.new('/:foo', type: :grape) === '/bar' # => true
  #
  # @see Mustermann::Pattern
  # @see file:README.md#grape Syntax description in the README
  class Grape < AST::Pattern
    register :grape

    on(nil, '?', ')') { |c| unexpected(c) }

    on('*')  { |_c| scan(/\w+/) ? node(:named_splat, buffer.matched) : node(:splat) }
    on(':')  { |_c| node(:capture, constraint: "[^/\\?#\.]") { scan(/\w+/) } }
    on('\\') { |_c| node(:char, expect(/./)) }
    on('(')  { |_c| node(:optional, node(:group) { read unless scan(')') }) }
    on('|')  { |_c| node(:or) }

    on '{' do |_char|
      type = scan('+') ? :named_splat : :capture
      name = expect(/[\w\.]+/)
      type = :splat if (type == :named_splat) && (name == 'splat')
      expect('}')
      node(type, name)
    end

    suffix '?' do |_char, element|
      node(:optional, element)
    end
  end
end

Version data entries

3 entries across 3 versions & 2 rubygems

Version Path
mustermann-grape-1.0.2 lib/mustermann/grape.rb
grape-extra_validators-2.0.0 vendor/bundle/ruby/2.6.0/gems/mustermann-grape-1.0.1/lib/mustermann/grape.rb
mustermann-grape-1.0.1 lib/mustermann/grape.rb