bel_parser 1.0.8 → 1.1.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (112) hide show
  1. checksums.yaml +4 -4
  2. data/.gemspec +12 -2
  3. data/VERSION +1 -1
  4. data/bin/bel2_compatibility +12 -9
  5. data/bin/bel2_upgrade +18 -6
  6. data/bin/bel2_validator +6 -3
  7. data/bin/bel_script_reader +1 -0
  8. data/lib/bel_parser/completion.rb +984 -0
  9. data/lib/bel_parser/language/apply_namespace_encoding.rb +14 -3
  10. data/lib/bel_parser/language/expression_validator.rb +1 -2
  11. data/lib/bel_parser/language/function.rb +4 -0
  12. data/lib/bel_parser/language/relationship.rb +4 -0
  13. data/lib/bel_parser/language/semantics/function_deprecation.rb +1 -0
  14. data/lib/bel_parser/language/semantics/list_function_subject.rb +2 -0
  15. data/lib/bel_parser/language/semantics/multiple_subject_object.rb +4 -1
  16. data/lib/bel_parser/language/semantics/nested_statement_without_object.rb +43 -0
  17. data/lib/bel_parser/language/semantics/non_object_list.rb +3 -0
  18. data/lib/bel_parser/language/semantics/relationship_not_listable.rb +2 -0
  19. data/lib/bel_parser/language/semantics/signature_mapping.rb +2 -0
  20. data/lib/bel_parser/language/semantics_ast.rb +0 -7
  21. data/lib/bel_parser/language/syntax/invalid_function.rb +6 -1
  22. data/lib/bel_parser/language/syntax/invalid_relationship.rb +1 -0
  23. data/lib/bel_parser/language/version1_0/relationships/acts_in.rb +9 -9
  24. data/lib/bel_parser/language/version1_0/relationships/analogous.rb +6 -4
  25. data/lib/bel_parser/language/version1_0/relationships/association.rb +8 -7
  26. data/lib/bel_parser/language/version1_0/relationships/biomarker_for.rb +6 -5
  27. data/lib/bel_parser/language/version1_0/relationships/causes_no_change.rb +8 -7
  28. data/lib/bel_parser/language/version1_0/relationships/decreases.rb +13 -12
  29. data/lib/bel_parser/language/version1_0/relationships/directly_decreases.rb +8 -6
  30. data/lib/bel_parser/language/version1_0/relationships/directly_increases.rb +8 -6
  31. data/lib/bel_parser/language/version1_0/relationships/has_component.rb +14 -14
  32. data/lib/bel_parser/language/version1_0/relationships/has_components.rb +12 -11
  33. data/lib/bel_parser/language/version1_0/relationships/has_member.rb +10 -8
  34. data/lib/bel_parser/language/version1_0/relationships/has_members.rb +12 -11
  35. data/lib/bel_parser/language/version1_0/relationships/has_modification.rb +9 -7
  36. data/lib/bel_parser/language/version1_0/relationships/has_product.rb +10 -8
  37. data/lib/bel_parser/language/version1_0/relationships/has_variant.rb +9 -7
  38. data/lib/bel_parser/language/version1_0/relationships/includes.rb +11 -10
  39. data/lib/bel_parser/language/version1_0/relationships/increases.rb +12 -13
  40. data/lib/bel_parser/language/version1_0/relationships/is_a.rb +10 -8
  41. data/lib/bel_parser/language/version1_0/relationships/negative_correlation.rb +9 -7
  42. data/lib/bel_parser/language/version1_0/relationships/orthologous.rb +9 -8
  43. data/lib/bel_parser/language/version1_0/relationships/positive_correlation.rb +8 -5
  44. data/lib/bel_parser/language/version1_0/relationships/prognostic_biomarker_for.rb +8 -6
  45. data/lib/bel_parser/language/version1_0/relationships/rate_limiting_step_of.rb +10 -9
  46. data/lib/bel_parser/language/version1_0/relationships/reactant_in.rb +10 -8
  47. data/lib/bel_parser/language/version1_0/relationships/sub_process_of.rb +13 -12
  48. data/lib/bel_parser/language/version1_0/relationships/transcribed_to.rb +8 -7
  49. data/lib/bel_parser/language/version1_0/relationships/translated_to.rb +9 -7
  50. data/lib/bel_parser/language/version1_0/relationships/translocates.rb +10 -9
  51. data/lib/bel_parser/language/version2_0/functions/protein_modification.rb +1 -2
  52. data/lib/bel_parser/language/version2_0/functions/translocation.rb +1 -2
  53. data/lib/bel_parser/language/version2_0/functions/variant.rb +1 -2
  54. data/lib/bel_parser/language/version2_0/relationships/acts_in.rb +10 -9
  55. data/lib/bel_parser/language/version2_0/relationships/biomarker_for.rb +7 -5
  56. data/lib/bel_parser/language/version2_0/relationships/causes_no_change.rb +9 -7
  57. data/lib/bel_parser/language/version2_0/relationships/decreases.rb +13 -12
  58. data/lib/bel_parser/language/version2_0/relationships/directly_decreases.rb +8 -6
  59. data/lib/bel_parser/language/version2_0/relationships/directly_increases.rb +8 -6
  60. data/lib/bel_parser/language/version2_0/relationships/has_component.rb +15 -14
  61. data/lib/bel_parser/language/version2_0/relationships/has_components.rb +12 -11
  62. data/lib/bel_parser/language/version2_0/relationships/has_member.rb +9 -8
  63. data/lib/bel_parser/language/version2_0/relationships/has_members.rb +12 -11
  64. data/lib/bel_parser/language/version2_0/relationships/has_modification.rb +9 -7
  65. data/lib/bel_parser/language/version2_0/relationships/has_product.rb +10 -8
  66. data/lib/bel_parser/language/version2_0/relationships/has_variant.rb +9 -7
  67. data/lib/bel_parser/language/version2_0/relationships/includes.rb +11 -10
  68. data/lib/bel_parser/language/version2_0/relationships/increases.rb +13 -12
  69. data/lib/bel_parser/language/version2_0/relationships/is_a.rb +10 -8
  70. data/lib/bel_parser/language/version2_0/relationships/negative_correlation.rb +9 -7
  71. data/lib/bel_parser/language/version2_0/relationships/orthologous.rb +10 -8
  72. data/lib/bel_parser/language/version2_0/relationships/positive_correlation.rb +7 -5
  73. data/lib/bel_parser/language/version2_0/relationships/prognostic_biomarker_for.rb +8 -6
  74. data/lib/bel_parser/language/version2_0/relationships/rate_limiting_step_of.rb +10 -9
  75. data/lib/bel_parser/language/version2_0/relationships/reactant_in.rb +10 -8
  76. data/lib/bel_parser/language/version2_0/relationships/regulates.rb +9 -8
  77. data/lib/bel_parser/language/version2_0/relationships/sub_process_of.rb +13 -12
  78. data/lib/bel_parser/language/version2_0/relationships/transcribed_to.rb +8 -7
  79. data/lib/bel_parser/language/version2_0/relationships/translated_to.rb +9 -7
  80. data/lib/bel_parser/language/version2_0/relationships/translocates.rb +10 -9
  81. data/lib/bel_parser/mixin/levenshtein.rb +20 -0
  82. data/lib/bel_parser/parsers/ast/node.rb +49 -2
  83. data/lib/bel_parser/parsers/bel_script/define_annotation.rb +156 -156
  84. data/lib/bel_parser/parsers/bel_script/define_namespace.rb +60 -60
  85. data/lib/bel_parser/parsers/bel_script/set.rb +200 -200
  86. data/lib/bel_parser/parsers/bel_script/set_document.rb +188 -188
  87. data/lib/bel_parser/parsers/bel_script/unset.rb +14 -14
  88. data/lib/bel_parser/parsers/common/common.rl +1 -0
  89. data/lib/bel_parser/parsers/common/function.rb +8 -8
  90. data/lib/bel_parser/parsers/common/function.rl +6 -6
  91. data/lib/bel_parser/parsers/common/identifier.rb +3 -3
  92. data/lib/bel_parser/parsers/common/identifier.rl +4 -4
  93. data/lib/bel_parser/parsers/common/list.rb +78 -78
  94. data/lib/bel_parser/parsers/common/multi_identifier.rb +275 -0
  95. data/lib/bel_parser/parsers/common/multi_identifier.rl +141 -0
  96. data/lib/bel_parser/parsers/common/string.rb +6 -6
  97. data/lib/bel_parser/parsers/common/string.rl +4 -4
  98. data/lib/bel_parser/parsers/common.rb +1 -0
  99. data/lib/bel_parser/parsers/expression/nested_statement.rb +30180 -29055
  100. data/lib/bel_parser/parsers/expression/observed_term.rb +1456 -1099
  101. data/lib/bel_parser/parsers/expression/parameter.rb +192 -111
  102. data/lib/bel_parser/parsers/expression/parameter.rl +6 -3
  103. data/lib/bel_parser/parsers/expression/relationship.rb +43 -19
  104. data/lib/bel_parser/parsers/expression/relationship.rl +1 -0
  105. data/lib/bel_parser/parsers/expression/simple_statement.rb +17805 -17093
  106. data/lib/bel_parser/parsers/expression/statement_autocomplete.rb +1035 -0
  107. data/lib/bel_parser/parsers/expression/statement_autocomplete.rl +736 -0
  108. data/lib/bel_parser/parsers/expression/term.rb +960 -705
  109. data/lib/bel_parser/parsers/serializer.rb +8 -4
  110. data/lib/bel_parser/version.rb +33 -0
  111. data/lib/bel_parser.rb +3 -0
  112. metadata +25 -3
