File: pyramid.rb

package info (click to toggle)
ruby-mustermann19 0.4.3%2Bgit20160621-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 756 kB
  • ctags: 445
  • sloc: ruby: 7,197; makefile: 3
file content (27 lines) | stat: -rw-r--r-- 707 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
require 'mustermann/ast/pattern'

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

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

    on(?{) do |char|
      name       = expect(/\w+/, char: char)
      constraint = read_brackets(?{, ?}) if scan(?:)
      expect(?}) unless constraint
      node(:capture, name, constraint: constraint)
    end

    on(?*) do |char|
      node(:named_splat, expect(/\w+$/, char: char), convert: -> e { e.split(?/) })
    end
  end
end