rip-parser 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (80) hide show
  1. checksums.yaml +7 -0
  2. data/.editorconfig +15 -0
  3. data/.gitignore +9 -0
  4. data/.rspec +1 -0
  5. data/.ruby-version +1 -0
  6. data/Gemfile +3 -0
  7. data/LICENSE.md +9 -0
  8. data/README.md +13 -0
  9. data/Rakefile +1 -0
  10. data/bin/console +8 -0
  11. data/bin/setup +8 -0
  12. data/legacy/normalizer.rb +279 -0
  13. data/legacy/parser_spec.rb +999 -0
  14. data/legacy/rules.rb +250 -0
  15. data/legacy/rules_spec.rb +1700 -0
  16. data/rip-parser.gemspec +20 -0
  17. data/source/rip/parser/about.rb +9 -0
  18. data/source/rip/parser/error.rb +36 -0
  19. data/source/rip/parser/grammar.rb +23 -0
  20. data/source/rip/parser/keywords.rb +84 -0
  21. data/source/rip/parser/location.rb +47 -0
  22. data/source/rip/parser/node.rb +115 -0
  23. data/source/rip/parser/rules/assignment.rb +23 -0
  24. data/source/rip/parser/rules/binary_condition.rb +24 -0
  25. data/source/rip/parser/rules/character.rb +40 -0
  26. data/source/rip/parser/rules/class.rb +60 -0
  27. data/source/rip/parser/rules/common.rb +47 -0
  28. data/source/rip/parser/rules/date_time.rb +31 -0
  29. data/source/rip/parser/rules/expression.rb +122 -0
  30. data/source/rip/parser/rules/import.rb +23 -0
  31. data/source/rip/parser/rules/invocation.rb +15 -0
  32. data/source/rip/parser/rules/invocation_index.rb +15 -0
  33. data/source/rip/parser/rules/keyword.rb +12 -0
  34. data/source/rip/parser/rules/lambda.rb +45 -0
  35. data/source/rip/parser/rules/list.rb +18 -0
  36. data/source/rip/parser/rules/map.rb +15 -0
  37. data/source/rip/parser/rules/module.rb +29 -0
  38. data/source/rip/parser/rules/number.rb +21 -0
  39. data/source/rip/parser/rules/pair.rb +13 -0
  40. data/source/rip/parser/rules/property.rb +33 -0
  41. data/source/rip/parser/rules/range.rb +15 -0
  42. data/source/rip/parser/rules/reference.rb +16 -0
  43. data/source/rip/parser/rules/string.rb +41 -0
  44. data/source/rip/parser/rules/unit.rb +17 -0
  45. data/source/rip/parser/rules.rb +7 -0
  46. data/source/rip/parser/utilities/normalizer.rb +638 -0
  47. data/source/rip/parser.rb +24 -0
  48. data/source/rip-parser.rb +1 -0
  49. data/spec/fixtures/syntax_sample.rip +96 -0
  50. data/spec/spec_helper.rb +20 -0
  51. data/spec/support/helpers.rb +57 -0
  52. data/spec/support/parslet.rb +1 -0
  53. data/spec/support/shared_examples.rb +9 -0
  54. data/spec/unit/rip/parser/grammar_spec.rb +8 -0
  55. data/spec/unit/rip/parser/location_spec.rb +89 -0
  56. data/spec/unit/rip/parser/node_spec.rb +157 -0
  57. data/spec/unit/rip/parser/rules/assignment_spec.rb +59 -0
  58. data/spec/unit/rip/parser/rules/binary_condition_spec.rb +41 -0
  59. data/spec/unit/rip/parser/rules/character_spec.rb +29 -0
  60. data/spec/unit/rip/parser/rules/class_spec.rb +181 -0
  61. data/spec/unit/rip/parser/rules/common_spec.rb +88 -0
  62. data/spec/unit/rip/parser/rules/date_time_spec.rb +61 -0
  63. data/spec/unit/rip/parser/rules/expression_spec.rb +95 -0
  64. data/spec/unit/rip/parser/rules/import_spec.rb +64 -0
  65. data/spec/unit/rip/parser/rules/invocation_index_spec.rb +46 -0
  66. data/spec/unit/rip/parser/rules/invocation_spec.rb +46 -0
  67. data/spec/unit/rip/parser/rules/keyword_spec.rb +17 -0
  68. data/spec/unit/rip/parser/rules/lambda_spec.rb +174 -0
  69. data/spec/unit/rip/parser/rules/list_spec.rb +45 -0
  70. data/spec/unit/rip/parser/rules/map_spec.rb +36 -0
  71. data/spec/unit/rip/parser/rules/module_spec.rb +63 -0
  72. data/spec/unit/rip/parser/rules/number_spec.rb +40 -0
  73. data/spec/unit/rip/parser/rules/pair_spec.rb +25 -0
  74. data/spec/unit/rip/parser/rules/property_spec.rb +27 -0
  75. data/spec/unit/rip/parser/rules/range_spec.rb +37 -0
  76. data/spec/unit/rip/parser/rules/reference_spec.rb +43 -0
  77. data/spec/unit/rip/parser/rules/string_spec.rb +166 -0
  78. data/spec/unit/rip/parser/rules/unit_spec.rb +17 -0
  79. data/spec/unit/rip/parser_spec.rb +106 -0
  80. metadata +192 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: da07388d321262375ccaa89af4d7bbc22464a05c
