trenni 3.8.0 → 3.13.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (88) hide show
  1. checksums.yaml +4 -4
  2. data/bake/trenni/entities.rb +57 -0
  3. data/bake/trenni/parsers.rb +66 -0
  4. data/ext/tmp/x86_64-linux/lib/trenni/trenni.so +0 -0
  5. data/ext/tmp/x86_64-linux/trenni/2.4.0/Makefile +263 -0
  6. data/ext/tmp/x86_64-linux/trenni/2.4.0/escape.o +0 -0
  7. data/ext/tmp/x86_64-linux/trenni/2.4.0/markup.o +0 -0
  8. data/ext/tmp/x86_64-linux/trenni/2.4.0/mkmf.log +108 -0
  9. data/ext/tmp/x86_64-linux/trenni/2.4.0/tag.o +0 -0
  10. data/ext/tmp/x86_64-linux/trenni/2.4.0/template.o +0 -0
  11. data/ext/tmp/x86_64-linux/trenni/2.4.0/trenni.o +0 -0
  12. data/ext/tmp/x86_64-linux/trenni/2.4.0/trenni.so +0 -0
  13. data/ext/tmp/x86_64-linux/trenni/2.7.0/Makefile +266 -0
  14. data/ext/tmp/x86_64-linux/trenni/2.7.0/escape.o +0 -0
  15. data/ext/tmp/x86_64-linux/trenni/2.7.0/markup.o +0 -0
  16. data/ext/tmp/x86_64-linux/trenni/2.7.0/mkmf.log +113 -0
  17. data/ext/tmp/x86_64-linux/trenni/2.7.0/query.o +0 -0
  18. data/ext/tmp/x86_64-linux/trenni/2.7.0/tag.o +0 -0
  19. data/ext/tmp/x86_64-linux/trenni/2.7.0/template.o +0 -0
  20. data/ext/tmp/x86_64-linux/trenni/2.7.0/trenni.o +0 -0
  21. data/ext/tmp/x86_64-linux/trenni/2.7.0/trenni.so +0 -0
  22. data/ext/tmp/x86_64-linux/trenni/2.7.1/Makefile +266 -0
  23. data/ext/tmp/x86_64-linux/trenni/2.7.1/escape.o +0 -0
  24. data/ext/tmp/x86_64-linux/trenni/2.7.1/markup.o +0 -0
  25. data/ext/tmp/x86_64-linux/trenni/2.7.1/mkmf.log +113 -0
  26. data/ext/tmp/x86_64-linux/trenni/2.7.1/query.o +0 -0
  27. data/ext/tmp/x86_64-linux/trenni/2.7.1/tag.o +0 -0
  28. data/ext/tmp/x86_64-linux/trenni/2.7.1/template.o +0 -0
  29. data/ext/tmp/x86_64-linux/trenni/2.7.1/trenni.o +0 -0
  30. data/ext/tmp/x86_64-linux/trenni/2.7.1/trenni.so +0 -0
  31. data/ext/trenni/markup.c +85 -85
  32. data/ext/trenni/markup.rl +11 -11
  33. data/ext/trenni/query.c +619 -0
  34. data/ext/trenni/query.h +6 -0
  35. data/ext/trenni/query.rl +82 -0
  36. data/ext/trenni/tag.c +8 -6
  37. data/ext/trenni/template.c +57 -57
  38. data/ext/trenni/template.rl +4 -4
  39. data/ext/trenni/trenni.c +9 -1
  40. data/ext/trenni/trenni.h +8 -3
  41. data/lib/trenni.rb +2 -0
  42. data/lib/trenni/builder.rb +79 -15
  43. data/lib/trenni/entities.rb +2082 -2084
  44. data/lib/trenni/entities.trenni +1 -3
  45. data/lib/trenni/{parse_error.rb → error.rb} +4 -1
  46. data/lib/trenni/fallback/markup.rb +1622 -1576
  47. data/lib/trenni/fallback/markup.rl +2 -2
  48. data/lib/trenni/fallback/markup.rl.dot +278 -0
  49. data/lib/trenni/fallback/markup.rl.pdf +0 -0
  50. data/lib/trenni/fallback/query.rb +565 -0
  51. data/lib/trenni/fallback/query.rl +105 -0
  52. data/lib/trenni/fallback/query.rl.dot +54 -0
  53. data/lib/trenni/fallback/query.rl.pdf +0 -0
  54. data/lib/trenni/fallback/template.rb +756 -748
  55. data/lib/trenni/fallback/template.rl +1 -1
  56. data/lib/trenni/fallback/template.rl.dot +270 -0
  57. data/lib/trenni/fallback/template.rl.pdf +0 -0
  58. data/lib/trenni/native.rb +1 -1
  59. data/lib/trenni/parsers.rb +1 -0
  60. data/lib/trenni/query.rb +94 -0
  61. data/lib/trenni/reference.rb +125 -0
  62. data/lib/trenni/strings.rb +15 -4
  63. data/lib/trenni/template.rb +23 -22
  64. data/lib/trenni/trenni.so +0 -0
  65. data/lib/trenni/uri.rb +1 -0
  66. data/lib/trenni/version.rb +1 -1
  67. data/parsers/trenni/query.rl +23 -0
  68. data/spec/trenni/builder_spec.rb +103 -60
  69. data/spec/trenni/markup_performance_spec.rb +14 -2
  70. data/spec/trenni/parsers_performance_spec.rb +31 -0
  71. data/spec/trenni/query_spec.rb +51 -0
  72. data/spec/trenni/reference_spec.rb +87 -0
  73. data/spec/trenni/template_spec/builder.trenni +2 -2
  74. metadata +91 -59
  75. data/.gitignore +0 -19
  76. data/.rspec +0 -5
  77. data/.simplecov +0 -9
  78. data/.travis.yml +0 -23
  79. data/Gemfile +0 -21
  80. data/README.md +0 -312
  81. data/Rakefile +0 -21
  82. data/benchmark/call_vs_yield.rb +0 -52
  83. data/benchmark/interpolation_vs_concat.rb +0 -30
  84. data/benchmark/io_vs_string.rb +0 -91
  85. data/entities.json +0 -2233
  86. data/tasks/entities.rake +0 -34
  87. data/tasks/parsers.rake +0 -44
  88. data/trenni.gemspec +0 -36