@@ -0,0 +1,1035 @@
1
+
2
+ =begin
3
+
4
+
5
+ =end
6
+
7
+ # begin ruby
8
+ require_relative '../../vendor/ast/processor/mixin'
9
+ require_relative '../ast/node'
10
+ require_relative '../mixin/buffer'
11
+ require_relative '../nonblocking_io_wrapper'
12
+ require_relative '../tracer'
13
+
14
+ module BELParser
15
+ module Parsers
16
+ module Expression
17
+ module StatementAutocomplete
18
+
19
+ class << self
20
+
21
+ MAX_LENGTH = 1024 * 128 # 128K
22
+
23
+ def parse(content, caret_position)
24
+ return nil unless content
25
+
26
+ if !content.end_with?("\n")
27
+ content = "#{content}\n"
28
+ end
29
+
30
+ parser = Parser.new(content, caret_position)
31
+ parser.each do |ast|
32
+ return [ast, parser.space_adjusted_caret_position]
33
+ end
34
+ end
35
+ end
36
+
37
+ private
38
+
39
+ class Parser
40
+ include Enumerable
41
+ include BELParser::Parsers::Buffer
42
+ include BELParser::Parsers::AST
43
+ include BELParser::Parsers::AST::Sexp
44
+ include BELParser::Parsers::Tracer
45
+
46
+ attr_reader :space_adjusted_caret_position
47
+
48
+ def initialize(content, caret_position)
49
+ @content = content
50
+ @original_caret = caret_position
51
+ @space_adjusted_caret_position = caret_position
52
+
53
+ class << self
54
+ attr_accessor :_statement_autocomplete_trans_keys
55
+ private :_statement_autocomplete_trans_keys, :_statement_autocomplete_trans_keys=
56
+ end
57
+ self._statement_autocomplete_trans_keys = [
58
+ 9, 122, 9, 32, 10, 92,
59
+ 10, 92, 48, 122, 33,
60
+ 126, 33, 126, 0
61
+ ]
62
+
63
+ class << self
64
+ attr_accessor :_statement_autocomplete_key_spans
65
+ private :_statement_autocomplete_key_spans, :_statement_autocomplete_key_spans=
66
+ end
67
+ self._statement_autocomplete_key_spans = [
68
+ 114, 24, 83, 83, 75, 94, 94
69
+ ]
70
+
71
+ class << self
72
+ attr_accessor :_statement_autocomplete_index_offsets
73
+ private :_statement_autocomplete_index_offsets, :_statement_autocomplete_index_offsets=
74
+ end
75
+ self._statement_autocomplete_index_offsets = [
76
+ 0, 115, 140, 224, 308, 384, 479
77
+ ]
78
+
79
+ class << self
80
+ attr_accessor :_statement_autocomplete_indicies
81
+ private :_statement_autocomplete_indicies, :_statement_autocomplete_indicies=
82
+ end
83
+ self._statement_autocomplete_indicies = [
84
+ 1, 0, 0, 0, 0, 0, 0, 0,
85
+ 0, 0, 0, 0, 0, 0, 0, 0,
86
+ 0, 0, 0, 0, 0, 0, 0, 1,
87
+ 0, 2, 0, 0, 0, 0, 0, 3,
88
+ 4, 0, 0, 5, 0, 0, 0, 6,
89
+ 6, 6, 6, 6, 6, 6, 6, 6,
90
+ 6, 7, 0, 0, 0, 0, 0, 0,
91
+ 6, 6, 6, 6, 6, 6, 6, 6,
92
+ 6, 6, 6, 6, 6, 6, 6, 6,
93
+ 6, 6, 6, 6, 6, 6, 6, 6,
94
+ 6, 6, 0, 0, 0, 0, 6, 0,
95
+ 6, 6, 6, 6, 6, 6, 6, 6,
96
+ 6, 6, 6, 6, 6, 6, 6, 6,
97
+ 6, 6, 6, 6, 6, 6, 6, 6,
98
+ 6, 6, 0, 1, 8, 8, 8, 8,
99
+ 8, 8, 8, 8, 8, 8, 8, 8,
100
+ 8, 8, 8, 8, 8, 8, 8, 8,
101
+ 8, 8, 1, 8, 9, 2, 2, 2,
102
+ 2, 2, 2, 2, 2, 2, 2, 2,
103
+ 2, 2, 2, 2, 2, 2, 2, 2,
104
+ 2, 2, 2, 2, 10, 2, 2, 2,
105
+ 2, 2, 2, 2, 2, 2, 2, 2,
106
+ 2, 2, 2, 2, 2, 2, 2, 2,
107
+ 2, 2, 2, 2, 2, 2, 2, 2,
108
+ 2, 2, 2, 2, 2, 2, 2, 2,
109
+ 2, 2, 2, 2, 2, 2, 2, 2,
110
+ 2, 2, 2, 2, 2, 2, 2, 2,
111
+ 2, 2, 2, 2, 2, 2, 11, 2,
112
+ 9, 2, 2, 2, 2, 2, 2, 2,
113
+ 2, 2, 2, 2, 2, 2, 2, 2,
114
+ 2, 2, 2, 2, 2, 2, 2, 2,
115
+ 2, 2, 2, 2, 2, 2, 2, 2,
116
+ 2, 2, 2, 2, 2, 2, 2, 2,
117
+ 2, 2, 2, 2, 2, 2, 2, 2,
118
+ 2, 2, 2, 2, 2, 2, 2, 2,
119
+ 2, 2, 2, 2, 2, 2, 2, 2,
120
+ 2, 2, 2, 2, 2, 2, 2, 2,
121
+ 2, 2, 2, 2, 2, 2, 2, 2,
122
+ 2, 2, 11, 2, 6, 6, 6, 6,
123
+ 6, 6, 6, 6, 6, 6, 12, 12,
124
+ 12, 12, 12, 12, 12, 6, 6, 6,
125
+ 6, 6, 6, 6, 6, 6, 6, 6,
126
+ 6, 6, 6, 6, 6, 6, 6, 6,
127
+ 6, 6, 6, 6, 6, 6, 6, 12,
128
+ 12, 12, 12, 6, 12, 6, 6, 6,
129
+ 6, 6, 6, 6, 6, 6, 6, 6,
130
+ 6, 6, 6, 6, 6, 6, 6, 6,
131
+ 6, 6, 6, 6, 6, 6, 6, 12,
132
+ 14, 14, 14, 14, 14, 14, 14, 14,
133
+ 14, 14, 14, 14, 14, 14, 14, 14,
134
+ 14, 14, 14, 14, 14, 14, 14, 14,
135
+ 14, 14, 14, 14, 14, 14, 14, 14,
136
+ 14, 14, 14, 14, 14, 14, 14, 14,
137
+ 14, 14, 14, 14, 14, 14, 14, 14,
138
+ 14, 14, 14, 14, 14, 14, 14, 14,
139
+ 14, 14, 14, 14, 14, 14, 14, 14,
140
+ 14, 14, 14, 14, 14, 14, 14, 14,
141
+ 14, 14, 14, 14, 14, 14, 14, 14,
142
+ 14, 14, 14, 14, 14, 14, 14, 14,
143
+ 14, 14, 14, 14, 14, 14, 13, 14,
144
+ 14, 14, 14, 14, 14, 14, 14, 14,
145
+ 14, 14, 14, 14, 14, 14, 14, 14,
146
+ 14, 14, 14, 14, 14, 14, 14, 14,
147
+ 14, 14, 14, 14, 14, 14, 14, 14,
148
+ 14, 14, 14, 14, 14, 14, 14, 14,
149
+ 14, 14, 14, 14, 14, 14, 14, 14,
150
+ 14, 14, 14, 14, 14, 14, 14, 14,
151
+ 14, 14, 14, 14, 14, 14, 14, 14,
152
+ 14, 14, 14, 14, 14, 14, 14, 14,
153
+ 14, 14, 14, 14, 14, 14, 14, 14,
154
+ 14, 14, 14, 14, 14, 14, 14, 14,
155
+ 14, 14, 14, 14, 14, 15, 0
156
+ ]
157
+
158
+ class << self
159
+ attr_accessor :_statement_autocomplete_trans_targs
160
+ private :_statement_autocomplete_trans_targs, :_statement_autocomplete_trans_targs=
161
+ end
162
+ self._statement_autocomplete_trans_targs = [
163
+ 0, 1, 2, 0, 0, 0, 4, 0,
164
+ 0, 0, 0, 3, 0, 5, 6, 5
165
+ ]
166
+
167
+ class << self
168
+ attr_accessor :_statement_autocomplete_trans_actions
169
+ private :_statement_autocomplete_trans_actions, :_statement_autocomplete_trans_actions=
170
+ end
171
+ self._statement_autocomplete_trans_actions = [
172
+ 3, 0, 0, 4, 5, 6, 0, 7,
173
+ 8, 9, 10, 0, 11, 12, 0, 13
174
+ ]
175
+
176
+ class << self
177
+ attr_accessor :_statement_autocomplete_to_state_actions
178
+ private :_statement_autocomplete_to_state_actions, :_statement_autocomplete_to_state_actions=
179
+ end
180
+ self._statement_autocomplete_to_state_actions = [
181
+ 1, 0, 0, 0, 0, 1, 0
182
+ ]
183
+
184
+ class << self
185
+ attr_accessor :_statement_autocomplete_from_state_actions
186
+ private :_statement_autocomplete_from_state_actions, :_statement_autocomplete_from_state_actions=
187
+ end
188
+ self._statement_autocomplete_from_state_actions = [
189
+ 2, 0, 0, 0, 0, 2, 0
190
+ ]
191
+
192
+ class << self
193
+ attr_accessor :_statement_autocomplete_eof_trans
194
+ private :_statement_autocomplete_eof_trans, :_statement_autocomplete_eof_trans=
195
+ end
196
+ self._statement_autocomplete_eof_trans = [
197
+ 0, 9, 10, 10, 13, 0, 16
198
+ ]
199
+
200
+ class << self
201
+ attr_accessor :statement_autocomplete_start
202
+ end
203
+ self.statement_autocomplete_start = 0;
204
+ class << self
205
+ attr_accessor :statement_autocomplete_first_final
206
+ end
207
+ self.statement_autocomplete_first_final = 0;
208
+ class << self
209
+ attr_accessor :statement_autocomplete_error
210
+ end
211
+ self.statement_autocomplete_error = -1;
212
+
213
+ class << self
214
+ attr_accessor :statement_autocomplete_en_relationship
215
+ end
216
+ self.statement_autocomplete_en_relationship = 5;
217
+ class << self
218
+ attr_accessor :statement_autocomplete_en_term
219
+ end
220
+ self.statement_autocomplete_en_term = 0;
221
+
222
+
223
+ end
224
+
225
+ def each
226
+ @last_state = nil
227
+ @spaces = 0
228
+ @value = nil
229
+ @prefix = nil
230
+ @param = nil
231
+ @term_stack = []
232
+ @statement_stack = [statement(nil, nil, nil)]
233
+ @paren_counter = 0
234
+ @relationship = nil
235
+ @bel_part = :term
236
+
237
+ stack = []
238
+ data = @content.unpack('C*')
239
+ p = 0
240
+ pe = data.length
241
+ eof = data.length
242
+
243
+ # begin: ragel
244
+
245
+ begin
246
+ p ||= 0
247
+ pe ||= data.length
248
+ cs = statement_autocomplete_start
249
+ top = 0
250
+ ts = nil
251
+ te = nil
252
+ act = 0
253
+ end
254
+
255
+
256
+ begin
257
+ testEof = false
258
+ _slen, _trans, _keys, _inds, _acts, _nacts = nil
259
+ _goto_level = 0
260
+ _resume = 10
261
+ _eof_trans = 15
262
+ _again = 20
263
+ _test_eof = 30
264
+ _out = 40
265
+ while true
266
+ if _goto_level <= 0
267
+ if p == pe
268
+ _goto_level = _test_eof
269
+ next
270
+ end
271
+ end
272
+ if _goto_level <= _resume
273
+ case _statement_autocomplete_from_state_actions[cs]
274
+ when 2 then
275
+ begin
276
+ ts = p
277
+ end
278
+ end
279
+ _keys = cs << 1
280
+ _inds = _statement_autocomplete_index_offsets[cs]
281
+ _slen = _statement_autocomplete_key_spans[cs]
282
+ _wide = data[p].ord
283
+ _trans = if ( _slen > 0 &&
284
+ _statement_autocomplete_trans_keys[_keys] <= _wide &&
285
+ _wide <= _statement_autocomplete_trans_keys[_keys + 1]
286
+ ) then
287
+ _statement_autocomplete_indicies[ _inds + _wide - _statement_autocomplete_trans_keys[_keys] ]
288
+ else
289
+ _statement_autocomplete_indicies[ _inds + _slen ]
290
+ end
291
+ end
292
+ if _goto_level <= _eof_trans
293
+ cs = _statement_autocomplete_trans_targs[_trans]
294
+ if _statement_autocomplete_trans_actions[_trans] != 0
295
+ case _statement_autocomplete_trans_actions[_trans]
296
+ when 12 then
297
+ begin
298
+ te = p+1
299
+ begin
300
+ # begin ruby
301
+ trace('EOF_RELATIONSHIP')
302
+
303
+ @relationship =
304
+ relationship(
305
+ nil,
306
+ character_range: [ts, ts])
307
+
308
+ range_end =
309
+ if @statement_stack[-1].object?
310
+ @statement_stack[-1].object.range_end
311
+ else
312
+ @relationship.range_end
313
+ end
314
+
315
+ @statement_stack[-1] =
316
+ statement(
317
+ @statement_stack[-1].subject,
318
+ @relationship,
319
+ @statement_stack[-1].object,
320
+ character_range: [@statement_stack[-1].range_start, range_end])
321
+
322
+ # go back one char to force EOF on return to term scanner
323
+ p -= 1
324
+
325
+ # return to term scanner
326
+ begin
327
+ top -= 1
328
+ cs = stack[top]
329
+ _goto_level = _again
330
+ next
331
+ end
332
+
333
+ # end ruby
334
+ end
335
+ end
336
+ when 13 then
337
+ begin
338
+ te = p
339
+ p = p - 1; begin
340
+ # begin ruby
341
+ trace('RELATIONSHIP')
342
+ @last_state = :RELATIONSHIP
343
+
344
+ @relationship =
345
+ relationship(
346
+ utf8_string(data[ts...te]),
347
+ character_range: [ts, te])
348
+
349
+ range_end =
350
+ if @statement_stack[-1].object?
351
+ @statement_stack[-1].object.range_end
352
+ else
353
+ @relationship.range_end
354
+ end
355
+
356
+ @statement_stack[-1] =
357
+ statement(
358
+ @statement_stack[-1].subject,
359
+ @relationship,
360
+ @statement_stack[-1].object,
361
+ character_range: [@statement_stack[-1].range_start, range_end])
362
+
363
+ # return to term scanner
364
+ begin
365
+ top -= 1
366
+ cs = stack[top]
367
+ _goto_level = _again
368
+ next
369
+ end
370
+
371
+
372
+ # end ruby
373
+ end
374
+ end
375
+ when 10 then
376
+ begin
377
+ te = p+1
378
+ begin
379
+ # begin ruby
380
+ trace('STRING')
381
+ @last_state = :STRING
382
+
383
+ @value =
384
+ string(
385
+ utf8_string(data[ts...te]),
386
+ character_range: [ts, te])
387
+ if !@prefix.nil?
388
+ @param =
389
+ parameter(
390
+ @prefix,
391
+ value(
392
+ @value,
393
+ character_range: @value.character_range),
394
+ character_range: [@prefix.range_start, @value.range_end])
395
+ @value = nil
396
+ end
397
+ # end ruby
398
+ end
399
+ end
400
+ when 4 then
401
+ begin
402
+ te = p+1
403
+ begin
404
+ # begin ruby
405
+ trace('O_PAREN')
406
+
407
+ case @last_state
408
+ when :RELATIONSHIP
409
+ @statement_stack << @statement_stack[-1]
410
+ @statement_stack[-1] = statement(nil, nil, nil)
411
+ @relationship = nil
412
+
413
+ @term_stack <<
414
+ term(
415
+ function(
416
+ nil,
417
+ character_range: [ts, ts]),
418
+ character_range: [ts, te])
419
+ else
420
+ term =
421
+ if @value == nil
422
+ term(
423
+ function(
424
+ nil,
425
+ character_range: [ts, ts]),
426
+ character_range: [ts, te])
427
+ else
428
+ term(
429
+ function(
430
+ @value,
431
+ character_range: @value.character_range),
432
+ character_range: [@value.range_start, @value.range_end + 1])
433
+ end
434
+
435
+ @term_stack << term
436
+ @value = nil
437
+ end
438
+
439
+ @paren_counter += 1
440
+ @last_state = :O_PAREN
441
+ # end ruby
442
+ end
443
+ end
444
+ when 5 then
445
+ begin
446
+ te = p+1
447
+ begin
448
+ # begin ruby
449
+ trace('C_PAREN')
450
+ @paren_counter -= 1
451
+
452
+ case @last_state
453
+ when :COMMA, :O_PAREN
454
+ function, *arguments = @term_stack[-1].children
455
+ empty_argument =
456
+ argument(
457
+ nil,
458
+ character_range: [ts, ts])
459
+ @term_stack[-1] =
460
+ term(
461
+ *[function, arguments, empty_argument].flatten.compact,
462
+ character_range: [function.range_start, te])
463
+ else
464
+ if !@param.nil?
465
+ function, *arguments = @term_stack[-1].children
466
+ arg_from_param =
467
+ argument(
468
+ @param,
469
+ character_range: @param.character_range)
470
+ @term_stack[-1] =
471
+ term(
472
+ *[function, arguments, arg_from_param].flatten.compact,
473
+ character_range: [function.range_start, te])
474
+ @param = nil
475
+ @prefix = nil
476
+ elsif !@value.nil?
477
+ function, *arguments = @term_stack[-1].children
478
+ arg_from_value =
479
+ argument(
480
+ parameter(
481
+ nil,
482
+ value(
483
+ @value,
484
+ character_range: @value.character_range),
485
+ character_range: @value.character_range),
486
+ character_range: @value.character_range)
487
+ @term_stack[-1] =
488
+ term(
489
+ *[function, arguments, arg_from_value].flatten.compact,
490
+ character_range: [function.range_start, @value.range_end + 1])
491
+ @value = nil
492
+ elsif !@prefix.nil?
493
+ function, *arguments = @term_stack[-1].children
494
+ arg_from_prefix =
495
+ argument(
496
+ parameter(
497
+ @prefix,
498
+ value(
499
+ identifier(
500
+ '',
501
+ character_range: [@prefix.range_end + 1, @prefix.range_end + 1]),
502
+ character_range: [@prefix.range_end + 1, @prefix.range_end + 1]),
503
+ character_range: @prefix.character_range),
504
+ character_range: @prefix.character_range)
505
+ @term_stack[-1] =
506
+ term(
507
+ *[function, arguments, arg_from_prefix].flatten.compact,
508
+ character_range: [function.range_start, @prefix.range_end + 1])
509
+ @prefix = nil
510
+ else
511
+ term = @term_stack[-1]
512
+ term.character_range = [term.range_start, term.range_end + 1]
513
+ end
514
+ end
515
+
516
+ if @term_stack.length > 1
517
+ # pop stack
518
+ inner = @term_stack.pop
519
+ outer = @term_stack[-1]
520
+
521
+ # reconstruct previous term on stack
522
+ @term_stack[-1] =
523
+ term(
524
+ outer.function,
525
+ *(outer.arguments << argument(inner, character_range: inner.character_range)),
526
+ character_range: [outer.range_start, inner.range_end + 1])
527
+ end
528
+
529
+ trace("C_PAREN: @term_stack is\n#{@term_stack}")
530
+ if @term_stack.length == 1
531
+ @bel_part = :relationship
532
+ trace("set @bel_part to #{@bel_part}")
533
+ end
534
+
535
+ @last_state = :C_PAREN
536
+ # end ruby
537
+ end
538
+ end
539
+ when 7 then
540
+ begin
541
+ te = p+1
542
+ begin
543
+ # begin ruby
544
+ trace('COLON')
545
+ @last_state = :COLON
546
+
547
+ if !@value.nil?
548
+ @prefix =
549
+ prefix(
550
+ @value,
551
+ character_range: @value.character_range)
552
+ @value = nil
553
+ else
554
+ @prefix =
555
+ prefix(
556
+ nil,
557
+ character_range: [ts, ts])
558
+ end
559
+ # end ruby
560
+ end
561
+ end
562
+ when 6 then
563
+ begin
564
+ te = p+1
565
+ begin
566
+ # begin ruby
567
+ trace('COMMA')
568
+ @last_state = :COMMA
569
+
570
+ if !@term_stack.empty?
571
+ if !@param.nil?
572
+ function, *arguments = @term_stack[-1].children
573
+ arg_from_param =
574
+ argument(
575
+ @param,
576
+ character_range: @param.character_range)
577
+ @term_stack[-1] =
578
+ term(
579
+ *([function, arguments, arg_from_param].flatten.compact),
580
+ character_range: [function.range_start, @param.range_end + 1])
581
+ elsif !@value.nil?
582
+ function, *arguments = @term_stack[-1].children
583
+ arg_from_value =
584
+ argument(
585
+ parameter(
586
+ nil,
587
+ value(
588
+ @value,
589
+ character_range: @value.character_range),
590
+ character_range: @value.character_range),
591
+ character_range: @value.character_range)
592
+ @term_stack[-1] =
593
+ term(
594
+ *[function, arguments, arg_from_value].flatten.compact,
595
+ character_range: [function.range_start, arg_from_value.range_end + 1])
596
+ end
597
+ end
598
+
599
+ @param = nil
600
+ @prefix = nil
601
+ @value = nil
602
+ # end ruby
603
+ end
604
+ end
605
+ when 3 then
606
+ begin
607
+ te = p+1
608
+ begin
609
+ # begin ruby
610
+ trace('EOF')
611
+
612
+ if @term_stack.empty?
613
+ # coerce what has been completed into a term
614
+ if !@param.nil?
615
+ @term_stack[0] =
616
+ term(
617
+ nil,
618
+ argument(
619
+ @param,
620
+ character_range: @param.character_range),
621
+ character_range: @param.character_range)
622
+ elsif !@prefix.nil?
623
+ range = [@prefix.range_start, @prefix.range_end + 1]
624
+ @term_stack[0] =
625
+ term(
626
+ nil,
627
+ argument(
628
+ parameter(
629
+ @prefix,
630
+ nil,
631
+ character_range: range),
632
+ character_range: range),
633
+ character_range: range)
634
+ elsif !@value.nil?
635
+ @term_stack[0] =
636
+ term(
637
+ nil,
638
+ argument(
639
+ parameter(
640
+ nil,
641
+ value(
642
+ @value,
643
+ character_range: @value.character_range),
644
+ character_range: @value.character_range),
645
+ character_range: @value.character_range),
646
+ character_range: @value.character_range)
647
+ end
648
+ else
649
+ case @last_state
650
+ when :IDENT
651
+ if !@param.nil?
652
+ function, *arguments = @term_stack[-1].children
653
+ arg_from_param =
654
+ argument(
655
+ @param,
656
+ character_range: @param.character_range)
657
+ @term_stack[-1] =
658
+ term(
659
+ *[function, arguments, arg_from_param].flatten.compact,
660
+ character_range: [function.range_start, arg_from_param.range_end])
661
+ elsif !@value.nil?
662
+ function, *arguments = @term_stack[-1].children
663
+ arg_from_value =
664
+ argument(
665
+ parameter(
666
+ nil,
667
+ value(
668
+ @value,
669
+ character_range: @value.character_range),
670
+ character_range: @value.character_range),
671
+ character_range: @value.character_range)
672
+ @term_stack[-1] =
673
+ term(
674
+ *[function, arguments, arg_from_value].flatten.compact,
675
+ character_range: [function.range_start, @value.range_end])
676
+ end
677
+ when :STRING
678
+ if !@param.nil?
679
+ function, *arguments = @term_stack[-1].children
680
+ arg_from_param =
681
+ argument(
682
+ @param,
683
+ character_range: @param.character_range)
684
+ @term_stack[-1] =
685
+ term(
686
+ *[function, arguments, arg_from_param].flatten.compact,
687
+ character_range: [function.range_start, arg_from_param.range_end])
688
+ elsif !@value.nil?
689
+ function, *arguments = @term_stack[-1].children
690
+ arg_from_value =
691
+ argument(
692
+ parameter(
693
+ nil,
694
+ value(
695
+ @value,
696
+ character_range: @value.character_range),
697
+ character_range: @value.character_range),
698
+ character_range: @value.character_range)
699
+ @term_stack[-1] =
700
+ term(
701
+ *[function, *arguments, arg_from_value],
702
+ character_range: [function.range_start, arg_from_value.range_end])
703
+ end
704
+ when :COMMA, :O_PAREN
705
+ function, *arguments = @term_stack[-1].children
706
+ empty_argument =
707
+ argument(
708
+ nil,
709
+ character_range: [te - 1, te - 1])
710
+ @term_stack[-1] =
711
+ term(
712
+ *[function, *arguments, empty_argument],
713
+ character_range: [function.range_start, empty_argument.range_end])
714
+ when :COLON
715
+ function, *arguments = @term_stack[-1].children
716
+ empty_argument =
717
+ argument(
718
+ parameter(
719
+ @prefix,
720
+ nil,
721
+ character_range: [@prefix.range_start, @prefix.range_end + 1]),
722
+ character_range: [@prefix.range_start, @prefix.range_end + 1])
723
+ @term_stack[-1] =
724
+ term(
725
+ *[function, *arguments, empty_argument].flatten.compact,
726
+ character_range: [function.range_start, empty_argument.range_end])
727
+ end
728
+ end
729
+
730
+ # iff we have completed any term-related node
731
+ if !@term_stack.empty?
732
+ # combine terms
733
+ while @term_stack.length > 1
734
+ # pop stack
735
+ inner = @term_stack.pop
736
+ outer = @term_stack[-1]
737
+
738
+ # reconstruct previous term on stack
739
+ @term_stack[-1] =
740
+ term(
741
+ outer.function,
742
+ *(outer.arguments << argument(inner, character_range: inner.character_range)),
743
+ character_range: [outer.range_start, inner.range_end + 1])
744
+ end
745
+
746
+ # add to statement_ast
747
+ completed_term = @term_stack[-1]
748
+ if @statement_stack[-1].subject.nil?
749
+ @statement_stack[-1] =
750
+ statement(
751
+ subject(
752
+ completed_term,
753
+ character_range: completed_term.character_range),
754
+ nil,
755
+ nil,
756
+ character_range: completed_term.character_range)
757
+ elsif @statement_stack[-1].object.nil?
758
+ object_node =
759
+ object(
760
+ completed_term,
761
+ character_range: completed_term.character_range)
762
+ @statement_stack[-1] =
763
+ statement(
764
+ @statement_stack[-1].subject,
765
+ @statement_stack[-1].relationship,
766
+ object_node,
767
+ character_range: [@statement_stack[-1].range_start, object_node.range_end])
768
+ end
769
+ end
770
+
771
+ # yield statement
772
+ while @statement_stack.length > 1
773
+ # pop stack
774
+ nested = @statement_stack.pop
775
+ outer = @statement_stack[-1]
776
+
777
+ # reconstruct previous statement on stack
778
+ @statement_stack[-1] =
779
+ statement(
780
+ outer.subject,
781
+ outer.relationship,
782
+ object(
783
+ nested,
784
+ character_range: nested.character_range),
785
+ character_range: [outer.range_start, nested.range_end + 1])
786
+ end
787
+ yield @statement_stack.pop
788
+ # end ruby
789
+ end
790
+ end
791
+ when 11 then
792
+ begin
793
+ te = p
794
+ p = p - 1; begin
795
+ # begin ruby
796
+ trace('IDENT')
797
+ @last_state = :IDENT
798
+
799
+ @value =
800
+ identifier(
801
+ utf8_string(data[ts...te]),
802
+ character_range: [ts, te])
803
+ if !@prefix.nil?
804
+ @param =
805
+ parameter(
806
+ @prefix,
807
+ value(
808
+ @value,
809
+ character_range: @value.character_range),
810
+ character_range: [@prefix.range_start, @value.range_end])
811
+ @value = nil
812
+ end
813
+ # end ruby
814
+ end
815
+ end
816
+ when 9 then
817
+ begin
818
+ te = p
819
+ p = p - 1; begin
820
+ # begin ruby
821
+ trace('STRING')
822
+ @last_state = :STRING
823
+
824
+ @value =
825
+ string(
826
+ utf8_string(data[ts...te]),
827
+ character_range: [ts, te])
828
+ if !@prefix.nil?
829
+ @param =
830
+ parameter(
831
+ @prefix,
832
+ value(
833
+ @value,
834
+ character_range: @value.character_range),
835
+ character_range: [@prefix.range_start, @value.range_end])
836
+ @value = nil
837
+ end
838
+ # end ruby
839
+ end
840
+ end
841
+ when 8 then
842
+ begin
843
+ te = p
844
+ p = p - 1; begin
845
+ # begin ruby
846
+ spaces = te-ts
847
+ trace("SPACES (#{spaces})")
848
+
849
+ case @bel_part
850
+ when :relationship
851
+ if @relationship
852
+ spaces -= 1
853
+ @bel_part = :term
854
+
855
+ # remove spaces and adjust pointers by the number of spaces removed
856
+ data.slice!(ts, spaces)
857
+ p -= spaces
858
+ pe -= spaces
859
+ eof -= spaces
860
+
861
+ if @original_caret > ts
862
+ if @original_caret < te
863
+ @space_adjusted_caret_position -= (@original_caret - ts)
864
+ else
865
+ @space_adjusted_caret_position -= spaces
866
+ end
867
+ end
868
+ else
869
+ spaces -= 1
870
+ # remove spaces and adjust pointers by the number of spaces removed
871
+ data.slice!(ts, spaces)
872
+ p -= spaces
873
+ pe -= spaces
874
+ eof -= spaces
875
+
876
+ if @original_caret > ts
877
+ if @original_caret < te
878
+ @space_adjusted_caret_position -= (@original_caret - ts)
879
+ else
880
+ @space_adjusted_caret_position -= spaces
881
+ end
882
+ end
883
+
884
+ # pop off term; add to statement_ast
885
+ completed_term = @term_stack[-1]
886
+ if @statement_stack[-1].subject.nil?
887
+ @statement_stack[-1] =
888
+ statement(
889
+ subject(
890
+ completed_term,
891
+ character_range: completed_term.character_range),
892
+ nil,
893
+ nil,
894
+ character_range: completed_term.character_range)
895
+ elsif @statement_stack[-1].object.nil?
896
+ object_node =
897
+ object(
898
+ completed_term,
899
+ character_range: completed_term.character_range)
900
+ @statement_stack[-1] =
901
+ statement(
902
+ @statement_stack[-1].subject,
903
+ @statement_stack[-1].relationship,
904
+ object_node,
905
+ character_range: [@statement_stack[-1].range_start, object_node.range_end])
906
+ end
907
+
908
+ @term_stack = []
909
+
910
+ # push the target state, jump to relationship scanner
911
+ # ...eventually to return
912
+ @relationship = nil
913
+ begin
914
+ stack[top] = cs
915
+ top+= 1
916
+ cs = 5
917
+ _goto_level = _again
918
+ next
919
+ end
920
+
921
+ end
922
+ when :term
923
+ if @last_state == :COMMA
924
+ spaces -= 1
925
+ end
926
+
927
+ # remove spaces and adjust pointers by the number of spaces removed
928
+ data.slice!(ts, spaces)
929
+ p -= spaces
930
+ pe -= spaces
931
+ eof -= spaces
932
+
933
+ if @original_caret > ts
934
+ if @original_caret < te
935
+ @space_adjusted_caret_position -= (@original_caret - ts)
936
+ else
937
+ @space_adjusted_caret_position -= spaces
938
+ end
939
+ end
940
+ end
941
+ # end ruby
942
+ end
943
+ end
944
+ end
945
+ end
946
+ end
947
+ if _goto_level <= _again
948
+ case _statement_autocomplete_to_state_actions[cs]
949
+ when 1 then
950
+ begin
951
+ ts = nil; end
952
+ end
953
+
954
+ p += 1
955
+ if p != pe
956
+ _goto_level = _resume
957
+ next
958
+ end
959
+ end
960
+ if _goto_level <= _test_eof
961
+ if p == eof
962
+ if _statement_autocomplete_eof_trans[cs] > 0
963
+ _trans = _statement_autocomplete_eof_trans[cs] - 1;
964
+ _goto_level = _eof_trans
965
+ next;
966
+ end
967
+ end
968
+
969
+ end
970
+ if _goto_level <= _out
971
+ break
972
+ end
973
+ end
974
+ end
975
+
976
+ # end: ragel
977
+ end
978
+ end
979
+ end
980
+ end
981
+ end
982
+ end
983
+
984
+ if __FILE__ == $0
985
+ require 'bel_parser/parsers/serializer'
986
+ class ::AST::Node
987
+ include BELParser::Parsers
988
+
989
+ def _metadata
990
+ ivars = instance_variables - [:@type, :@children, :@hash]
991
+ ivars.map { |iv| [iv, instance_variable_get(iv)] }.to_s
992
+ end
993
+ private :_metadata
994
+
995
+ def to_sexp(indent=0)
996
+ indented = " " * indent
997
+ sexp = "#{indented}(#{fancy_type} #{_metadata}"
998
+
999
+ first_node_child = children.index do |child|
1000
+ child.is_a?(::AST::Node) || child.is_a?(Array)
1001
+ end || children.count
1002
+
1003
+ children.each_with_index do |child, idx|
1004
+ if child.is_a?(::AST::Node) && idx >= first_node_child
1005
+ sexp << "\n#{child.to_sexp(indent + 1)}"
1006
+ else
1007
+ sexp << " #{child.inspect}"
1008
+ end
1009
+ end
1010
+
1011
+ sexp << ")"
1012
+
1013
+ sexp
1014
+ end
1015
+
1016
+ def to_bel
1017
+ serialize(self)
1018
+ end
1019
+ end
1020
+
1021
+ $stdin.each_line do |line|
1022
+ ast, caret = BELParser::Parsers::Expression::StatementAutocomplete.parse(
1023
+ line,
1024
+ line.length-1 # adjust for newline
1025
+ )
1026
+ puts ast.to_sexp(1)
1027
+ puts ast.to_bel
1028
+ puts "#{' ' * caret}|"
1029
+ puts "caret: #{caret}"
1030
+ end
1031
+ end
1032
+ # end ruby
1033
+
1034
+ # vim: ft=ragel ts=2 sw=2 expandtab:
1035
+ # encoding: utf-8