rley 0.8.01 → 0.8.02

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5652efd2016050e7a40a94c279fa545a03b69b705bdcc5089a479d72c4743325
4
- data.tar.gz: c3f46ba853632a95e3c1fbe16f0f77f8e8a07ed3acd69a82297ca13eec9b6ccc
3
+ metadata.gz: 490bce9cb193d9e7b49222e8e1bae90d0dba64e663990cacef56ea69881c418a
4
+ data.tar.gz: 2f9380c53ff442992acb2629ab1f197078e0b403f373a2ed394a1cbfca85bc2c
5
5
  SHA512:
6
- metadata.gz: 532ca6d76a277e6fd234d66ec74e9ade425a91f8ace039e0dc0636547663ac52f64613001e9c36497d5a0e0d9fe67f09eb2fff80b77d8c2f4950095e58ac3b2e
7
- data.tar.gz: b288a0345c1696b8987b362ab99dafa4354f94ece73c5c80c87d7a28aa3c3d9ccaf0f516f29cead1707727582e917812435e4254e372aa2d62c110af7aa99a99
6
+ metadata.gz: 0b1ffe315ef7ddbb50e18244e233320ac592347997a7a84ce55545d24564e39aeeda56f5002e4645cac54e7e91dfa1ac79bc0567592e84720113622e3976539e
7
+ data.tar.gz: e7272a73b9f2359564d6783a5b0d8f38564ea9788cd7e568a4207de0176233bf0b29f7b37be991dc039dea45e5795d2eb59f7e8751940302c59d02dc23f33b85
data/.rubocop.yml CHANGED
@@ -1,7 +1,7 @@
1
1
  AllCops:
2
2
  Exclude:
3
- - 'exp/**/*'
4
- - 'demo/**/*'
3
+ - 'examples/**/*'
4
+ - 'exp/**/*'
5
5
 
6
6
  Gemspec/DateAssignment:
7
7
  Enabled: true
@@ -50,6 +50,9 @@ Layout/IndentationConsistency:
50
50
  Layout/HeredocIndentation:
51
51
  Enabled: false
52
52
 
53
+ Layout/LineEndStringConcatenationIndentation:
54
+ Enabled: true
55
+
53
56
  Layout/MultilineHashBraceLayout:
54
57
  Enabled: true
55
58
 
@@ -81,6 +84,9 @@ Layout/TrailingWhitespace:
81
84
  Lint/AmbiguousAssignment:
82
85
  Enabled: true
83
86
 
87
+ Lint/AmbiguousRange:
88
+ Enabled: true
89
+
84
90
  Lint/DeprecatedConstants:
85
91
  Enabled: true
86
92
 
@@ -96,6 +102,9 @@ Lint/EmptyBlock:
96
102
  Lint/EmptyClass:
97
103
  Enabled: false
98
104
 
105
+ Lint/EmptyInPattern:
106
+ Enabled: true
107
+
99
108
  Lint/LambdaWithoutLiteralBlock:
100
109
  Enabled: true
101
110
 
@@ -192,6 +201,9 @@ Naming/ClassAndModuleCamelCase:
192
201
  Naming/BlockParameterName:
193
202
  Enabled: true
194
203
 
204
+ Naming/InclusiveLanguage:
205
+ Enabled: true
206
+
195
207
  Naming/MethodParameterName:
196
208
  Enabled: false
197
209
 
@@ -201,9 +213,6 @@ Naming/MethodName:
201
213
  Naming/VariableName:
202
214
  Enabled: false
203
215
 
204
- Style/AccessorGrouping:
205
- Enabled: false
206
-
207
216
  Style/Alias:
208
217
  Enabled: true
209
218
 
@@ -276,11 +285,17 @@ Style/HashTransformValues:
276
285
  Style/IfUnlessModifier:
277
286
  Enabled: false
278
287
 
288
+ Style/InPatternThen:
289
+ Enabled: true
290
+
279
291
  Style/InverseMethods:
280
292
  Enabled: false
281
293
 
282
294
  Style/MissingRespondToMissing:
283
295
  Enabled: false
296
+
297
+ Style/MultilineInPatternThen:
298
+ Enabled: true
284
299
 
285
300
  Style/NegatedIfElseCondition:
286
301
  Enabled: true
@@ -293,6 +308,9 @@ Style/NilLambda:
293
308
 