@@ -67,7 +67,7 @@
67
67
  include template "trenni/template.rl";
68
68
  }%%
69
69
 
70
- require_relative '../parse_error'
70
+ require_relative '../error'
71
71
 
72
72
  module Trenni
73
73
  module Fallback
@@ -0,0 +1,270 @@
1
+ digraph template {
2
+ rankdir=LR;
3
+ node [ shape = point ];
4
+ ENTRY;
5
+ en_21;
6
+ en_32;
7
+ en_43;
8
+ eof_1;
9
+ eof_2;
10
+ eof_3;
11
+ eof_4;
12
+ eof_5;
13
+ eof_6;
14
+ eof_7;
15
+ eof_8;
16
+ eof_9;
17
+ eof_10;
18
+ eof_11;
19
+ eof_14;
20
+ eof_15;
21
+ eof_16;
22
+ eof_17;
23
+ eof_18;
24
+ eof_19;
25
+ eof_20;
26
+ eof_32;
27
+ eof_33;
28
+ eof_34;
29
+ eof_35;
30
+ eof_36;
31
+ eof_37;
32
+ eof_38;
33
+ eof_39;
34
+ eof_40;
35
+ eof_41;
36
+ eof_42;
37
+ eof_44;
38
+ eof_45;
39
+ eof_46;
40
+ eof_47;
41
+ eof_48;
42
+ node [ shape = circle, height = 0.2 ];
43
+ err_14 [ label=""];
44
+ err_15 [ label=""];
45
+ err_18 [ label=""];
46
+ node [ fixedsize = true, height = 0.65, shape = doublecircle ];
47
+ 43;
48
+ 44;
49
+ 45;
50
+ 46;
51
+ 47;
52
+ 48;
53
+ 49;
54
+ 50;
55
+ 51;
56
+ 52;
57
+ 53;
58
+ 54;
59
+ 55;
60
+ 56;
61
+ node [ shape = circle ];
62
+ 1 -> 45 [ label = "'\\n' / te" ];
63
+ 1 -> 2 [ label = "'#'" ];
64
+ 1 -> 3 [ label = "'<'" ];
65
+ 1 -> 1 [ label = "DEF" ];
66
+ 2 -> 43 [ label = "'{' / lag2, initts" ];
67
+ 2 -> 1 [ label = "DEF" ];
68
+ 3 -> 43 [ label = "'?' / lag2, initts" ];
69
+ 3 -> 1 [ label = "DEF" ];
70
+ 4 -> 43 [ label = "'{' / lag6, initts" ];
71
+ 4 -> 44 [ label = "DEF / te" ];
72
+ 5 -> 43 [ label = "'?' / lag6, initts" ];
73
+ 5 -> 44 [ label = "DEF / te" ];
74
+ 6 -> 7 [ label = "'?'" ];
75
+ 6 -> 44 [ label = "DEF / te" ];
76
+ 7 -> 8 [ label = "'r'" ];
77
+ 7 -> 43 [ label = "DEF / lag6, initts" ];
78
+ 8 -> 9 [ label = "'\\t'..'\\r', SP / instruction_begin" ];
79
+ 8 -> 43 [ label = "DEF / lag6, initts" ];
80
+ 9 -> 10 [ label = "'?' / instruction_end" ];
81
+ 9 -> 9 [ label = "DEF" ];
82
+ 10 -> 11 [ label = "'>'" ];
83
+ 10 -> 9 [ label = "DEF" ];
84
+ 11 -> 11 [ label = "'\\t', '\\v'..'\\r', SP" ];
85
+ 11 -> 43 [ label = "'\\n' / last1, initts" ];
86
+ 11 -> 43 [ label = "DEF / switch, initts" ];
87
+ 12 -> 47 [ label = "'{'" ];
88
+ 12 -> 44 [ label = "DEF / te" ];
89
+ 13 -> 14 [ label = "'?'" ];
90
+ 13 -> 44 [ label = "DEF / te" ];
91
+ 14 -> err_14 [ label = "0..',', '/', ';'..'@', '['..'^', '`', '{'..127 / instruction_error" ];
92
+ 14 -> 18 [ label = "'r'" ];
93
+ 14 -> 15 [ label = "DEF" ];
94
+ 15 -> err_15 [ label = "0..'\\b', 14..31, '!'..',', '/', ';'..'@', '['..'^', '`', '{'..127 / instruction_error" ];
95
+ 15 -> 16 [ label = "'\\t'..'\\r', SP" ];
96
+ 15 -> 15 [ label = "DEF" ];
97
+ 16 -> 17 [ label = "'?'" ];
98
+ 16 -> 16 [ label = "DEF" ];
99
+ 17 -> 43 [ label = "'>' / last5, initts" ];
100
+ 17 -> 16 [ label = "DEF" ];
101
+ 18 -> err_18 [ label = "0..'\\b', 14..31, '!'..',', '/', ';'..'@', '['..'^', '`', '{'..127 / instruction_error" ];
102
+ 18 -> 19 [ label = "'\\t'..'\\r', SP / instruction_begin" ];
103
+ 18 -> 15 [ label = "DEF" ];
104
+ 19 -> 20 [ label = "'?' / instruction_end" ];
105
+ 19 -> 19 [ label = "DEF" ];
106
+ 20 -> 48 [ label = "'>' / te, store3" ];
107
+ 20 -> 19 [ label = "DEF" ];
108
+ 21 -> 22 [ label = "'\"' / initts" ];
109
+ 21 -> 30 [ label = "'''" ];
110
+ 21 -> 21 [ label = "'{' / 17:27, initts" ];
111
+ 21 -> 52 [ label = "'}' / 20:51" ];
112
+ 21 -> 21 [ label = "DEF / initts" ];
113
+ 22 -> 23 [ label = "'\"' / initts" ];
114
+ 22 -> 29 [ label = "'#'" ];
115
+ 22 -> 22 [ label = "DEF / initts" ];
116
+ 23 -> 24 [ label = "'#'" ];
117
+ 23 -> 25 [ label = "''' / initts" ];
118
+ 23 -> 23 [ label = "'{' / 17:27, initts" ];
119
+ 23 -> 50 [ label = "'}' / 20:51" ];
120
+ 23 -> 23 [ label = "DEF / initts" ];
121
+ 24 -> 24 [ label = "'#'" ];
122
+ 24 -> 25 [ label = "''' / initts" ];
123
+ 24 -> 23 [ label = "'{' / 13:33, 17:27, initts" ];
124
+ 24 -> 50 [ label = "'}' / 20:51" ];
125
+ 24 -> 23 [ label = "DEF / initts" ];
126
+ 25 -> 26 [ label = "'\"', ''' / initts" ];
127
+ 25 -> 28 [ label = "'#'" ];
128
+ 25 -> 25 [ label = "DEF / initts" ];
129
+ 26 -> 27 [ label = "'#'" ];
130
+ 26 -> 26 [ label = "'{' / 17:27, initts" ];
131
+ 26 -> 49 [ label = "'}' / 20:51" ];
132
+ 26 -> 26 [ label = "DEF / initts" ];
133
+ 27 -> 27 [ label = "'#'" ];
134
+ 27 -> 26 [ label = "'{' / 13:33, 17:27, initts" ];
135
+ 27 -> 49 [ label = "'}' / 20:51" ];
136
+ 27 -> 26 [ label = "DEF / initts" ];
137
+ 28 -> 26 [ label = "'\"', ''' / initts" ];
138
+ 28 -> 28 [ label = "'#'" ];
139
+ 28 -> 25 [ label = "'{' / 13:33, initts" ];
140
+ 28 -> 25 [ label = "DEF / initts" ];
141
+ 29 -> 23 [ label = "'\"' / initts" ];
142
+ 29 -> 29 [ label = "'#'" ];
143
+ 29 -> 22 [ label = "'{' / 13:33, initts" ];
144
+ 29 -> 22 [ label = "DEF / initts" ];
145
+ 30 -> 31 [ label = "''' / initts" ];
146
+ 30 -> 30 [ label = "DEF" ];
147
+ 31 -> 25 [ label = "'\"' / initts" ];
148
+ 31 -> 31 [ label = "'{' / 17:27, initts" ];
149
+ 31 -> 51 [ label = "'}' / 20:51" ];
150
+ 31 -> 31 [ label = "DEF / initts" ];
151
+ 32 -> 33 [ label = "'\"' / initts" ];
152
+ 32 -> 41 [ label = "'''" ];
153
+ 32 -> 32 [ label = "'{' / 17:27, initts" ];
154
+ 32 -> 56 [ label = "'}' / expression_end, emit_expression, 21:102" ];
155
+ 32 -> 32 [ label = "DEF / initts" ];
156
+ 33 -> 34 [ label = "'\"' / initts" ];
157
+ 33 -> 40 [ label = "'#'" ];
158
+ 33 -> 33 [ label = "DEF / initts" ];
159
+ 34 -> 35 [ label = "'#'" ];
160
+ 34 -> 36 [ label = "''' / initts" ];
161
+ 34 -> 34 [ label = "'{' / 17:27, initts" ];
162
+ 34 -> 54 [ label = "'}' / expression_end, emit_expression, 21:102" ];
163
+ 34 -> 34 [ label = "DEF / initts" ];
164
+ 35 -> 35 [ label = "'#'" ];
165
+ 35 -> 36 [ label = "''' / initts" ];
166
+ 35 -> 34 [ label = "'{' / 13:33, 17:27, initts" ];
167
+ 35 -> 54 [ label = "'}' / expression_end, emit_expression, 21:102" ];
168
+ 35 -> 34 [ label = "DEF / initts" ];
169
+ 36 -> 37 [ label = "'\"', ''' / initts" ];
170
+ 36 -> 39 [ label = "'#'" ];
171
+ 36 -> 36 [ label = "DEF / initts" ];
172
+ 37 -> 38 [ label = "'#'" ];
173
+ 37 -> 37 [ label = "'{' / 17:27, initts" ];
174
+ 37 -> 53 [ label = "'}' / expression_end, emit_expression, 21:102" ];
175
+ 37 -> 37 [ label = "DEF / initts" ];
176
+ 38 -> 38 [ label = "'#'" ];
177
+ 38 -> 37 [ label = "'{' / 13:33, 17:27, initts" ];
178
+ 38 -> 53 [ label = "'}' / expression_end, emit_expression, 21:102" ];
179
+ 38 -> 37 [ label = "DEF / initts" ];
180
+ 39 -> 37 [ label = "'\"', ''' / initts" ];
181
+ 39 -> 39 [ label = "'#'" ];
182
+ 39 -> 36 [ label = "'{' / 13:33, initts" ];
183
+ 39 -> 36 [ label = "DEF / initts" ];
184
+ 40 -> 34 [ label = "'\"' / initts" ];
185
+ 40 -> 40 [ label = "'#'" ];
186
+ 40 -> 33 [ label = "'{' / 13:33, initts" ];
187
+ 40 -> 33 [ label = "DEF / initts" ];
188
+ 41 -> 42 [ label = "''' / initts" ];
189
+ 41 -> 41 [ label = "DEF" ];
190
+ 42 -> 36 [ label = "'\"' / initts" ];
191
+ 42 -> 42 [ label = "'{' / 17:27, initts" ];
192
+ 42 -> 55 [ label = "'}' / expression_end, emit_expression, 21:102" ];
193
+ 42 -> 42 [ label = "DEF / initts" ];
194
+ 43 -> 46 [ label = "'\\t', '\\v'..'\\r', SP / ts, te, store6" ];
195
+ 43 -> 45 [ label = "'\\n' / ts, te" ];
196
+ 43 -> 12 [ label = "'#' / ts" ];
197
+ 43 -> 13 [ label = "'<' / ts" ];
198
+ 43 -> 44 [ label = "DEF / ts, te" ];
199
+ 44 -> 45 [ label = "'\\n' / te" ];
200
+ 44 -> 4 [ label = "'#'" ];
201
+ 44 -> 5 [ label = "'<'" ];
202
+ 44 -> 44 [ label = "DEF / te" ];
203
+ 45 -> 45 [ label = "'\\n' / te" ];
204
+ 45 -> 2 [ label = "'#'" ];
205
+ 45 -> 3 [ label = "'<'" ];
206
+ 45 -> 1 [ label = "DEF" ];
207
+ 46 -> 46 [ label = "'\\t', '\\v'..'\\r', SP / te, store6" ];
208
+ 46 -> 45 [ label = "'\\n' / te" ];
209
+ 46 -> 4 [ label = "'#'" ];
210
+ 46 -> 6 [ label = "'<'" ];
211
+ 46 -> 44 [ label = "DEF / te" ];
212
+ 47 -> 43 [ label = "DEF / expression_begin, next4, initts" ];
213
+ 48 -> 11 [ label = "'\\t', '\\v'..'\\r', SP" ];
214
+ 48 -> 43 [ label = "'\\n' / last1, initts" ];
215
+ 48 -> 43 [ label = "DEF / next3, initts" ];
216
+ 49 -> 26 [ label = "'\"', ''' / initts" ];
217
+ 49 -> 28 [ label = "'#'" ];
218
+ 49 -> 25 [ label = "DEF / initts" ];
219
+ 50 -> 23 [ label = "'\"' / initts" ];
220
+ 50 -> 29 [ label = "'#'" ];
221
+ 50 -> 22 [ label = "DEF / initts" ];
222
+ 51 -> 31 [ label = "''' / initts" ];
223
+ 51 -> 30 [ label = "DEF" ];
224
+ 53 -> 37 [ label = "'\"', ''' / initts" ];
225
+ 53 -> 39 [ label = "'#'" ];
226
+ 53 -> 36 [ label = "DEF / initts" ];
227
+ 54 -> 34 [ label = "'\"' / initts" ];
228
+ 54 -> 40 [ label = "'#'" ];
229
+ 54 -> 33 [ label = "DEF / initts" ];
230
+ 55 -> 42 [ label = "''' / initts" ];
231
+ 55 -> 41 [ label = "DEF" ];
232
+ ENTRY -> 43 [ label = "IN" ];
233
+ en_21 -> 21 [ label = "parse_nested_expression" ];
234
+ en_32 -> 32 [ label = "parse_expression" ];
235
+ en_43 -> 43 [ label = "main" ];
236
+ 1 -> eof_1 [ label = "EOF / lag2" ];
237
+ 2 -> eof_2 [ label = "EOF / lag2" ];
238
+ 3 -> eof_3 [ label = "EOF / lag2" ];
239
+ 4 -> eof_4 [ label = "EOF / lag6" ];
240
+ 5 -> eof_5 [ label = "EOF / lag6" ];
241
+ 6 -> eof_6 [ label = "EOF / lag6" ];
242
+ 7 -> eof_7 [ label = "EOF / lag6" ];
243
+ 8 -> eof_8 [ label = "EOF / instruction_error, lag6" ];
244
+ 9 -> eof_9 [ label = "EOF / instruction_error, lag6" ];
245
+ 10 -> eof_10 [ label = "EOF / instruction_error, lag6" ];
246
+ 11 -> eof_11 [ label = "EOF / switch" ];
247
+ 14 -> eof_14 [ label = "EOF / instruction_error" ];
248
+ 15 -> eof_15 [ label = "EOF / instruction_error" ];
249
+ 16 -> eof_16 [ label = "EOF / instruction_error" ];
250
+ 17 -> eof_17 [ label = "EOF / instruction_error" ];
251
+ 18 -> eof_18 [ label = "EOF / instruction_error" ];
252
+ 19 -> eof_19 [ label = "EOF / instruction_error" ];
253
+ 20 -> eof_20 [ label = "EOF / instruction_error" ];
254
+ 32 -> eof_32 [ label = "EOF / expression_error" ];
255
+ 33 -> eof_33 [ label = "EOF / expression_error" ];
256
+ 34 -> eof_34 [ label = "EOF / expression_error" ];
257
+ 35 -> eof_35 [ label = "EOF / expression_error" ];
258
+ 36 -> eof_36 [ label = "EOF / expression_error" ];
259
+ 37 -> eof_37 [ label = "EOF / expression_error" ];
260
+ 38 -> eof_38 [ label = "EOF / expression_error" ];
261
+ 39 -> eof_39 [ label = "EOF / expression_error" ];
262
+ 40 -> eof_40 [ label = "EOF / expression_error" ];
263
+ 41 -> eof_41 [ label = "EOF / expression_error" ];
264
+ 42 -> eof_42 [ label = "EOF / expression_error" ];
265
+ 44 -> eof_44 [ label = "EOF / next6" ];
266
+ 45 -> eof_45 [ label = "EOF / next2" ];
267
+ 46 -> eof_46 [ label = "EOF / next6" ];
268
+ 47 -> eof_47 [ label = "EOF / expression_begin, next4" ];
269
+ 48 -> eof_48 [ label = "EOF / next3" ];
270
+ }
@@ -20,7 +20,7 @@
20
20
  # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
