File: express.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 (36 lines) | stat: -rw-r--r-- 987 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
28
29
30
31
32
33
34
35
36
require 'mustermann/ast/pattern'

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

    on(nil, ??, ?+, ?*, ?)) { |c| unexpected(c) }
    on(?:) { |c| node(:capture) { scan(/\w+/) } }
    on(?() { |c| node(:splat, constraint: read_brackets(?(, ?))) }

    suffix ??, after: :capture do |char, element|
      unexpected(char) unless element.is_a? :capture
      node(:optional, element)
    end

    suffix ?*, after: :capture do |match, element|
      node(:named_splat, element.name)
    end

    suffix ?+, after: :capture do |match, element|
      node(:named_splat, element.name, constraint: ".+")
    end

    suffix ?(, after: :capture do |match, element|
      element.constraint = read_brackets(?(, ?))
      element
    end
  end
end