rley 0.7.03 → 0.7.08

Sign up to get free protection for your applications and to get access to all the features.
Files changed (174) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +362 -62
  3. data/.travis.yml +6 -7
  4. data/CHANGELOG.md +20 -1
  5. data/LICENSE.txt +1 -1
  6. data/README.md +6 -7
  7. data/Rakefile +2 -0
  8. data/appveyor.yml +2 -4
  9. data/examples/NLP/benchmark_pico_en.rb +2 -0
  10. data/examples/NLP/engtagger.rb +193 -188
  11. data/examples/NLP/nano_eng/nano_en_demo.rb +2 -0
  12. data/examples/NLP/nano_eng/nano_grammar.rb +7 -5
  13. data/examples/NLP/pico_en_demo.rb +2 -0
  14. data/examples/data_formats/JSON/cli_options.rb +3 -1
  15. data/examples/data_formats/JSON/json_ast_builder.rb +14 -9
  16. data/examples/data_formats/JSON/json_ast_nodes.rb +14 -21
  17. data/examples/data_formats/JSON/json_demo.rb +2 -0
  18. data/examples/data_formats/JSON/json_grammar.rb +4 -2
  19. data/examples/data_formats/JSON/json_lexer.rb +10 -8
  20. data/examples/data_formats/JSON/json_minifier.rb +3 -1
  21. data/examples/general/calc_iter1/calc_ast_builder.rb +15 -10
  22. data/examples/general/calc_iter1/calc_ast_nodes.rb +25 -37
  23. data/examples/general/calc_iter1/calc_demo.rb +2 -0
  24. data/examples/general/calc_iter1/calc_grammar.rb +4 -2
  25. data/examples/general/calc_iter1/calc_lexer.rb +8 -4
  26. data/examples/general/calc_iter1/spec/calculator_spec.rb +7 -5
  27. data/examples/general/calc_iter2/calc_ast_builder.rb +7 -3
  28. data/examples/general/calc_iter2/calc_ast_nodes.rb +29 -43
  29. data/examples/general/calc_iter2/calc_demo.rb +2 -0
  30. data/examples/general/calc_iter2/calc_grammar.rb +5 -3
  31. data/examples/general/calc_iter2/calc_lexer.rb +13 -10
  32. data/examples/general/calc_iter2/spec/calculator_spec.rb +28 -26
  33. data/examples/general/left.rb +4 -2
  34. data/examples/general/right.rb +4 -2
  35. data/lib/rley.rb +2 -0
  36. data/lib/rley/base/base_parser.rb +2 -0
  37. data/lib/rley/base/dotted_item.rb +38 -41
  38. data/lib/rley/base/grm_items_builder.rb +2 -0
  39. data/lib/rley/constants.rb +5 -3
  40. data/lib/rley/engine.rb +22 -24
  41. data/lib/rley/formatter/asciitree.rb +6 -4
  42. data/lib/rley/formatter/base_formatter.rb +2 -0
  43. data/lib/rley/formatter/bracket_notation.rb +3 -8
  44. data/lib/rley/formatter/debug.rb +8 -6
  45. data/lib/rley/formatter/json.rb +4 -2
  46. data/lib/rley/gfg/call_edge.rb +3 -1
  47. data/lib/rley/gfg/edge.rb +7 -5
  48. data/lib/rley/gfg/end_vertex.rb +4 -6
  49. data/lib/rley/gfg/epsilon_edge.rb +3 -5
  50. data/lib/rley/gfg/grm_flow_graph.rb +31 -25
  51. data/lib/rley/gfg/item_vertex.rb +12 -22
  52. data/lib/rley/gfg/non_terminal_vertex.rb +6 -4
  53. data/lib/rley/gfg/return_edge.rb +2 -0
  54. data/lib/rley/gfg/scan_edge.rb +3 -1
  55. data/lib/rley/gfg/shortcut_edge.rb +4 -2
  56. data/lib/rley/gfg/start_vertex.rb +6 -8
  57. data/lib/rley/gfg/vertex.rb +47 -41
  58. data/lib/rley/lexical/token.rb +3 -1
  59. data/lib/rley/lexical/token_range.rb +8 -6
  60. data/lib/rley/parse_forest_visitor.rb +7 -5
  61. data/lib/rley/parse_rep/ast_base_builder.rb +11 -11
  62. data/lib/rley/parse_rep/cst_builder.rb +7 -4
  63. data/lib/rley/parse_rep/parse_forest_builder.rb +36 -25
  64. data/lib/rley/parse_rep/parse_forest_factory.rb +5 -3
  65. data/lib/rley/parse_rep/parse_rep_creator.rb +18 -13
  66. data/lib/rley/parse_rep/parse_tree_builder.rb +15 -15
  67. data/lib/rley/parse_rep/parse_tree_factory.rb +27 -25
  68. data/lib/rley/parse_tree_visitor.rb +3 -1
  69. data/lib/rley/parser/error_reason.rb +9 -8
  70. data/lib/rley/parser/gfg_chart.rb +54 -22
  71. data/lib/rley/parser/gfg_earley_parser.rb +3 -1
  72. data/lib/rley/parser/gfg_parsing.rb +51 -31
  73. data/lib/rley/parser/parse_entry.rb +29 -33
  74. data/lib/rley/parser/parse_entry_set.rb +32 -27
  75. data/lib/rley/parser/parse_entry_tracker.rb +6 -4
  76. data/lib/rley/parser/parse_state.rb +18 -21
  77. data/lib/rley/parser/parse_state_tracker.rb +6 -4
  78. data/lib/rley/parser/parse_tracer.rb +15 -13
  79. data/lib/rley/parser/parse_walker_factory.rb +28 -29
  80. data/lib/rley/parser/state_set.rb +11 -10
  81. data/lib/rley/ptree/non_terminal_node.rb +10 -6
  82. data/lib/rley/ptree/parse_tree.rb +6 -4
  83. data/lib/rley/ptree/parse_tree_node.rb +7 -5
  84. data/lib/rley/ptree/terminal_node.rb +9 -7
  85. data/lib/rley/rley_error.rb +12 -10
  86. data/lib/rley/sppf/alternative_node.rb +8 -6
  87. data/lib/rley/sppf/composite_node.rb +9 -7
  88. data/lib/rley/sppf/epsilon_node.rb +5 -3
  89. data/lib/rley/sppf/leaf_node.rb +5 -3
  90. data/lib/rley/sppf/non_terminal_node.rb +2 -0
  91. data/lib/rley/sppf/parse_forest.rb +19 -17
  92. data/lib/rley/sppf/sppf_node.rb +9 -8
  93. data/lib/rley/sppf/token_node.rb +5 -3
  94. data/lib/rley/syntax/grammar.rb +7 -5
  95. data/lib/rley/syntax/grammar_builder.rb +11 -9
  96. data/lib/rley/syntax/grm_symbol.rb +8 -6
  97. data/lib/rley/syntax/literal.rb +2 -0
  98. data/lib/rley/syntax/non_terminal.rb +11 -15
  99. data/lib/rley/syntax/production.rb +13 -11
  100. data/lib/rley/syntax/symbol_seq.rb +10 -10
  101. data/lib/rley/syntax/terminal.rb +6 -5
  102. data/lib/rley/syntax/verbatim_symbol.rb +5 -3
  103. data/lib/support/base_tokenizer.rb +23 -20
  104. data/spec/rley/base/dotted_item_spec.rb +4 -2
  105. data/spec/rley/base/grm_items_builder_spec.rb +2 -0
  106. data/spec/rley/engine_spec.rb +47 -9
  107. data/spec/rley/formatter/asciitree_spec.rb +11 -9
  108. data/spec/rley/formatter/bracket_notation_spec.rb +16 -14
  109. data/spec/rley/formatter/debug_spec.rb +4 -2
  110. data/spec/rley/formatter/json_spec.rb +5 -3
  111. data/spec/rley/gfg/call_edge_spec.rb +2 -0
  112. data/spec/rley/gfg/edge_spec.rb +2 -0
  113. data/spec/rley/gfg/end_vertex_spec.rb +7 -5
  114. data/spec/rley/gfg/epsilon_edge_spec.rb +2 -0
  115. data/spec/rley/gfg/grm_flow_graph_spec.rb +2 -0
  116. data/spec/rley/gfg/item_vertex_spec.rb +12 -10
  117. data/spec/rley/gfg/non_terminal_vertex_spec.rb +5 -3
  118. data/spec/rley/gfg/return_edge_spec.rb +2 -0
  119. data/spec/rley/gfg/scan_edge_spec.rb +2 -0
  120. data/spec/rley/gfg/shortcut_edge_spec.rb +3 -1
  121. data/spec/rley/gfg/start_vertex_spec.rb +7 -5
  122. data/spec/rley/gfg/vertex_spec.rb +5 -3
  123. data/spec/rley/lexical/token_range_spec.rb +18 -16
  124. data/spec/rley/lexical/token_spec.rb +4 -2
  125. data/spec/rley/parse_forest_visitor_spec.rb +167 -163
  126. data/spec/rley/parse_rep/ambiguous_parse_spec.rb +46 -44
  127. data/spec/rley/parse_rep/ast_builder_spec.rb +8 -6
  128. data/spec/rley/parse_rep/cst_builder_spec.rb +7 -5
  129. data/spec/rley/parse_rep/groucho_spec.rb +25 -25
  130. data/spec/rley/parse_rep/parse_forest_builder_spec.rb +28 -26
  131. data/spec/rley/parse_rep/parse_forest_factory_spec.rb +8 -6
  132. data/spec/rley/parse_rep/parse_tree_factory_spec.rb +4 -2
  133. data/spec/rley/parse_tree_visitor_spec.rb +12 -8
  134. data/spec/rley/parser/error_reason_spec.rb +8 -6
  135. data/spec/rley/parser/gfg_chart_spec.rb +17 -4
  136. data/spec/rley/parser/gfg_earley_parser_spec.rb +16 -11
  137. data/spec/rley/parser/gfg_parsing_spec.rb +41 -252
  138. data/spec/rley/parser/parse_entry_set_spec.rb +2 -0
  139. data/spec/rley/parser/parse_entry_spec.rb +21 -19
  140. data/spec/rley/parser/parse_state_spec.rb +7 -5
  141. data/spec/rley/parser/parse_tracer_spec.rb +16 -14
  142. data/spec/rley/parser/parse_walker_factory_spec.rb +10 -8
  143. data/spec/rley/parser/state_set_spec.rb +24 -22
  144. data/spec/rley/ptree/non_terminal_node_spec.rb +7 -3
  145. data/spec/rley/ptree/parse_tree_node_spec.rb +6 -4
  146. data/spec/rley/ptree/parse_tree_spec.rb +2 -0
  147. data/spec/rley/ptree/terminal_node_spec.rb +8 -6
  148. data/spec/rley/sppf/alternative_node_spec.rb +8 -6
  149. data/spec/rley/sppf/non_terminal_node_spec.rb +5 -3
  150. data/spec/rley/sppf/token_node_spec.rb +6 -4
  151. data/spec/rley/support/ambiguous_grammar_helper.rb +5 -4
  152. data/spec/rley/support/expectation_helper.rb +2 -0
  153. data/spec/rley/support/grammar_abc_helper.rb +4 -4
  154. data/spec/rley/support/grammar_ambig01_helper.rb +6 -5
  155. data/spec/rley/support/grammar_arr_int_helper.rb +6 -5
  156. data/spec/rley/support/grammar_b_expr_helper.rb +6 -5
  157. data/spec/rley/support/grammar_helper.rb +2 -0
  158. data/spec/rley/support/grammar_l0_helper.rb +15 -16
  159. data/spec/rley/support/grammar_pb_helper.rb +8 -5
  160. data/spec/rley/support/grammar_sppf_helper.rb +3 -1
  161. data/spec/rley/syntax/grammar_builder_spec.rb +7 -5
  162. data/spec/rley/syntax/grammar_spec.rb +8 -6
  163. data/spec/rley/syntax/grm_symbol_spec.rb +3 -1
  164. data/spec/rley/syntax/literal_spec.rb +2 -0
  165. data/spec/rley/syntax/non_terminal_spec.rb +10 -8
  166. data/spec/rley/syntax/production_spec.rb +15 -13
  167. data/spec/rley/syntax/symbol_seq_spec.rb +4 -2
  168. data/spec/rley/syntax/terminal_spec.rb +7 -5
  169. data/spec/rley/syntax/verbatim_symbol_spec.rb +3 -1
  170. data/spec/spec_helper.rb +2 -12
  171. data/spec/support/base_tokenizer_spec.rb +9 -2
  172. metadata +21 -63
  173. data/.simplecov +0 -7
  174. data/Gemfile +0 -8
