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
data/legacy/rules.rb ADDED
@@ -0,0 +1,250 @@
1
+ require 'parslet'
2
+
3
+ module Rip::Parser
4
+ class Rules < Parslet::Parser
5
+ root(:module)
6
+
7
+
8
+ rule(:whitespace) { space | line_break | comment }
9
+ rule(:whitespaces) { whitespace.repeat(1) }
10
+ rule(:whitespaces?) { whitespaces.maybe }
11
+
12
+ rule(:space) { str(' ') | str("\t") }
13
+ rule(:spaces) { space.repeat(1) }
14
+ rule(:spaces?) { spaces.maybe }
15
+
16
+ rule(:line_break) { str("\r\n") | str("\n") | str("\r") }
17
+ rule(:line_breaks) { line_break.repeat(1) }
18
+ rule(:line_breaks?) { line_breaks.maybe }
19
+
20
+ rule(:comment) { pound >> (line_break.absent? >> any).repeat >> (line_break | eof) }
21
+
22
+
23
+ rule(:expression_terminator) { semicolon | line_break }
24
+ rule(:expression_terminator?) { expression_terminator.maybe }
25
+
26
+
27
+ rule(:eof) { any.absent? }
28
+
29
+ rule(:dot) { str('.') }
30
+ rule(:comma) { str(',') }
31
+ rule(:semicolon) { str(';') }
32
+ rule(:colon) { str(':') }
33
+ rule(:pound) { str('#') }
34
+ rule(:dash) { str('-') }
35
+ rule(:underscore) { str('_') }
36
+ rule(:equals) { str('=') }
37
+
38
+ rule(:slash_back) { str('\\') }
39
+ rule(:slash_forward) { str('/') }
40
+
41
+ rule(:angled_open) { str('<') }
42
+ rule(:angled_close) { str('>') }
43
+
44
+ rule(:brace_open) { str('{') }
45
+ rule(:brace_close) { str('}') }
46
+
47
+ rule(:bracket_open) { str('[') }
48
+ rule(:bracket_close) { str(']') }
49
+
50
+ rule(:parenthesis_open) { str('(') }
51
+ rule(:parenthesis_close) { str(')') }
52
+
53
+ rule(:quote_single) { str('\'') }
54
+ rule(:quote_double) { str('"') }
55
+ rule(:backtick) { str('`') }
56
+
57
+
58
+ rule(:module) { lines.as(:module) }
59
+ rule(:lines) { line.repeat }
60
+ rule(:line) { whitespaces | expression }
61
+
62
+ rule(:expression) { expression_base >> spaces? >> expression_terminator? }
63
+
64
+ rule(:expression_base) { (keyword >> spaces >> phrase.as(:payload)) | keyword | phrase }
65
+
66
+ rule(:keyword) { %i[exit return throw].map { |kw| str(kw.to_s).as(kw) >> reference.absent? }.inject(:|) }
67
+
68
+
69
+ rule(:phrase) { operator_invocation? }
70
+
71
+ rule(:operator_invocation?) { (assignment? >> (expression_terminator.absent? >> operator_invocation).repeat).as(:atom) }
72
+ rule(:operator_invocation) { (whitespaces >> property_name.as(:operator) >> whitespaces >> assignment?.as(:argument)).as(:operator_invocation) }
73
+
74
+ rule(:assignment?) { (key_value_pair? >> (expression_terminator.absent? >> assignment).repeat).as(:atom) }
75
+ rule(:assignment) { (whitespaces >> equals.as(:location) >> whitespaces >> phrase.as(:rhs)).as(:assignment) }
76
+
77
+ rule(:key_value_pair?) { time | (range? >> (expression_terminator.absent? >> key_value_pair).repeat).as(:atom) }
78
+ rule(:key_value_pair) { (whitespaces? >> colon.as(:location) >> whitespaces? >> range?.as(:value)).as(:key_value_pair) }
79
+
80
+ rule(:range?) { (atom? >> (expression_terminator.absent? >> range).repeat).as(:atom) }
81
+ rule(:range) { (whitespaces? >> dot.repeat(2, 2).as(:location) >> dot.maybe.as(:exclusivity) >> whitespaces? >> atom?.as(:end)).as(:range) }
82
+
83
+ rule(:atom?) { (object >> (expression_terminator.absent? >> (regular_invocation | index_invocation | property)).repeat).as(:atom) }
84
+ rule(:regular_invocation) { (parenthesis_open.as(:location) >> whitespaces? >> csv(phrase).as(:arguments) >> whitespaces? >> parenthesis_close).as(:regular_invocation) }
85
+ rule(:index_invocation) { (bracket_open.as(:open) >> whitespaces? >> csv(phrase).as(:arguments) >> whitespaces? >> bracket_close.as(:close)).as(:index_invocation) }
86
+
87
+ rule(:property) { whitespaces? >> dot.as(:location) >> property_name.as(:property_name) }
88
+ rule(:property_name) do
89
+ word |
90
+ str('/%') |
91
+ str('/') |
92
+ str('<=>') |
93
+ str('<=') |
94
+ str('<').repeat(1, 2) |
95
+ str('>=') |
96
+ str('>').repeat(1, 2) |
97
+ str('[]')
98
+ end
99
+
100
+ # TODO literals for unit
101
+ # TODO literals for version (maybe)
102
+ rule(:object) do
103
+ condition_block_sequence |
104
+ exception_block_sequence |
105
+ type_block |
106
+ lambda_block |
107
+ overload_block |
108
+ switch_block |
109
+ datetime |
110
+ date |
111
+ number |
112
+ character |
113
+ string |
114
+ regular_expression |
115
+ heredoc |
116
+ map |
117
+ list |
118
+ reference |
119
+ parenthesis_open >> whitespaces? >> phrase >> whitespaces? >> parenthesis_close
120
+ end
121
+
122
+
123
+ rule(:reference) { word.as(:reference) }
124
+
125
+ rule(:word) { word_legal >> (word_legal | digit).repeat }
126
+ rule(:word_legal) { match['^\d\s\`\'",.:;#\/\\()<>\[\]{}'] }
127
+
128
+
129
+ rule(:condition_block_sequence) { if_block >> whitespaces? >> else_block }
130
+
131
+ rule(:exception_block_sequence) { try_block >> (whitespaces? >> catch_block).repeat.as(:catch_blocks) >> whitespaces? >> finally_block.maybe }
132
+
133
+ rule(:lambda_block) { str('=>').as(:fat_rocket) >> spaces? >> block_body_lambda }
134
+
135
+ rule(:overload_block) { str('->').as(:dash_rocket) >> spaces? >> parameters.maybe >> block_body }
136
+
137
+ rule(:type_block) { str('type').as(:type) >> spaces? >> multiple_arguments.maybe >> block_body }
138
+ rule(:case_block) { str('case').as(:case) >> spaces? >> multiple_arguments >> block_body }
139
+
140
+ rule(:switch_block) { str('switch').as(:switch) >> spaces? >> single_argument.maybe >> block_body_switch }
141
+ rule(:catch_block) { str('catch').as(:catch) >> spaces? >> single_argument >> block_body }
142
+
143
+ rule(:if_block) { (str('if').as(:if) >> spaces? >> single_argument >> block_body).as(:if_block) }
144
+
145
+ rule(:try_block) { (str('try').as(:try) >> block_body).as(:try_block) }
146
+ rule(:finally_block) { (str('finally').as(:finally) >> block_body).as(:finally_block) }
147
+ rule(:else_block) { (str('else').as(:else) >> block_body).as(:else_block) }
148
+
149
+ rule(:parameters) { parenthesis_open >> whitespaces? >> csv(optional_parameter | required_parameter).as(:parameters) >> whitespaces? >> parenthesis_close }
150
+ rule(:required_parameter) { word.as(:parameter) >> parameter_type_argument.maybe }
151
+ rule(:optional_parameter) { word.as(:parameter) >> parameter_type_argument.maybe >> whitespaces? >> equals >> whitespaces? >> phrase.as(:default_expression) }
152
+ rule(:parameter_type_argument) { angled_open >> spaces? >> phrase.as(:type_argument) >> spaces? >> angled_close }
153
+
154
+ rule(:multiple_arguments) { parenthesis_open >> whitespaces? >> csv(phrase).as(:arguments) >> whitespaces? >> parenthesis_close }
155
+ rule(:single_argument) { parenthesis_open >> whitespaces? >> phrase.as(:argument) >> whitespaces? >> parenthesis_close }
156
+
157
+ rule(:block_body) { whitespaces? >> brace_open.as(:location_body) >> whitespaces? >> lines.as(:body) >> whitespaces? >> brace_close }
158
+ rule(:block_body_switch) { whitespaces? >> brace_open >> whitespaces? >> (case_block >> whitespaces?).repeat(1).as(:case_blocks) >> else_block >> whitespaces? >> brace_close }
159
+ rule(:block_body_lambda) { whitespaces? >> brace_open.as(:location_body) >> whitespaces? >> (overload_block >> whitespaces?).repeat(1).as(:overload_blocks) >> whitespaces? >> brace_close }
160
+
161
+
162
+ rule(:datetime) { date.as(:date) >> str('T') >> time.as(:time) }
163
+
164
+ rule(:date) { digit.repeat(4, 4).as(:year) >> dash >> digit.repeat(2, 2).as(:month) >> dash >> digit.repeat(2, 2).as(:day) }
165
+
166
+ rule(:time) do
167
+ (digit.repeat(2, 2).as(:hour) >> colon >> digit.repeat(2, 2).as(:minute) >> colon >> digit.repeat(2, 2).as(:second)) >>
168
+ (dot >> digits.as(:sub_second)).maybe >>
169
+ (sign >> digit.repeat(2, 2).as(:hour) >> digit.repeat(2, 2).as(:minute)).as(:offset).maybe
170
+ end
171
+
172
+
173
+ # WARNING order is important here: decimal must be before integer or the integral part of
174
+ # a decimal could be interpreted as a integer followed by a decimal starting with a dot
175
+ rule(:number) { sign.maybe >> (decimal | integer) }
176
+
177
+ rule(:decimal) { (digits >> dot >> digits).as(:decimal) }
178
+ rule(:integer) { digits.as(:integer) }
179
+
180
+ rule(:sign) { match['+-'].as(:sign) }
181
+
182
+ rule(:digit) { match['0-9'] }
183
+ rule(:digits) { digit.repeat(1) >> (underscore.maybe >> digit.repeat(1)).repeat }
184
+
185
+
186
+ rule(:character) { backtick.as(:location) >> (escape_advanced | character_legal).as(:character) }
187
+ rule(:character_legal) { digit | word_legal }
188
+
189
+
190
+ rule(:escape_simple) { escape_token_quote_single | escape_token_slash_back }
191
+ rule(:escape_regex) { escape_token_slash_forward | escape_token_slash_back }
192
+ rule(:escape_advanced) { escape_token_unicode | escape_token_any }
193
+
194
+ rule(:escape_token_quote_single) { slash_back.as(:location) >> quote_single.as(:escaped_token) }
195
+ rule(:escape_token_double) { slash_back.as(:location) >> quote_double.as(:escaped_token) }
196
+ rule(:escape_token_slash_back) { slash_back.as(:location) >> slash_back.as(:escaped_token) }
197
+ rule(:escape_token_slash_forward) { slash_back.as(:location) >> slash_forward.as(:escaped_token) }
198
+ rule(:escape_token_unicode) { slash_back.as(:location) >> str('u') >> match['0-9a-f'].repeat(4, 4).as(:escaped_token_unicode) }
199
+ rule(:escape_token_any) { slash_back.as(:location) >> any.as(:escaped_token) }
200
+
201
+
202
+ rule(:string) { string_symbol | string_single | string_double }
203
+
204
+ rule(:string_symbol) { colon.as(:location) >> (escape_simple | character_legal).as(:character).repeat(1).as(:string) }
205
+
206
+ rule(:string_single) { string_parser(quote_single, escape_simple.as(:character)) }
207
+ rule(:string_double) { string_parser(quote_double, escape_advanced.as(:character) | interpolation) }
208
+
209
+ rule(:regular_expression) { string_parser(slash_forward, escape_regex.as(:character) | interpolation(:interpolation_regex), :regex) }
210
+
211
+
212
+ # https://github.com/kschiess/parslet/blob/master/example/capture.rb
213
+ rule(:heredoc) do
214
+ scope { heredoc_start >> heredoc_content.as(:string) >> heredoc_end }
215
+ end
216
+
217
+ rule(:heredoc_start) { angled_open.repeat(2, 2).as(:location) >> heredoc_label >> line_break }
218
+ rule(:heredoc_label) { match['A-Z_'].repeat(1).capture(:heredoc_label) }
219
+
220
+ rule(:heredoc_content) { (heredoc_end.absent? >> heredoc_line).repeat }
221
+ rule(:heredoc_line) { (line_break.absent? >> heredoc_content_any).repeat >> line_break.as(:line_break) }
222
+ rule(:heredoc_content_any) { escape_advanced.as(:character) | interpolation | any.as(:character) }
223
+
224
+ rule(:heredoc_end) do
225
+ dynamic { |source, context| spaces? >> str(context.captures[:heredoc_label]) >> (line_break | eof) }
226
+ end
227
+
228
+
229
+ rule(:map) { brace_open.as(:location) >> whitespaces? >> csv(phrase).as(:map) >> whitespaces? >> brace_close }
230
+
231
+ rule(:list) { bracket_open.as(:location) >> whitespaces? >> csv(phrase).as(:list) >> whitespaces? >> bracket_close }
232
+
233
+
234
+ protected
235
+
236
+ # "borrowed" from http://jmettraux.wordpress.com/2011/05/11/parslet-and-json/
237
+ def csv(value)
238
+ _value = whitespaces? >> value >> whitespaces?
239
+ (_value >> (comma >> _value).repeat).repeat(0, 1)
240
+ end
241
+
242
+ def interpolation(target = :interpolation)
243
+ (pound >> brace_open).as(:start) >> (brace_close.absent? >> line.repeat(1)).repeat.as(target) >> brace_close.as(:end)
244
+ end
245
+
246
+ def string_parser(delimiter, inner_special, delimited_flag = :string)
247
+ delimiter.as(:location) >> (delimiter.absent? >> (inner_special | any.as(:character))).repeat.as(delimited_flag) >> delimiter
248
+ end
249
+ end
250
+ end