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
@@ -1,8 +1,8 @@
1
1
  %class Pl0d::Parser
2
- %extend Depager::Lexer ('plugins/lex.rb')
3
- %extend Depager::Action ('plugins/action.rb')
2
+ %extend Depager::Lexer ('depager/plugins/lex.rb')
3
+ %extend Depager::Action ('depager/plugins/action.rb')
4
4
  %decorate @Action
5
- #%decorate Depager::LALR::ShiftReducePrinter ('plugins/srp.rb')
5
+ #%decorate Depager::LALR::ShiftReducePrinter ('depager/plugins/srp.rb')
6
6
  %inner {
7
7
  KEYWORDS = {
8
8
  'const' => :CONST,
@@ -418,4 +418,4 @@ end
418
418
 
419
419
  require 'pp'
420
420
  parser = Pl0d.create_decorated_parser
421
- r, = parser.parse(STDIN)
421
+ r, = parser.parse(ARGF)
@@ -16,8 +16,7 @@ end;
16
16
  function fact(x)
17
17
  begin
18
18
  write x; writeln;
19
-
20
- if x = 1 then return 1;
19
+ if x = 0 then return 1;
21
20
  return x * fact(x - 1)
22
21
  end;
23
22
 
@@ -36,7 +35,7 @@ end;
36
35
  begin
37
36
  x := m; y := n;
38
37
  write x; write y; writeln;
39
- x := 84; y := 36;
38
+ x := 84; y := 36;
40
39
  write x; write y; writeln;
41
40
 
42
41
  y := puts_ntimes(5, 10);
data/examples/c89/c89.dr CHANGED
@@ -1,8 +1,8 @@
1
1
  %class CParser::Parser
2
2
  # from http://www.lysator.liu.se/c/ANSI-C-grammar-y.html and
3
3
  # http://www.lysator.liu.se/c/ANSI-C-grammar-l.html
4
- %extend Depager::Lexer ('plugins/lex.rb')
5
- #%decorate Depager::LALR::ShiftReducePrinter ('plugins/srp.rb')
4
+ %extend Depager::Lexer ('depager/plugins/lex.rb')
5
+ #%decorate Depager::LALR::ShiftReducePrinter ('depager/plugins/srp.rb')
6
6
  %inner{
7
7
  KEYWORDS = {
8
8
  "auto" => :AUTO,
@@ -489,5 +489,5 @@ jump_statement
489
489
  ;
490
490
  #end-rule
491
491
  %%
492
- p = CParser.create_decorated_parser
493
- p.parse(STDIN)
492
+ parser = CParser.create_decorated_parser
493
+ parser.parse(ARGF)
@@ -3,7 +3,7 @@
3
3
  */
4
4
  int fact(int n)
5
5
  {
6
- if (n == 1)
6
+ if (n == 0)
7
7
  return 1;
8
8
  else
9
9
  return n * fact(n - 1);
@@ -1,10 +1,9 @@
1
- # -*- coding: utf-8 -*-
2
- require 'plugins/ast.rb'
1
+ require "plugins/ast"
3
2
 
4
- module Examples ; end
3
+ module Examples; end
5
4
 
6
- class Examples::ASTBuilderDepthFirstExtension < Depager::ASTBuilderExtension #:nodoc:all
7
- def gen_accept_code sym
5
+ class Examples::ASTBuilderDepthFirstExtension < Depager::ASTBuilderExtension # :nodoc:all
6
+ def gen_accept_code(sym)
8
7
  " @#{sym}.accept(v)\n"
9
8
  end
10
9
  end
@@ -1,8 +1,8 @@
1
1
  %defext Examples::ATreeBuilderExtension
2
- %extend Depager::Action ('plugins/action.rb')
3
- %extend Depager::Lexer ('plugins/lex.rb')
2
+ %extend Depager::Action ('depager/plugins/action.rb')
3
+ %extend Depager::Lexer ('depager/plugins/lex.rb')
4
4
  %decorate @Action
5
- #%decorate Depager::LALR::ShiftReducePrinter ('plugins/srp.rb')
5
+ #%decorate Depager::LALR::ShiftReducePrinter ('depager/plugins/srp.rb')
6
6
  %expanded_code_delimiter DEPAGER_EXPANDED_CODE
7
7
  %inner{
8
8
  attr_accessor :action_code, :on_reduce
@@ -1,8 +1,8 @@
1
1
  %class TinyCalc::Parser
2
- %extend Depager::Lexer ('plugins/lex.rb')
3
- %extend Examples::ATreeBuilder ('./atree.rb')
2
+ %extend Depager::Lexer ('depager/plugins/lex.rb')
3
+ %extend Examples::ATreeBuilder ('./atree.dr.rb')
4
4
  %decorate @ATreeBuilder
5
- #%decorate Depager::LALR::ShiftReducePrinter ('plugins/srp.rb')
5
+ #%decorate Depager::LALR::ShiftReducePrinter ('depager/plugins/srp.rb')
6
6
  %%
7
7
 
8
8
  %LEX{
@@ -38,5 +38,5 @@
38
38
  %%
39
39
  require 'pp'
40
40
  parser = TinyCalc.create_decorated_parser()
41
- t, = parser.parse(STDIN)
41
+ t, = parser.parse(ARGF)
42
42
  pp t
@@ -1,8 +1,8 @@
1
1
  %class TinyCalc::Parser
2
- %extend Depager::Lexer ('plugins/lex.rb')
2
+ %extend Depager::Lexer ('depager/plugins/lex.rb')
3
3
  %extend Examples::SimpleAction ('./simple_action.rb')
4
4
  %decorate @SimpleAction
5
- #%decorate Depager::LALR::ShiftReducePrinter ('plugins/srp.rb')
5
+ #%decorate Depager::LALR::ShiftReducePrinter ('depager/plugins/srp.rb')
6
6
  %%
7
7
 
8
8
  %LEX{
@@ -29,5 +29,5 @@
29
29
  #end-rule
30
30
  %%
31
31
  parser = TinyCalc.create_decorated_parser
32
- r, = parser.parse(STDIN)
32
+ r, = parser.parse(ARGF)
33
33
  puts r
@@ -1,8 +1,8 @@
1
1
  %defext Examples::PseudoActionExtension
2
- %extend Depager::Lexer ('plugins/lex.rb')
3
- %extend Depager::Action ('plugins/action.rb')
2
+ %extend Depager::Lexer ('depager/plugins/lex.rb')
3
+ %extend Depager::Action ('depager/plugins/action.rb')
4
4
  %decorate @Action
5
- #%decorate Depager::LALR::ShiftReducePrinter ('plugins/srp.rb')
5
+ #%decorate Depager::LALR::ShiftReducePrinter ('depager/plugins/srp.rb')
6
6
  %hook post_rhs
7
7
  %%
8
8
  %LEX{
@@ -1,5 +1,5 @@
1
1
  %class PseudoActionTest::Parser
2
- %extend Examples::PseudoAction ('./paction.rb')
2
+ %extend Examples::PseudoAction ('./paction.dr.rb')
3
3
  %%
4
4
  #begin-rule
5
5
  expr:
@@ -10,5 +10,5 @@
10
10
  ;
11
11
  #end-rule
12
12
  %%
13
- p = PseudoActionTest.new()
14
- #p.parse(STDIN)
13
+ parser = PseudoActionTest.new()
14
+ pareser.parse(ARGF)
@@ -1,12 +1,11 @@
1
- # -*- coding: utf-8 -*-
2
- require 'depager/parser.rb'
1
+ require "depager/parser"
3
2
 
4
- module Examples ; end
3
+ module Examples; end
5
4
 
6
- class Examples::SimpleActionExtension < Depager::Extension #:nodoc:all
5
+ class Examples::SimpleActionExtension < Depager::Extension # :nodoc:all
7
6
  def init_extension
8
- @line0 = @line = @oldline = ''
9
- @action_code = ''
7
+ @line0 = @line = @oldline = ""
8
+ @action_code = ""
10
9
  @on_reduce = []
11
10
  end
12
11
 
@@ -15,30 +14,33 @@ class Examples::SimpleActionExtension < Depager::Extension #:nodoc:all
15
14
  generate_action_decorator_code(@on_reduce, @action_code)
16
15
  end
17
16
 
18
- def modify_action_code code
19
- return code, 0
17
+ def modify_action_code(code)
18
+ [code, 0]
20
19
  end
21
20
 
22
21
  def pre_rule_list
23
- if g_parser.original_line =~ /^%ACTION\{\s*$/
24
- while line = file.gets
25
- break if line =~ /^%\}\s*$/
26
- @action_code << line
27
- end
28
- g_parser.update_context ''
22
+ return unless /^%ACTION\{\s*$/.match?(g_parser.original_line)
23
+
24
+ while (line = file.gets)
25
+ break if /^%\}\s*$/.match?(line)
26
+
27
+ @action_code << line
29
28
  end
29
+ g_parser.update_context ""
30
30
  end
31
31
 
32
32
  def post_rhs
33
- if g_parser.line =~ /\s*\{/
34
- @original_line, @line = g_parser.original_line, g_parser.line
35
- lineno = g_parser.file.lineno
36
- code, d = parse_block, 1
37
- n = g_parser.rules.size - 1
38
- @action_code <<
39
- expand_inline_code(code, lineno, :wrap => "def _act_#{n} val", :delta => d)
40
- g_parser.update_context @line
41
- @on_reduce[n] = ":_act_#{n}"
42
- end
33
+ return unless /\s*\{/.match?(g_parser.line)
34
+
35
+ @original_line = g_parser.original_line
36
+ @line = g_parser.line
37
+ lineno = g_parser.file.lineno
38
+ code = parse_block
39
+ d = 1
40
+ n = g_parser.rules.size - 1
41
+ @action_code <<
42
+ expand_inline_code(code, lineno, wrap: "def _act_#{n} val", delta: d)
43
+ g_parser.update_context @line
44
+ @on_reduce[n] = ":_act_#{n}"
43
45
  end
44
46
  end
@@ -1,8 +1,8 @@
1
1
  %class PL0d::Parser
2
- %extend Depager::Lexer ('plugins/lex.rb')
3
- %extend Depager::ASTBuilder ('plugins/ast.rb')
2
+ %extend Depager::Lexer ('depager/plugins/lex.rb')
3
+ %extend Depager::ASTBuilder ('depager/plugins/ast.rb')
4
4
  %decorate @ASTBuilder
5
- #%decorate Depager::LALR::ShiftReducePrinter ('plugins/srp.rb')
5
+ #%decorate Depager::LALR::ShiftReducePrinter ('depager/plugins/srp.rb')
6
6
  %inner{
7
7
  KEYWORDS = {
8
8
  'const' => :CONST,
@@ -330,8 +330,8 @@
330
330
  #end-rule
331
331
  %%
332
332
  require 'pp'
333
- psr = PL0d.create_decorated_parser
334
- t, = psr.parse(STDIN)
333
+ parser = PL0d.create_decorated_parser
334
+ t, = parser.parse(ARGF)
335
335
  # pp t
336
336
  v = PL0d::Visitor.new
337
337
  t.accept(v)
@@ -16,14 +16,14 @@ end;
16
16
  function fact(x)
17
17
  begin
18
18
  write x; writeln;
19
- if x = 1 then return 1;
19
+ if x = 0 then return 1;
20
20
  return x * fact(x - 1);
21
21
  end;
22
22
 
23
23
  begin
24
24
  x := m; y := n;
25
25
  write x; write y; writeln;
26
- x := 84; y := 36;
26
+ x := 84; y := 36;
27
27
  write x; write y; writeln;
28
28
 
29
29
  y := puts10(5);
@@ -1,7 +1,7 @@
1
1
  %class TinyCalc::Parser
2
- %extend Depager::Lexer ('plugins/lex.rb')
3
- %extend Depager::Rie ('plugins/rie.rb')
4
- #%decorate Depager::LALR::ShiftReducePrinter ('plugins/srp.rb')
2
+ %extend Depager::Lexer ('depager/plugins/lex.rb')
3
+ %extend Depager::Rie ('depager/plugins/rie.rb')
4
+ #%decorate Depager::LALR::ShiftReducePrinter ('depager/plugins/srp.rb')
5
5
  %decorate @Rie
6
6
  %%
7
7
 
@@ -54,4 +54,4 @@
54
54
  #end-rule
55
55
  %%
56
56
  parser = TinyCalc.create_decorated_parser
57
- r, = parser.parse(STDIN)
57
+ r, = parser.parse(ARGF)
@@ -1,7 +1,7 @@
1
1
  %class DcUse::Parser
2
- %extend Depager::Lexer ('plugins/lex.rb')
3
- %extend Depager::Rie ('plugins/rie.rb')
4
- #%decorate Depager::LALR::ShiftReducePrinter ('plugins/srp.rb')
2
+ %extend Depager::Lexer ('depager/plugins/lex.rb')
3
+ %extend Depager::Rie ('depager/plugins/rie.rb')
4
+ #%decorate Depager::LALR::ShiftReducePrinter ('depager/plugins/srp.rb')
5
5
  %decorate @Rie
6
6
  %%
7
7
 
@@ -68,4 +68,4 @@ def message *args
68
68
  printf(*args)
69
69
  end
70
70
  parser = DcUse.create_decorated_parser
71
- r, = parser.parse(STDIN)
71
+ r, = parser.parse(ARGF)
@@ -1,7 +1,7 @@
1
1
  %class Pl0::Parser
2
- %extend Depager::Lexer ('plugins/lex.rb')
3
- %extend Depager::Rie ('plugins/rie.rb')
4
- #%decorate Depager::LALR::ShiftReducePrinter ('plugins/srp.rb')
2
+ %extend Depager::Lexer ('depager/plugins/lex.rb')
3
+ %extend Depager::Rie ('depager/plugins/rie.rb')
4
+ #%decorate Depager::LALR::ShiftReducePrinter ('depager/plugins/srp.rb')
5
5
  %decorate @Rie
6
6
  %inner{
7
7
  KEYWORDS = {
@@ -1,9 +1,9 @@
1
1
  %class StatefulLexerTest1::Parser
2
- %extend Depager::StatefulLexer ('plugins/slex.rb')
3
- %extend Depager::Action ('plugins/action.rb')
2
+ %extend Depager::StatefulLexer ('depager/plugins/slex.rb')
3
+ %extend Depager::Action ('depager/plugins/action.rb')
4
4
  %decorate @Action
5
5
  %decorate @StatefulLexer
6
- #%decorate Depager::LALR::ShiftReducePrinter ('plugins/srp.rb')
6
+ #%decorate Depager::LALR::ShiftReducePrinter ('depager/plugins/srp.rb')
7
7
  %%
8
8
  %LEX{
9
9
  <START>
@@ -25,5 +25,5 @@
25
25
  ;
26
26
  #end-rule
27
27
  %%
28
- p = StatefulLexerTest1.create_decorated_parser
29
- p.parse(STDIN)
28
+ parser = StatefulLexerTest1.create_decorated_parser
29
+ parser.parse(ARGF)
@@ -1,9 +1,9 @@
1
1
  %class StatefulLexerTest2::Parser
2
- %extend Depager::StatefulLexer ('plugins/slex.rb')
3
- %extend Depager::Action ('plugins/action.rb')
2
+ %extend Depager::StatefulLexer ('depager/plugins/slex.rb')
3
+ %extend Depager::Action ('depager/plugins/action.rb')
4
4
  %decorate @Action
5
5
  %decorate @StatefulLexer
6
- #%decorate Depager::LALR::ShiftReducePrinter ('plugins/srp.rb')
6
+ #%decorate Depager::LALR::ShiftReducePrinter ('depager/plugins/srp.rb')
7
7
  %%
8
8
  %LEX{
9
9
  <START>
@@ -32,5 +32,5 @@
32
32
  ;
33
33
  #end-rule
34
34
  %%
35
- p = StatefulLexerTest2.create_decorated_parser
36
- p.parse(STDIN)
35
+ parser = StatefulLexerTest2.create_decorated_parser
36
+ parser.parse(ARGF)
@@ -1,8 +1,8 @@
1
1
  %class TinyCalc::Parser
2
- %extend Depager::Lexer ('plugins/lex.rb')
3
- %extend Depager::Action ('plugins/action.rb')
2
+ %extend Depager::Lexer ('depager/plugins/lex.rb')
3
+ %extend Depager::Action ('depager/plugins/action.rb')
4
4
  %decorate @Action
5
- #%decorate Depager::LALR::ShiftReducePrinter ('plugins/srp.rb')
5
+ #%decorate Depager::LALR::ShiftReducePrinter ('depager/plugins/srp.rb')
6
6
  %%
7
7
 
8
8
  %LEX{
@@ -29,5 +29,5 @@
29
29
  #end-rule
30
30
  %%
31
31
  parser = TinyCalc.create_decorated_parser
32
- r, = parser.parse(STDIN)
32
+ r, = parser.parse(ARGF)
33
33
  puts r
@@ -1,10 +1,10 @@
1
1
  %class TinyCalc::Parser
2
- %extend Depager::Lexer ('plugins/lex.rb')
3
- %extend Depager::ASTBuilder ('plugins/ast.rb')
4
- %extend Depager::Action ('plugins/action.rb')
2
+ %extend Depager::Lexer ('depager/plugins/lex.rb')
3
+ %extend Depager::ASTBuilder ('depager/plugins/ast.rb')
4
+ %extend Depager::Action ('depager/plugins/action.rb')
5
5
  %decorate @ASTBuilder
6
6
  %decorate @Action
7
- #%decorate Depager::LALR::ShiftReducePrinter ('plugins/srp.rb')
7
+ #%decorate Depager::LALR::ShiftReducePrinter ('depager/plugins/srp.rb')
8
8
  %%
9
9
 
10
10
  %LEX{
@@ -56,10 +56,21 @@
56
56
  ;
57
57
  #end-rule
58
58
  %%
59
- require 'pp'
59
+
60
+ module TinyCalc
61
+ class Node
62
+ def to_s
63
+ "#{self.class.name}:\n" + attributes.flat_map do |k, v|
64
+ lines = v.to_s.lines.map(&:rstrip)
65
+ " #{k} = #{lines.shift}\n" + lines.map { |j| " #{j}\n" }.join
66
+ end.join
67
+ end
68
+ end
69
+ end
70
+
60
71
  parser = TinyCalc.create_decorated_parser()
61
- t, r = parser.parse(STDIN)
72
+ r, a = parser.parse(ARGF)
62
73
  v = TinyCalc::Visitor.new
63
- pp r
64
- pp t.accept(v).value
65
- pp t
74
+ puts r
75
+ puts r.accept(v).value
76
+ puts a
@@ -1,8 +1,8 @@
1
1
  %class TinyCalc::Parser
2
- %extend Depager::Lexer ('plugins/lex.rb')
3
- %extend Depager::ASTBuilder ('plugins/ast.rb')
2
+ %extend Depager::Lexer ('depager/plugins/lex.rb')
3
+ %extend Depager::ASTBuilder ('depager/plugins/ast.rb')
4
4
  %decorate @ASTBuilder
5
- #%decorate Depager::LALR::ShiftReducePrinter ('plugins/srp.rb')
5
+ #%decorate Depager::LALR::ShiftReducePrinter ('depager/plugins/srp.rb')
6
6
  %%
7
7
 
8
8
  %LEX{
@@ -46,10 +46,22 @@
46
46
  ;
47
47
  #end-rule
48
48
  %%
49
- require 'pp'
49
+
50
+ module TinyCalc
51
+ class Node
52
+ def to_s
53
+ "#{self.class.name}:\n" + attributes.flat_map do |k, v|
54
+ lines = v.to_s.lines.map(&:rstrip)
55
+ " #{k} = #{lines.shift}\n" + lines.map { |j| " #{j}\n" }.join
56
+ end.join
57
+ end
58
+ end
59
+ end
60
+
50
61
  parser = TinyCalc.create_decorated_parser()
51
- t, = parser.parse(STDIN)
62
+ r, = parser.parse(ARGF)
52
63
  v = TinyCalc::Visitor.new
53
- pp t.accept(v).value
54
- pp t
64
+ puts r
65
+ puts r.accept(v).value
66
+
55
67
 
@@ -1,6 +1,6 @@
1
1
  %class TinyCalc::Parser
2
- %extend Depager::Lexer ('plugins/lex.rb')
3
- %extend Depager::CSTBuilder ('plugins/cst.rb')
2
+ %extend Depager::Lexer ('depager/plugins/lex.rb')
3
+ %extend Depager::CSTBuilder ('depager/plugins/cst.rb')
4
4
  %decorate @CSTBuilder
5
5
  %%
6
6
 
@@ -16,6 +16,12 @@
16
16
  def initialize
17
17
  @value = nil
18
18
  end
19
+
20
+ def to_s
21
+ "#{self.class.name}:\n" + children.flat_map do |i|
22
+ i.to_s.lines.map { |j| " #{j.rstrip}\n" }
23
+ end.join
24
+ end
19
25
  }
20
26
  %}
21
27
 
@@ -36,10 +42,9 @@
36
42
  ;
37
43
  #end-rule
38
44
  %%
39
- require 'pp'
45
+
40
46
  parser = TinyCalc.create_decorated_parser
41
- r, = parser.parse(STDIN)
47
+ r, = parser.parse(ARGF)
42
48
  v = TinyCalc::Visitor.new
43
- r.accept(v)
44
- pp r
45
- puts r.value
49
+ puts r
50
+ puts r.accept(v)
@@ -40,4 +40,4 @@
40
40
  #end-rule
41
41
  %%
42
42
  parser = TinyCalc.create_decorated_parser()
43
- parser.parse(STDIN)
43
+ parser.parse(ARGF)
@@ -1,5 +1,5 @@
1
1
  %class TinyCalc::Parser
2
- %extend Depager::Lexer ('plugins/lex.rb')
2
+ %extend Depager::Lexer ('depager/plugins/lex.rb')
3
3
  %%
4
4
 
5
5
  %LEX{
@@ -26,4 +26,4 @@
26
26
  #end-rule
27
27
  %%
28
28
  parser = TinyCalc.create_decorated_parser()
29
- parser.parse(STDIN)
29
+ parser.parse(ARGF)
@@ -1,8 +1,8 @@
1
1
  %class TinyCalc::Parser
2
- %extend Depager::Lexer ('plugins/lex.rb')
3
- %extend Depager::Action ('plugins/action.rb')
2
+ %extend Depager::Lexer ('depager/plugins/lex.rb')
3
+ %extend Depager::Action ('depager/plugins/action.rb')
4
4
  %decorate @Action
5
- #%decorate Depager::LALR::ShiftReducePrinter ('plugins/srp.rb')
5
+ #%decorate Depager::LALR::ShiftReducePrinter ('depager/plugins/srp.rb')
6
6
  %prec{
7
7
  left '*' '/'
8
8
  left '+' '-'
@@ -27,5 +27,5 @@
27
27
  #end-rule
28
28
  %%
29
29
  parser = TinyCalc.create_decorated_parser
30
- r, = parser.parse(STDIN)
30
+ r, = parser.parse(ARGF)
31
31
  puts r
@@ -0,0 +1,44 @@
1
+ require "depager"
2
+ require "depager/version"
3
+ require "optparse"
4
+
5
+ module Depager
6
+ class CLI
7
+ def run(argv)
8
+ output_file_name = nil
9
+
10
+ OptionParser.new do |opt|
11
+ opt.banner = "Usage: depager [options] [file]"
12
+ opt.version = Depager::VERSION
13
+ opt.on("-v", "Enable verbose mode") { Depager.configuration[:verbose] = true }
14
+ opt.on("-g[FLAGS]", "Enable debug mode") { |v| Depager.configuration[:debug] = v || "sgc" }
15
+ opt.on("-I DIRECTORY", "Add the directory to $LOAD_PATH") { |v| $LOAD_PATH.unshift(v) }
16
+ opt.on("-o OUTPUT", "Set the output file name") { |v| output_file_name = v }
17
+ opt.parse!(argv)
18
+ end
19
+
20
+ d_parser = Depager::DeclarationPartParser.new
21
+ result = d_parser.parse(argv[0] ? File.open(argv[0]) : $stdin)
22
+
23
+ if output_file_name
24
+ File.write(output_file_name, result)
25
+ else
26
+ $stdout.write result
27
+ end
28
+ 0
29
+ rescue Depager::ErrorExit
30
+ 1
31
+ rescue SystemExit => e
32
+ e.status
33
+ rescue Exception => e # rubocop:disable Lint/RescueException
34
+ warn "#{File.basename $PROGRAM_NAME}: fatal error."
35
+ warn "| #{e}"
36
+ warn "| - #{d_parser.input_path}:#{d_parser.file.lineno}" if d_parser
37
+ warn "| ( #{File.basename $1}:#{$2} )" if e.backtrace[0] =~ /^(.+):(\d+)(:.+)?$/
38
+ raise if Depager.debug_mode?
39
+
40
+ warn "Try -g option if you want to see more error infomation."
41
+ 2
42
+ end
43
+ end
44
+ end