294
309
  Style/NumericLiterals:
295
310
  Enabled: false
311
+
312
+ Style/QuotedSymbols:
313
+ Enabled: true
296
314
 
297
315
  Style/RaiseArgs:
298
316
  Enabled: true
@@ -305,6 +323,9 @@ Style/RedundantReturn:
305
323
 
306
324
  Style/RedundantSelf:
307
325
  Enabled: true
326
+
327
+ Style/RedundantSelfAssignmentBranch:
328
+ Enabled: true
308
329
 
309
330
  Style/RegexpLiteral:
310
331
  Enabled: false
@@ -367,6 +388,9 @@ Lint/TopLevelReturnWithArgument:
367
388
  Lint/UnreachableLoop:
368
389
  Enabled: true
369
390
 
391
+ Style/AccessorGrouping:
392
+ Enabled: true
393
+
370
394
  Style/ArrayCoercion:
371
395
  Enabled: true
372
396
 
data/CHANGELOG.md CHANGED
@@ -1,3 +1,10 @@
1
+ ### 0.8.02 / 2021-08-23
2
+ - Code restyling to please Rubocop 1.19.1
3
+
4
+ * [CHANGE] File `.rubocp.yml` updated to take into account new cops.
5
+ * [CHANGE] File `*.rb` files updated to fix Rubocop findings
6
+
7
+
1
8
  ### 0.8.01 / 2021-08-22
2
9
  - Unused/redundant file removal. Fix in rule generation
3
10
 
@@ -26,8 +26,8 @@ engine.build_grammar do
26
26
  rule 'S' => 'NP VP'
27
27
  rule 'NP' => 'Proper-Noun'
28
28
  rule 'NP' => 'Determiner Noun'
29
- rule 'NP' => 'Determiner Noun PP'
30
- rule 'VP' => 'Verb NP'
29
+ rule 'NP' => 'Determiner Noun PP'
30
+ rule 'VP' => 'Verb NP'
31
31
  rule 'VP' => 'Verb NP PP'
32
32
  rule 'PP' => 'Preposition NP'
33
33
  end
@@ -5,7 +5,7 @@
5
5
 
6
6
  module Rley # Module used as a namespace
7
7
  # The version number of the gem.
8
- Version = '0.8.01'
8
+ Version = '0.8.02'
9
9
 
10
10
  # Brief description of the gem.
11
11
  Description = "Ruby implementation of the Earley's parsing algorithm"
