depager 0.3.0.b20160729 → 0.3.0.b20250423

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.
Files changed (63) hide show
  1. checksums.yaml +5 -5
  2. data/.rubocop.yml +44 -0
  3. data/.simplecov +5 -0
  4. data/Gemfile +12 -0
  5. data/LICENSE.gpl +339 -0
  6. data/Manifest.txt +73 -0
  7. data/README.en +4 -3
  8. data/README.ja +4 -47
  9. data/Rakefile +31 -0
  10. data/bin/depager +3 -38
  11. data/examples/action_pl0d/pl0d.action.dr +4 -4
  12. data/examples/action_pl0d/test.pl0ds +2 -3
  13. data/examples/c89/c89.dr +4 -4
  14. data/examples/c89/test.c89 +1 -1
  15. data/examples/extension/astdf.rb +4 -5
  16. data/examples/extension/atree.dr +3 -3
  17. data/examples/extension/calc.atree.dr +4 -4
  18. data/examples/extension/calc.simple_action.dr +3 -3
  19. data/examples/extension/paction.dr +3 -3
  20. data/examples/extension/pactiontest.dr +3 -3
  21. data/examples/extension/simple_action.rb +26 -24
  22. data/examples/pl0d/pl0ds.dr +5 -5
  23. data/examples/pl0d/test.pl0ds +2 -2
  24. data/examples/rie_calc/calc.rie.dr +4 -4
  25. data/examples/rie_dcuse/dcuse.rie.dr +4 -4
  26. data/examples/rie_pl0/pl0.rie.dr +3 -3
  27. data/examples/slex_test/divreg.slex.dr +5 -5
  28. data/examples/slex_test/ljoin.slex.dr +5 -5
  29. data/examples/{sample_calc → tiny_calc}/calc.action.dr +4 -4
  30. data/examples/{sample_calc → tiny_calc}/calc.ast.action.dr +20 -9
  31. data/examples/{sample_calc → tiny_calc}/calc.ast.dr +19 -7
  32. data/examples/{sample_calc → tiny_calc}/calc.cst.dr +12 -7
  33. data/examples/{sample_calc → tiny_calc}/calc.dr +1 -1
  34. data/examples/{sample_calc → tiny_calc}/calc.lex.dr +2 -2
  35. data/examples/{sample_calc → tiny_calc}/calc_prec.action.dr +4 -4
  36. data/lib/depager/cli.rb +44 -0
  37. data/lib/depager/grammar.rb +72 -75
  38. data/lib/depager/lr.rb +169 -154
  39. data/lib/depager/parser.rb +90 -103
  40. data/lib/depager/plugins/_rie_debug.rb +63 -0
  41. data/lib/depager/plugins/action.rb +47 -0
  42. data/lib/depager/{ruby/plugins → plugins}/ast.dr +20 -17
  43. data/lib/depager/{ruby/plugins → plugins}/ast.rb +266 -304
  44. data/lib/depager/{ruby/plugins → plugins}/cst.dr +18 -16
  45. data/lib/depager/{ruby/plugins → plugins}/cst.rb +152 -148
  46. data/lib/depager/{ruby/plugins → plugins}/lex.dr +7 -7
  47. data/lib/depager/{ruby/plugins → plugins}/lex.rb +72 -69
  48. data/lib/depager/{ruby/plugins → plugins}/rie.dr +12 -10
  49. data/lib/depager/{ruby/plugins → plugins}/rie.rb +224 -263
  50. data/lib/depager/{ruby/plugins → plugins}/slex.dr +13 -14
  51. data/lib/depager/{ruby/plugins → plugins}/slex.rb +183 -194
  52. data/lib/depager/plugins/srp.rb +46 -0
  53. data/lib/depager/ruby/templates/extension_lalr_master.erb +6 -12
  54. data/lib/depager/ruby/templates/extension_lalr_slave.erb +31 -17
  55. data/lib/depager/ruby/templates/single_lalr_parser.erb +35 -26
  56. data/lib/depager/utils.rb +56 -46
  57. data/lib/depager/version.rb +1 -2
  58. data/lib/depager.rb +166 -176
  59. metadata +38 -33
  60. data/lib/depager/ruby/plugins/_rie_debug.rb +0 -35
  61. data/lib/depager/ruby/plugins/action.rb +0 -53
  62. data/lib/depager/ruby/plugins/srp.rb +0 -56
  63. /data/examples/{sample_calc → tiny_calc}/test.calc +0 -0
