rley 0.7.03 → 0.7.08
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +362 -62
- data/.travis.yml +6 -7
- data/CHANGELOG.md +20 -1
- data/LICENSE.txt +1 -1
- data/README.md +6 -7
- data/Rakefile +2 -0
- data/appveyor.yml +2 -4
- data/examples/NLP/benchmark_pico_en.rb +2 -0
- data/examples/NLP/engtagger.rb +193 -188
- data/examples/NLP/nano_eng/nano_en_demo.rb +2 -0
- data/examples/NLP/nano_eng/nano_grammar.rb +7 -5
- data/examples/NLP/pico_en_demo.rb +2 -0
- data/examples/data_formats/JSON/cli_options.rb +3 -1
- data/examples/data_formats/JSON/json_ast_builder.rb +14 -9
- data/examples/data_formats/JSON/json_ast_nodes.rb +14 -21
- data/examples/data_formats/JSON/json_demo.rb +2 -0
- data/examples/data_formats/JSON/json_grammar.rb +4 -2
- data/examples/data_formats/JSON/json_lexer.rb +10 -8
- data/examples/data_formats/JSON/json_minifier.rb +3 -1
- data/examples/general/calc_iter1/calc_ast_builder.rb +15 -10
- data/examples/general/calc_iter1/calc_ast_nodes.rb +25 -37
- data/examples/general/calc_iter1/calc_demo.rb +2 -0
- data/examples/general/calc_iter1/calc_grammar.rb +4 -2
- data/examples/general/calc_iter1/calc_lexer.rb +8 -4
- data/examples/general/calc_iter1/spec/calculator_spec.rb +7 -5
- data/examples/general/calc_iter2/calc_ast_builder.rb +7 -3
- data/examples/general/calc_iter2/calc_ast_nodes.rb +29 -43
- data/examples/general/calc_iter2/calc_demo.rb +2 -0
- data/examples/general/calc_iter2/calc_grammar.rb +5 -3
- data/examples/general/calc_iter2/calc_lexer.rb +13 -10
- data/examples/general/calc_iter2/spec/calculator_spec.rb +28 -26
- data/examples/general/left.rb +4 -2
- data/examples/general/right.rb +4 -2
- data/lib/rley.rb +2 -0
- data/lib/rley/base/base_parser.rb +2 -0
- data/lib/rley/base/dotted_item.rb +38 -41
- data/lib/rley/base/grm_items_builder.rb +2 -0
- data/lib/rley/constants.rb +5 -3
- data/lib/rley/engine.rb +22 -24
- data/lib/rley/formatter/asciitree.rb +6 -4
- data/lib/rley/formatter/base_formatter.rb +2 -0
- data/lib/rley/formatter/bracket_notation.rb +3 -8
- data/lib/rley/formatter/debug.rb +8 -6
- data/lib/rley/formatter/json.rb +4 -2
- data/lib/rley/gfg/call_edge.rb +3 -1
- data/lib/rley/gfg/edge.rb +7 -5
- data/lib/rley/gfg/end_vertex.rb +4 -6
- data/lib/rley/gfg/epsilon_edge.rb +3 -5
- data/lib/rley/gfg/grm_flow_graph.rb +31 -25
- data/lib/rley/gfg/item_vertex.rb +12 -22
- data/lib/rley/gfg/non_terminal_vertex.rb +6 -4
- data/lib/rley/gfg/return_edge.rb +2 -0
- data/lib/rley/gfg/scan_edge.rb +3 -1
- data/lib/rley/gfg/shortcut_edge.rb +4 -2
- data/lib/rley/gfg/start_vertex.rb +6 -8
- data/lib/rley/gfg/vertex.rb +47 -41
- data/lib/rley/lexical/token.rb +3 -1
- data/lib/rley/lexical/token_range.rb +8 -6
- data/lib/rley/parse_forest_visitor.rb +7 -5
- data/lib/rley/parse_rep/ast_base_builder.rb +11 -11
- data/lib/rley/parse_rep/cst_builder.rb +7 -4
- data/lib/rley/parse_rep/parse_forest_builder.rb +36 -25
- data/lib/rley/parse_rep/parse_forest_factory.rb +5 -3
- data/lib/rley/parse_rep/parse_rep_creator.rb +18 -13
- data/lib/rley/parse_rep/parse_tree_builder.rb +15 -15
- data/lib/rley/parse_rep/parse_tree_factory.rb +27 -25
- data/lib/rley/parse_tree_visitor.rb +3 -1
- data/lib/rley/parser/error_reason.rb +9 -8
- data/lib/rley/parser/gfg_chart.rb +54 -22
- data/lib/rley/parser/gfg_earley_parser.rb +3 -1
- data/lib/rley/parser/gfg_parsing.rb +51 -31
- data/lib/rley/parser/parse_entry.rb +29 -33
- data/lib/rley/parser/parse_entry_set.rb +32 -27
- data/lib/rley/parser/parse_entry_tracker.rb +6 -4
- data/lib/rley/parser/parse_state.rb +18 -21
- data/lib/rley/parser/parse_state_tracker.rb +6 -4
- data/lib/rley/parser/parse_tracer.rb +15 -13
- data/lib/rley/parser/parse_walker_factory.rb +28 -29
- data/lib/rley/parser/state_set.rb +11 -10
- data/lib/rley/ptree/non_terminal_node.rb +10 -6
- data/lib/rley/ptree/parse_tree.rb +6 -4
- data/lib/rley/ptree/parse_tree_node.rb +7 -5
- data/lib/rley/ptree/terminal_node.rb +9 -7
- data/lib/rley/rley_error.rb +12 -10
- data/lib/rley/sppf/alternative_node.rb +8 -6
- data/lib/rley/sppf/composite_node.rb +9 -7
- data/lib/rley/sppf/epsilon_node.rb +5 -3
- data/lib/rley/sppf/leaf_node.rb +5 -3
- data/lib/rley/sppf/non_terminal_node.rb +2 -0
- data/lib/rley/sppf/parse_forest.rb +19 -17
- data/lib/rley/sppf/sppf_node.rb +9 -8
- data/lib/rley/sppf/token_node.rb +5 -3
- data/lib/rley/syntax/grammar.rb +7 -5
- data/lib/rley/syntax/grammar_builder.rb +11 -9
- data/lib/rley/syntax/grm_symbol.rb +8 -6
- data/lib/rley/syntax/literal.rb +2 -0
- data/lib/rley/syntax/non_terminal.rb +11 -15
- data/lib/rley/syntax/production.rb +13 -11
- data/lib/rley/syntax/symbol_seq.rb +10 -10
- data/lib/rley/syntax/terminal.rb +6 -5
- data/lib/rley/syntax/verbatim_symbol.rb +5 -3
- data/lib/support/base_tokenizer.rb +23 -20
- data/spec/rley/base/dotted_item_spec.rb +4 -2
- data/spec/rley/base/grm_items_builder_spec.rb +2 -0
- data/spec/rley/engine_spec.rb +47 -9
- data/spec/rley/formatter/asciitree_spec.rb +11 -9
- data/spec/rley/formatter/bracket_notation_spec.rb +16 -14
- data/spec/rley/formatter/debug_spec.rb +4 -2
- data/spec/rley/formatter/json_spec.rb +5 -3
- data/spec/rley/gfg/call_edge_spec.rb +2 -0
- data/spec/rley/gfg/edge_spec.rb +2 -0
- data/spec/rley/gfg/end_vertex_spec.rb +7 -5
- data/spec/rley/gfg/epsilon_edge_spec.rb +2 -0
- data/spec/rley/gfg/grm_flow_graph_spec.rb +2 -0
- data/spec/rley/gfg/item_vertex_spec.rb +12 -10
- data/spec/rley/gfg/non_terminal_vertex_spec.rb +5 -3
- data/spec/rley/gfg/return_edge_spec.rb +2 -0
- data/spec/rley/gfg/scan_edge_spec.rb +2 -0
- data/spec/rley/gfg/shortcut_edge_spec.rb +3 -1
- data/spec/rley/gfg/start_vertex_spec.rb +7 -5
- data/spec/rley/gfg/vertex_spec.rb +5 -3
- data/spec/rley/lexical/token_range_spec.rb +18 -16
- data/spec/rley/lexical/token_spec.rb +4 -2
- data/spec/rley/parse_forest_visitor_spec.rb +167 -163
- data/spec/rley/parse_rep/ambiguous_parse_spec.rb +46 -44
- data/spec/rley/parse_rep/ast_builder_spec.rb +8 -6
- data/spec/rley/parse_rep/cst_builder_spec.rb +7 -5
- data/spec/rley/parse_rep/groucho_spec.rb +25 -25
- data/spec/rley/parse_rep/parse_forest_builder_spec.rb +28 -26
- data/spec/rley/parse_rep/parse_forest_factory_spec.rb +8 -6
- data/spec/rley/parse_rep/parse_tree_factory_spec.rb +4 -2
- data/spec/rley/parse_tree_visitor_spec.rb +12 -8
- data/spec/rley/parser/error_reason_spec.rb +8 -6
- data/spec/rley/parser/gfg_chart_spec.rb +17 -4
- data/spec/rley/parser/gfg_earley_parser_spec.rb +16 -11
- data/spec/rley/parser/gfg_parsing_spec.rb +41 -252
- data/spec/rley/parser/parse_entry_set_spec.rb +2 -0
- data/spec/rley/parser/parse_entry_spec.rb +21 -19
- data/spec/rley/parser/parse_state_spec.rb +7 -5
- data/spec/rley/parser/parse_tracer_spec.rb +16 -14
- data/spec/rley/parser/parse_walker_factory_spec.rb +10 -8
- data/spec/rley/parser/state_set_spec.rb +24 -22
- data/spec/rley/ptree/non_terminal_node_spec.rb +7 -3
- data/spec/rley/ptree/parse_tree_node_spec.rb +6 -4
- data/spec/rley/ptree/parse_tree_spec.rb +2 -0
- data/spec/rley/ptree/terminal_node_spec.rb +8 -6
- data/spec/rley/sppf/alternative_node_spec.rb +8 -6
- data/spec/rley/sppf/non_terminal_node_spec.rb +5 -3
- data/spec/rley/sppf/token_node_spec.rb +6 -4
- data/spec/rley/support/ambiguous_grammar_helper.rb +5 -4
- data/spec/rley/support/expectation_helper.rb +2 -0
- data/spec/rley/support/grammar_abc_helper.rb +4 -4
- data/spec/rley/support/grammar_ambig01_helper.rb +6 -5
- data/spec/rley/support/grammar_arr_int_helper.rb +6 -5
- data/spec/rley/support/grammar_b_expr_helper.rb +6 -5
- data/spec/rley/support/grammar_helper.rb +2 -0
- data/spec/rley/support/grammar_l0_helper.rb +15 -16
- data/spec/rley/support/grammar_pb_helper.rb +8 -5
- data/spec/rley/support/grammar_sppf_helper.rb +3 -1
- data/spec/rley/syntax/grammar_builder_spec.rb +7 -5
- data/spec/rley/syntax/grammar_spec.rb +8 -6
- data/spec/rley/syntax/grm_symbol_spec.rb +3 -1
- data/spec/rley/syntax/literal_spec.rb +2 -0
- data/spec/rley/syntax/non_terminal_spec.rb +10 -8
- data/spec/rley/syntax/production_spec.rb +15 -13
- data/spec/rley/syntax/symbol_seq_spec.rb +4 -2
- data/spec/rley/syntax/terminal_spec.rb +7 -5
- data/spec/rley/syntax/verbatim_symbol_spec.rb +3 -1
- data/spec/spec_helper.rb +2 -12
- data/spec/support/base_tokenizer_spec.rb +9 -2
- metadata +21 -63
- data/.simplecov +0 -7
- data/Gemfile +0 -8
data/examples/general/left.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
# Purpose: define a grammar with left-recursive rule
|
2
4
|
require 'rley' # Load Rley library
|
3
5
|
|
@@ -6,10 +8,10 @@ builder = Rley::Syntax::GrammarBuilder.new do
|
|
6
8
|
# The grammar defines a language that consists in a sequence
|
7
9
|
# of 0 or more dots...
|
8
10
|
add_terminals('DOT')
|
9
|
-
|
11
|
+
|
10
12
|
# Grammar with left recursive rule.
|
11
13
|
rule 'l_dots' => []
|
12
|
-
rule 'l_dots' => %w[l_dots DOT]
|
14
|
+
rule 'l_dots' => %w[l_dots DOT]
|
13
15
|
end
|
14
16
|
|
15
17
|
# And now, let's build the grammar...
|
data/examples/general/right.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
# Purpose: define a grammar with right-recursive rule
|
2
4
|
require 'rley' # Load Rley library
|
3
5
|
|
@@ -6,10 +8,10 @@ builder = Rley::Syntax::GrammarBuilder.new do
|
|
6
8
|
# The grammar defines a language that consists in a sequence
|
7
9
|
# of 0 or more dots...
|
8
10
|
add_terminals('DOT')
|
9
|
-
|
11
|
+
|
10
12
|
# Grammar with right recursive rule.
|
11
13
|
rule 'r_dots' => []
|
12
|
-
rule 'r_dots' => %w[DOT r_dots]
|
14
|
+
rule 'r_dots' => %w[DOT r_dots]
|
13
15
|
end
|
14
16
|
|
15
17
|
# And now, let's build the grammar...
|
data/lib/rley.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module Rley # This module is used as a namespace
|
2
4
|
module Base # This module is used as a namespace
|
3
5
|
# A dotted item is a parse state for a given production/grammar rule
|
@@ -17,14 +19,14 @@ module Rley # This module is used as a namespace
|
|
17
19
|
class DottedItem
|
18
20
|
# Production rule
|
19
21
|
# @return [Syntax::Production]
|
20
|
-
attr_reader
|
22
|
+
attr_reader :production
|
21
23
|
|
22
24
|
# Index of the next symbol (from the rhs) after the 'dot'.
|
23
25
|
# If the dot is at the end of the rhs (i.e.) there is no next
|
24
26
|
# symbol, then the position takes the value -1.
|
25
27
|
# It the rhs is empty, then the position is -2
|
26
28
|
# @return [Integer]
|
27
|
-
attr_reader
|
29
|
+
attr_reader :position
|
28
30
|
|
29
31
|
# @param aProduction [Syntax::Production]
|
30
32
|
# @param aPosition [Integer] Position of the dot in rhs of production.
|
@@ -35,23 +37,23 @@ module Rley # This module is used as a namespace
|
|
35
37
|
|
36
38
|
# Return a String representation of the dotted item.
|
37
39
|
# @return [String]
|
38
|
-
def to_s
|
40
|
+
def to_s
|
39
41
|
prefix = "#{production.lhs} => "
|
40
42
|
text_values = production.rhs.map(&:to_s)
|
41
|
-
if position
|
43
|
+
if position.negative?
|
42
44
|
text_values << '.'
|
43
45
|
else
|
44
46
|
text_values.insert(position, '.')
|
45
47
|
end
|
46
48
|
suffix = text_values.join(' ')
|
47
49
|
|
48
|
-
|
50
|
+
prefix + suffix
|
49
51
|
end
|
50
52
|
|
51
53
|
# Return true if the dot position is at the start of the rhs.
|
52
54
|
# @return [Boolean]
|
53
|
-
def at_start?
|
54
|
-
|
55
|
+
def at_start?
|
56
|
+
position.zero? || position == -2
|
55
57
|
end
|
56
58
|
|
57
59
|
# An item with the dot at the beginning is called
|
@@ -60,51 +62,48 @@ module Rley # This module is used as a namespace
|
|
60
62
|
|
61
63
|
# A dotted item is called a reduce item if the dot is at the end.
|
62
64
|
# @return [Boolean]
|
63
|
-
def reduce_item?
|
64
|
-
|
65
|
+
def reduce_item?
|
66
|
+
position.negative? # Either -1 or -2
|
65
67
|
end
|
66
68
|
|
67
69
|
# The non-terminal symbol that is on the left-side of the production
|
68
70
|
# @return [Syntax::NonTerminal]
|
69
|
-
def lhs
|
70
|
-
|
71
|
+
def lhs
|
72
|
+
production.lhs
|
71
73
|
end
|
72
74
|
|
73
75
|
# Return the symbol before the dot.
|
74
76
|
# nil is returned if the dot is at the start of the rhs
|
75
77
|
# @return [Syntax::GrmSymbol, NilClass]
|
76
|
-
def prev_symbol
|
78
|
+
def prev_symbol
|
77
79
|
before_position = prev_position
|
78
|
-
|
79
|
-
nil
|
80
|
-
else
|
81
|
-
production.rhs[before_position]
|
82
|
-
end
|
83
|
-
|
84
|
-
return result
|
80
|
+
before_position.nil? ? nil : production.rhs[before_position]
|
85
81
|
end
|
86
82
|
|
87
83
|
# Return the symbol after the dot.
|
88
84
|
# nil is returned if the dot is at the end
|
89
85
|
# @return [Syntax::GrmSymbol, NilClass]
|
90
|
-
def next_symbol
|
91
|
-
|
86
|
+
def next_symbol
|
87
|
+
position.negative? ? nil : production.rhs[position]
|
92
88
|
end
|
93
89
|
|
94
90
|
# Calculate the position of the dot if were moved by
|
95
91
|
# one step on the left.
|
96
92
|
# @return [Integer]
|
97
|
-
def prev_position
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
93
|
+
def prev_position
|
94
|
+
unless @k_prev_position
|
95
|
+
case position
|
96
|
+
when -2, 0
|
97
|
+
result = nil
|
98
|
+
when -1
|
99
|
+
result = production.rhs.size == 1 ? 0 : production.rhs.size - 1
|
100
|
+
else
|
101
|
+
result = position - 1
|
102
|
+
end
|
103
|
+
@k_prev_position = [result]
|
105
104
|
end
|
106
105
|
|
107
|
-
|
106
|
+
@k_prev_position[0]
|
108
107
|
end
|
109
108
|
|
110
109
|
# Return true if this dotted item has a dot one place
|
@@ -117,7 +116,7 @@ module Rley # This module is used as a namespace
|
|
117
116
|
to_the_left = prev_position
|
118
117
|
return false if to_the_left.nil?
|
119
118
|
|
120
|
-
|
119
|
+
to_the_left == another.position
|
121
120
|
end
|
122
121
|
|
123
122
|
|
@@ -126,19 +125,17 @@ module Rley # This module is used as a namespace
|
|
126
125
|
# Return the given position after its validation.
|
127
126
|
def valid_position(aPosition)
|
128
127
|
rhs_size = production.rhs.size
|
129
|
-
if aPosition
|
128
|
+
if aPosition.negative? || aPosition > rhs_size
|
130
129
|
raise StandardError, 'Out of bound index'
|
131
130
|
end
|
132
131
|
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
return index
|
132
|
+
if rhs_size.zero?
|
133
|
+
-2 # Minus 2 at start/end of empty production
|
134
|
+
elsif aPosition == rhs_size
|
135
|
+
-1 # Minus 1 at end of non-empty production
|
136
|
+
else
|
137
|
+
aPosition
|
138
|
+
end
|
142
139
|
end
|
143
140
|
end # class
|
144
141
|
end # module
|
data/lib/rley/constants.rb
CHANGED
@@ -1,12 +1,14 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
# File: constants.rb
|
2
4
|
# Purpose: definition of Rley constants.
|
3
5
|
|
4
6
|
module Rley # Module used as a namespace
|
5
7
|
# The version number of the gem.
|
6
|
-
Version = '0.7.
|
8
|
+
Version = '0.7.08'
|
7
9
|
|
8
10
|
# Brief description of the gem.
|
9
|
-
Description = "Ruby implementation of the Earley's parsing algorithm"
|
11
|
+
Description = "Ruby implementation of the Earley's parsing algorithm"
|
10
12
|
|
11
13
|
# Constant Rley::RootDir contains the absolute path of Rley's
|
12
14
|
# start directory. Note: it also ends with a slash character.
|
@@ -18,7 +20,7 @@ module Rley # Module used as a namespace
|
|
18
20
|
RootDir = begin
|
19
21
|
require 'pathname' # Load Pathname class from standard library
|
20
22
|
startdir = Pathname(__FILE__).dirname.parent.parent.expand_path
|
21
|
-
startdir
|
23
|
+
"#{startdir}/" # Append trailing slash character to it
|
22
24
|
end
|
23
25
|
end
|
24
26
|
end # module
|
data/lib/rley/engine.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require_relative './syntax/grammar_builder'
|
2
4
|
require_relative './parser/gfg_earley_parser'
|
3
5
|
require_relative './parse_tree_visitor'
|
@@ -13,16 +15,14 @@ module Rley # This module is used as a namespace
|
|
13
15
|
# @return [Symbol] allowed values are: :parse_tree, :parse_forest
|
14
16
|
:parse_repr,
|
15
17
|
:repr_builder,
|
16
|
-
:diagnose
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
self.diagnose = false
|
25
|
-
end
|
18
|
+
:diagnose) do
|
19
|
+
# Constructor with default initialization.
|
20
|
+
def initialize
|
21
|
+
super()
|
22
|
+
self.parse_repr = :parse_tree
|
23
|
+
self.repr_builder = :default
|
24
|
+
self.diagnose = false
|
25
|
+
end
|
26
26
|
end
|
27
27
|
|
28
28
|
# Implementation of the GoF Facade design pattern.
|
@@ -42,7 +42,7 @@ module Rley # This module is used as a namespace
|
|
42
42
|
# Engine.new do |config|
|
43
43
|
# config.parse_repr = :parse_forest
|
44
44
|
# end
|
45
|
-
def initialize
|
45
|
+
def initialize
|
46
46
|
@configuration = EngineConfig.new
|
47
47
|
yield configuration if block_given?
|
48
48
|
end
|
@@ -90,7 +90,7 @@ module Rley # This module is used as a namespace
|
|
90
90
|
result = parser.parse(tokens)
|
91
91
|
result.tidy_up!
|
92
92
|
|
93
|
-
|
93
|
+
result
|
94
94
|
end
|
95
95
|
|
96
96
|
# Convert raw parse result into a more convenient representation
|
@@ -98,14 +98,12 @@ module Rley # This module is used as a namespace
|
|
98
98
|
# @param aRawParse [Parser::GFGParsing]
|
99
99
|
# @return [Rley::PTree::ParseTree, Rley::SPPF::ParseForest]
|
100
100
|
def convert(aRawParse)
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
return result
|
101
|
+
case configuration.parse_repr
|
102
|
+
when :parse_tree
|
103
|
+
to_ptree(aRawParse)
|
104
|
+
when :parse_forest
|
105
|
+
to_pforest(aRawParse)
|
106
|
+
end
|
109
107
|
end
|
110
108
|
|
111
109
|
# Convert raw parse result into a parse tree representation
|
@@ -119,7 +117,7 @@ module Rley # This module is used as a namespace
|
|
119
117
|
result = factory.create(configuration.repr_builder)
|
120
118
|
end
|
121
119
|
|
122
|
-
|
120
|
+
result
|
123
121
|
end
|
124
122
|
|
125
123
|
# Convert raw parse result into a parse forest representation
|
@@ -133,7 +131,7 @@ module Rley # This module is used as a namespace
|
|
133
131
|
result = factory.create(configuration.repr_builder)
|
134
132
|
end
|
135
133
|
|
136
|
-
|
134
|
+
result
|
137
135
|
end
|
138
136
|
|
139
137
|
# Build a visitor for the given parse tree
|
@@ -147,13 +145,13 @@ module Rley # This module is used as a namespace
|
|
147
145
|
# @param aPForest [SPPF::ParseForest]
|
148
146
|
# @return [ParseForestVisitor]
|
149
147
|
def pforest_visitor(aPForest)
|
150
|
-
|
148
|
+
ParseForestVisitor.new(aPForest)
|
151
149
|
end
|
152
150
|
|
153
151
|
protected
|
154
152
|
|
155
153
|
def build_parser(aGrammar)
|
156
|
-
|
154
|
+
Parser::GFGEarleyParser.new(aGrammar)
|
157
155
|
end
|
158
156
|
end # class
|
159
157
|
end # module
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require_relative 'base_formatter'
|
2
4
|
|
3
5
|
|
@@ -13,7 +15,7 @@ module Rley # This module is used as a namespace
|
|
13
15
|
# Allowed string values are: 'first', 'last', 'first_and_last', 'other'
|
14
16
|
attr_reader(:ranks)
|
15
17
|
|
16
|
-
# @return [String] The character pattern used for rendering
|
18
|
+
# @return [String] The character pattern used for rendering
|
17
19
|
# a parent - child nesting
|
18
20
|
attr_reader(:nesting_prefix)
|
19
21
|
|
@@ -97,10 +99,10 @@ module Rley # This module is used as a namespace
|
|
97
99
|
end
|
98
100
|
|
99
101
|
# 'root', 'first', 'first_and_last', 'last', 'other'
|
100
|
-
def path_prefix
|
102
|
+
def path_prefix
|
101
103
|
return '' if ranks.empty?
|
102
104
|
|
103
|
-
prefix = ''
|
105
|
+
prefix = +''
|
104
106
|
@ranks.each_with_index do |rank, i|
|
105
107
|
next if i.zero?
|
106
108
|
|
@@ -114,7 +116,7 @@ module Rley # This module is used as a namespace
|
|
114
116
|
end
|
115
117
|
|
116
118
|
prefix << nesting_prefix
|
117
|
-
|
119
|
+
prefix
|
118
120
|
end
|
119
121
|
|
120
122
|
def emit(aNode, aSuffix = '')
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require_relative 'base_formatter'
|
2
4
|
|
3
5
|
|
@@ -11,13 +13,6 @@ module Rley # This module is used as a namespace
|
|
11
13
|
# For Ruby developers, there is RSyntaxTree by Yoichiro Hasebe.
|
12
14
|
# (accessible via: http://yohasebe.com/rsyntaxtree/)
|
13
15
|
class BracketNotation < BaseFormatter
|
14
|
-
# Constructor.
|
15
|
-
# @param anIO [IO] The output stream to which the rendered grammar
|
16
|
-
# is written.
|
17
|
-
def initialize(anIO)
|
18
|
-
super(anIO)
|
19
|
-
end
|
20
|
-
|
21
16
|
# Method called by a ParseTreeVisitor to which the formatter subscribed.
|
22
17
|
# Notification of a visit event: the visitor is about to visit
|
23
18
|
# a non-terminal node
|
@@ -42,7 +37,7 @@ module Rley # This module is used as a namespace
|
|
42
37
|
# Escape all opening and closing square brackets
|
43
38
|
escape_lbrackets = aTerm.token.lexeme.gsub(/\[/, '\[')
|
44
39
|
escaped = escape_lbrackets.gsub(/\]/, '\]')
|
45
|
-
write(escaped
|
40
|
+
write("#{escaped}]")
|
46
41
|
end
|
47
42
|
|
48
43
|
# Method called by a ParseTreeVisitor to which the formatter subscribed.
|
data/lib/rley/formatter/debug.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require_relative 'base_formatter'
|
2
4
|
|
3
5
|
|
@@ -17,17 +19,17 @@ module Rley # This module is used as a namespace
|
|
17
19
|
super(anIO)
|
18
20
|
@indentation = 0
|
19
21
|
end
|
20
|
-
|
22
|
+
|
21
23
|
# Indicates that this formatter accepts all visit events
|
22
24
|
# provided their names start with 'before_' or 'after_'
|
23
25
|
# @return [Boolean]
|
24
26
|
def accept_all
|
25
27
|
return true
|
26
28
|
end
|
27
|
-
|
29
|
+
|
28
30
|
# Ghost method pattern.
|
29
|
-
def method_missing(mth, *args)
|
30
|
-
mth_name = mth.to_s
|
31
|
+
def method_missing(mth, *args)
|
32
|
+
mth_name = mth.to_s
|
31
33
|
case mth_name
|
32
34
|
when /^before_/
|
33
35
|
output_event(mth_name, indentation)
|
@@ -42,11 +44,11 @@ module Rley # This module is used as a namespace
|
|
42
44
|
|
43
45
|
private
|
44
46
|
|
45
|
-
def indent
|
47
|
+
def indent
|
46
48
|
@indentation += 1
|
47
49
|
end
|
48
50
|
|
49
|
-
def dedent
|
51
|
+
def dedent
|
50
52
|
@indentation -= 1
|
51
53
|
end
|
52
54
|
|