21
  # THE SOFTWARE.
22
22
 
23
- require_relative 'parse_error'
23
+ require_relative 'error'
24
24
 
25
25
  # Methods on the following classes may be replaced by native implementations:
26
26
  require_relative 'tag'
@@ -8,6 +8,7 @@ if defined? Trenni::Native
8
8
  else
9
9
  require_relative 'fallback/markup'
10
10
  require_relative 'fallback/template'
11
+ require_relative 'fallback/query'
11
12
 
12
13
  Trenni::Parsers = Trenni::Fallback
13
14
  end
@@ -0,0 +1,94 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Copyright, 2020, by Samuel G. D. Williams. <http://www.codeotaku.com>
4
+ #
5
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ # of this software and associated documentation files (the "Software"), to deal
7
+ # in the Software without restriction, including without limitation the rights
8
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ # copies of the Software, and to permit persons to whom the Software is
10
+ # furnished to do so, subject to the following conditions:
11
+ #
12
+ # The above copyright notice and this permission notice shall be included in
13
+ # all copies or substantial portions of the Software.
14
+ #
15
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ # THE SOFTWARE.
22
+
23
+ require_relative 'buffer'
24
+
25
+ require 'uri'
26
+
27
+ module Trenni
28
+ class Query < Hash
29
+ def parse(buffer)
30
+ Parsers.parse_query(buffer, Delegate.new(self))
31
+ end
32
+
33
+ class Delegate
34
+ def initialize(top = {})
35
+ @top = top
36
+
37
+ @current = @top
38
+ @index = nil
39
+ end
40
+
41
+ def string(key, encoded)
42
+ if encoded
43
+ key = ::URI.decode_www_form_component(key)
44
+ end
45
+
46
+ index(key.to_sym)
47
+ end
48
+
49
+ def integer(key)
50
+ index(key.to_i)
51
+ end
52
+
53
+ def index(key)
54
+ if @index
55
+ @current = @current.fetch(@index) do
56
+ @current[@index] = {}
57
+ end
58
+ end
59
+
60
+ @index = key
61
+ end
62
+
63
+ def append
64
+ if @index
65
+ @current = @current.fetch(@index) do
66
+ @current[@index] = []
67
+ end
68
+ end
69
+
70
+ @index = @current.size
71
+ end
72
+
73
+ def assign(value, encoded)
74
+ if encoded
75
+ value = ::URI.decode_www_form_component(value)
76
+ end
77
+
78
+ @current[@index] = value
79
+
80
+ @current = @top
81
+ @index = nil
82
+ end
83
+
84
+ def pair
85
+ if @index
86
+ @current[@index] = true
87
+ end
88
+
89
+ @current = @top
90
+ @index = nil
91
+ end
92
+ end
93
+ end
94
+ end
@@ -0,0 +1,125 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Copyright, 2020, by Samuel G. D. Williams. <http://www.codeotaku.com>
4
+ #
5
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ # of this software and associated documentation files (the "Software"), to deal
7
+ # in the Software without restriction, including without limitation the rights
8
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ # copies of the Software, and to permit persons to whom the Software is
10
+ # furnished to do so, subject to the following conditions:
11
+ #
12
+ # The above copyright notice and this permission notice shall be included in
13
+ # all copies or substantial portions of the Software.
14
+ #
15
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ # THE SOFTWARE.
22
+
23
+ require_relative 'query'
24
+
25
+ module Trenni
26
+ class Reference
27
+ def initialize(path, query = {}, fragment: nil)
28
+ @path = path.to_s
29
+ @query = query
30
+ @fragment = fragment
31
+ end
32
+
33
+ # The path component of the URI, e.g. /foo/bar/index.html
34
+ attr :path
35
+
36
+ # The query parameters.
37
+ attr :query
38
+
39
+ # A fragment identifier, the part after the '#'
40
+ attr :fragment
41
+
42
+ def append(buffer)
43
+ buffer << escape_path(@path)
44
+
45
+ unless @query.empty?
46
+ buffer << '?' << query_string
47
+ end
48
+
49
+ if @fragment
50
+ buffer << '#' << escape(@fragment)
51
+ end
52
+
53
+ return buffer
54
+ end
55
+
56
+ def to_str
57
+ append(String.new)
58
+ end
59
+
60
+ alias to_s to_str
61
+
62
+ private
63
+
64
+ # According to https://tools.ietf.org/html/rfc3986#section-3.3, we escape non-pchar.
65
+ NON_PCHAR = /([^
66
+ a-zA-Z0-9
67
+ \-\._~
68
+ !\$&'\(\)\*\+,;=
69
+ :@\/
70
+ ]+)/x.freeze
71
+
72
+ def escape_path(path)
73
+ encoding = path.encoding
74
+ path.b.gsub(NON_PCHAR) do |m|
75
+ '%' + m.unpack('H2' * m.bytesize).join('%').upcase
76
+ end.force_encoding(encoding)
77
+ end
78
+
79
+ # Escapes a generic string, using percent encoding.
80
+ def escape(string)
81
+ encoding = string.encoding
82
+ string.b.gsub(/([^a-zA-Z0-9_.\-]+)/) do |m|
83
+ '%' + m.unpack('H2' * m.bytesize).join('%').upcase
84
+ end.force_encoding(encoding)
85
+ end
86
+
87
+ def query_string
88
+ build_nested_query(@query)
89
+ end
90
+
91
+ def build_nested_query(value, prefix = nil)
92
+ case value
93
+ when Array
94
+ value.map { |v|
95
+ build_nested_query(v, "#{prefix}[]")
96
+ }.join("&")
97
+ when Hash
98
+ value.map { |k, v|
99
+ build_nested_query(v, prefix ? "#{prefix}[#{escape(k.to_s)}]" : escape(k.to_s))
100
+ }.reject(&:empty?).join('&')
101
+ when nil
102
+ prefix
103
+ else
104
+ raise ArgumentError, "value must be a Hash" if prefix.nil?
105
+ "#{prefix}=#{escape(value.to_s)}"
106
+ end
107
+ end
108
+ end
109
+
110
+ # Generate a URI from a path and user parameters. The path may contain a `#fragment` or `?query=parameters`.
111
+ def self.Reference(path = '', **parameters)
112
+ base, fragment = path.split('#', 2)
113
+ path, query_string = base.split('?', 2)
114
+
115
+ query = Query.new
116
+
117
+ if query_string
118
+ query.parse(Buffer.new(query_string))
119
+ end
120
+
121
+ query.update(parameters)
122
+
123
+ Reference.new(path, query, fragment: fragment)
124
+ end
125
+ end