4
+ data.tar.gz: 5565c4dbfcd9702157839b6aa0e6ce9cc7eccfee
5
+ SHA512:
6
+ metadata.gz: 7e37d5077ae71f610effe5840e2f10c715d5460d2c523edde202b7b01acc1ab1b54d36f918928e9f505b936c49febcf7177e5413b339391aad8c8a5b76c5c4d1
7
+ data.tar.gz: 9446353584fdddf852fb1472e67a2e462510ede2c9645165072a97961de481fe647a62ca7ccffb337a2476d38230f0c51378e129fa6fb4976be6a11cbfc6e078
data/.editorconfig ADDED
@@ -0,0 +1,15 @@
1
+ root = true
2
+
3
+ [*]
4
+ charset = utf-8
5
+ end_of_line = lf
6
+ insert_final_newline = true
7
+ trim_trailing_whitespace = true
8
+
9
+ [*.md]
10
+ indent_style = space
11
+ indent_size = 2
12
+
13
+ [{Gemfile,*.gemspec,*.rb}]
14
+ indent_style = space
15
+ indent_size = 2
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ .bundle/
2
+ .yardoc
3
+ Gemfile.lock
4
+ _yardoc/
5
+ coverage/
6
+ doc/
7
+ pkg/
8
+ spec/reports/
9
+ tmp/
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --no-profile
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.3.1
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org/'
2
+
3
+ gemspec
data/LICENSE.md ADDED
@@ -0,0 +1,9 @@
1
+ ## The MIT License (MIT)
2
+
3
+ Copyright © Thomas Ingram
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6
+
7
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8
+
9
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,13 @@
1
+ ## Rip Parser
2
+
3
+ This project parses and validates [Rip](http://www.rip-lang.org/) source code.
4
+
5
+
6
+ ### Usage
7
+
8
+ ```ruby
9
+ require 'rip-parser'
10
+
11
+ package_root = Pathname.new(__dir__)
12
+ syntax_tree = Rip::Parser.load_file(package_root + 'source' + 'foo.rip')
13
+ ```
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require 'bundler/gem_tasks'
data/bin/console ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'pry'
5
+
6
+ require 'rip-parser'
7
+
8
+ Pry.start
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,279 @@
1
+ require 'parslet'
2
+
3
+ module Rip::Parser
4
+ class Normalizer < Parslet::Transform
5
+ ESCAPES = {
6
+ '\'' => '\'',
7
+ '"' => '"',
8
+ 'b' => "\b",
9
+ 'a' => "\a",
10
+ 'e' => "\e",
11
+ 'f' => "\f",
12
+ 'n' => "\n",
13
+ '#' => '#',
14
+ 'r' => "\r",
15
+ 't' => "\t"
16
+ }
17
+
18
+ def self.apply(tree)
19
+ new.apply(tree)
20
+ end
21
+
22
+ def apply(tree, context = nil)
23
+ _tree = normalize_atom(tree)
24
+ __tree = super(_tree)
25
+ collapse_atom(__tree)
26
+ end
27
+
28
+ def collapse_atom(tree)
29
+ case tree
30
+ when Array
31
+ tree.map { |t| collapse_atom(t) }
32
+ when Hash
33
+ _tree = tree.map do |key, value|
34
+ [ key, collapse_atom(value) ]
35
+ end
36
+ reply = _tree.to_h
37
+ if reply.key?(:atom)
38
+ reply[:atom].is_a?(Array) ? reply : reply[:atom]
39
+ else
40
+ reply
41
+ end
42
+ else
43
+ tree
44
+ end
45
+ end
46
+
47
+ def normalize_characters(tree)
48
+ case
49
+ when tree.is_a?(Hash) && tree.has_key?(:regex)
50
+ normalize_character_hash(tree, :regex)
51
+ when tree.is_a?(Hash) && tree.has_key?(:string)
52
+ normalize_character_hash(tree, :string)
53
+ else
54
+ tree
55
+ end
56
+ end
57
+
58
+ def normalize_character_hash(tree, type)
59
+ _parts = tree[type].inject([]) do |memo, part|
60
+ line_break = part[:line_break]
61
+
62
+ if line_break
63
+ line_break.to_s.split('').inject(memo) do |_memo, line_break_part|
64
+ _memo << {
65
+ :character => self.class.slice(line_break, line_break_part)
66
+ }
67
+ end
68
+ else
69
+ memo << part
70
+ end
71
+ end
72
+
73
+ parts = _parts.inject([]) do |memo, part|
74
+ if part[:character] && memo.last && memo.last[type]
75
+ memo.last[type] << part.merge(:location => part[:character])
76
+ elsif part[:character]
77
+ memo << tree.merge(type => [ part.merge(:location => part[:character]) ])
78
+ else
79
+ memo << part
80
+ end
81
+
82
+ memo
83
+ end
84
+
85
+ reply = parts.inject do |memo, part|
86
+ location = part[:start] ||
87
+ memo[:end] ||
88
+ memo[:atom].last[:operator_invocation][:argument][:end]
89
+ plus = self.class.slice(location, '+')
90
+
91
+ {
92
+ :atom => [
93
+ memo,
94
+ {
95
+ :operator_invocation => {
96
+ :operator => plus,
97
+ :argument => part
98
+ }
99
+ }
100
+ ]
101
+ }
102
+ end
103
+
104
+ reply || tree
105
+ end
106
+
107
+
108
+ def normalize_atom(tree)
109
+ _tree = normalize_characters(tree)
110
+
111
+ case _tree
112
+ when Array
113
+ normalize_atom_array(_tree)
114
+ when Hash
115
+ normalize_atom_hash(_tree)
116
+ else
117
+ _tree
118
+ end
119
+ end
120
+
121
+ def normalize_atom_array(tree)
122
+ tree.map { |leaf| normalize_atom(leaf) }
123
+ end
124
+
125
+ def normalize_atom_hash(tree)
126
+ atom_or_parts = tree[:atom]
127
+ case atom_or_parts
128
+ when Array
129
+ normalize_atom_parts(atom_or_parts)
130
+ when Hash
131
+ normalize_atom(atom_or_parts)
132
+ else
133
+ tree
134
+ end
135
+ end
136
+
137
+ def normalize_atom_parts(atom_or_parts)
138
+ atom_or_parts.inject do |atom_base, part|
139
+ case
140
+ when part.has_key?(:assignment)
141
+ {
142
+ :lhs => normalize_atom(atom_base),
143
+ :location => part[:assignment][:location],
144
+ :rhs => normalize_atom(part[:assignment][:rhs])
145
+ }
146
+ when part.has_key?(:key_value_pair)
147
+ {
148
+ :key => atom_base,
149
+ :location => normalize_atom(part[:key_value_pair][:location]),
150
+ :value => normalize_atom(part[:key_value_pair][:value])
151
+ }
152
+ when part.has_key?(:range)
153
+ {
154
+ :start => atom_base,
155
+ :location => normalize_atom(part[:range][:location]),
156
+ :exclusivity => part[:range][:exclusivity],
157
+ :end => normalize_atom(part[:range][:end])
158
+ }
159
+ when part.has_key?(:regular_invocation)
160
+ {
161
+ :callable => atom_base,
162
+ :location => part[:regular_invocation][:location],
163
+ :arguments => normalize_atom(part[:regular_invocation][:arguments])
164
+ }
165
+ when part.has_key?(:index_invocation)
166
+ {
167
+ :callable => {
168
+ :object => normalize_atom(atom_base),
169
+ :location => part[:index_invocation][:open],
170
+ :property_name => (part[:index_invocation][:open] + part[:index_invocation][:close])
171
+ },
172
+ :location => part[:index_invocation][:open],
173
+ :arguments => normalize_atom(part[:index_invocation][:arguments])
174
+ }
175
+ when part.has_key?(:operator_invocation)
176
+ {
177
+ :callable => {
178
+ :object => normalize_atom(atom_base),
179
+ :location => part[:operator_invocation][:operator],
180
+ :property_name => part[:operator_invocation][:operator]
181
+ },
182
+ :location => part[:operator_invocation][:operator],
183
+ :arguments => [ normalize_atom(part[:operator_invocation][:argument]) ]
184
+ }
185
+ when part.has_key?(:property_name)
186
+ {
187
+ :object => normalize_atom(atom_base),
188
+ :location => part[:location],
189
+ :property_name => normalize_atom(part[:property_name])
190
+ }
191
+ else
192
+ part
193
+ end
194
+ end
195
+ end
196
+
197
+
198
+ def self.slice(location_slice, text)
199
+ Parslet::Slice.new(location_slice.position, text.to_s, location_slice.line_cache)
200
+ end
201
+
202
+
203
+ rule(:location => simple(:location), :escaped_token => simple(:token)) do |locals|
204
+ token = locals[:token].to_s
205
+ slice(locals[:location], ESCAPES[token] || token)
206
+ end
207
+
208
+ rule(:location => simple(:location), :escaped_token_unicode => simple(:unicode)) do |locals|
209
+ slice(locals[:location], locals[:unicode].to_s.to_i(16).chr('UTF-8'))
210
+ end
211
+
212
+ %i[decimal integer].each do |number|
213
+ rule(number => simple(number)) do |locals|
214
+ {
215
+ :sign => slice(locals[number], '+'),
216
+ number => locals[number]
217
+ }
218
+ end
219
+ end
220
+
221
+ %i[dash_rocket fat_rocket].each do |keyword|
222
+ rule(keyword => simple(keyword), :location_body => simple(:location_body), :body => subtree(:body)) do |locals|
223
+ {
224
+ :dash_rocket => locals[keyword],
225
+ :parameters => [],
226
+ :location_body => locals[:location_body],
227
+ :body => locals[:body]
228
+ }
229
+ end
230
+ end
231
+
232
+ rule(:type => simple(:type), :location_body => simple(:location_body), :body => subtree(:body)) do |locals|
233
+ {
234
+ :type => locals[:type],
235
+ :arguments => [],
236
+ :location_body => locals[:location_body],
237
+ :body => locals[:body]
238
+ }
239
+ end
240
+
241
+ rule(:hour => simple(:hour), :minute => simple(:minute), :second => simple(:second)) do |locals|
242
+ {
243
+ :hour => locals[:hour],
244
+ :minute => locals[:minute],
245
+ :second => locals[:second],
246
+ :sub_second => '0',
247
+ :offset => {
248
+ :sign => '+',
249
+ :hour => '00',
250
+ :minute => '00'
251
+ }
252
+ }
253
+ end
254
+
255
+ rule(:hour => simple(:hour), :minute => simple(:minute), :second => simple(:second), :sub_second => simple(:sub_second)) do |locals|
256
+ {
257
+ :hour => locals[:hour],
258
+ :minute => locals[:minute],
259
+ :second => locals[:second],
260
+ :sub_second => locals[:sub_second],
261
+ :offset => {
262
+ :sign => '+',
263
+ :hour => '00',
264
+ :minute => '00'
265
+ }
266
+ }
267
+ end
268
+
269
+ rule(:hour => simple(:hour), :minute => simple(:minute), :second => simple(:second), :offset => subtree(:offset)) do |locals|
270
+ {
271
+ :hour => locals[:hour],
272
+ :minute => locals[:minute],
273
+ :second => locals[:second],
274
+ :sub_second => '0',
275
+ :offset => locals[:offset]
276
+ }
277
+ end
278
+ end
279
+ end