@@ -0,0 +1,46 @@
1
+ class Depager::LALR::ShiftReducePrinter < Depager::LALR::AdvancedParser
2
+ def dumpstack(stack)
3
+ stack.map.with_index do |v, x|
4
+ if x.even?
5
+ "(#{v})"
6
+ elsif v[1] == :NT
7
+ "#{int_to_nonterm[v[0]]}:#{v[1..].inspect}"
8
+ elsif int_to_term[v[0]].instance_of?(String)
9
+ "'#{int_to_term[v[0]]}':#{v[1..].inspect}"
10
+ else
11
+ "#{int_to_term[v[0]]}:#{v[1..].inspect}"
12
+ end
13
+ end.join(" ")
14
+ end
15
+
16
+ def after_shift
17
+ st0 = stack[stack.size - 3]
18
+ st = stack.last
19
+ sh = int_to_term[stack[stack.size - 2][0]]
20
+ la = int_to_term[lookahead[0]]
21
+ warn "LA:<#{la}> shift:<#{sh}> state:#{st0}->#{st}"
22
+ warn "LINE:<#{basis.line}>"
23
+ warn dumpstack(stack), "\n"
24
+ end
25
+
26
+ def after_reduce
27
+ st = stack.last
28
+ re = int_to_nonterm[stack[stack.size - 2][0]]
29
+ la = int_to_term[lookahead[0]]
30
+ warn "LA:<#{la}> reduce:<#{re}> state: ->#{st}"
31
+ warn "LINE:<#{basis.line}>"
32
+ warn dumpstack(stack), "\n"
33
+ end
34
+
35
+ def after_accept
36
+ warn "acc."
37
+ end
38
+
39
+ def after_error
40
+ st = stack.last
41
+ la = int_to_term[lookahead[0]]
42
+ warn "LA:<#{la}> last_state:#{st}"
43
+ warn "LINE:<#{basis.line}>"
44
+ warn dumpstack(stack), "\n"
45
+ end
46
+ end
@@ -1,5 +1,3 @@
1
- # -*- coding: utf-8 -*-
2
-
3
1
  ###
4
2
  ### <%= d_parser.target_namespace %> - Depager Extension (master)
5
3
  ###
@@ -20,6 +18,7 @@ class <%= d_parser.target_namespace %> < Depager::Extension
20
18
  <%- end -%>
21
19
 
22
20
  def initialize
21
+ super
23
22
  @master = self
24
23
  @slaves = []
25
24
  end
@@ -27,20 +26,15 @@ class <%= d_parser.target_namespace %> < Depager::Extension
27
26
  def extension_registered g_parser
28
27
  super g_parser
29
28
 
30
- <%- x = 0; h = {}; -%>
31
- <%- @slaves.each do |i, m| -%>
32
- <%- unless h[m] -%>
33
- @slaves[<%= x %>] = <%= m %>.new(g_parser, self)
34
- <%- h[m] = x -%>
35
- <%- else -%>
36
- # use @slaves[<%= h[m] %>](<%= m %>)
29
+ <%- @slaves.each do |name, hooks| -%>
30
+ @slaves << <%= name %>.new(g_parser, self)
31
+ <%- hooks.each do |hook| -%>
32
+ g_parser.hooks[:<%= hook %>].push [@slaves.last, :do_parse]
37
33
  <%- end -%>
38
- g_parser.hooks[:<%= i %>].push [@slaves[<%= h[m] %>], :do_parse]
39
- <%- x += 1; -%>
40
34
  <%- end -%>
41
35
  end
42
36
 
43
37
  <%= @inner_code %>
44
38
  end
45
39
 
46
- <%= @outer_code %>
40
+ <%= @outer_code %>
@@ -6,68 +6,82 @@
6
6
  module <%= target_namespace %> #:nodoc:all
7
7
  class <%= target_name %> < Depager::LALR::Basis #:nodoc:all
8
8
  include Depager::Utils::ExtensionSlaveMethods
