rley 0.7.07 → 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 +348 -54
- data/LICENSE.txt +1 -1
- data/README.md +3 -2
- data/examples/NLP/engtagger.rb +193 -190
- data/examples/NLP/nano_eng/nano_grammar.rb +5 -5
- data/examples/data_formats/JSON/cli_options.rb +1 -1
- data/examples/data_formats/JSON/json_ast_builder.rb +12 -9
- data/examples/data_formats/JSON/json_ast_nodes.rb +12 -21
- data/examples/data_formats/JSON/json_grammar.rb +2 -2
- 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 +2 -2
- 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 +3 -3
- 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/base/dotted_item.rb +23 -31
- data/lib/rley/constants.rb +2 -2
- data/lib/rley/engine.rb +20 -23
- 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/lexical/token_range.rb +6 -6
- data/lib/rley/parse_forest_visitor.rb +5 -5
- data/lib/rley/parse_rep/ast_base_builder.rb +9 -11
- data/lib/rley/parse_rep/cst_builder.rb +5 -6
- data/lib/rley/parse_rep/parse_forest_builder.rb +20 -18
- data/lib/rley/parse_rep/parse_forest_factory.rb +3 -3
- data/lib/rley/parse_rep/parse_rep_creator.rb +11 -13
- 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 +20 -22
- data/lib/rley/parser/gfg_parsing.rb +16 -30
- data/lib/rley/parser/parse_entry.rb +25 -31
- data/lib/rley/parser/parse_entry_set.rb +18 -15
- data/lib/rley/parser/parse_entry_tracker.rb +4 -4
- data/lib/rley/parser/parse_state.rb +16 -21
- data/lib/rley/parser/parse_state_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/parser/state_set.rb +9 -10
- 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.rb +5 -5
- data/lib/rley/syntax/grammar_builder.rb +9 -9
- data/lib/rley/syntax/grm_symbol.rb +6 -6
- data/lib/rley/syntax/non_terminal.rb +9 -15
- data/lib/rley/syntax/production.rb +10 -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 +17 -15
- 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/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/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 -6
- data/spec/rley/parse_rep/cst_builder_spec.rb +5 -5
- data/spec/rley/parse_rep/groucho_spec.rb +21 -21
- data/spec/rley/parse_rep/parse_forest_builder_spec.rb +26 -26
- data/spec/rley/parse_rep/parse_forest_factory_spec.rb +6 -6
- data/spec/rley/parse_rep/parse_tree_factory_spec.rb +2 -2
- data/spec/rley/parse_tree_visitor_spec.rb +10 -8
- data/spec/rley/parser/error_reason_spec.rb +6 -6
- data/spec/rley/parser/gfg_earley_parser_spec.rb +4 -2
- data/spec/rley/parser/gfg_parsing_spec.rb +4 -8
- data/spec/rley/parser/parse_entry_spec.rb +19 -19
- data/spec/rley/parser/parse_state_spec.rb +5 -5
- data/spec/rley/parser/parse_walker_factory_spec.rb +1 -1
- data/spec/rley/parser/state_set_spec.rb +22 -22
- 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 +3 -4
- data/spec/rley/support/grammar_abc_helper.rb +2 -4
- data/spec/rley/support/grammar_ambig01_helper.rb +4 -5
- data/spec/rley/support/grammar_arr_int_helper.rb +4 -5
- data/spec/rley/support/grammar_b_expr_helper.rb +4 -5
- data/spec/rley/support/grammar_l0_helper.rb +10 -11
- data/spec/rley/support/grammar_pb_helper.rb +6 -5
- data/spec/rley/support/grammar_sppf_helper.rb +1 -1
- data/spec/rley/syntax/grammar_builder_spec.rb +5 -5
- data/spec/rley/syntax/grammar_spec.rb +6 -6
- data/spec/rley/syntax/grm_symbol_spec.rb +1 -1
- data/spec/rley/syntax/non_terminal_spec.rb +8 -8
- data/spec/rley/syntax/production_spec.rb +13 -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 +21 -62
- data/.simplecov +0 -8
@@ -55,7 +55,7 @@ END_TEXT
|
|
55
55
|
|
56
56
|
rep_help = <<-END_TEXT
|
57
57
|
Set the parse tree representation (default: cst)
|
58
|
-
cst Concrete Syntax Tree. The out-of-the-box parse tree
|
58
|
+
cst Concrete Syntax Tree. The out-of-the-box parse tree
|
59
59
|
representation.
|
60
60
|
ast Abstract Syntaxt Tree. A customized parse tree for JSON.
|
61
61
|
It is a more compact and practical representation.
|
@@ -9,7 +9,7 @@ require_relative 'json_ast_nodes'
|
|
9
9
|
# The Builder pattern creates a complex object
|
10
10
|
# (say, a parse tree) from simpler objects (terminal and non-terminal
|
11
11
|
# nodes) and using a step by step approach.
|
12
|
-
class JSONASTBuilder < Rley::ParseRep::ASTBaseBuilder
|
12
|
+
class JSONASTBuilder < Rley::ParseRep::ASTBaseBuilder
|
13
13
|
Terminal2NodeClass = {
|
14
14
|
'false' => JSONBooleanNode,
|
15
15
|
'true' => JSONBooleanNode,
|
@@ -19,21 +19,23 @@ class JSONASTBuilder < Rley::ParseRep::ASTBaseBuilder
|
|
19
19
|
}.freeze
|
20
20
|
|
21
21
|
protected
|
22
|
-
|
23
|
-
def terminal2node
|
22
|
+
|
23
|
+
def terminal2node
|
24
24
|
Terminal2NodeClass
|
25
25
|
end
|
26
|
-
|
26
|
+
|
27
27
|
# Default class for representing terminal nodes.
|
28
28
|
# @return [Class]
|
29
|
-
def terminalnode_class
|
29
|
+
def terminalnode_class
|
30
30
|
JSONTerminalNode
|
31
31
|
end
|
32
|
-
|
32
|
+
|
33
|
+
# rubocop: disable Naming/VariableNumber
|
34
|
+
|
33
35
|
def reduce_JSON_text_0(_aProd, aRange, theTokens, theChildren)
|
34
36
|
return_first_child(aRange, theTokens, theChildren)
|
35
37
|
end
|
36
|
-
|
38
|
+
|
37
39
|
# rule 'object' => %w[begin-object member-list end-object]
|
38
40
|
def reduce_object_0(aProduction, _range, _tokens, theChildren)
|
39
41
|
second_child = theChildren[1]
|
@@ -69,7 +71,7 @@ class JSONASTBuilder < Rley::ParseRep::ASTBaseBuilder
|
|
69
71
|
def reduce_array_0(aProduction, _range, _tokens, theChildren)
|
70
72
|
second_child = theChildren[1]
|
71
73
|
second_child.symbol = aProduction.lhs
|
72
|
-
return second_child
|
74
|
+
return second_child
|
73
75
|
end
|
74
76
|
|
75
77
|
# rule 'array' => %w[begin-array end-array]
|
@@ -83,12 +85,13 @@ class JSONASTBuilder < Rley::ParseRep::ASTBaseBuilder
|
|
83
85
|
node.children << theChildren[2]
|
84
86
|
return node
|
85
87
|
end
|
86
|
-
|
88
|
+
|
87
89
|
# rule 'array-items' => %w[value]
|
88
90
|
def reduce_array_items_1(aProduction, _range, _tokens, theChildren)
|
89
91
|
node = JSONArrayNode.new(aProduction.lhs)
|
90
92
|
node.children << theChildren[0]
|
91
93
|
return node
|
92
94
|
end
|
95
|
+
# rubocop: enable Naming/VariableNumber
|
93
96
|
end # class
|
94
97
|
# End of file
|
@@ -16,11 +16,11 @@ JSONTerminalNode = Struct.new(:token, :value, :position) do
|
|
16
16
|
self.value = aLiteral.dup
|
17
17
|
end
|
18
18
|
|
19
|
-
def symbol
|
19
|
+
def symbol
|
20
20
|
token.terminal
|
21
21
|
end
|
22
22
|
|
23
|
-
def to_ruby
|
23
|
+
def to_ruby
|
24
24
|
value
|
25
25
|
end
|
26
26
|
|
@@ -29,9 +29,9 @@ JSONTerminalNode = Struct.new(:token, :value, :position) do
|
|
29
29
|
def accept(aVisitor)
|
30
30
|
aVisitor.visit_terminal(self)
|
31
31
|
end
|
32
|
-
|
32
|
+
|
33
33
|
def done!
|
34
|
-
# Do nothing
|
34
|
+
# Do nothing
|
35
35
|
end
|
36
36
|
end
|
37
37
|
|
@@ -77,23 +77,18 @@ class JSONCompositeNode
|
|
77
77
|
def accept(aVisitor)
|
78
78
|
aVisitor.visit_nonterminal(self)
|
79
79
|
end
|
80
|
-
|
80
|
+
|
81
81
|
def done!
|
82
|
-
# Do nothing
|
82
|
+
# Do nothing
|
83
83
|
end
|
84
84
|
|
85
85
|
alias subnodes children
|
86
86
|
end # class
|
87
87
|
|
88
|
-
|
89
88
|
class JSONArrayNode < JSONCompositeNode
|
90
|
-
def initialize(aSymbol)
|
91
|
-
super(aSymbol)
|
92
|
-
end
|
93
|
-
|
94
89
|
# Convert this tree node in a simpler Ruby representation.
|
95
90
|
# Basically a JSON object corresponds to a Ruhy Hash
|
96
|
-
def to_ruby
|
91
|
+
def to_ruby
|
97
92
|
rep = []
|
98
93
|
children.each do |child|
|
99
94
|
rep << child.to_ruby
|
@@ -114,7 +109,7 @@ class JSONPair
|
|
114
109
|
@symbol = aSymbol
|
115
110
|
end
|
116
111
|
|
117
|
-
def children
|
112
|
+
def children
|
118
113
|
return [name, value]
|
119
114
|
end
|
120
115
|
|
@@ -125,27 +120,23 @@ class JSONPair
|
|
125
120
|
def accept(aVisitor)
|
126
121
|
aVisitor.visit_nonterminal(self)
|
127
122
|
end
|
128
|
-
|
123
|
+
|
129
124
|
def done!
|
130
125
|
# Do nothing
|
131
126
|
end
|
132
|
-
|
127
|
+
|
133
128
|
def to_ruby
|
134
129
|
rep = {}
|
135
130
|
rep[name.to_ruby] = value.to_ruby
|
136
131
|
|
137
|
-
return rep
|
132
|
+
return rep
|
138
133
|
end
|
139
134
|
end # class
|
140
135
|
|
141
136
|
class JSONObjectNode < JSONCompositeNode
|
142
|
-
def initialize(aSymbol)
|
143
|
-
super(aSymbol)
|
144
|
-
end
|
145
|
-
|
146
137
|
# Convert this tree node in a simpler Ruby representation.
|
147
138
|
# Basically a JSON object corresponds to a Ruby Hash
|
148
|
-
def to_ruby
|
139
|
+
def to_ruby
|
149
140
|
rep = {}
|
150
141
|
members.each do |pair|
|
151
142
|
rep[pair.name.to_ruby] = pair.value.to_ruby
|
@@ -18,11 +18,11 @@ builder = Rley::Syntax::GrammarBuilder.new do
|
|
18
18
|
rule 'JSON_text' => 'value'
|
19
19
|
rule 'value' => 'false'
|
20
20
|
rule 'value' => 'null'
|
21
|
-
rule 'value' => 'true'
|
21
|
+
rule 'value' => 'true'
|
22
22
|
rule 'value' => 'object'
|
23
23
|
rule 'value' => 'array'
|
24
24
|
rule 'value' => 'number'
|
25
|
-
rule 'value' => 'string'
|
25
|
+
rule 'value' => 'string'
|
26
26
|
rule 'object' => %w[begin-object member-list end-object]
|
27
27
|
rule 'object' => %w[begin-object end-object]
|
28
28
|
# Next rule is an example of a left recursive rule
|
@@ -28,19 +28,19 @@ class JSONLexer
|
|
28
28
|
@line_start = 0
|
29
29
|
end
|
30
30
|
|
31
|
-
def tokens
|
31
|
+
def tokens
|
32
32
|
tok_sequence = []
|
33
33
|
until @scanner.eos?
|
34
34
|
token = _next_token
|
35
35
|
tok_sequence << token unless token.nil?
|
36
36
|
end
|
37
37
|
|
38
|
-
|
38
|
+
tok_sequence
|
39
39
|
end
|
40
40
|
|
41
41
|
private
|
42
42
|
|
43
|
-
def _next_token
|
43
|
+
def _next_token
|
44
44
|
token = nil
|
45
45
|
skip_whitespaces
|
46
46
|
curr_ch = scanner.getch # curr_ch is at start of token or eof reached...
|
@@ -58,7 +58,7 @@ class JSONLexer
|
|
58
58
|
keyw = scanner.scan(/false|true|null/)
|
59
59
|
if keyw.nil?
|
60
60
|
invalid_keyw = scanner.scan(/\w+/)
|
61
|
-
raise ScanError
|
61
|
+
raise ScanError, "Invalid keyword: #{invalid_keyw}"
|
62
62
|
else
|
63
63
|
token = build_token(keyw, keyw)
|
64
64
|
end
|
@@ -68,7 +68,7 @@ class JSONLexer
|
|
68
68
|
value = scanner.scan(/([^"\\]|\\.)*/)
|
69
69
|
end_delimiter = scanner.getch
|
70
70
|
err_msg = 'No closing quotes (") found'
|
71
|
-
raise ScanError
|
71
|
+
raise ScanError, err_msg if end_delimiter.nil?
|
72
72
|
|
73
73
|
token = build_token(value, 'string')
|
74
74
|
|
@@ -81,12 +81,12 @@ class JSONLexer
|
|
81
81
|
erroneous = curr_ch.nil? ? '' : curr_ch
|
82
82
|
sequel = scanner.scan(/.{1,20}/)
|
83
83
|
erroneous += sequel unless sequel.nil?
|
84
|
-
raise ScanError
|
84
|
+
raise ScanError, "Unknown token #{erroneous}"
|
85
85
|
end # case
|
86
86
|
break unless token.nil? && (curr_ch = scanner.getch)
|
87
87
|
end
|
88
88
|
|
89
|
-
|
89
|
+
token
|
90
90
|
end
|
91
91
|
|
92
92
|
def build_token(lexeme, token)
|
@@ -94,7 +94,7 @@ class JSONLexer
|
|
94
94
|
Rley::Lexical::Token.new(lexeme, token, pos)
|
95
95
|
end
|
96
96
|
|
97
|
-
def skip_whitespaces
|
97
|
+
def skip_whitespaces
|
98
98
|
matched = scanner.scan(/[ \t\f\n\r]+/)
|
99
99
|
return if matched.nil?
|
100
100
|
|
@@ -15,8 +15,8 @@ class CalcASTBuilder < Rley::ParseRep::ASTBaseBuilder
|
|
15
15
|
}.freeze
|
16
16
|
|
17
17
|
protected
|
18
|
-
|
19
|
-
def terminal2node
|
18
|
+
|
19
|
+
def terminal2node
|
20
20
|
Terminal2NodeClass
|
21
21
|
end
|
22
22
|
|
@@ -26,7 +26,9 @@ class CalcASTBuilder < Rley::ParseRep::ASTBaseBuilder
|
|
26
26
|
operator_node.children << theChildren[2]
|
27
27
|
return operator_node
|
28
28
|
end
|
29
|
-
|
29
|
+
|
30
|
+
# rubocop: disable Naming/VariableNumber
|
31
|
+
|
30
32
|
# rule 'expression' => 'simple_expression'
|
31
33
|
def reduce_expression_0(_production, aRange, theTokens, theChildren)
|
32
34
|
return_first_child(aRange, theTokens, theChildren)
|
@@ -35,32 +37,32 @@ class CalcASTBuilder < Rley::ParseRep::ASTBaseBuilder
|
|
35
37
|
# rule 'simple_expression' => 'term'
|
36
38
|
def reduce_simple_expression_0(_production, aRange, theTokens, theChildren)
|
37
39
|
return_first_child(aRange, theTokens, theChildren)
|
38
|
-
end
|
40
|
+
end
|
39
41
|
|
40
42
|
# rule 'simple_expression' => %w[simple_expression add_operator term]
|
41
43
|
def reduce_simple_expression_1(_production, _range, _tokens, theChildren)
|
42
44
|
reduce_binary_operator(theChildren)
|
43
45
|
end
|
44
|
-
|
45
|
-
# rule 'term' => 'factor'
|
46
|
+
|
47
|
+
# rule 'term' => 'factor'
|
46
48
|
def reduce_term_0(_production, aRange, theTokens, theChildren)
|
47
49
|
return_first_child(aRange, theTokens, theChildren)
|
48
|
-
end
|
50
|
+
end
|
49
51
|
|
50
52
|
# rule 'term' => %w[term mul_operator factor]
|
51
53
|
def reduce_term_1(_production, _range, _tokens, theChildren)
|
52
54
|
reduce_binary_operator(theChildren)
|
53
55
|
end
|
54
|
-
|
56
|
+
|
55
57
|
# rule 'factor' => 'NUMBER'
|
56
58
|
def reduce_factor_0(_aProd, aRange, theTokens, theChildren)
|
57
59
|
return_first_child(aRange, theTokens, theChildren)
|
58
60
|
end
|
59
|
-
|
61
|
+
|
60
62
|
# # rule 'factor' => %w[LPAREN expression RPAREN]
|
61
63
|
def reduce_factor_1(_aProd, aRange, theTokens, theChildren)
|
62
64
|
return_second_child(aRange, theTokens, theChildren)
|
63
|
-
end
|
65
|
+
end
|
64
66
|
|
65
67
|
# rule 'add_operator' => 'PLUS'
|
66
68
|
def reduce_add_operator_0(_production, _range, _tokens, theChildren)
|
@@ -81,5 +83,6 @@ class CalcASTBuilder < Rley::ParseRep::ASTBaseBuilder
|
|
81
83
|
def reduce_mul_operator_1(_production, _range, _tokens, theChildren)
|
82
84
|
return CalcDivideNode.new(theChildren[0].symbol)
|
83
85
|
end
|
86
|
+
# rubocop: enable Naming/VariableNumber
|
84
87
|
end # class
|
85
88
|
# End of file
|
@@ -3,7 +3,6 @@
|
|
3
3
|
# Classes that implement nodes of Abstract Syntax Trees (AST) representing
|
4
4
|
# calculator parse results.
|
5
5
|
|
6
|
-
|
7
6
|
CalcTerminalNode = Struct.new(:token, :value, :position) do
|
8
7
|
def initialize(aToken, aPosition)
|
9
8
|
self.token = aToken
|
@@ -16,23 +15,23 @@ CalcTerminalNode = Struct.new(:token, :value, :position) do
|
|
16
15
|
self.value = aLiteral.dup
|
17
16
|
end
|
18
17
|
|
19
|
-
def symbol
|
18
|
+
def symbol
|
20
19
|
token.terminal
|
21
20
|
end
|
22
21
|
|
23
|
-
def interpret
|
24
|
-
|
22
|
+
def interpret
|
23
|
+
value
|
25
24
|
end
|
26
|
-
|
27
|
-
def done!
|
25
|
+
|
26
|
+
def done!
|
28
27
|
# Do nothing
|
29
28
|
end
|
30
|
-
|
29
|
+
|
31
30
|
# Part of the 'visitee' role in Visitor design pattern.
|
32
31
|
# @param aVisitor[ParseTreeVisitor] the visitor
|
33
32
|
def accept(aVisitor)
|
34
33
|
aVisitor.visit_terminal(self)
|
35
|
-
end
|
34
|
+
end
|
36
35
|
end
|
37
36
|
|
38
37
|
class CalcNumberNode < CalcTerminalNode
|
@@ -53,30 +52,26 @@ class CalcCompositeNode
|
|
53
52
|
|
54
53
|
def initialize(aSymbol)
|
55
54
|
@symbol = aSymbol
|
56
|
-
@children = []
|
55
|
+
@children = []
|
57
56
|
end
|
58
|
-
|
59
|
-
def done!
|
57
|
+
|
58
|
+
def done!
|
60
59
|
# Do nothing
|
61
|
-
end
|
60
|
+
end
|
62
61
|
|
63
62
|
# Part of the 'visitee' role in Visitor design pattern.
|
64
63
|
# @param aVisitor[ParseTreeVisitor] the visitor
|
65
64
|
def accept(aVisitor)
|
66
65
|
aVisitor.visit_nonterminal(self)
|
67
66
|
end
|
68
|
-
|
67
|
+
|
69
68
|
alias subnodes children
|
70
69
|
end # class
|
71
70
|
|
72
71
|
class CalcUnaryOpNode < CalcCompositeNode
|
73
|
-
def initialize(aSymbol)
|
74
|
-
super(aSymbol)
|
75
|
-
end
|
76
|
-
|
77
72
|
# Convert this tree node in a simpler Ruby representation.
|
78
73
|
# Basically a Calc object corresponds to a Ruhy Hash
|
79
|
-
def to_ruby
|
74
|
+
def to_ruby
|
80
75
|
rep = {}
|
81
76
|
members.each do |pair|
|
82
77
|
rep[pair.name.to_ruby] = pair.value.to_ruby
|
@@ -92,13 +87,9 @@ class CalcNegateNode < CalcUnaryOpNode
|
|
92
87
|
end # class
|
93
88
|
|
94
89
|
class CalcBinaryOpNode < CalcCompositeNode
|
95
|
-
def initialize(aSymbol)
|
96
|
-
super(aSymbol)
|
97
|
-
end
|
98
|
-
|
99
90
|
protected
|
100
|
-
|
101
|
-
def retrieve_operands
|
91
|
+
|
92
|
+
def retrieve_operands
|
102
93
|
operands = []
|
103
94
|
children.each do |child|
|
104
95
|
oper = child.respond_to?(:interpret) ? child.interpret : child
|
@@ -111,42 +102,37 @@ end # class
|
|
111
102
|
|
112
103
|
class CalcAddNode < CalcBinaryOpNode
|
113
104
|
# TODO
|
114
|
-
def interpret
|
105
|
+
def interpret
|
115
106
|
operands = retrieve_operands
|
116
107
|
|
117
|
-
|
118
|
-
return sum
|
108
|
+
operands[0] + operands[1]
|
119
109
|
end
|
120
110
|
end # class
|
121
111
|
|
122
|
-
|
123
112
|
class CalcSubtractNode < CalcBinaryOpNode
|
124
113
|
# TODO
|
125
|
-
def interpret
|
114
|
+
def interpret
|
126
115
|
operands = retrieve_operands
|
127
116
|
|
128
|
-
|
129
|
-
return substraction
|
117
|
+
operands[0] - operands[1]
|
130
118
|
end
|
131
119
|
end # class
|
132
120
|
|
133
121
|
class CalcMultiplyNode < CalcBinaryOpNode
|
134
122
|
# TODO
|
135
|
-
def interpret
|
123
|
+
def interpret
|
136
124
|
operands = retrieve_operands
|
137
|
-
|
138
|
-
return multiplication
|
125
|
+
operands[0] * operands[1]
|
139
126
|
end
|
140
127
|
end # class
|
141
128
|
|
142
129
|
class CalcDivideNode < CalcBinaryOpNode
|
143
130
|
# TODO
|
144
|
-
def interpret
|
131
|
+
def interpret
|
145
132
|
operands = retrieve_operands
|
146
133
|
numerator = operands[0].to_f
|
147
134
|
denominator = operands[1]
|
148
|
-
|
149
|
-
return division
|
135
|
+
numerator / denominator
|
150
136
|
end
|
151
137
|
end # class
|
152
138
|
# End of file
|