@@ -1,2 +1,4 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require_relative 'grouping_node'
2
- require_relative 'symbol_node'
4
+ require_relative 'symbol_node'
@@ -1,191 +1,185 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative '../parse_rep/ast_base_builder'
4
- require_relative '../engine'
5
- require_relative 'all_notation_nodes'
6
-
7
- module Rley
8
- module Notation
9
- # The purpose of ASTBuilder is to build piece by piece an AST
10
- # (Abstract Syntax Tree) from a sequence of input tokens and
11
- # visit events produced by walking over a GFGParsing object.
12
- class ASTBuilder < Rley::ParseRep::ASTBaseBuilder
13
- unless defined?(Name2special)
14
- # Mapping Token name => operator | separator | delimiter characters
15
- # @return [Hash{String => String}]
16
- Name2special = {
17
- 'COMMA' => ',',
18
- 'ELLIPSIS' => '..',
19
- 'LEFT_BRACE' => '{',
20
- 'LEFT_PAREN' => '(',
21
- 'PLUS' => '+',
22
- 'QUESTION_MARK' => '?',
23
- 'RIGHT_BRACE' => '}',
24
- 'RIGHT_PAREN' => ')',
25
- 'STAR' => '*'
26
- }.freeze
27
- end
28
-
29
- # Create a new AST builder instance.
30
- # @param theTokens [Array<Rley::Lexical::Token>] The sequence of input tokens.
31
- def initialize(theTokens)
32
- super(theTokens)
33
- end
34
-
35
- protected
36
-
37
- def terminal2node
38
- Terminal2NodeClass
39
- end
40
-
41
- # Method override
42
- def new_leaf_node(_production, _terminal, aTokenPosition, aToken)
43
- Rley::PTree::TerminalNode.new(aToken, aTokenPosition)
44
- end
45
-
46
- # Factory method for creating a parent node object.
47
- # @param aProduction [Production] Production rule
48
- # @param aRange [Range] Range of tokens matched by the rule
49
- # @param theTokens [Array] The input tokens
50
- # @param theChildren [Array] Children nodes (one per rhs symbol)
51
- def new_parent_node(aProduction, aRange, theTokens, theChildren)
52
- mth_name = method_name(aProduction.name)
53
- if respond_to?(mth_name, true)
54
- node = send(mth_name, aProduction, aRange, theTokens, theChildren)
55
- else
56
- # Default action...
57
- node = case aProduction.rhs.size
58
- when 0
59
- return_epsilon(aRange, theTokens, theChildren)
60
- when 1
61
- return_first_child(aRange, theTokens, theChildren)
62
- else
63
- node = Rley::PTree::NonTerminalNode.new(aProduction.lhs, aRange)
64
- theChildren&.reverse_each do |child|
65
- node.add_subnode(child) if child
66
- end
67
-
68
- node
69
- end
70
- end
71
-
72
- node
73
- end
74
-
75
- # Return the AST node corresponding to the second symbol in the rhs
76
- def reduce_to_2nd_symbol(_production, _range, _tokens, theChildren)
77
- theChildren[1]
78
- end
79
-
80
- #####################################
81
- # SEMANTIC ACTIONS
82
- #####################################
83
-
84
- # rule('rhs' => 'member_seq').tag 'sequence'
85
- def reduce_sequence(_production, _range, _tokens, theChildren)
86
- if theChildren[0].size == 1
87
- theChildren[0].first
88
- else
89
- SequenceNode.new(theChildren[0].first.position, theChildren[0], nil)
90
- end
91
- end
92
-
93
- # rule('member_seq' => 'member_seq member').tag 'more_members'
94
- def reduce_more_members(_production, _range, _tokens, theChildren)
95
- theChildren[0] << theChildren[1]
96
- end
97
-
98
- # rule('member_seq' => 'member')
99
- def reduce_one_member(_production, _range, _tokens, theChildren)
100
- [theChildren[0]]
101
- end
102
-
103
- # rule('strait_member' => 'base_member annotation')
104
- def reduce_annotated_member(_production, _range, _tokens, theChildren)
105
- theChildren[0].annotation = theChildren[1]
106
-
107
- theChildren[0]
108
- end
109
-
110
- # rule('base_member' => 'SYMBOL')
111
- def reduce_symbol(_production, _range, _tokens, theChildren)
112
- SymbolNode.new(theChildren[0].token.position, theChildren[0].token.lexeme)
113
- end
114
-
115
- # rule('base_member' => 'LEFT_PAREN member_seq RIGHT_PAREN')
116
- def reduce_grouping(_production, _range, tokens, theChildren)
117
- if theChildren[1].size == 1
118
- theChildren[1].first
119
- else
120
- rank = theChildren[0].range.high
121
- pos = tokens[rank].position
122
- GroupingNode.new(pos, theChildren[1], nil)
123
- end
124
- end
125
-
126
- # rule('quantified_member' => 'base_member quantifier')
127
- def reduce_quantified_member(_production, _range, _tokens, theChildren)
128
- theChildren[0].repetition = theChildren[1]
129
- theChildren[0]
130
- end
131
-
132
- # rule('quantifier' => 'QUESTION_MARK')
133
- def reduce_question_mark(_production, _range, _tokens, _theChildren)
134
- :zero_or_one
135
- end
136
-
137
- # rule('quantifier' => 'STAR')
138
- def reduce_star(_production, _range, _tokens, _theChildren)
139
- :zero_or_more
140
- end
141
-
142
- # rule('quantifier' => 'PLUS')
143
- def reduce_plus(_production, _range, _tokens, _theChildren)
144
- :one_or_more
145
- end
146
-
147
- # rule('annotation' => 'LEFT_BRACE mapping RIGHT_BRACE').tag ''
148
- def reduce_annotation(_production, _range, _tokens, theChildren)
149
- theChildren[1]
150
- end
151
-
152
- # rule('mapping' => 'mapping COMMA key_value')
153
- def reduce_more_pairs(_production, _range, _tokens, theChildren)
154
- hsh = theChildren[0]
155
- hsh[theChildren[2].first] = theChildren[2].last
156
-
157
- hsh
158
- end
159
-
160
- # rule('mapping' => 'key_value').tag 'one_pair'
161
- def reduce_one_pair(_production, _range, _tokens, theChildren)
162
- { theChildren[0].first => theChildren[0].last }
163
- end
164
-
165
- # rule('key_value' => 'KEY value')
166
- def reduce_raw_pair(_production, _range, _tokens, theChildren)
167
- key = theChildren[0].token.lexeme
168
- value = if theChildren[1].kind_of?(Rley::PTree::TerminalNode)
169
- theChildren[1].token.lexeme
170
- else
171
- theChildren[1]
172
- end
173
- [key, value]
174
- end
175
-
176
- # rule('range' => 'INT_LIT ELLIPSIS INT_LIT')
177
- def reduce_bound_range(_production, _range, _tokens, theChildren)
178
- low = theChildren[0].token.lexeme
179
- high = theChildren[2].token.lexeme
180
- case [low, high]
181
- when ['0', '1']
182
- :zero_or_one
183
- when ['1', '1']
184
- :exactly_one
185
- else
186
- Range.new(low.to_i, high.to_i)
187
- end
188
- end
189
- end # class
190
- end # module
191
- end # module
1
+ # frozen_string_literal: true
2
+
3
+ require_relative '../parse_rep/ast_base_builder'
4
+ require_relative '../engine'
5
+ require_relative 'all_notation_nodes'
6
+
7
+ module Rley
8
+ module Notation
9
+ # The purpose of ASTBuilder is to build piece by piece an AST
10
+ # (Abstract Syntax Tree) from a sequence of input tokens and
11
+ # visit events produced by walking over a GFGParsing object.
12
+ class ASTBuilder < Rley::ParseRep::ASTBaseBuilder
13
+ unless defined?(Name2special)
14
+ # Mapping Token name => operator | separator | delimiter characters
15
+ # @return [Hash{String => String}]
16
+ Name2special = {
17
+ 'COMMA' => ',',
18
+ 'ELLIPSIS' => '..',
19
+ 'LEFT_BRACE' => '{',
20
+ 'LEFT_PAREN' => '(',
21
+ 'PLUS' => '+',
22
+ 'QUESTION_MARK' => '?',
23
+ 'RIGHT_BRACE' => '}',
24
+ 'RIGHT_PAREN' => ')',
25
+ 'STAR' => '*'
26
+ }.freeze
27
+ end
28
+
29
+ protected
30
+
31
+ def terminal2node
32
+ Terminal2NodeClass
33
+ end
34
+
35
+ # Method override
36
+ def new_leaf_node(_production, _terminal, aTokenPosition, aToken)
37
+ Rley::PTree::TerminalNode.new(aToken, aTokenPosition)
38
+ end
39
+
40
+ # Factory method for creating a parent node object.
41
+ # @param aProduction [Production] Production rule
42
+ # @param aRange [Range] Range of tokens matched by the rule
43
+ # @param theTokens [Array] The input tokens
44
+ # @param theChildren [Array] Children nodes (one per rhs symbol)
45
+ def new_parent_node(aProduction, aRange, theTokens, theChildren)
46
+ mth_name = method_name(aProduction.name)
47
+ if respond_to?(mth_name, true)
48
+ node = send(mth_name, aProduction, aRange, theTokens, theChildren)
49
+ else
50
+ # Default action...
51
+ node = case aProduction.rhs.size
52
+ when 0
53
+ return_epsilon(aRange, theTokens, theChildren)
54
+ when 1
55
+ return_first_child(aRange, theTokens, theChildren)
56
+ else
57
+ node = Rley::PTree::NonTerminalNode.new(aProduction.lhs, aRange)
58
+ theChildren&.reverse_each do |child|
59
+ node.add_subnode(child) if child
60
+ end
61
+
62
+ node
63
+ end
64
+ end
65
+
66
+ node
67
+ end
68
+
69
+ # Return the AST node corresponding to the second symbol in the rhs
70
+ def reduce_to_2nd_symbol(_production, _range, _tokens, theChildren)
71
+ theChildren[1]
72
+ end
73
+
74
+ #####################################
75
+ # SEMANTIC ACTIONS
76
+ #####################################
77
+
78
+ # rule('rhs' => 'member_seq').tag 'sequence'
79
+ def reduce_sequence(_production, _range, _tokens, theChildren)
80
+ if theChildren[0].size == 1
81
+ theChildren[0].first
82
+ else
83
+ SequenceNode.new(theChildren[0].first.position, theChildren[0], nil)
84
+ end
85
+ end
86
+
87
+ # rule('member_seq' => 'member_seq member').tag 'more_members'
88
+ def reduce_more_members(_production, _range, _tokens, theChildren)
89
+ theChildren[0] << theChildren[1]
90
+ end
91
+
92
+ # rule('member_seq' => 'member')
93
+ def reduce_one_member(_production, _range, _tokens, theChildren)
94
+ [theChildren[0]]
95
+ end
96
+
97
+ # rule('strait_member' => 'base_member annotation')
98
+ def reduce_annotated_member(_production, _range, _tokens, theChildren)
99
+ theChildren[0].annotation = theChildren[1]
100
+
101
+ theChildren[0]
102
+ end
103
+
104
+ # rule('base_member' => 'SYMBOL')
105
+ def reduce_symbol(_production, _range, _tokens, theChildren)
106
+ SymbolNode.new(theChildren[0].token.position, theChildren[0].token.lexeme)
107
+ end
108
+
109
+ # rule('base_member' => 'LEFT_PAREN member_seq RIGHT_PAREN')
110
+ def reduce_grouping(_production, _range, tokens, theChildren)
111
+ if theChildren[1].size == 1
112
+ theChildren[1].first
113
+ else
114
+ rank = theChildren[0].range.high
115
+ pos = tokens[rank].position
116
+ GroupingNode.new(pos, theChildren[1], nil)
117
+ end
118
+ end
119
+
120
+ # rule('quantified_member' => 'base_member quantifier')
121
+ def reduce_quantified_member(_production, _range, _tokens, theChildren)
122
+ theChildren[0].repetition = theChildren[1]
123
+ theChildren[0]
124
+ end
125
+
126
+ # rule('quantifier' => 'QUESTION_MARK')
127
+ def reduce_question_mark(_production, _range, _tokens, _theChildren)
128
+ :zero_or_one
129
+ end
130
+
131
+ # rule('quantifier' => 'STAR')
132
+ def reduce_star(_production, _range, _tokens, _theChildren)
133
+ :zero_or_more
134
+ end
135
+
136
+ # rule('quantifier' => 'PLUS')
137
+ def reduce_plus(_production, _range, _tokens, _theChildren)
138
+ :one_or_more
139
+ end
140
+
141
+ # rule('annotation' => 'LEFT_BRACE mapping RIGHT_BRACE').tag ''
142
+ def reduce_annotation(_production, _range, _tokens, theChildren)
143
+ theChildren[1]
144
+ end
145
+
146
+ # rule('mapping' => 'mapping COMMA key_value')
147
+ def reduce_more_pairs(_production, _range, _tokens, theChildren)
148
+ hsh = theChildren[0]
149
+ hsh[theChildren[2].first] = theChildren[2].last
150
+
151
+ hsh
152
+ end
153
+
154
+ # rule('mapping' => 'key_value').tag 'one_pair'
155
+ def reduce_one_pair(_production, _range, _tokens, theChildren)
156
+ { theChildren[0].first => theChildren[0].last }
157
+ end
158
+
159
+ # rule('key_value' => 'KEY value')
160
+ def reduce_raw_pair(_production, _range, _tokens, theChildren)
161
+ key = theChildren[0].token.lexeme
162
+ value = if theChildren[1].kind_of?(Rley::PTree::TerminalNode)
163
+ theChildren[1].token.lexeme
164
+ else
165
+ theChildren[1]
166
+ end
167
+ [key, value]
168
+ end
169
+
170
+ # rule('range' => 'INT_LIT ELLIPSIS INT_LIT')
171
+ def reduce_bound_range(_production, _range, _tokens, theChildren)
172
+ low = theChildren[0].token.lexeme
173
+ high = theChildren[2].token.lexeme
174
+ case [low, high]
175
+ when %w[0 1]
176
+ :zero_or_one
177
+ when %w[1 1]
178
+ :exactly_one
179
+ else
180
+ Range.new(low.to_i, high.to_i)
181
+ end
182
+ end
183
+ end # class
184
+ end # module
185
+ end # module