9
- <% @slave_mixins.uniq.each do |i| %>
10
- include <%=i%><%end%>
9
+ <%- @slave_mixins.uniq.each do |i| -%>
10
+ include <%=i%>
11
+ <%- end -%>
11
12
 
12
13
  ### Reduce Table
13
- reduce_table = [
14
+ REDUCE_TABLE = [
14
15
  <%- g_parser.table.grammar.rulelist.each do |i| -%>
15
16
  [ <%= i.lhs - 1%>, <%= i.rhs.size %> ],
16
17
  <%- end -%>
17
18
  ]
19
+ def reduce_table; REDUCE_TABLE; end
20
+
18
21
  ### Term to Int
19
22
  <%- values = (g.nonterms.size ... g.syms.size).map{|i| [ g.syms[i].inspect, i - g.nonterms.size] } -%>
20
- term_to_int = {
23
+ TERM_TO_INT = {
21
24
  <%- values.each do |k, v| -%>
22
25
  <%= k %> => <%= v %>,
23
26
  <%- end -%>
24
27
  }
28
+ def term_to_int; TERM_TO_INT; end
29
+
25
30
  ### Int to Term
26
- int_to_term = [
31
+ INT_TO_TERM = [
27
32
  <%- values.each do |k, v| -%>
28
33
  <%= k %>,
29
34
  <%- end -%>
30
35
  ]
36
+ def int_to_term; INT_TO_TERM; end
37
+
31
38
  ### Action Table
32
- action_table = [
39
+ ACTION_TABLE = [
33
40
  <%- g_parser.table.action_table.each do |i| -%>
34
41
  [ <% i.each do |j| %><%= j ? "#{j}" : 'nil' %>, <% end %>],
35
42
  <%- end -%>
36
43
  ]
44
+ def action_table; ACTION_TABLE; end
45
+
37
46
  ### Default Reduce Table
38
- defred_table = [
47
+ DEFRED_TABLE = [
39
48
  <%- g_parser.table.defred_table.each do |i| -%>
40
49
  <%= i ? "#{i}" : 'nil' %>,
41
50
  <%- end -%>
42
51
  ]
43
- defred_after_shift_table = [
52
+ def defred_table; DEFRED_TABLE; end
53
+
54
+ DEFRED_AFTER_SHIFT_TABLE = [
44
55
  <%- g_parser.table.defred_after_shift_table.each do |i| -%>
45
56
  <%= i ? "#{i}" : 'nil' %>,
46
57
  <%- end -%>
47
58
  ]
59
+ def defred_after_shift_table; DEFRED_AFTER_SHIFT_TABLE; end
60
+
48
61
  ### Nonterm to Int
49
62
  <%- values = (1 ... g.nonterms.size).map{|i| [ ":#{g.syms[i]}", i-1] } -%>
50
- nonterm_to_int = {
63
+ NONTERM_TO_INT = {
51
64
  <%- values.each do |k, v| -%>
52
- <%= k %> => <%= v %>,
65
+ <%= k %> => <%= v %>,
53
66
  <%- end -%>
54
67
  }
68
+ def nonterm_to_int; NONTERM_TO_INT; end
69
+
55
70
  ### Int to Nonterm
56
- int_to_nonterm = [
71
+ INT_TO_NONTERM = [
57
72
  <%- values.each do |k, v| -%>
58
- <%= k %>,
73
+ <%= k %>,
59
74
  <%- end -%>
60
75
  ]
76
+ def int_to_nonterm; INT_TO_NONTERM; end
77
+
61
78
  ### Goto Table
62
- goto_table = [
79
+ GOTO_TABLE = [
63
80
  <%- g_parser.table.goto_table.each do |i| -%>
64
81
  [ <% i.each do |j| %><%= j ? "#{j}" : 'nil' %>, <% end %>],
65
82
  <%- end -%>
66
83
  ]
67
-
68
- Tables = [ reduce_table, action_table,
69
- defred_table, defred_after_shift_table, goto_table,
70
- term_to_int, int_to_term, nonterm_to_int, int_to_nonterm ]
84
+ def goto_table; GOTO_TABLE; end
71
85
 
72
86
  def initialize g_parser, master
73
87
  super()
@@ -1,23 +1,19 @@
1
- # -*- coding: utf-8 -*-
2
1
  <%- g = g_parser.table.grammar -%>
3
- begin
4
- require 'rubygems'
5
- rescue LoadError
6
- end
7
-
8
- <%- if Depager.configuration[:embed_runtime] -%>
9
- <%= File.read File.expand_path("#{Depager::TEMPLATES_DIR}/../parser.rb") %>
10
- <%- else -%>
2
+ require 'rubygems'
3
+ <%= @setup_code %>
11
4
  require 'depager/parser.rb'
12
- <%- end -%>
13
5
 
14
6
  <%- @requirements.uniq.each do |i| -%>
7
+ <%- if i.match?(/\A'\.\.?\//) -%>
8
+ require_relative <%=i%>
9
+ <%- else -%>
15
10
  require <%=i%>
16
11
  <%- end -%>
12
+ <%- end -%>
17
13
 
18
14
  <%- r = ''; target_namespace.split('::')[0..-2].each do |i| -%>
19
15
  module <%= "#{r}#{i}" %> ; end
20
- <%- r << "#{i}::"-%>
16
+ <%- r << "#{i}::"-%>
21
17
  <%- end-%>
22
18
 
23
19
  module <%= target_namespace %>
@@ -32,70 +28,87 @@ module <%= target_namespace %>
32
28
  <%- end -%>
33
29
 
34
30
  ### Reduce Table
35
- reduce_table = [
31
+ REDUCE_TABLE = [
36
32
  <%- g_parser.table.grammar.rulelist.each do |i| -%>
37
33
  [ <%= '%04s' % (i.lhs - 1)%>, <%= "%04s" % i.rhs.size %> ], # <%= i %>
38
34
  <%- end -%>
39
35
  ]
36
+ def reduce_table; REDUCE_TABLE; end
37
+
40
38
  ### Term to Int
41
39
  <%- values = (g.nonterms.size ... g.syms.size).map{|i| [ g.syms[i].inspect, i - g.nonterms.size] } -%>
42
- term_to_int = {
40
+ TERM_TO_INT = {
43
41
  <%- values.each do |k, v| -%>
44
42
  <%= k %> => <%= v %>,
45
43
  <%- end -%>
46
44
  }
45
+ def term_to_int; TERM_TO_INT; end
46
+
47
47
  ### Int to Term
48
- int_to_term = [
48
+ INT_TO_TERM = [
49
49
  <%- values.each do |k, v| -%>
50
50
  <%= k %>,
51
51
  <%- end -%>
52
52
  ]
53
+ def int_to_term; INT_TO_TERM; end
54
+
53
55
  ### Action Table
54
- action_table = [
56
+ ACTION_TABLE = [
55
57
  <%- g_parser.table.action_table.each do |i| -%>
56
58
  [ <% i.each do |j| %><%= j ? "#{j}" : 'nil' %>, <% end %>],
57
59
  <%- end -%>
58
60
  ]
61
+ def action_table; ACTION_TABLE; end
62
+
59
63
  ### Default Reduce Table
60
- defred_table = [
64
+ DEFRED_TABLE = [
61
65
  <%- g_parser.table.defred_table.each do |i| -%>
62
66
  <%= i ? "#{i}" : 'nil' %>,
63
67
  <%- end -%>
64
68
  ]
65
- defred_after_shift_table = [
69
+ def defred_table; DEFRED_TABLE; end
70
+
71
+ DEFRED_AFTER_SHIFT_TABLE = [
66
72
  <%- g_parser.table.defred_after_shift_table.each do |i| -%>
67
73
  <%= i ? "#{i}" : 'nil' %>,
68
74
  <%- end -%>
69
75
  ]
76
+ def defred_after_shift_table; DEFRED_AFTER_SHIFT_TABLE; end
77
+
70
78
  ### Nonterm to Int
71
79
  <%- values = (1 ... g.nonterms.size).map{|i| [ ":#{g.syms[i]}", i-1] } -%>
72
- nonterm_to_int = {
80
+ NONTERM_TO_INT = {
73
81
  <%- values.each do |k, v| -%>
74
82
  <%= k %> => <%= v %>,
75
83
  <%- end -%>
76
84
  }
85
+ def nonterm_to_int; NONTERM_TO_INT; end
86
+
77
87
  ### Int to Nonterm
78
- int_to_nonterm = [
88
+ INT_TO_NONTERM = [
79
89
  <%- values.each do |k, v| -%>
80
90
  <%= k %>,
81
91
  <%- end -%>
82
92
  ]
93
+ def int_to_nonterm; INT_TO_NONTERM; end
94
+
83
95
  ### Goto Table
84
- goto_table = [
96
+ GOTO_TABLE = [
85
97
  <%- g_parser.table.goto_table.each do |i| -%>
86
98
  [ <% i.each do |j| %><%= j ? "#{j}" : 'nil' %>, <% end %>],
87
99
  <%- end -%>
88
100
  ]
101
+ def goto_table; GOTO_TABLE; end
89
102
 
90
103
  <%- if Depager.debug_mode? -%>
91
104
  alias orig_error error
92
105
  def error
93
106
  orig_error
94
- warn "current state: #{StateInfo[@stack.last]}"
107
+ warn "current state: #{STATE_INFO[@stack.last]}"
95
108
  end
96
109
 
97
110
  ### States
98
- StateInfo = [
111
+ STATE_INFO = [
99
112
  <%- g_parser.table.state_info.each do |s| -%>
100
113
  <<'----------',
101
114
  <%= s.strip %>
@@ -104,10 +117,6 @@ module <%= target_namespace %>
104
117
  ]
105
118
  <%- end -%>
106
119
 
107
- Tables = [ reduce_table, action_table,
108
- defred_table, defred_after_shift_table, goto_table,
109
- term_to_int, int_to_term, nonterm_to_int, int_to_nonterm ]
110
-
111
120
  <%= g_parser.inner_code + @inner_code %>
112
121
  end
113
122
  end
data/lib/depager/utils.rb CHANGED
@@ -1,11 +1,16 @@
1
- # -*- coding: utf-8 -*-
2
1
  module Depager
2
+ class ErrorExit < ::RuntimeError; end
3
+
3
4
  module Utils
4
5
  module CommonMethods
5
6
  def file
6
7
  d_parser.file
7
8
  end
8
9
 
10
+ def input_path
11
+ Depager.path_of(file)
12
+ end
13
+
9
14
  def target_name
10
15
  g_parser.target_name
11
16
  end
@@ -18,89 +23,91 @@ module Depager
18
23
  "#{target_namespace}::#{target_name}"
19
24
  end
20
25
 
21
- def error_message msg, lineno = nil
22
- "#{file.path}:#{lineno || file.lineno}: #{msg}"
26
+ def error_message(msg, lineno = nil)
27
+ "#{input_path}:#{lineno || file.lineno}: #{msg}"
23
28
  end
24
29
 
25
- def error_exit msg, lineno = nil
30
+ def error_exit(msg, lineno = nil)
26
31
  warn error_message(msg, lineno)
27
- exit 1
32
+ raise Depager::ErrorExit
28
33
  end
29
34
 
30
- def warning msg, lineno = nil
35
+ def warning(msg, lineno = nil)
31
36
  warn error_message(" warning: #{msg}", lineno)
32
37
  end
38
+
39
+ def expanded_code_delimiter
40
+ Depager.configuration[:expanded_code_delimiter] || "DEPAGER_EXPANDED_CODE"
41
+ end
42
+
43
+ def inspect
44
+ "#<#{self.class.name} object_id=#{object_id}>"
45
+ end
33
46
  end
34
47
 
35
48
  module CodeGeneratorMethods
36
- def generate_decorator_code name, super_class_name, code
37
- if d_parser.generator.is_a?(Depager::ExtensionGenerator)
38
- mixin_code = 'include Depager::Utils::ExtensionSlaveDecoratorMethods'
39
- else
40
- mixin_code = ''
41
- end
42
-
43
- %{
44
- \ class #{target_namespace}::#{name} < #{super_class_name} #:nodoc:all
49
+ def generate_decorator_code(name, super_class_name, code)
50
+ mixin_code = if d_parser.generator.is_a?(Depager::ExtensionGenerator)
51
+ "include Depager::Utils::ExtensionSlaveDecoratorMethods"
52
+ else
53
+ ""
54
+ end
55
+ <<~CODE
56
+ class #{target_namespace}::#{name} < #{super_class_name} #:nodoc:all
45
57
  #{mixin_code}
46
- %s
58
+ #{code}
47
59
  end
48
- }.unindent % code; #code
60
+ CODE
49
61
  end
50
62
 
51
- def generate_action_decorator_code on_reduce, code
52
- on_reduce_code = on_reduce.map{|i| " #{i || 'nil'},\n"}
63
+ def generate_action_decorator_code(on_reduce, code)
64
+ on_reduce_code = on_reduce.map { |i| " #{i || 'nil'},\n" }
53
65
 
54
- generate_decorator_code(decorator_name, "#{d_parser.parsing_method}::Action", %{
55
- on_reduce = [
56
- #{ on_reduce_code.join }
57
- ]
58
- Tables = [ on_reduce ]
66
+ generate_decorator_code(decorator_name, "#{d_parser.parsing_method}::Action", <<~CODE)
67
+ ON_REDUCE = [
68
+ #{on_reduce_code.join}
69
+ ]
70
+ def on_reduce; ON_REDUCE; end
59
71
 
60
- %s
61
- } % code)
72
+ #{code}
73
+ CODE
62
74
  end
63
75
 
64
- def expand_inline_code body, lineno, options = {}
76
+ def expand_inline_code(body, lineno, options = {})
65
77
  delimiter = expanded_code_delimiter
78
+ delta = options[:delta] || 0
66
79
 
67
- if wrap = options[:wrap]
80
+ if (wrap = options[:wrap])
68
81
  wrap = [wrap, "end"] if wrap.is_a?(String)
69
82
  body = "#{wrap[0]}\n#{body}\n#{wrap[1]}"
83
+ delta += wrap[0].lines.size
70
84
  end
71
85
 
72
- delta = options[:delta] || 0
73
-
74
- %{
75
- \ module_eval <<\-'#{delimiter}', '#{file.path}', #{lineno - delta - 1}
76
- %s
86
+ <<~CODE
87
+ module_eval <<-'#{delimiter}', '#{input_path}', #{lineno - delta}
88
+ #{body}
77
89
  #{delimiter}
78
- }.unindent % body
79
- end
80
-
81
- def expanded_code_delimiter
82
- Depager.configuration[:expanded_code_delimiter] || ".,.,#{Time.now.to_i}#{rand(0xffff)}.,.,"
90
+ CODE
83
91
  end
84
92
 
85
93
  def parse_block
86
- result = ''
94
+ result = ""
87
95
  start_lineno = file.lineno
88
96
  if @line =~ /\A\s*\{(.*)\}\s*(#.*)?\Z/
89
97
  result = " #{$1}\n"
90
98
  else
91
- result.concat @line.sub(/\A\s*\{/, '')
99
+ result.concat @line.sub(/\A\s*\{/, "")
92
100
  ind = @original_line.match(/\A([ \t]*)/)[1]
93
101
  until file.eof?
94
102
  line = file.gets
95
103
  break if line =~ /\A#{ind}\}\s*(#.*)?\Z/
104
+
96
105
  result.concat line
97
106
  end
98
- if file.eof?
99
- error_exit 'syntax error(parse_block).', start_lineno
100
- end
107
+ error_exit "syntax error(parse_block).", start_lineno if file.eof?
101
108
  end
102
109
  @line = $'
103
- return result
110
+ result
104
111
  end
105
112
  end
106
113
 
@@ -110,8 +117,7 @@ module Depager
110
117
 
111
118
  attr_reader :d_parser, :g_parser, :master
112
119
 
113
- def do_default
114
- end
120
+ def do_default; end
115
121
 
116
122
  def do_parse?
117
123
  true
@@ -127,6 +133,10 @@ module Depager
127
133
  do_default
128
134
  end
129
135
  end
136
+
137
+ def abort_driver
138
+ raise ErrorExit
139
+ end
130
140
  end
131
141
 
132
142
  module ExtensionSlaveDecoratorMethods
@@ -1,4 +1,3 @@
1
- # -*- coding: utf-8 -*-
2
1
  module Depager
3
- VERSION = '0.3.0'
2
+ VERSION = "0.3.0.b20250423".freeze
4
3
  end