rley 0.7.06 → 0.8.01
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/.rubocop.yml +362 -62
- data/.travis.yml +6 -6
- data/CHANGELOG.md +20 -4
- data/LICENSE.txt +1 -1
- data/README.md +7 -7
- data/examples/NLP/engtagger.rb +193 -190
- data/examples/NLP/nano_eng/nano_en_demo.rb +7 -11
- data/examples/NLP/nano_eng/nano_grammar.rb +21 -21
- data/examples/NLP/pico_en_demo.rb +2 -2
- data/examples/data_formats/JSON/cli_options.rb +1 -1
- data/examples/data_formats/JSON/json_ast_builder.rb +21 -27
- data/examples/data_formats/JSON/json_ast_nodes.rb +12 -21
- data/examples/data_formats/JSON/json_demo.rb +1 -2
- data/examples/data_formats/JSON/json_grammar.rb +13 -13
- data/examples/data_formats/JSON/json_lexer.rb +8 -8
- data/examples/data_formats/JSON/json_minifier.rb +1 -1
- data/examples/general/calc_iter1/calc_ast_builder.rb +13 -10
- data/examples/general/calc_iter1/calc_ast_nodes.rb +23 -37
- data/examples/general/calc_iter1/calc_grammar.rb +7 -6
- data/examples/general/calc_iter1/calc_lexer.rb +6 -4
- data/examples/general/calc_iter1/spec/calculator_spec.rb +5 -5
- data/examples/general/calc_iter2/calc_ast_builder.rb +5 -3
- data/examples/general/calc_iter2/calc_ast_nodes.rb +27 -43
- data/examples/general/calc_iter2/calc_grammar.rb +12 -12
- data/examples/general/calc_iter2/calc_lexer.rb +11 -10
- data/examples/general/calc_iter2/spec/calculator_spec.rb +26 -26
- data/examples/general/left.rb +2 -2
- data/examples/general/right.rb +2 -2
- data/lib/rley.rb +1 -1
- data/lib/rley/base/dotted_item.rb +28 -31
- data/lib/rley/base/grm_items_builder.rb +6 -0
- data/lib/rley/constants.rb +2 -2
- data/lib/rley/engine.rb +22 -25
- data/lib/rley/formatter/asciitree.rb +3 -3
- data/lib/rley/formatter/bracket_notation.rb +1 -8
- data/lib/rley/formatter/debug.rb +6 -6
- data/lib/rley/formatter/json.rb +2 -2
- data/lib/rley/gfg/call_edge.rb +1 -1
- data/lib/rley/gfg/edge.rb +5 -5
- data/lib/rley/gfg/end_vertex.rb +2 -6
- data/lib/rley/gfg/epsilon_edge.rb +1 -5
- data/lib/rley/gfg/grm_flow_graph.rb +27 -23
- data/lib/rley/gfg/item_vertex.rb +10 -10
- data/lib/rley/gfg/non_terminal_vertex.rb +4 -4
- data/lib/rley/gfg/scan_edge.rb +1 -1
- data/lib/rley/gfg/shortcut_edge.rb +2 -2
- data/lib/rley/gfg/start_vertex.rb +4 -8
- data/lib/rley/gfg/vertex.rb +43 -39
- data/lib/rley/interface.rb +16 -0
- data/lib/rley/lexical/token_range.rb +6 -6
- data/lib/rley/notation/all_notation_nodes.rb +2 -0
- data/lib/rley/notation/ast_builder.rb +191 -0
- data/lib/rley/notation/ast_node.rb +44 -0
- data/lib/rley/notation/ast_visitor.rb +113 -0
- data/lib/rley/notation/grammar.rb +49 -0
- data/lib/rley/notation/grammar_builder.rb +504 -0
- data/lib/rley/notation/grouping_node.rb +23 -0
- data/lib/rley/notation/parser.rb +56 -0
- data/lib/rley/notation/sequence_node.rb +35 -0
- data/lib/rley/notation/symbol_node.rb +29 -0
- data/lib/rley/notation/tokenizer.rb +192 -0
- data/lib/rley/parse_forest_visitor.rb +5 -5
- data/lib/rley/parse_rep/ast_base_builder.rb +48 -11
- data/lib/rley/parse_rep/cst_builder.rb +5 -6
- data/lib/rley/parse_rep/parse_forest_builder.rb +22 -18
- data/lib/rley/parse_rep/parse_forest_factory.rb +3 -3
- data/lib/rley/parse_rep/parse_rep_creator.rb +14 -16
- data/lib/rley/parse_rep/parse_tree_builder.rb +4 -4
- data/lib/rley/parse_rep/parse_tree_factory.rb +27 -27
- data/lib/rley/parse_tree_visitor.rb +1 -1
- data/lib/rley/parser/error_reason.rb +4 -5
- data/lib/rley/parser/gfg_chart.rb +118 -26
- data/lib/rley/parser/gfg_parsing.rb +22 -33
- data/lib/rley/parser/parse_entry.rb +25 -31
- data/lib/rley/parser/parse_entry_set.rb +19 -16
- data/lib/rley/parser/parse_entry_tracker.rb +4 -4
- data/lib/rley/parser/parse_tracer.rb +13 -13
- data/lib/rley/parser/parse_walker_factory.rb +23 -28
- data/lib/rley/ptree/non_terminal_node.rb +7 -5
- data/lib/rley/ptree/parse_tree.rb +3 -3
- data/lib/rley/ptree/parse_tree_node.rb +5 -5
- data/lib/rley/ptree/terminal_node.rb +7 -7
- data/lib/rley/rley_error.rb +12 -12
- data/lib/rley/sppf/alternative_node.rb +6 -6
- data/lib/rley/sppf/composite_node.rb +7 -7
- data/lib/rley/sppf/epsilon_node.rb +3 -3
- data/lib/rley/sppf/leaf_node.rb +3 -3
- data/lib/rley/sppf/parse_forest.rb +16 -16
- data/lib/rley/sppf/sppf_node.rb +7 -8
- data/lib/rley/sppf/token_node.rb +3 -3
- data/lib/rley/syntax/{grammar_builder.rb → base_grammar_builder.rb} +61 -23
- data/lib/rley/syntax/grammar.rb +5 -5
- data/lib/rley/syntax/grm_symbol.rb +7 -7
- data/lib/rley/syntax/match_closest.rb +43 -0
- data/lib/rley/syntax/non_terminal.rb +9 -15
- data/lib/rley/syntax/production.rb +16 -10
- data/lib/rley/syntax/symbol_seq.rb +7 -9
- data/lib/rley/syntax/terminal.rb +4 -5
- data/lib/rley/syntax/verbatim_symbol.rb +3 -3
- data/lib/support/base_tokenizer.rb +19 -18
- data/spec/rley/base/dotted_item_spec.rb +2 -2
- data/spec/rley/engine_spec.rb +23 -21
- data/spec/rley/formatter/asciitree_spec.rb +7 -7
- data/spec/rley/formatter/bracket_notation_spec.rb +13 -13
- data/spec/rley/formatter/json_spec.rb +1 -1
- data/spec/rley/gfg/end_vertex_spec.rb +5 -5
- data/spec/rley/gfg/grm_flow_graph_spec.rb +2 -2
- data/spec/rley/gfg/item_vertex_spec.rb +10 -10
- data/spec/rley/gfg/non_terminal_vertex_spec.rb +3 -3
- data/spec/rley/gfg/shortcut_edge_spec.rb +1 -1
- data/spec/rley/gfg/start_vertex_spec.rb +5 -5
- data/spec/rley/gfg/vertex_spec.rb +3 -3
- data/spec/rley/lexical/token_range_spec.rb +16 -16
- data/spec/rley/lexical/token_spec.rb +2 -2
- data/spec/rley/notation/grammar_builder_spec.rb +302 -0
- data/spec/rley/notation/parser_spec.rb +184 -0
- data/spec/rley/notation/tokenizer_spec.rb +370 -0
- data/spec/rley/parse_forest_visitor_spec.rb +165 -163
- data/spec/rley/parse_rep/ambiguous_parse_spec.rb +44 -44
- data/spec/rley/parse_rep/ast_builder_spec.rb +6 -7
- data/spec/rley/parse_rep/cst_builder_spec.rb +5 -5
- data/spec/rley/parse_rep/groucho_spec.rb +24 -26
- data/spec/rley/parse_rep/parse_forest_builder_spec.rb +27 -27
- data/spec/rley/parse_rep/parse_forest_factory_spec.rb +8 -8
- data/spec/rley/parse_rep/parse_tree_factory_spec.rb +3 -3
- data/spec/rley/parse_tree_visitor_spec.rb +10 -8
- data/spec/rley/parser/dangling_else_spec.rb +445 -0
- data/spec/rley/parser/error_reason_spec.rb +6 -6
- data/spec/rley/parser/gfg_earley_parser_spec.rb +120 -12
- data/spec/rley/parser/gfg_parsing_spec.rb +6 -13
- data/spec/rley/parser/parse_entry_spec.rb +19 -19
- data/spec/rley/parser/parse_walker_factory_spec.rb +10 -10
- data/spec/rley/ptree/non_terminal_node_spec.rb +5 -3
- data/spec/rley/ptree/parse_tree_node_spec.rb +4 -4
- data/spec/rley/ptree/terminal_node_spec.rb +6 -6
- data/spec/rley/sppf/alternative_node_spec.rb +6 -6
- data/spec/rley/sppf/non_terminal_node_spec.rb +3 -3
- data/spec/rley/sppf/token_node_spec.rb +4 -4
- data/spec/rley/support/ambiguous_grammar_helper.rb +4 -5
- data/spec/rley/support/grammar_abc_helper.rb +3 -5
- data/spec/rley/support/grammar_ambig01_helper.rb +5 -6
- data/spec/rley/support/grammar_arr_int_helper.rb +5 -6
- data/spec/rley/support/grammar_b_expr_helper.rb +5 -6
- data/spec/rley/support/grammar_int_seq_helper.rb +51 -0
- data/spec/rley/support/grammar_l0_helper.rb +14 -17
- data/spec/rley/support/grammar_pb_helper.rb +8 -7
- data/spec/rley/support/grammar_sppf_helper.rb +3 -3
- data/spec/rley/syntax/{grammar_builder_spec.rb → base_grammar_builder_spec.rb} +35 -16
- data/spec/rley/syntax/grammar_spec.rb +6 -6
- data/spec/rley/syntax/grm_symbol_spec.rb +1 -1
- data/spec/rley/syntax/match_closest_spec.rb +46 -0
- data/spec/rley/syntax/non_terminal_spec.rb +8 -8
- data/spec/rley/syntax/production_spec.rb +17 -13
- data/spec/rley/syntax/symbol_seq_spec.rb +2 -2
- data/spec/rley/syntax/terminal_spec.rb +5 -5
- data/spec/rley/syntax/verbatim_symbol_spec.rb +1 -1
- data/spec/spec_helper.rb +0 -12
- data/spec/support/base_tokenizer_spec.rb +7 -2
- metadata +48 -74
- data/.simplecov +0 -7
- data/lib/rley/parser/parse_state.rb +0 -83
- data/lib/rley/parser/parse_state_tracker.rb +0 -59
- data/lib/rley/parser/state_set.rb +0 -101
- data/spec/rley/parser/parse_state_spec.rb +0 -125
- data/spec/rley/parser/parse_tracer_spec.rb +0 -200
- data/spec/rley/parser/state_set_spec.rb +0 -130
@@ -91,6 +91,7 @@ module Rley # This module is used as a namespace
|
|
91
91
|
if succ.dotted_item.production.nullable?
|
92
92
|
succ_entry = apply_rule(start_entry, succ, pos, pos, :nullable_rule)
|
93
93
|
next unless succ_entry.exit_entry?
|
94
|
+
|
94
95
|
apply_rule(succ_entry, end_vertex, pos, pos, :nullable_rule)
|
95
96
|
end
|
96
97
|
end
|
@@ -178,50 +179,36 @@ module Rley # This module is used as a namespace
|
|
178
179
|
end
|
179
180
|
end
|
180
181
|
|
181
|
-
|
182
|
+
true
|
182
183
|
end
|
183
184
|
|
184
185
|
# Return true if the parse was successful (= input tokens
|
185
186
|
# followed the syntax specified by the grammar)
|
186
|
-
def success?
|
187
|
+
def success?
|
187
188
|
return false if @failure_reason
|
188
189
|
|
189
|
-
|
190
|
+
chart.accepting_entry ? true : false
|
190
191
|
end
|
191
192
|
|
192
193
|
# Return true if there are more than one complete state
|
193
194
|
# for the same lhs and same origin in any state set.
|
194
|
-
def ambiguous?
|
195
|
+
def ambiguous?
|
195
196
|
found = chart.sets.find { |set| !set.ambiguities.empty? }
|
196
|
-
|
197
|
-
end
|
198
|
-
|
199
|
-
# Factory method. Builds a ParseForest from the parse result.
|
200
|
-
# @return [ParseForest]
|
201
|
-
def parse_forest()
|
202
|
-
msg = <<-END_MSG
|
203
|
-
Method Rley::Parser::GFGParsing.parse_forest is deprecated, call
|
204
|
-
Rley::Engine::to_pforest. It will be removed June 1st
|
205
|
-
or version 0.6.1 (whichever is first)
|
206
|
-
END_MSG
|
207
|
-
# warn(msg)
|
208
|
-
factory = ParseRep::ParseForestFactory.new(self)
|
209
|
-
|
210
|
-
return factory.create
|
197
|
+
!found.nil?
|
211
198
|
end
|
212
199
|
|
213
200
|
# Retrieve the very first parse entry added to the chart.
|
214
201
|
# This entry corresponds to the start vertex of the GF graph
|
215
202
|
# with origin equal to zero.
|
216
|
-
def initial_entry
|
217
|
-
|
203
|
+
def initial_entry
|
204
|
+
chart.initial_entry
|
218
205
|
end
|
219
206
|
|
220
207
|
# Retrieve the accepting parse entry that represents
|
221
208
|
# a complete, successful parse
|
222
209
|
# After a successful parse, the last chart entry set
|
223
210
|
# has an end parse entry that involves the start symbol
|
224
|
-
def accepting_entry
|
211
|
+
def accepting_entry
|
225
212
|
return chart.accepting_entry
|
226
213
|
end
|
227
214
|
|
@@ -231,7 +218,7 @@ END_MSG
|
|
231
218
|
end
|
232
219
|
|
233
220
|
# A notification that the parsing reached an end
|
234
|
-
def done
|
221
|
+
def done
|
235
222
|
# Parse not successful and no reason identified
|
236
223
|
# Assuming that parse failed because of a premature end
|
237
224
|
premature_end unless success? || failure_reason
|
@@ -239,7 +226,7 @@ END_MSG
|
|
239
226
|
|
240
227
|
# Clean and normalize the object.
|
241
228
|
# Call this method when the parsing is complete.
|
242
|
-
def tidy_up!
|
229
|
+
def tidy_up!
|
243
230
|
antecedence.each_key do |entry|
|
244
231
|
antecedence[entry].uniq!
|
245
232
|
end
|
@@ -258,7 +245,7 @@ END_MSG
|
|
258
245
|
end
|
259
246
|
|
260
247
|
# @return [String] A human readable representation of itself.
|
261
|
-
def to_s
|
248
|
+
def to_s
|
262
249
|
result = +''
|
263
250
|
result << "success? #{success?}\n"
|
264
251
|
result << "chart:\n"
|
@@ -293,9 +280,8 @@ END_MSG
|
|
293
280
|
|
294
281
|
def apply_rule(antecedentEntry, aVertex, anOrigin, aPosition, aRuleId)
|
295
282
|
consequent = push_entry(aVertex, anOrigin, aPosition, aRuleId)
|
296
|
-
|
297
|
-
|
298
|
-
|
283
|
+
if consequent
|
284
|
+
antecedence[consequent] << antecedentEntry
|
299
285
|
|
300
286
|
=begin
|
301
287
|
# Invariant checks
|
@@ -333,8 +319,10 @@ END_MSG
|
|
333
319
|
end
|
334
320
|
end
|
335
321
|
=end
|
336
|
-
|
337
|
-
|
322
|
+
consequent.add_antecedent(antecedentEntry)
|
323
|
+
end
|
324
|
+
|
325
|
+
consequent
|
338
326
|
end
|
339
327
|
|
340
328
|
# Push a parse entry (vertex + origin) to the
|
@@ -349,15 +337,16 @@ END_MSG
|
|
349
337
|
def forest_builder(anIndex)
|
350
338
|
full_range = { low: 0, high: anIndex }
|
351
339
|
start_production = chart.start_dotted_rule.production
|
352
|
-
|
340
|
+
|
341
|
+
ParseForestBuilder.new(start_production, full_range)
|
353
342
|
end
|
354
343
|
|
355
344
|
# Factory method. Creates and initializes a ParseEntryTracker instance.
|
356
|
-
def new_entry_tracker
|
345
|
+
def new_entry_tracker
|
357
346
|
instance = ParseEntryTracker.new(chart.last_index)
|
358
347
|
instance.parse_entry = accepting_entry
|
359
348
|
|
360
|
-
|
349
|
+
instance
|
361
350
|
end
|
362
351
|
end # class
|
363
352
|
end # module
|
@@ -32,7 +32,7 @@ module Rley # This module is used as a namespace
|
|
32
32
|
# Returns a string containing a human-readable representation of the
|
33
33
|
# production.
|
34
34
|
# @return [String]
|
35
|
-
def inspect
|
35
|
+
def inspect
|
36
36
|
result = selfie
|
37
37
|
result << ' @antecedents=['
|
38
38
|
antecedents.each do |antec|
|
@@ -40,7 +40,7 @@ module Rley # This module is used as a namespace
|
|
40
40
|
end
|
41
41
|
result << ']>'
|
42
42
|
|
43
|
-
|
43
|
+
result
|
44
44
|
end
|
45
45
|
|
46
46
|
# Add a link to an antecedent parse entry
|
@@ -53,56 +53,55 @@ module Rley # This module is used as a namespace
|
|
53
53
|
def ==(other)
|
54
54
|
return true if equal? other
|
55
55
|
|
56
|
-
|
57
|
-
return result
|
56
|
+
(vertex == other.vertex) && (origin == other.origin)
|
58
57
|
end
|
59
|
-
|
58
|
+
|
60
59
|
def hash
|
61
|
-
@hash ||=
|
60
|
+
@hash ||= (vertex.oid_str + "-#{origin}").hash
|
62
61
|
end
|
63
62
|
|
64
63
|
# Returns true iff the vertex is a start vertex (i.e. of the form: .X)
|
65
|
-
def start_entry?
|
66
|
-
|
64
|
+
def start_entry?
|
65
|
+
vertex.kind_of?(GFG::StartVertex)
|
67
66
|
end
|
68
67
|
|
69
68
|
# Returns true iff the vertex is at the start of rhs
|
70
69
|
# (i.e. of the form: X => .Y
|
71
|
-
def entry_entry?
|
70
|
+
def entry_entry?
|
72
71
|
return false unless vertex.kind_of?(GFG::ItemVertex)
|
73
72
|
|
74
|
-
|
73
|
+
vertex.dotted_item.at_start?
|
75
74
|
end
|
76
75
|
|
77
76
|
# Returns true iff the vertex corresponds to a dotted item
|
78
77
|
# X => Y
|
79
78
|
def dotted_entry?
|
80
|
-
|
79
|
+
vertex.kind_of?(GFG::ItemVertex)
|
81
80
|
end
|
82
81
|
|
83
82
|
# Returns true iff the vertex is at end of rhs (i.e. of the form: X => Y.)
|
84
|
-
def exit_entry?
|
85
|
-
|
83
|
+
def exit_entry?
|
84
|
+
vertex.complete?
|
86
85
|
end
|
87
86
|
|
88
87
|
# Returns true iff the vertex is an end vertex (i.e. of the form: X.)
|
89
|
-
def end_entry?
|
90
|
-
|
88
|
+
def end_entry?
|
89
|
+
vertex.kind_of?(GFG::EndVertex)
|
91
90
|
end
|
92
91
|
|
93
92
|
# Return the symbol before the dot (if any)
|
94
|
-
def prev_symbol
|
95
|
-
|
93
|
+
def prev_symbol
|
94
|
+
vertex.prev_symbol
|
96
95
|
end
|
97
96
|
|
98
97
|
# Return the symbol after the dot (if any)
|
99
|
-
def next_symbol
|
100
|
-
|
98
|
+
def next_symbol
|
99
|
+
vertex.next_symbol
|
101
100
|
end
|
102
101
|
|
103
102
|
# Return true if the entry has no antecedent entry
|
104
|
-
def orphan?
|
105
|
-
|
103
|
+
def orphan?
|
104
|
+
antecedents.empty?
|
106
105
|
end
|
107
106
|
|
108
107
|
=begin
|
@@ -117,11 +116,6 @@ module Rley # This module is used as a namespace
|
|
117
116
|
return vertex.predicted_item?
|
118
117
|
end
|
119
118
|
|
120
|
-
# Next expected symbol in the production
|
121
|
-
def next_symbol()
|
122
|
-
return vertex.next_symbol
|
123
|
-
end
|
124
|
-
|
125
119
|
# Does this parse state have the 'other' as successor?
|
126
120
|
def precedes?(other)
|
127
121
|
return false if self == other
|
@@ -145,21 +139,21 @@ module Rley # This module is used as a namespace
|
|
145
139
|
# The format of the text representation is
|
146
140
|
# "format of dotted rule" + " | " + origin
|
147
141
|
# @return [String]
|
148
|
-
def to_s
|
149
|
-
|
142
|
+
def to_s
|
143
|
+
vertex.label + " | #{origin}"
|
150
144
|
end
|
151
145
|
|
152
146
|
protected
|
153
147
|
|
154
148
|
# Returns a human-readable and partial representation of itself.
|
155
149
|
# @return [String]
|
156
|
-
def selfie
|
150
|
+
def selfie
|
157
151
|
result = +"#<#{self.class.name}:#{object_id}"
|
158
152
|
result << " @vertex=<#{vertex.class.name}:#{vertex.object_id}"
|
159
153
|
result << " label=#{vertex.label}>"
|
160
154
|
result << " @origin=#{origin}"
|
161
155
|
|
162
|
-
|
156
|
+
result
|
163
157
|
end
|
164
158
|
|
165
159
|
private
|
@@ -169,7 +163,7 @@ module Rley # This module is used as a namespace
|
|
169
163
|
def valid_vertex(aVertex)
|
170
164
|
raise StandardError, 'GFG vertex cannot be nil' if aVertex.nil?
|
171
165
|
|
172
|
-
|
166
|
+
aVertex
|
173
167
|
end
|
174
168
|
end # class
|
175
169
|
end # module
|
@@ -16,44 +16,45 @@ module Rley # This module is used as a namespace
|
|
16
16
|
|
17
17
|
# @return [Array<ParseEntry>] The array of parse entries
|
18
18
|
attr_reader :entries
|
19
|
-
|
19
|
+
|
20
20
|
# @return [Hash] A Hash with pairs { hash of ParseEntry => ParseEntry }
|
21
|
-
attr_reader :membership
|
21
|
+
attr_reader :membership
|
22
22
|
|
23
23
|
# Constructor.
|
24
|
-
def initialize
|
24
|
+
def initialize
|
25
25
|
@entries = []
|
26
26
|
@membership = {}
|
27
27
|
@entries4term = Hash.new { |hash, key| hash[key] = [] }
|
28
28
|
@entries4n_term = Hash.new { |hash, key| hash[key] = [] }
|
29
29
|
end
|
30
|
-
|
31
|
-
# Returns a string containing a human-readable representation of the
|
30
|
+
|
31
|
+
# Returns a string containing a human-readable representation of the
|
32
32
|
# set of parse entries.
|
33
33
|
# @return [String]
|
34
|
-
def inspect
|
34
|
+
def inspect
|
35
35
|
result = +"#<#{self.class.name}:#{object_id}"
|
36
36
|
result << ' @entries=['
|
37
37
|
entries.each { |e| result << e.inspect }
|
38
38
|
result << ']>'
|
39
|
-
|
39
|
+
|
40
|
+
result
|
40
41
|
end
|
41
42
|
|
42
43
|
# Access the entry at given position
|
43
44
|
def [](index)
|
44
|
-
|
45
|
+
entries[index]
|
45
46
|
end
|
46
47
|
|
47
48
|
# Returns a Hash with pairs of the form:
|
48
49
|
# terminal symbol => [ parse entry expecting the terminal ]
|
49
50
|
def entries4term(aTerminal)
|
50
|
-
|
51
|
+
@entries4term.fetch(aTerminal, [])
|
51
52
|
end
|
52
53
|
|
53
54
|
# Returns a Hash with pairs of the form:
|
54
55
|
# non terminal symbol => [ parse entry expecting the non-terminal ]
|
55
56
|
def entries4n_term(aNonTerminal)
|
56
|
-
|
57
|
+
@entries4n_term.fetch(aNonTerminal, [])
|
57
58
|
end
|
58
59
|
|
59
60
|
# Append the given entry (if it isn't yet in the set)
|
@@ -71,17 +72,17 @@ module Rley # This module is used as a namespace
|
|
71
72
|
result = anEntry
|
72
73
|
end
|
73
74
|
|
74
|
-
|
75
|
+
result
|
75
76
|
end
|
76
77
|
|
77
78
|
# Return an Array of Arrays of ambiguous parse entries.
|
78
|
-
def ambiguities
|
79
|
+
def ambiguities
|
79
80
|
complete_entries = entries.select(&:exit_entry?)
|
80
81
|
return [] if complete_entries.size <= 1
|
81
82
|
|
82
83
|
# Group parse entries by lhs symbol and origin
|
83
84
|
groupings = complete_entries.group_by do |entry|
|
84
|
-
entry.vertex.
|
85
|
+
entry.vertex.dotted_item.lhs.object_id.to_s
|
85
86
|
end
|
86
87
|
|
87
88
|
# Retain the groups having more than one element.
|
@@ -90,20 +91,22 @@ module Rley # This module is used as a namespace
|
|
90
91
|
ambiguous_groups << a_group if a_group.size > 1
|
91
92
|
end
|
92
93
|
|
93
|
-
|
94
|
+
ambiguous_groups
|
94
95
|
end
|
95
96
|
|
96
97
|
# The list of distinct expected terminal symbols. An expected symbol
|
97
98
|
# is on the left of a dot in a parse state of the parse set.
|
98
|
-
def expected_terminals
|
99
|
+
def expected_terminals
|
99
100
|
return @entries4term.keys
|
100
101
|
end
|
101
|
-
|
102
|
+
|
102
103
|
def count_edges
|
104
|
+
# rubocop: disable Lint/UselessAssignment
|
103
105
|
entries.reduce(0) do |sub_result, entry|
|
104
106
|
sub_result += entry.vertex.edges.size
|
105
107
|
end
|
106
108
|
end
|
109
|
+
# rubocop: enable Lint/UselessAssignment
|
107
110
|
|
108
111
|
private
|
109
112
|
|
@@ -40,16 +40,16 @@ module Rley # This module is used as a namespace
|
|
40
40
|
end
|
41
41
|
|
42
42
|
# The dotted item for the current parse entry.
|
43
|
-
def curr_dotted_item
|
43
|
+
def curr_dotted_item
|
44
44
|
parse_entry.dotted_rule
|
45
45
|
end
|
46
46
|
|
47
|
-
def symbol_on_left
|
48
|
-
|
47
|
+
def symbol_on_left
|
48
|
+
curr_dotted_item.prev_symbol
|
49
49
|
end
|
50
50
|
|
51
51
|
# Notification that one begins with the previous entry set
|
52
|
-
def to_prev_entry_set
|
52
|
+
def to_prev_entry_set
|
53
53
|
self.entry_set_index = entry_set_index - 1
|
54
54
|
end
|
55
55
|
end # class
|
@@ -11,9 +11,7 @@ module Rley # This module is used as a namespace
|
|
11
11
|
|
12
12
|
# The trace level
|
13
13
|
attr_reader(:level)
|
14
|
-
|
15
14
|
attr_reader(:lexemes)
|
16
|
-
|
17
15
|
attr_reader(:col_width)
|
18
16
|
|
19
17
|
def initialize(aTraceLevel, anIO, aTokenSequence)
|
@@ -36,8 +34,8 @@ module Rley # This module is used as a namespace
|
|
36
34
|
def trace_scanning(aStatesetIndex, aParseState)
|
37
35
|
return unless level
|
38
36
|
|
39
|
-
scan_picture =
|
40
|
-
org = OpenStruct.new(origin: aStatesetIndex - 1,
|
37
|
+
scan_picture = "[#{'-' * (col_width - 1)}]"
|
38
|
+
org = OpenStruct.new(origin: aStatesetIndex - 1,
|
41
39
|
dotted_rule: aParseState.dotted_rule)
|
42
40
|
trace_diagram(aStatesetIndex, org, scan_picture)
|
43
41
|
end
|
@@ -47,35 +45,36 @@ module Rley # This module is used as a namespace
|
|
47
45
|
|
48
46
|
trace_diagram(aStatesetIndex, aParseState, '>')
|
49
47
|
end
|
50
|
-
|
48
|
+
|
51
49
|
def trace_completion(aStatesetIndex, aParseState)
|
52
50
|
return unless level
|
53
51
|
|
54
|
-
if aStatesetIndex == lexemes.size && aParseState.origin.zero? &&
|
52
|
+
if aStatesetIndex == lexemes.size && aParseState.origin.zero? &&
|
55
53
|
aParseState.complete?
|
56
54
|
picture = '=' * (col_width * lexemes.size - 1)
|
57
55
|
else
|
58
56
|
count = col_width * (aStatesetIndex - aParseState.origin) - 1
|
59
57
|
picture = '-' * count
|
60
58
|
end
|
61
|
-
completion_picture =
|
59
|
+
completion_picture = "[#{picture}#{aParseState.complete? ? ']' : '>'}"
|
62
60
|
trace_diagram(aStatesetIndex, aParseState, completion_picture)
|
63
61
|
end
|
64
62
|
|
65
63
|
private
|
66
64
|
|
67
|
-
def emit_tokens
|
65
|
+
def emit_tokens
|
68
66
|
literals = lexemes.map { |lx| "'#{lx}'" }
|
69
|
-
print_if 1,
|
67
|
+
print_if 1, "[#{literals.join(', ')}]\n"
|
70
68
|
end
|
71
69
|
|
72
|
-
def emit_heading
|
70
|
+
def emit_heading
|
73
71
|
longest = lexemes.map(&:length).max
|
74
72
|
@col_width = longest + 3
|
75
73
|
headers = lexemes.map { |l| l.center(col_width - 1, ' ').to_s }
|
76
|
-
print_if 1,
|
74
|
+
print_if 1, "|.#{headers.join('.')}.|\n"
|
77
75
|
end
|
78
76
|
|
77
|
+
# rubocop: disable Style/StringConcatenation
|
79
78
|
def padding(aStatesetIndex, aParseState, aPicture)
|
80
79
|
l_pad_pattern = '.' + ' ' * (col_width - 1)
|
81
80
|
left_padding = l_pad_pattern * [0, aParseState.origin].max
|
@@ -83,6 +82,7 @@ module Rley # This module is used as a namespace
|
|
83
82
|
right_padding = r_pad_pattern * (lexemes.size - aStatesetIndex)
|
84
83
|
return left_padding + aPicture + right_padding
|
85
84
|
end
|
85
|
+
# rubocop: enable Style/StringConcatenation
|
86
86
|
|
87
87
|
def parse_state_str(aStatesetIndex, aParseState)
|
88
88
|
"[#{aParseState.origin}:#{aStatesetIndex}] #{aParseState.dotted_rule}"
|
@@ -91,10 +91,10 @@ module Rley # This module is used as a namespace
|
|
91
91
|
def trace_diagram(aStatesetIndex, aParseState, aPicture)
|
92
92
|
diagram = padding(aStatesetIndex, aParseState, aPicture)
|
93
93
|
prefix = '|'
|
94
|
-
suffix =
|
94
|
+
suffix = "| #{parse_state_str(aStatesetIndex, aParseState)}"
|
95
95
|
trace = prefix + diagram + suffix
|
96
96
|
|
97
|
-
print_if 1, trace
|
97
|
+
print_if 1, "#{trace}\n"
|
98
98
|
end
|
99
99
|
end # class
|
100
100
|
end # module
|