mustermann 0.1.0 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +472 -321
- data/bench/capturing.rb +4 -4
- data/bench/regexp.rb +21 -0
- data/lib/mustermann/ast/boundaries.rb +44 -0
- data/lib/mustermann/ast/compiler.rb +13 -7
- data/lib/mustermann/ast/expander.rb +45 -14
- data/lib/mustermann/ast/node.rb +69 -6
- data/lib/mustermann/ast/param_scanner.rb +20 -0
- data/lib/mustermann/ast/parser.rb +140 -22
- data/lib/mustermann/ast/pattern.rb +68 -21
- data/lib/mustermann/ast/template_generator.rb +28 -0
- data/lib/mustermann/ast/transformer.rb +32 -24
- data/lib/mustermann/ast/translator.rb +24 -10
- data/lib/mustermann/ast/validation.rb +10 -6
- data/lib/mustermann/caster.rb +116 -0
- data/lib/mustermann/composite.rb +101 -0
- data/lib/mustermann/expander.rb +204 -0
- data/lib/mustermann/identity.rb +56 -0
- data/lib/mustermann/mapper.rb +94 -0
- data/lib/mustermann/pattern.rb +193 -13
- data/lib/mustermann/pattern_cache.rb +49 -0
- data/lib/mustermann/regexp.rb +1 -0
- data/lib/mustermann/regexp_based.rb +19 -2
- data/lib/mustermann/regular.rb +29 -0
- data/lib/mustermann/router/rack.rb +47 -0
- data/lib/mustermann/router/simple.rb +142 -0
- data/lib/mustermann/router.rb +9 -0
- data/lib/mustermann/simple_match.rb +5 -0
- data/lib/mustermann/sinatra.rb +22 -5
- data/lib/mustermann/to_pattern.rb +50 -0
- data/lib/mustermann/version.rb +1 -1
- data/lib/mustermann.rb +102 -19
- data/mustermann.gemspec +2 -12
- data/spec/ast_spec.rb +14 -0
- data/spec/composite_spec.rb +147 -0
- data/spec/expander_spec.rb +120 -0
- data/spec/identity_spec.rb +44 -0
- data/spec/mapper_spec.rb +83 -0
- data/spec/mustermann_spec.rb +50 -11
- data/spec/pattern_spec.rb +34 -7
- data/spec/regexp_based_spec.rb +1 -1
- data/spec/regular_spec.rb +61 -0
- data/spec/router/rack_spec.rb +39 -0
- data/spec/router/simple_spec.rb +32 -0
- data/spec/simple_match_spec.rb +1 -1
- data/spec/sinatra_spec.rb +197 -9
- data/spec/to_pattern_spec.rb +69 -0
- metadata +44 -146
- data/.gitignore +0 -18
- data/.rspec +0 -2
- data/.travis.yml +0 -7
- data/.yardopts +0 -1
- data/Gemfile +0 -2
- data/LICENSE +0 -22
- data/Rakefile +0 -6
- data/internals.md +0 -64
- data/lib/mustermann/ast/tree_renderer.rb +0 -29
- data/lib/mustermann/rails.rb +0 -17
- data/lib/mustermann/shell.rb +0 -22
- data/lib/mustermann/simple.rb +0 -35
- data/lib/mustermann/template.rb +0 -47
- data/spec/rails_spec.rb +0 -521
- data/spec/shell_spec.rb +0 -108
- data/spec/simple_spec.rb +0 -236
- data/spec/support/coverage.rb +0 -18
- data/spec/support/env.rb +0 -6
- data/spec/support/expand_matcher.rb +0 -27
- data/spec/support/match_matcher.rb +0 -39
- data/spec/support/pattern.rb +0 -28
- data/spec/support.rb +0 -5
- data/spec/template_spec.rb +0 -814
data/bench/capturing.rb
CHANGED
|
@@ -7,14 +7,14 @@ require 'addressable/template'
|
|
|
7
7
|
|
|
8
8
|
|
|
9
9
|
Mustermann.register(:regexp, Class.new(Mustermann::RegexpBased) {
|
|
10
|
-
def compile(
|
|
11
|
-
Regexp.new(string)
|
|
10
|
+
def compile(**options)
|
|
11
|
+
Regexp.new(@string)
|
|
12
12
|
end
|
|
13
13
|
}, load: false)
|
|
14
14
|
|
|
15
15
|
Mustermann.register(:addressable, Class.new(Mustermann::RegexpBased) {
|
|
16
|
-
def compile(
|
|
17
|
-
Addressable::Template.new(string)
|
|
16
|
+
def compile(**options)
|
|
17
|
+
Addressable::Template.new(@string)
|
|
18
18
|
end
|
|
19
19
|
}, load: false)
|
|
20
20
|
|
data/bench/regexp.rb
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
require 'benchmark'
|
|
2
|
+
|
|
3
|
+
puts " atomic vs normal segments ".center(52, '=')
|
|
4
|
+
|
|
5
|
+
types = {
|
|
6
|
+
normal: /\A\/(?:a|%61)\/(?<b>[^\/\?#]+)(?:\/(?<c>[^\/\?#]+))?\Z/,
|
|
7
|
+
atomic: /\A\/(?:a|%61)\/(?<b>(?>[^\/\?#]+))(?:\/(?<c>(?>[^\/\?#]+)))?\Z/
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
Benchmark.bmbm do |x|
|
|
11
|
+
types.each do |name, regexp|
|
|
12
|
+
string = "/a/" << ?a * 10000 << "/" << ?a * 5000
|
|
13
|
+
fail unless regexp.match(string)
|
|
14
|
+
string << "/"
|
|
15
|
+
fail if regexp.match(string)
|
|
16
|
+
|
|
17
|
+
x.report name.to_s do
|
|
18
|
+
100.times { regexp.match(string) }
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
end
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
require 'mustermann/ast/translator'
|
|
2
|
+
|
|
3
|
+
module Mustermann
|
|
4
|
+
module AST
|
|
5
|
+
# Make sure #start and #stop is set on every node and within its parents #start and #stop.
|
|
6
|
+
# @!visibility private
|
|
7
|
+
class Boundaries < Translator
|
|
8
|
+
# @return [Mustermann::AST::Node] the ast passed as first argument
|
|
9
|
+
# @!visibility private
|
|
10
|
+
def self.set_boundaries(ast, string: nil, start: 0, stop: string.length)
|
|
11
|
+
new.translate(ast, start, stop)
|
|
12
|
+
ast
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
translate(:node) do |start, stop|
|
|
16
|
+
t.set_boundaries(node, start, stop)
|
|
17
|
+
t(payload, node.start, node.stop)
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
translate(:with_look_ahead) do |start, stop|
|
|
21
|
+
t.set_boundaries(node, start, stop)
|
|
22
|
+
t(head, node.start, node.stop)
|
|
23
|
+
t(payload, node.start, node.stop)
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
translate(Array) do |start, stop|
|
|
27
|
+
each do |subnode|
|
|
28
|
+
t(subnode, start, stop)
|
|
29
|
+
start = subnode.stop
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
translate(Object) { |*| node }
|
|
34
|
+
|
|
35
|
+
# Checks that a node is within the given boundaries.
|
|
36
|
+
# @!visibility private
|
|
37
|
+
def set_boundaries(node, start, stop)
|
|
38
|
+
node.start = start if node.start.nil? or node.start < start
|
|
39
|
+
node.stop = node.start + node.min_size if node.stop.nil? or node.stop < node.start
|
|
40
|
+
node.stop = stop if node.stop > stop
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
end
|
|
@@ -15,6 +15,10 @@ module Mustermann
|
|
|
15
15
|
translate(:optional) { |**o| "(?:%s)?" % t(payload, **o) }
|
|
16
16
|
translate(:char) { |**o| t.encoded(payload, **o) }
|
|
17
17
|
|
|
18
|
+
translate :union do |**options|
|
|
19
|
+
"(?:%s)" % payload.map { |e| "(?:%s)" % t(e, **options) }.join(?|)
|
|
20
|
+
end
|
|
21
|
+
|
|
18
22
|
translate :expression do |greedy: true, **options|
|
|
19
23
|
t(payload, allow_reserved: operator.allow_reserved, greedy: greedy && !operator.allow_reserved,
|
|
20
24
|
parametric: operator.parametric, separator: operator.separator, **options)
|
|
@@ -53,14 +57,14 @@ module Mustermann
|
|
|
53
57
|
end
|
|
54
58
|
|
|
55
59
|
private
|
|
56
|
-
def qualified(string, greedy: true, **options)
|
|
60
|
+
def qualified(string, greedy: true, **options) "#{string}#{qualifier || "+#{?? unless greedy}"}" end
|
|
57
61
|
def with_lookahead(string, lookahead: nil, **options) lookahead ? "(?:(?!#{lookahead})#{string})" : string end
|
|
58
62
|
def from_hash(hash, **options) pattern(capture: hash[name.to_sym], **options) end
|
|
59
63
|
def from_array(array, **options) Regexp.union(*array.map { |e| pattern(capture: e, **options) }) end
|
|
60
64
|
def from_symbol(symbol, **options) qualified(with_lookahead("[[:#{symbol}:]]", **options), **options) end
|
|
61
65
|
def from_string(string, **options) Regexp.new(string.chars.map { |c| t.encoded(c, **options) }.join) end
|
|
62
66
|
def from_nil(**options) qualified(with_lookahead(default(**options), **options), **options) end
|
|
63
|
-
def default(**options) "[^/\\?#]"
|
|
67
|
+
def default(**options) constraint || "[^/\\?#]" end
|
|
64
68
|
end
|
|
65
69
|
|
|
66
70
|
# @!visibility private
|
|
@@ -69,7 +73,7 @@ module Mustermann
|
|
|
69
73
|
# splats are always non-greedy
|
|
70
74
|
# @!visibility private
|
|
71
75
|
def pattern(**options)
|
|
72
|
-
".*?"
|
|
76
|
+
constraint || ".*?"
|
|
73
77
|
end
|
|
74
78
|
end
|
|
75
79
|
|
|
@@ -120,7 +124,10 @@ module Mustermann
|
|
|
120
124
|
return Regexp.escape(char) unless uri_decode
|
|
121
125
|
encoded = escape(char, escape: /./)
|
|
122
126
|
list = [escape(char), encoded.downcase, encoded.upcase].uniq.map { |c| Regexp.escape(c) }
|
|
123
|
-
|
|
127
|
+
if char == " "
|
|
128
|
+
list << encoded('+') if space_matches_plus
|
|
129
|
+
list << " "
|
|
130
|
+
end
|
|
124
131
|
"(?:%s)" % list.join("|")
|
|
125
132
|
end
|
|
126
133
|
|
|
@@ -139,9 +146,8 @@ module Mustermann
|
|
|
139
146
|
#
|
|
140
147
|
# @!visibility private
|
|
141
148
|
def compile(ast, except: nil, **options)
|
|
142
|
-
except
|
|
143
|
-
|
|
144
|
-
Regexp.new(expression)
|
|
149
|
+
except &&= "(?!#{translate(except, no_captures: true, **options)}\\Z)"
|
|
150
|
+
Regexp.new("#{except}#{translate(ast, **options)}")
|
|
145
151
|
end
|
|
146
152
|
end
|
|
147
153
|
|
|
@@ -10,21 +10,25 @@ module Mustermann
|
|
|
10
10
|
class Expander < Translator
|
|
11
11
|
raises ExpandError
|
|
12
12
|
|
|
13
|
-
translate Array do
|
|
13
|
+
translate Array do |*args|
|
|
14
14
|
inject(t.pattern) do |pattern, element|
|
|
15
|
-
t.add_to(pattern, t(element))
|
|
15
|
+
t.add_to(pattern, t(element, *args))
|
|
16
16
|
end
|
|
17
17
|
end
|
|
18
18
|
|
|
19
|
-
translate :capture do
|
|
20
|
-
t.for_capture(node)
|
|
19
|
+
translate :capture do |**options|
|
|
20
|
+
t.for_capture(node, **options)
|
|
21
21
|
end
|
|
22
22
|
|
|
23
23
|
translate :named_splat, :splat do
|
|
24
24
|
t.pattern + t.for_capture(node)
|
|
25
25
|
end
|
|
26
26
|
|
|
27
|
-
translate :
|
|
27
|
+
translate :expression do
|
|
28
|
+
t(payload, allow_reserved: operator.allow_reserved)
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
translate :root, :group do
|
|
28
32
|
t(payload)
|
|
29
33
|
end
|
|
30
34
|
|
|
@@ -46,11 +50,15 @@ module Mustermann
|
|
|
46
50
|
nested
|
|
47
51
|
end
|
|
48
52
|
|
|
53
|
+
translate :union do
|
|
54
|
+
payload.map { |e| t(e) }.inject(:+)
|
|
55
|
+
end
|
|
56
|
+
|
|
49
57
|
# helper method for captures
|
|
50
58
|
# @!visibility private
|
|
51
|
-
def for_capture(node)
|
|
59
|
+
def for_capture(node, **options)
|
|
52
60
|
name = node.name.to_sym
|
|
53
|
-
pattern('%s', name, name => /(?!#{pattern_for(node)})./)
|
|
61
|
+
pattern('%s', name, name => /(?!#{pattern_for(node, **options)})./)
|
|
54
62
|
end
|
|
55
63
|
|
|
56
64
|
# maps sorted key list to sprintf patterns and filters
|
|
@@ -59,11 +67,18 @@ module Mustermann
|
|
|
59
67
|
@mappings ||= {}
|
|
60
68
|
end
|
|
61
69
|
|
|
70
|
+
# all the known keys
|
|
71
|
+
# @!visibility private
|
|
72
|
+
def keys
|
|
73
|
+
@keys ||= []
|
|
74
|
+
end
|
|
75
|
+
|
|
62
76
|
# add a tree for expansion
|
|
63
77
|
# @!visibility private
|
|
64
78
|
def add(ast)
|
|
65
79
|
translate(ast).each do |keys, pattern, filter|
|
|
66
|
-
|
|
80
|
+
self.keys.concat(keys).uniq!
|
|
81
|
+
mappings[keys.sort] ||= [keys, pattern, filter]
|
|
67
82
|
end
|
|
68
83
|
end
|
|
69
84
|
|
|
@@ -75,10 +90,26 @@ module Mustermann
|
|
|
75
90
|
|
|
76
91
|
# @see Mustermann::Pattern#expand
|
|
77
92
|
# @!visibility private
|
|
78
|
-
def expand(
|
|
79
|
-
|
|
93
|
+
def expand(values)
|
|
94
|
+
values = values.each_with_object({}){ |(key, value), new_hash|
|
|
95
|
+
new_hash[value.instance_of?(Array) ? [key] * value.length : key] = value }
|
|
96
|
+
keys, pattern, filters = mappings.fetch(values.keys.flatten.sort) { error_for(values) }
|
|
80
97
|
filters.each { |key, filter| values[key] &&= escape(values[key], also_escape: filter) }
|
|
81
|
-
pattern % values.values_at(*keys)
|
|
98
|
+
pattern % (values[keys] || values.values_at(*keys))
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
# @see Mustermann::Pattern#expandable?
|
|
102
|
+
# @!visibility private
|
|
103
|
+
def expandable?(values)
|
|
104
|
+
values = values.keys if values.respond_to? :keys
|
|
105
|
+
values = values.sort if values.respond_to? :sort
|
|
106
|
+
mappings.include? values
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
# @see Mustermann::Expander#with_rest
|
|
110
|
+
# @!visibility private
|
|
111
|
+
def expandable_keys(keys)
|
|
112
|
+
mappings.keys.select { |k| (k - keys).empty? }.max_by(&:size) || keys
|
|
82
113
|
end
|
|
83
114
|
|
|
84
115
|
# helper method for raising an error for unexpandable values
|
|
@@ -91,17 +122,17 @@ module Mustermann
|
|
|
91
122
|
# @see Mustermann::AST::Translator#expand
|
|
92
123
|
# @!visibility private
|
|
93
124
|
def escape(string, *args)
|
|
94
|
-
# URI::Parser is pretty slow, let's not
|
|
125
|
+
# URI::Parser is pretty slow, let's not send every string to it, even if it's unnecessary
|
|
95
126
|
string =~ /\A\w*\Z/ ? string : super
|
|
96
127
|
end
|
|
97
128
|
|
|
98
|
-
# Turns a sprintf pattern into our secret internal data
|
|
129
|
+
# Turns a sprintf pattern into our secret internal data structure.
|
|
99
130
|
# @!visibility private
|
|
100
131
|
def pattern(string = "", *keys, **filters)
|
|
101
132
|
[[keys, string, filters]]
|
|
102
133
|
end
|
|
103
134
|
|
|
104
|
-
# Creates the product of two of our secret internal data
|
|
135
|
+
# Creates the product of two of our secret internal data structures.
|
|
105
136
|
# @!visibility private
|
|
106
137
|
def add_to(list, result)
|
|
107
138
|
list << [[], ""] if list.empty?
|
data/lib/mustermann/ast/node.rb
CHANGED
|
@@ -4,14 +4,23 @@ module Mustermann
|
|
|
4
4
|
# @!visibility private
|
|
5
5
|
class Node
|
|
6
6
|
# @!visibility private
|
|
7
|
-
attr_accessor :payload
|
|
7
|
+
attr_accessor :payload, :start, :stop
|
|
8
8
|
|
|
9
9
|
# @!visibility private
|
|
10
10
|
# @param [Symbol] name of the node
|
|
11
11
|
# @return [Class] factory for the node
|
|
12
12
|
def self.[](name)
|
|
13
|
-
@names
|
|
14
|
-
@names
|
|
13
|
+
@names ||= {}
|
|
14
|
+
@names[name] ||= begin
|
|
15
|
+
const_name = constant_name(name)
|
|
16
|
+
Object.const_get(const_name) if Object.const_defined?(const_name)
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
# Turns a class name into a node identifier.
|
|
21
|
+
# @!visibility private
|
|
22
|
+
def self.type
|
|
23
|
+
name[/[^:]+$/].split(/(?<=.)(?=[A-Z])/).map(&:downcase).join(?_).to_sym
|
|
15
24
|
end
|
|
16
25
|
|
|
17
26
|
# @!visibility private
|
|
@@ -36,6 +45,12 @@ module Mustermann
|
|
|
36
45
|
self.payload = payload
|
|
37
46
|
end
|
|
38
47
|
|
|
48
|
+
# @!visibility private
|
|
49
|
+
def is_a?(type)
|
|
50
|
+
type = Node[type] if type.is_a? Symbol
|
|
51
|
+
super(type)
|
|
52
|
+
end
|
|
53
|
+
|
|
39
54
|
# Double dispatch helper for reading from the buffer into the payload.
|
|
40
55
|
# @!visibility private
|
|
41
56
|
def parse
|
|
@@ -58,8 +73,38 @@ module Mustermann
|
|
|
58
73
|
yield(self) unless called
|
|
59
74
|
end
|
|
60
75
|
|
|
76
|
+
# @return [Integer] length of the substring
|
|
77
|
+
# @!visibility private
|
|
78
|
+
def length
|
|
79
|
+
stop - start if start and stop
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
# @return [Integer] minimum size for a node
|
|
83
|
+
# @!visibility private
|
|
84
|
+
def min_size
|
|
85
|
+
0
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
# Turns a class name into a node identifier.
|
|
89
|
+
# @!visibility private
|
|
90
|
+
def type
|
|
91
|
+
self.class.type
|
|
92
|
+
end
|
|
93
|
+
|
|
61
94
|
# @!visibility private
|
|
62
95
|
class Capture < Node
|
|
96
|
+
# @see Mustermann::AST::Compiler::Capture#default
|
|
97
|
+
# @!visibility private
|
|
98
|
+
attr_accessor :constraint
|
|
99
|
+
|
|
100
|
+
# @see Mustermann::AST::Compiler::Capture#qualified
|
|
101
|
+
# @!visibility private
|
|
102
|
+
attr_accessor :qualifier
|
|
103
|
+
|
|
104
|
+
# @see Mustermann::AST::Pattern#map_param
|
|
105
|
+
# @!visibility private
|
|
106
|
+
attr_accessor :convert
|
|
107
|
+
|
|
63
108
|
# @see Mustermann::AST::Node#parse
|
|
64
109
|
# @!visibility private
|
|
65
110
|
def parse
|
|
@@ -73,6 +118,11 @@ module Mustermann
|
|
|
73
118
|
|
|
74
119
|
# @!visibility private
|
|
75
120
|
class Char < Node
|
|
121
|
+
# @return [Integer] minimum size for a node
|
|
122
|
+
# @!visibility private
|
|
123
|
+
def min_size
|
|
124
|
+
1
|
|
125
|
+
end
|
|
76
126
|
end
|
|
77
127
|
|
|
78
128
|
# AST node for template expressions.
|
|
@@ -83,13 +133,21 @@ module Mustermann
|
|
|
83
133
|
end
|
|
84
134
|
|
|
85
135
|
# @!visibility private
|
|
86
|
-
class
|
|
136
|
+
class Composition < Node
|
|
87
137
|
# @!visibility private
|
|
88
138
|
def initialize(payload = nil, **options)
|
|
89
139
|
super(Array(payload), **options)
|
|
90
140
|
end
|
|
91
141
|
end
|
|
92
142
|
|
|
143
|
+
# @!visibility private
|
|
144
|
+
class Group < Composition
|
|
145
|
+
end
|
|
146
|
+
|
|
147
|
+
# @!visibility private
|
|
148
|
+
class Union < Composition
|
|
149
|
+
end
|
|
150
|
+
|
|
93
151
|
# @!visibility private
|
|
94
152
|
class Optional < Node
|
|
95
153
|
end
|
|
@@ -107,13 +165,17 @@ module Mustermann
|
|
|
107
165
|
root = new
|
|
108
166
|
root.pattern = string
|
|
109
167
|
root.parse(&block)
|
|
110
|
-
#root.transform
|
|
111
168
|
root
|
|
112
169
|
end
|
|
113
170
|
end
|
|
114
171
|
|
|
115
172
|
# @!visibility private
|
|
116
173
|
class Separator < Node
|
|
174
|
+
# @return [Integer] minimum size for a node
|
|
175
|
+
# @!visibility private
|
|
176
|
+
def min_size
|
|
177
|
+
1
|
|
178
|
+
end
|
|
117
179
|
end
|
|
118
180
|
|
|
119
181
|
# @!visibility private
|
|
@@ -145,7 +207,8 @@ module Mustermann
|
|
|
145
207
|
attr_accessor :head, :at_end
|
|
146
208
|
|
|
147
209
|
# @!visibility private
|
|
148
|
-
def initialize(payload, at_end)
|
|
210
|
+
def initialize(payload, at_end, **options)
|
|
211
|
+
super(**options)
|
|
149
212
|
self.head, *self.payload = Array(payload)
|
|
150
213
|
self.at_end = at_end
|
|
151
214
|
end
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
require 'mustermann/ast/translator'
|
|
2
|
+
|
|
3
|
+
module Mustermann
|
|
4
|
+
module AST
|
|
5
|
+
# Scans an AST for param converters.
|
|
6
|
+
# @!visibility private
|
|
7
|
+
# @see Mustermann::AST::Pattern#to_templates
|
|
8
|
+
class ParamScanner < Translator
|
|
9
|
+
# @!visibility private
|
|
10
|
+
def self.scan_params(ast)
|
|
11
|
+
new.translate(ast)
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
translate(:node) { t(payload) }
|
|
15
|
+
translate(Array) { map { |e| t(e) }.inject(:merge) }
|
|
16
|
+
translate(Object) { {} }
|
|
17
|
+
translate(:capture) { convert ? { name => convert } : {} }
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
end
|
|
@@ -9,11 +9,10 @@ module Mustermann
|
|
|
9
9
|
# @!visibility private
|
|
10
10
|
class Parser
|
|
11
11
|
# @param [String] string to be parsed
|
|
12
|
-
# @param [Hash] **options parse options
|
|
13
12
|
# @return [Mustermann::AST::Node] parse tree for string
|
|
14
13
|
# @!visibility private
|
|
15
|
-
def self.parse(string)
|
|
16
|
-
new.parse(string)
|
|
14
|
+
def self.parse(string, **options)
|
|
15
|
+
new(**options).parse(string)
|
|
17
16
|
end
|
|
18
17
|
|
|
19
18
|
# Defines another grammar rule for first character.
|
|
@@ -32,24 +31,29 @@ module Mustermann
|
|
|
32
31
|
#
|
|
33
32
|
# @see Mustermann::Sinatra
|
|
34
33
|
# @!visibility private
|
|
35
|
-
def self.suffix(pattern = /./, &block)
|
|
34
|
+
def self.suffix(pattern = /./, after: :node, &block)
|
|
36
35
|
@suffix ||= []
|
|
37
|
-
@suffix << [pattern, block] if block
|
|
36
|
+
@suffix << [pattern, after, block] if block
|
|
38
37
|
@suffix
|
|
39
38
|
end
|
|
40
39
|
|
|
41
40
|
# @!visibility private
|
|
42
|
-
attr_reader :buffer, :string
|
|
41
|
+
attr_reader :buffer, :string, :pattern
|
|
43
42
|
|
|
44
43
|
extend Forwardable
|
|
45
|
-
def_delegators :buffer, :eos?, :getch
|
|
44
|
+
def_delegators :buffer, :eos?, :getch, :pos
|
|
45
|
+
|
|
46
|
+
# @!visibility private
|
|
47
|
+
def initialize(pattern: nil, **options)
|
|
48
|
+
@pattern = pattern
|
|
49
|
+
end
|
|
46
50
|
|
|
47
51
|
# @param [String] string to be parsed
|
|
48
52
|
# @return [Mustermann::AST::Node] parse tree for string
|
|
49
53
|
# @!visibility private
|
|
50
54
|
def parse(string)
|
|
51
55
|
@string = string
|
|
52
|
-
@buffer = StringScanner.new(string)
|
|
56
|
+
@buffer = ::StringScanner.new(string)
|
|
53
57
|
node(:root, string) { read unless eos? }
|
|
54
58
|
end
|
|
55
59
|
|
|
@@ -60,11 +64,13 @@ module Mustermann
|
|
|
60
64
|
# @return [Mustermann::AST::Node]
|
|
61
65
|
# @!visibility private
|
|
62
66
|
def node(type, *args, &block)
|
|
63
|
-
type
|
|
64
|
-
|
|
67
|
+
type = Node[type] unless type.respond_to? :new
|
|
68
|
+
start = pos
|
|
69
|
+
node = block ? type.parse(*args, &block) : type.new(*args)
|
|
70
|
+
min_size(start, pos, node)
|
|
65
71
|
end
|
|
66
72
|
|
|
67
|
-
# Create a node for a character we don't have an
|
|
73
|
+
# Create a node for a character we don't have an explicit rule for.
|
|
68
74
|
#
|
|
69
75
|
# @param [String] char the character
|
|
70
76
|
# @return [Mustermann::AST::Node] the node
|
|
@@ -77,20 +83,35 @@ module Mustermann
|
|
|
77
83
|
# @return [Mustermann::AST::Node] next element
|
|
78
84
|
# @!visibility private
|
|
79
85
|
def read
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
86
|
+
start = pos
|
|
87
|
+
char = getch
|
|
88
|
+
method = "read %p" % char
|
|
89
|
+
element= respond_to?(method) ? send(method, char) : default_node(char)
|
|
90
|
+
min_size(start, pos, element)
|
|
83
91
|
read_suffix(element)
|
|
84
92
|
end
|
|
85
93
|
|
|
94
|
+
# sets start on node to start if it's not set to a lower value.
|
|
95
|
+
# sets stop on node to stop if it's not set to a higher value.
|
|
96
|
+
# @return [Mustermann::AST::Node] the node passed as third argument
|
|
97
|
+
# @!visibility private
|
|
98
|
+
def min_size(start, stop, node)
|
|
99
|
+
stop ||= start
|
|
100
|
+
start ||= stop
|
|
101
|
+
node.start = start unless node.start and node.start < start
|
|
102
|
+
node.stop = stop unless node.stop and node.stop > stop
|
|
103
|
+
node
|
|
104
|
+
end
|
|
105
|
+
|
|
86
106
|
# Checks for a potential suffix on the buffer.
|
|
87
107
|
# @param [Mustermann::AST::Node] element node without suffix
|
|
88
108
|
# @return [Mustermann::AST::Node] node with suffix
|
|
89
109
|
# @!visibility private
|
|
90
110
|
def read_suffix(element)
|
|
91
|
-
self.class.suffix.inject(element) do |
|
|
92
|
-
next
|
|
93
|
-
instance_exec(payload,
|
|
111
|
+
self.class.suffix.inject(element) do |ele, (regexp, after, callback)|
|
|
112
|
+
next ele unless ele.is_a?(after) and payload = scan(regexp)
|
|
113
|
+
content = instance_exec(payload, ele, &callback)
|
|
114
|
+
min_size(element.start, pos, content)
|
|
94
115
|
end
|
|
95
116
|
end
|
|
96
117
|
|
|
@@ -103,8 +124,25 @@ module Mustermann
|
|
|
103
124
|
# @return [String, MatchData, nil]
|
|
104
125
|
# @!visibility private
|
|
105
126
|
def scan(regexp)
|
|
127
|
+
match_buffer(:scan, regexp)
|
|
128
|
+
end
|
|
129
|
+
|
|
130
|
+
# Wrapper around {StringScanner#check} that turns strings into escaped
|
|
131
|
+
# regular expressions and returns a MatchData if the regexp has any
|
|
132
|
+
# named captures.
|
|
133
|
+
#
|
|
134
|
+
# @param [Regexp, String] regexp
|
|
135
|
+
# @see StringScanner#check
|
|
136
|
+
# @return [String, MatchData, nil]
|
|
137
|
+
# @!visibility private
|
|
138
|
+
def check(regexp)
|
|
139
|
+
match_buffer(:check, regexp)
|
|
140
|
+
end
|
|
141
|
+
|
|
142
|
+
# @!visibility private
|
|
143
|
+
def match_buffer(method, regexp)
|
|
106
144
|
regexp = Regexp.new(Regexp.escape(regexp)) unless regexp.is_a? Regexp
|
|
107
|
-
string = buffer.
|
|
145
|
+
string = buffer.public_send(method, regexp)
|
|
108
146
|
regexp.names.any? ? regexp.match(string) : string
|
|
109
147
|
end
|
|
110
148
|
|
|
@@ -115,20 +153,100 @@ module Mustermann
|
|
|
115
153
|
# @return [String, MatchData] the match
|
|
116
154
|
# @raise [Mustermann::ParseError] if expectation wasn't met
|
|
117
155
|
# @!visibility private
|
|
118
|
-
def expect(regexp, **options)
|
|
119
|
-
scan(regexp)|| unexpected(**options)
|
|
156
|
+
def expect(regexp, char: nil, **options)
|
|
157
|
+
scan(regexp) || unexpected(char, **options)
|
|
158
|
+
end
|
|
159
|
+
|
|
160
|
+
# Allows to read a string inside brackets. It does not expect the string
|
|
161
|
+
# to start with an opening bracket.
|
|
162
|
+
#
|
|
163
|
+
# @example
|
|
164
|
+
# buffer.string = "fo<o>>ba<r>"
|
|
165
|
+
# read_brackets(?<, ?>) # => "fo<o>"
|
|
166
|
+
# buffer.rest # => "ba<r>"
|
|
167
|
+
#
|
|
168
|
+
# @!visibility private
|
|
169
|
+
def read_brackets(open, close, char: nil, escape: ?\\, quote: false, **options)
|
|
170
|
+
result = ""
|
|
171
|
+
escape = false if escape.nil?
|
|
172
|
+
while current = getch
|
|
173
|
+
case current
|
|
174
|
+
when close then return result
|
|
175
|
+
when open then result << open << read_brackets(open, close) << close
|
|
176
|
+
when escape then result << escape << getch
|
|
177
|
+
else result << current
|
|
178
|
+
end
|
|
179
|
+
end
|
|
180
|
+
unexpected(char, **options)
|
|
181
|
+
end
|
|
182
|
+
|
|
183
|
+
|
|
184
|
+
# Reads an argument string of the format arg1,args2,key:value
|
|
185
|
+
#
|
|
186
|
+
# @!visibility private
|
|
187
|
+
def read_args(key_separator, close, separator: ?,, symbol_keys: true, **options)
|
|
188
|
+
list, map = [], {}
|
|
189
|
+
while buffer.peek(1) != close
|
|
190
|
+
scan(separator)
|
|
191
|
+
entries = read_list(close, separator, separator: key_separator, **options)
|
|
192
|
+
case entries.size
|
|
193
|
+
when 1 then list += entries
|
|
194
|
+
when 2 then map[symbol_keys ? entries.first.to_sym : entries.first] = entries.last
|
|
195
|
+
else unexpected(key_separator)
|
|
196
|
+
end
|
|
197
|
+
buffer.pos -= 1
|
|
198
|
+
end
|
|
199
|
+
expect(close)
|
|
200
|
+
[list, map]
|
|
201
|
+
end
|
|
202
|
+
|
|
203
|
+
# Reads a separated list with the ability to quote, escape and add spaces.
|
|
204
|
+
#
|
|
205
|
+
# @!visibility private
|
|
206
|
+
def read_list(*close, separator: ?,, escape: ?\\, quotes: [?", ?'], ignore: " ", **options)
|
|
207
|
+
result = []
|
|
208
|
+
while current = getch
|
|
209
|
+
element = result.empty? ? result : result.last
|
|
210
|
+
case current
|
|
211
|
+
when *close then return result
|
|
212
|
+
when ignore then nil # do nothing
|
|
213
|
+
when separator then result << ""
|
|
214
|
+
when escape then element << getch
|
|
215
|
+
when *quotes then element << read_escaped(current, escape: escape)
|
|
216
|
+
else element << current
|
|
217
|
+
end
|
|
218
|
+
end
|
|
219
|
+
unexpected(current, **options)
|
|
220
|
+
end
|
|
221
|
+
|
|
222
|
+
# Read a string until a terminating character, ignoring escaped versions of said character.
|
|
223
|
+
#
|
|
224
|
+
# @!visibility private
|
|
225
|
+
def read_escaped(close, escape: ?\\, **options)
|
|
226
|
+
result = ""
|
|
227
|
+
while current = getch
|
|
228
|
+
case current
|
|
229
|
+
when close then return result
|
|
230
|
+
when escape then result << getch
|
|
231
|
+
else result << current
|
|
232
|
+
end
|
|
233
|
+
end
|
|
234
|
+
unexpected(current, **options)
|
|
120
235
|
end
|
|
121
236
|
|
|
122
237
|
# Helper for raising an exception for an unexpected character.
|
|
123
238
|
# Will read character from buffer if buffer is passed in.
|
|
124
239
|
#
|
|
125
|
-
# @param [String, nil] char the
|
|
240
|
+
# @param [String, nil] char the unexpected character
|
|
126
241
|
# @raise [Mustermann::ParseError, Exception]
|
|
127
242
|
# @!visibility private
|
|
128
|
-
def unexpected(char =
|
|
243
|
+
def unexpected(char = nil, exception: ParseError)
|
|
244
|
+
char ||= getch
|
|
129
245
|
char = "space" if char == " "
|
|
130
246
|
raise exception, "unexpected #{char || "end of string"} while parsing #{string.inspect}"
|
|
131
247
|
end
|
|
248
|
+
|
|
249
|
+
private :match_buffer
|
|
132
250
|
end
|
|
133
251
|
|
|
134
252
|
private_constant :Parser
|