@@ -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...
@@ -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
  # File: rley.rb
2
4
  # This file acts as a jumping-off point for loading dependencies expected
3
5
  # for a Rley client.
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require_relative '../syntax/grammar'
2
4
  require_relative 'grm_items_builder' # Use mix-in module
3
5
 
@@ -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(:production)
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(:position)
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 < 0
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
- return prefix + suffix
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
- return position.zero? || position == -2
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
- return position < 0 # Either -1 or -2
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
- return production.lhs
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
- result = if before_position.nil?
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
- return position < 0 ? nil : production.rhs[position]
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
- case position
99
- when -2, 0
100
- result = nil
101
- when -1
102
- result = production.rhs.size == 1 ? 0 : production.rhs.size - 1
103
- else
104
- result = position - 1
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
- return result
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
- return to_the_left == another.position
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 < 0 || aPosition > rhs_size
128
+ if aPosition.negative? || aPosition > rhs_size
130
129
  raise StandardError, 'Out of bound index'
131
130
  end
132
131
 
133
- index = if rhs_size.zero?
134
- -2 # Minus 2 at start/end of empty production
135
- elsif aPosition == rhs_size
136
- -1 # Minus 1 at end of non-empty production
137
- else
138
- aPosition
139
- end
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
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require_relative 'dotted_item'
2
4
 
3
5
  module Rley # This module is used as a namespace
@@ -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.03'.freeze
8
+ Version = '0.7.08'
7
9
 
8
10
  # Brief description of the gem.
9
- Description = "Ruby implementation of the Earley's parsing algorithm".freeze
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.to_s + '/' # Append trailing slash character to it
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
- ) do
18
-
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
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
- return result
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
- result = case configuration.parse_repr
102
- when :parse_tree
103
- to_ptree(aRawParse)
104
- when :parse_forest
105
- to_pforest(aRawParse)
106
- end
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
- return result
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
- return result
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
- return ParseForestVisitor.new(aPForest)
148
+ ParseForestVisitor.new(aPForest)
151
149
  end
152
150
 
153
151
  protected
154
152
 
155
153
  def build_parser(aGrammar)
156
- return Parser::GFGEarleyParser.new(aGrammar)
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
- return prefix
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
  module Rley # This module is used as a namespace
2
4
  # Namespace dedicated to parse tree formatters.
3
5
  module Formatter
@@ -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.
@@ -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