depager 0.2.3 → 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.
- checksums.yaml +7 -0
 - data/.rubocop.yml +44 -0
 - data/.simplecov +5 -0
 - data/Gemfile +12 -0
 - data/LICENSE.gpl +339 -0
 - data/Manifest.txt +73 -0
 - data/README.en +7 -21
 - data/README.ja +19 -99
 - data/Rakefile +31 -0
 - data/bin/depager +7 -45
 - data/examples/action_pl0d/pl0d.action.dr +421 -0
 - data/examples/action_pl0d/test.pl0ds +48 -0
 - data/examples/c89/c89.dr +493 -496
 - data/examples/c89/test.c89 +10 -10
 - data/examples/extension/astdf.rb +9 -0
 - data/examples/extension/atree.dr +55 -0
 - data/examples/{sample_calc → extension}/calc.atree.dr +42 -43
 - data/examples/{sample_calc/calc.action.dr → extension/calc.simple_action.dr} +33 -33
 - data/examples/extension/paction.dr +16 -15
 - data/examples/extension/pactiontest.dr +14 -14
 - data/examples/extension/simple_action.rb +46 -0
 - data/examples/pl0d/pl0ds.dr +337 -334
 - data/examples/pl0d/test.pl0ds +33 -33
 - data/examples/rie_calc/calc.rie.dr +57 -0
 - data/examples/rie_calc/test.calc +4 -0
 - data/examples/rie_dcuse/dcuse.rie.dr +71 -0
 - data/examples/rie_dcuse/test.dcuse +1 -0
 - data/examples/rie_pl0/orig_ex/exerrdg.pl0 +44 -0
 - data/examples/rie_pl0/orig_ex/exerrm.pl0 +19 -0
 - data/examples/rie_pl0/orig_ex/exerrmre.pl0 +20 -0
 - data/examples/rie_pl0/orig_ex/exerrtok.pl0 +18 -0
 - data/examples/rie_pl0/orig_ex/exmdg.pl0 +40 -0
 - data/examples/rie_pl0/orig_ex/exmdgwwl.pl0 +43 -0
 - data/examples/rie_pl0/orig_ex/exmrw.pl0 +22 -0
 - data/examples/rie_pl0/orig_ex/exmwwl.pl0 +18 -0
 - data/examples/rie_pl0/orig_ex/exnorw.pl0 +17 -0
 - data/examples/rie_pl0/pl0.rie.dr +450 -0
 - data/examples/rie_pl0/test.pl0 +10 -0
 - data/examples/slex_test/divreg.slex.dr +29 -29
 - data/examples/slex_test/ljoin.slex.dr +36 -36
 - data/examples/slex_test/test.divreg +1 -1
 - data/examples/slex_test/test.ljoin +3 -3
 - data/examples/{sample_calc/calc.nvaction.dr → tiny_calc/calc.action.dr} +33 -33
 - data/examples/{sample_calc → tiny_calc}/calc.ast.action.dr +76 -66
 - data/examples/{sample_calc → tiny_calc}/calc.ast.dr +67 -55
 - data/examples/tiny_calc/calc.cst.dr +50 -0
 - data/examples/{sample_calc → tiny_calc}/calc.dr +43 -43
 - data/examples/{sample_calc → tiny_calc}/calc.lex.dr +29 -29
 - data/examples/{sample_calc/calc_prec.nvaction.dr → tiny_calc/calc_prec.action.dr} +31 -31
 - data/lib/depager/cli.rb +44 -0
 - data/lib/depager/grammar.rb +253 -291
 - data/lib/depager/lr.rb +589 -579
 - data/lib/depager/parser.rb +269 -277
 - data/lib/depager/plugins/_rie_debug.rb +63 -0
 - data/lib/depager/plugins/action.rb +47 -0
 - data/lib/depager/plugins/ast.dr +367 -0
 - data/lib/depager/plugins/ast.rb +1329 -0
 - data/lib/depager/{ruby/plugins → plugins}/cst.dr +174 -180
 - data/lib/depager/plugins/cst.rb +591 -0
 - data/lib/depager/{ruby/plugins → plugins}/lex.dr +85 -89
 - data/lib/depager/plugins/lex.rb +313 -0
 - data/lib/depager/plugins/rie.dr +725 -0
 - data/lib/depager/plugins/rie.rb +1614 -0
 - data/lib/depager/{ruby/plugins → plugins}/slex.dr +201 -200
 - data/lib/depager/plugins/slex.rb +769 -0
 - data/lib/depager/plugins/srp.rb +46 -0
 - data/lib/depager/ruby/templates/extension_lalr_master.erb +40 -51
 - data/lib/depager/ruby/templates/extension_lalr_slave.erb +113 -107
 - data/lib/depager/ruby/templates/single_lalr_parser.erb +124 -117
 - data/lib/depager/utils.rb +158 -318
 - data/lib/depager/version.rb +3 -3
 - data/lib/depager.rb +572 -670
 - metadata +77 -80
 - data/ChangeLog +0 -16
 - data/data/depager/pre-setup.rb +0 -3
 - data/examples/c89/c89.tab.rb +0 -7127
 - data/examples/pl0d/pl0ds.tab.rb +0 -2698
 - data/examples/sample_calc/calc.action.tab.rb +0 -457
 - data/examples/sample_calc/calc.ast.action.tab.rb +0 -749
 - data/examples/sample_calc/calc.ast.tab.rb +0 -665
 - data/examples/sample_calc/calc.astdf.dr +0 -54
 - data/examples/sample_calc/calc.astdf.tab.rb +0 -672
 - data/examples/sample_calc/calc.atree.tab.rb +0 -451
 - data/examples/sample_calc/calc.cst.dr +0 -45
 - data/examples/sample_calc/calc.cst.tab.rb +0 -644
 - data/examples/sample_calc/calc.lex.tab.rb +0 -374
 - data/examples/sample_calc/calc.nvaction.tab.rb +0 -465
 - data/examples/sample_calc/calc.tab.rb +0 -365
 - data/examples/sample_calc/calc_prec.nvaction.tab.rb +0 -431
 - data/examples/slex_test/divreg.slex.tab.rb +0 -303
 - data/examples/slex_test/ljoin.slex.tab.rb +0 -370
 - data/lib/depager/ruby/plugins/_ast_tmpl.rb +0 -73
 - data/lib/depager/ruby/plugins/action.rb +0 -43
 - data/lib/depager/ruby/plugins/ast.dr +0 -269
 - data/lib/depager/ruby/plugins/ast.rb +0 -1308
 - data/lib/depager/ruby/plugins/astdf.rb +0 -6
 - data/lib/depager/ruby/plugins/atree.dr +0 -55
 - data/lib/depager/ruby/plugins/atree.rb +0 -347
 - data/lib/depager/ruby/plugins/cst.rb +0 -626
 - data/lib/depager/ruby/plugins/lex.rb +0 -336
 - data/lib/depager/ruby/plugins/nvaction.rb +0 -19
 - data/lib/depager/ruby/plugins/slex.rb +0 -817
 - data/lib/depager/ruby/plugins/srp.rb +0 -51
 - data/lib/depager/ruby/templates/simple.erb +0 -23
 - data/setup.rb +0 -1585
 - /data/examples/{sample_calc → tiny_calc}/test.calc +0 -0
 
| 
         @@ -1,66 +1,76 @@ 
     | 
|
| 
       1 
     | 
    
         
            -
            %class TinyCalc
         
     | 
| 
       2 
     | 
    
         
            -
            %extend Lexer ('plugins/lex.rb')
         
     | 
| 
       3 
     | 
    
         
            -
            %extend ASTBuilder ('plugins/ast.rb')
         
     | 
| 
       4 
     | 
    
         
            -
            %extend Action ('plugins/action.rb')
         
     | 
| 
       5 
     | 
    
         
            -
            %decorate @ASTBuilder
         
     | 
| 
       6 
     | 
    
         
            -
            %decorate @Action
         
     | 
| 
       7 
     | 
    
         
            -
            #%decorate ShiftReducePrinter ('plugins/srp.rb')
         
     | 
| 
       8 
     | 
    
         
            -
             
     | 
| 
       9 
     | 
    
         
            -
             
     | 
| 
       10 
     | 
    
         
            -
             
     | 
| 
       11 
     | 
    
         
            -
             
     | 
| 
       12 
     | 
    
         
            -
               
     | 
| 
       13 
     | 
    
         
            -
               
     | 
| 
       14 
     | 
    
         
            -
             
     | 
| 
       15 
     | 
    
         
            -
             
     | 
| 
       16 
     | 
    
         
            -
             
     | 
| 
       17 
     | 
    
         
            -
             
     | 
| 
       18 
     | 
    
         
            -
               
     | 
| 
       19 
     | 
    
         
            -
               
     | 
| 
       20 
     | 
    
         
            -
               
     | 
| 
       21 
     | 
    
         
            -
               
     | 
| 
       22 
     | 
    
         
            -
               
     | 
| 
       23 
     | 
    
         
            -
               
     | 
| 
       24 
     | 
    
         
            -
             
     | 
| 
       25 
     | 
    
         
            -
             
     | 
| 
       26 
     | 
    
         
            -
             
     | 
| 
       27 
     | 
    
         
            -
             
     | 
| 
       28 
     | 
    
         
            -
             
     | 
| 
       29 
     | 
    
         
            -
                  expr  
     | 
| 
       30 
     | 
    
         
            -
                   
     | 
| 
       31 
     | 
    
         
            -
             
     | 
| 
       32 
     | 
    
         
            -
             
     | 
| 
       33 
     | 
    
         
            -
                   
     | 
| 
       34 
     | 
    
         
            -
             
     | 
| 
       35 
     | 
    
         
            -
             
     | 
| 
       36 
     | 
    
         
            -
                   
     | 
| 
       37 
     | 
    
         
            -
             
     | 
| 
       38 
     | 
    
         
            -
               
     | 
| 
       39 
     | 
    
         
            -
             
     | 
| 
       40 
     | 
    
         
            -
                  term  
     | 
| 
       41 
     | 
    
         
            -
                   
     | 
| 
       42 
     | 
    
         
            -
             
     | 
| 
       43 
     | 
    
         
            -
             
     | 
| 
       44 
     | 
    
         
            -
                   
     | 
| 
       45 
     | 
    
         
            -
             
     | 
| 
       46 
     | 
    
         
            -
             
     | 
| 
       47 
     | 
    
         
            -
                   
     | 
| 
       48 
     | 
    
         
            -
             
     | 
| 
       49 
     | 
    
         
            -
               
     | 
| 
       50 
     | 
    
         
            -
             
     | 
| 
       51 
     | 
    
         
            -
                  NUM
         
     | 
| 
       52 
     | 
    
         
            -
                   
     | 
| 
       53 
     | 
    
         
            -
             
     | 
| 
       54 
     | 
    
         
            -
             
     | 
| 
       55 
     | 
    
         
            -
                   
     | 
| 
       56 
     | 
    
         
            -
             
     | 
| 
       57 
     | 
    
         
            -
             
     | 
| 
       58 
     | 
    
         
            -
             
     | 
| 
       59 
     | 
    
         
            -
             
     | 
| 
       60 
     | 
    
         
            -
             
     | 
| 
       61 
     | 
    
         
            -
             
     | 
| 
       62 
     | 
    
         
            -
             
     | 
| 
       63 
     | 
    
         
            -
             
     | 
| 
       64 
     | 
    
         
            -
             
     | 
| 
       65 
     | 
    
         
            -
             
     | 
| 
       66 
     | 
    
         
            -
             
     | 
| 
      
 1 
     | 
    
         
            +
            %class TinyCalc::Parser
         
     | 
| 
      
 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 
     | 
    
         
            +
            %decorate @ASTBuilder
         
     | 
| 
      
 6 
     | 
    
         
            +
            %decorate @Action
         
     | 
| 
      
 7 
     | 
    
         
            +
            #%decorate Depager::LALR::ShiftReducePrinter ('depager/plugins/srp.rb')
         
     | 
| 
      
 8 
     | 
    
         
            +
            %%
         
     | 
| 
      
 9 
     | 
    
         
            +
             
     | 
| 
      
 10 
     | 
    
         
            +
            %LEX{
         
     | 
| 
      
 11 
     | 
    
         
            +
              /\s+/, /\#.*/, /\n/  { }
         
     | 
| 
      
 12 
     | 
    
         
            +
              /[1-9][0-9]*/        { yield token(:NUM, $&.to_i) }
         
     | 
| 
      
 13 
     | 
    
         
            +
              /./                  { yield token($&, $&) }
         
     | 
| 
      
 14 
     | 
    
         
            +
            %}
         
     | 
| 
      
 15 
     | 
    
         
            +
             
     | 
| 
      
 16 
     | 
    
         
            +
            %AST{
         
     | 
| 
      
 17 
     | 
    
         
            +
              Node [value]     { @value = nil }
         
     | 
| 
      
 18 
     | 
    
         
            +
              Visitor          { }
         
     | 
| 
      
 19 
     | 
    
         
            +
              add(left, right) { $.value = visit($.left).value + visit($.right).value }
         
     | 
| 
      
 20 
     | 
    
         
            +
              sub(left, right) { $.value = visit($.left).value - visit($.right).value }
         
     | 
| 
      
 21 
     | 
    
         
            +
              mul(left, right) { $.value = visit($.left).value * visit($.right).value }
         
     | 
| 
      
 22 
     | 
    
         
            +
              div(left, right) { $.value = visit($.left).value / visit($.right).value }
         
     | 
| 
      
 23 
     | 
    
         
            +
              literal(-n)      { $.value = $.n.value }
         
     | 
| 
      
 24 
     | 
    
         
            +
            %}
         
     | 
| 
      
 25 
     | 
    
         
            +
             
     | 
| 
      
 26 
     | 
    
         
            +
            #begin-rule
         
     | 
| 
      
 27 
     | 
    
         
            +
              expr :
         
     | 
| 
      
 28 
     | 
    
         
            +
                  expr '+' term
         
     | 
| 
      
 29 
     | 
    
         
            +
                  => add(expr, term)
         
     | 
| 
      
 30 
     | 
    
         
            +
                  { val[0] + val[2] }
         
     | 
| 
      
 31 
     | 
    
         
            +
                | expr '-' term
         
     | 
| 
      
 32 
     | 
    
         
            +
                  => sub(expr, term)
         
     | 
| 
      
 33 
     | 
    
         
            +
                  { val[0] - val[2] }
         
     | 
| 
      
 34 
     | 
    
         
            +
                | term
         
     | 
| 
      
 35 
     | 
    
         
            +
                  => term
         
     | 
| 
      
 36 
     | 
    
         
            +
                  { val[0] }
         
     | 
| 
      
 37 
     | 
    
         
            +
              ;
         
     | 
| 
      
 38 
     | 
    
         
            +
              term :
         
     | 
| 
      
 39 
     | 
    
         
            +
                  term '*' fact
         
     | 
| 
      
 40 
     | 
    
         
            +
                  => mul(term, fact)
         
     | 
| 
      
 41 
     | 
    
         
            +
                  { val[0] * val[2] }
         
     | 
| 
      
 42 
     | 
    
         
            +
                | term '/' fact
         
     | 
| 
      
 43 
     | 
    
         
            +
                  => div(term, fact)
         
     | 
| 
      
 44 
     | 
    
         
            +
                  { val[0] / val[2] }
         
     | 
| 
      
 45 
     | 
    
         
            +
                | fact
         
     | 
| 
      
 46 
     | 
    
         
            +
                  => fact
         
     | 
| 
      
 47 
     | 
    
         
            +
                  { val[0] }
         
     | 
| 
      
 48 
     | 
    
         
            +
              ;
         
     | 
| 
      
 49 
     | 
    
         
            +
              fact :
         
     | 
| 
      
 50 
     | 
    
         
            +
                  NUM
         
     | 
| 
      
 51 
     | 
    
         
            +
                  => literal(NUM)
         
     | 
| 
      
 52 
     | 
    
         
            +
                  { val[0].value }
         
     | 
| 
      
 53 
     | 
    
         
            +
                | '(' expr ')'
         
     | 
| 
      
 54 
     | 
    
         
            +
                  => expr
         
     | 
| 
      
 55 
     | 
    
         
            +
                  { val[1].value }
         
     | 
| 
      
 56 
     | 
    
         
            +
              ;
         
     | 
| 
      
 57 
     | 
    
         
            +
            #end-rule
         
     | 
| 
      
 58 
     | 
    
         
            +
            %%
         
     | 
| 
      
 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 
     | 
    
         
            +
             
     | 
| 
      
 71 
     | 
    
         
            +
            parser = TinyCalc.create_decorated_parser()
         
     | 
| 
      
 72 
     | 
    
         
            +
            r, a = parser.parse(ARGF)
         
     | 
| 
      
 73 
     | 
    
         
            +
            v = TinyCalc::Visitor.new
         
     | 
| 
      
 74 
     | 
    
         
            +
            puts r
         
     | 
| 
      
 75 
     | 
    
         
            +
            puts r.accept(v).value
         
     | 
| 
      
 76 
     | 
    
         
            +
            puts a
         
     | 
| 
         @@ -1,55 +1,67 @@ 
     | 
|
| 
       1 
     | 
    
         
            -
            %class TinyCalc
         
     | 
| 
       2 
     | 
    
         
            -
            %extend Lexer ('plugins/lex.rb')
         
     | 
| 
       3 
     | 
    
         
            -
            %extend ASTBuilder ('plugins/ast.rb')
         
     | 
| 
       4 
     | 
    
         
            -
            %decorate @ASTBuilder
         
     | 
| 
       5 
     | 
    
         
            -
            #%decorate ShiftReducePrinter ('plugins/srp.rb')
         
     | 
| 
       6 
     | 
    
         
            -
            %%
         
     | 
| 
       7 
     | 
    
         
            -
             
     | 
| 
       8 
     | 
    
         
            -
            %LEX{
         
     | 
| 
       9 
     | 
    
         
            -
              /\s+/, /\#.*/ { }
         
     | 
| 
       10 
     | 
    
         
            -
              /[1-9][0-9]*/ { yield token(:NUM, $&.to_i) }
         
     | 
| 
       11 
     | 
    
         
            -
              /./           { yield token($&, $&) }
         
     | 
| 
       12 
     | 
    
         
            -
            %}
         
     | 
| 
       13 
     | 
    
         
            -
             
     | 
| 
       14 
     | 
    
         
            -
            %AST{
         
     | 
| 
       15 
     | 
    
         
            -
              Node [value]     { @value = nil }
         
     | 
| 
       16 
     | 
    
         
            -
              Visitor          { }
         
     | 
| 
       17 
     | 
    
         
            -
              add(left, right) { $.value = visit($.left).value + visit($.right).value }
         
     | 
| 
       18 
     | 
    
         
            -
              sub(left, right) { $.value = visit($.left).value - visit($.right).value }
         
     | 
| 
       19 
     | 
    
         
            -
              mul(left, right) { $.value = visit($.left).value * visit($.right).value }
         
     | 
| 
       20 
     | 
    
         
            -
              div(left, right) { $.value = visit($.left).value / visit($.right).value }
         
     | 
| 
       21 
     | 
    
         
            -
              literal(-n)      { $.value = $.n.value }
         
     | 
| 
       22 
     | 
    
         
            -
            %}
         
     | 
| 
       23 
     | 
    
         
            -
             
     | 
| 
       24 
     | 
    
         
            -
            #begin-rule
         
     | 
| 
       25 
     | 
    
         
            -
              expr :
         
     | 
| 
       26 
     | 
    
         
            -
                  expr '+' term
         
     | 
| 
       27 
     | 
    
         
            -
                  => add(expr, term)
         
     | 
| 
       28 
     | 
    
         
            -
                | expr '-' term
         
     | 
| 
       29 
     | 
    
         
            -
                  => sub(expr, term)
         
     | 
| 
       30 
     | 
    
         
            -
                | term
         
     | 
| 
       31 
     | 
    
         
            -
                  => term
         
     | 
| 
       32 
     | 
    
         
            -
              ;
         
     | 
| 
       33 
     | 
    
         
            -
              term :
         
     | 
| 
       34 
     | 
    
         
            -
                  term '*' fact
         
     | 
| 
       35 
     | 
    
         
            -
                  => mul(term, fact)
         
     | 
| 
       36 
     | 
    
         
            -
                | term '/' fact
         
     | 
| 
       37 
     | 
    
         
            -
                  => div(term, fact)
         
     | 
| 
       38 
     | 
    
         
            -
                | fact
         
     | 
| 
       39 
     | 
    
         
            -
                  => fact
         
     | 
| 
       40 
     | 
    
         
            -
              ;
         
     | 
| 
       41 
     | 
    
         
            -
              fact :
         
     | 
| 
       42 
     | 
    
         
            -
                  NUM
         
     | 
| 
       43 
     | 
    
         
            -
                  => literal(NUM)
         
     | 
| 
       44 
     | 
    
         
            -
                | '(' expr ')'
         
     | 
| 
       45 
     | 
    
         
            -
                  => expr
         
     | 
| 
       46 
     | 
    
         
            -
              ;
         
     | 
| 
       47 
     | 
    
         
            -
            #end-rule
         
     | 
| 
       48 
     | 
    
         
            -
            %%
         
     | 
| 
       49 
     | 
    
         
            -
             
     | 
| 
       50 
     | 
    
         
            -
             
     | 
| 
       51 
     | 
    
         
            -
             
     | 
| 
       52 
     | 
    
         
            -
             
     | 
| 
       53 
     | 
    
         
            -
             
     | 
| 
       54 
     | 
    
         
            -
             
     | 
| 
       55 
     | 
    
         
            -
             
     | 
| 
      
 1 
     | 
    
         
            +
            %class TinyCalc::Parser
         
     | 
| 
      
 2 
     | 
    
         
            +
            %extend Depager::Lexer ('depager/plugins/lex.rb')
         
     | 
| 
      
 3 
     | 
    
         
            +
            %extend Depager::ASTBuilder ('depager/plugins/ast.rb')
         
     | 
| 
      
 4 
     | 
    
         
            +
            %decorate @ASTBuilder
         
     | 
| 
      
 5 
     | 
    
         
            +
            #%decorate Depager::LALR::ShiftReducePrinter ('depager/plugins/srp.rb')
         
     | 
| 
      
 6 
     | 
    
         
            +
            %%
         
     | 
| 
      
 7 
     | 
    
         
            +
             
     | 
| 
      
 8 
     | 
    
         
            +
            %LEX{
         
     | 
| 
      
 9 
     | 
    
         
            +
              /\s+/, /\#.*/ { }
         
     | 
| 
      
 10 
     | 
    
         
            +
              /[1-9][0-9]*/ { yield token(:NUM, $&.to_i) }
         
     | 
| 
      
 11 
     | 
    
         
            +
              /./           { yield token($&, $&) }
         
     | 
| 
      
 12 
     | 
    
         
            +
            %}
         
     | 
| 
      
 13 
     | 
    
         
            +
             
     | 
| 
      
 14 
     | 
    
         
            +
            %AST{
         
     | 
| 
      
 15 
     | 
    
         
            +
              Node [value]     { @value = nil }
         
     | 
| 
      
 16 
     | 
    
         
            +
              Visitor          { }
         
     | 
| 
      
 17 
     | 
    
         
            +
              add(left, right) { $.value = visit($.left).value + visit($.right).value }
         
     | 
| 
      
 18 
     | 
    
         
            +
              sub(left, right) { $.value = visit($.left).value - visit($.right).value }
         
     | 
| 
      
 19 
     | 
    
         
            +
              mul(left, right) { $.value = visit($.left).value * visit($.right).value }
         
     | 
| 
      
 20 
     | 
    
         
            +
              div(left, right) { $.value = visit($.left).value / visit($.right).value }
         
     | 
| 
      
 21 
     | 
    
         
            +
              literal(-n)      { $.value = $.n.value }
         
     | 
| 
      
 22 
     | 
    
         
            +
            %}
         
     | 
| 
      
 23 
     | 
    
         
            +
             
     | 
| 
      
 24 
     | 
    
         
            +
            #begin-rule
         
     | 
| 
      
 25 
     | 
    
         
            +
              expr :
         
     | 
| 
      
 26 
     | 
    
         
            +
                  expr '+' term
         
     | 
| 
      
 27 
     | 
    
         
            +
                  => add(expr, term)
         
     | 
| 
      
 28 
     | 
    
         
            +
                | expr '-' term
         
     | 
| 
      
 29 
     | 
    
         
            +
                  => sub(expr, term)
         
     | 
| 
      
 30 
     | 
    
         
            +
                | term
         
     | 
| 
      
 31 
     | 
    
         
            +
                  => term
         
     | 
| 
      
 32 
     | 
    
         
            +
              ;
         
     | 
| 
      
 33 
     | 
    
         
            +
              term :
         
     | 
| 
      
 34 
     | 
    
         
            +
                  term '*' fact
         
     | 
| 
      
 35 
     | 
    
         
            +
                  => mul(term, fact)
         
     | 
| 
      
 36 
     | 
    
         
            +
                | term '/' fact
         
     | 
| 
      
 37 
     | 
    
         
            +
                  => div(term, fact)
         
     | 
| 
      
 38 
     | 
    
         
            +
                | fact
         
     | 
| 
      
 39 
     | 
    
         
            +
                  => fact
         
     | 
| 
      
 40 
     | 
    
         
            +
              ;
         
     | 
| 
      
 41 
     | 
    
         
            +
              fact :
         
     | 
| 
      
 42 
     | 
    
         
            +
                  NUM
         
     | 
| 
      
 43 
     | 
    
         
            +
                  => literal(NUM)
         
     | 
| 
      
 44 
     | 
    
         
            +
                | '(' expr ')'
         
     | 
| 
      
 45 
     | 
    
         
            +
                  => expr
         
     | 
| 
      
 46 
     | 
    
         
            +
              ;
         
     | 
| 
      
 47 
     | 
    
         
            +
            #end-rule
         
     | 
| 
      
 48 
     | 
    
         
            +
            %%
         
     | 
| 
      
 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 
     | 
    
         
            +
             
     | 
| 
      
 61 
     | 
    
         
            +
            parser = TinyCalc.create_decorated_parser()
         
     | 
| 
      
 62 
     | 
    
         
            +
            r, = parser.parse(ARGF)
         
     | 
| 
      
 63 
     | 
    
         
            +
            v = TinyCalc::Visitor.new
         
     | 
| 
      
 64 
     | 
    
         
            +
            puts r
         
     | 
| 
      
 65 
     | 
    
         
            +
            puts r.accept(v).value
         
     | 
| 
      
 66 
     | 
    
         
            +
             
     | 
| 
      
 67 
     | 
    
         
            +
             
     | 
| 
         @@ -0,0 +1,50 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            %class TinyCalc::Parser
         
     | 
| 
      
 2 
     | 
    
         
            +
            %extend Depager::Lexer ('depager/plugins/lex.rb')
         
     | 
| 
      
 3 
     | 
    
         
            +
            %extend Depager::CSTBuilder ('depager/plugins/cst.rb')
         
     | 
| 
      
 4 
     | 
    
         
            +
            %decorate @CSTBuilder
         
     | 
| 
      
 5 
     | 
    
         
            +
            %%
         
     | 
| 
      
 6 
     | 
    
         
            +
             
     | 
| 
      
 7 
     | 
    
         
            +
            %LEX{
         
     | 
| 
      
 8 
     | 
    
         
            +
              /\s+/, /\#.*/ { }
         
     | 
| 
      
 9 
     | 
    
         
            +
              /[1-9][0-9]*/ { yield token(:NUM, $&.to_i) }
         
     | 
| 
      
 10 
     | 
    
         
            +
              /./           { yield token($&, $&) }
         
     | 
| 
      
 11 
     | 
    
         
            +
            %}
         
     | 
| 
      
 12 
     | 
    
         
            +
             
     | 
| 
      
 13 
     | 
    
         
            +
            %CST{
         
     | 
| 
      
 14 
     | 
    
         
            +
              Node {
         
     | 
| 
      
 15 
     | 
    
         
            +
                attr_accessor :value
         
     | 
| 
      
 16 
     | 
    
         
            +
                def initialize
         
     | 
| 
      
 17 
     | 
    
         
            +
                  @value = nil
         
     | 
| 
      
 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
         
     | 
| 
      
 25 
     | 
    
         
            +
              }
         
     | 
| 
      
 26 
     | 
    
         
            +
            %}
         
     | 
| 
      
 27 
     | 
    
         
            +
             
     | 
| 
      
 28 
     | 
    
         
            +
            #begin-rule
         
     | 
| 
      
 29 
     | 
    
         
            +
              expr :
         
     | 
| 
      
 30 
     | 
    
         
            +
                  expr '+' term { $.value = $.expr.value + $.term.value }
         
     | 
| 
      
 31 
     | 
    
         
            +
                | expr '-' term { $.value = $.expr.value + $.term.value }
         
     | 
| 
      
 32 
     | 
    
         
            +
                | term          { $.value = $.term.value }
         
     | 
| 
      
 33 
     | 
    
         
            +
              ;
         
     | 
| 
      
 34 
     | 
    
         
            +
              term :
         
     | 
| 
      
 35 
     | 
    
         
            +
                  term '*' fact { $.value = $.term.value * $.fact.value }
         
     | 
| 
      
 36 
     | 
    
         
            +
                | term '/' fact { $.value = $.term.value / $.fact.value }
         
     | 
| 
      
 37 
     | 
    
         
            +
                | fact          { $.value = $.fact.value }
         
     | 
| 
      
 38 
     | 
    
         
            +
              ;
         
     | 
| 
      
 39 
     | 
    
         
            +
              fact :
         
     | 
| 
      
 40 
     | 
    
         
            +
                  NUM           { $.value = $.num.value }
         
     | 
| 
      
 41 
     | 
    
         
            +
                | '(' expr ')'  { $.value = $.expr }
         
     | 
| 
      
 42 
     | 
    
         
            +
              ;
         
     | 
| 
      
 43 
     | 
    
         
            +
            #end-rule
         
     | 
| 
      
 44 
     | 
    
         
            +
            %%
         
     | 
| 
      
 45 
     | 
    
         
            +
             
     | 
| 
      
 46 
     | 
    
         
            +
            parser = TinyCalc.create_decorated_parser
         
     | 
| 
      
 47 
     | 
    
         
            +
            r, = parser.parse(ARGF)
         
     | 
| 
      
 48 
     | 
    
         
            +
            v = TinyCalc::Visitor.new
         
     | 
| 
      
 49 
     | 
    
         
            +
            puts r
         
     | 
| 
      
 50 
     | 
    
         
            +
            puts r.accept(v)
         
     | 
| 
         @@ -1,43 +1,43 @@ 
     | 
|
| 
       1 
     | 
    
         
            -
            %class TinyCalc
         
     | 
| 
       2 
     | 
    
         
            -
            %inner{
         
     | 
| 
       3 
     | 
    
         
            -
              def lex
         
     | 
| 
       4 
     | 
    
         
            -
                until @file.eof?
         
     | 
| 
       5 
     | 
    
         
            -
                  @line = @file.gets
         
     | 
| 
       6 
     | 
    
         
            -
                  until @line.empty? do
         
     | 
| 
       7 
     | 
    
         
            -
                    case @line
         
     | 
| 
       8 
     | 
    
         
            -
                    when /\A\s+/, /\A\#.*/, /\A\n/
         
     | 
| 
       9 
     | 
    
         
            -
                      #skip blank and comment
         
     | 
| 
       10 
     | 
    
         
            -
                    when /\A[0-9]+/
         
     | 
| 
       11 
     | 
    
         
            -
                      yield :NUM, $&
         
     | 
| 
       12 
     | 
    
         
            -
                    when /\A./
         
     | 
| 
       13 
     | 
    
         
            -
                      yield $&, $&
         
     | 
| 
       14 
     | 
    
         
            -
                    else
         
     | 
| 
       15 
     | 
    
         
            -
                      raise RuntimeError, "must not happen #{line}"
         
     | 
| 
       16 
     | 
    
         
            -
                    end
         
     | 
| 
       17 
     | 
    
         
            -
                    @line = $'
         
     | 
| 
       18 
     | 
    
         
            -
                  end
         
     | 
| 
       19 
     | 
    
         
            -
                end
         
     | 
| 
       20 
     | 
    
         
            -
                yield nil, nil
         
     | 
| 
       21 
     | 
    
         
            -
              end
         
     | 
| 
       22 
     | 
    
         
            -
            %}
         
     | 
| 
       23 
     | 
    
         
            -
            %%
         
     | 
| 
       24 
     | 
    
         
            -
             
     | 
| 
       25 
     | 
    
         
            -
            #begin-rule
         
     | 
| 
       26 
     | 
    
         
            -
              expr :
         
     | 
| 
       27 
     | 
    
         
            -
                  expr '+' term
         
     | 
| 
       28 
     | 
    
         
            -
                | expr '-' term
         
     | 
| 
       29 
     | 
    
         
            -
                | term
         
     | 
| 
       30 
     | 
    
         
            -
              ;
         
     | 
| 
       31 
     | 
    
         
            -
              term :
         
     | 
| 
       32 
     | 
    
         
            -
                  term '*' fact
         
     | 
| 
       33 
     | 
    
         
            -
                | term '/' fact
         
     | 
| 
       34 
     | 
    
         
            -
                | fact
         
     | 
| 
       35 
     | 
    
         
            -
              ;
         
     | 
| 
       36 
     | 
    
         
            -
              fact :
         
     | 
| 
       37 
     | 
    
         
            -
                  NUM
         
     | 
| 
       38 
     | 
    
         
            -
                | '(' expr ')'
         
     | 
| 
       39 
     | 
    
         
            -
              ;
         
     | 
| 
       40 
     | 
    
         
            -
            #end-rule
         
     | 
| 
       41 
     | 
    
         
            -
            %%
         
     | 
| 
       42 
     | 
    
         
            -
            parser = TinyCalc. 
     | 
| 
       43 
     | 
    
         
            -
            parser.parse( 
     | 
| 
      
 1 
     | 
    
         
            +
            %class TinyCalc::Parser
         
     | 
| 
      
 2 
     | 
    
         
            +
            %inner{
         
     | 
| 
      
 3 
     | 
    
         
            +
              def lex
         
     | 
| 
      
 4 
     | 
    
         
            +
                until @file.eof?
         
     | 
| 
      
 5 
     | 
    
         
            +
                  @line = @file.gets
         
     | 
| 
      
 6 
     | 
    
         
            +
                  until @line.empty? do
         
     | 
| 
      
 7 
     | 
    
         
            +
                    case @line
         
     | 
| 
      
 8 
     | 
    
         
            +
                    when /\A\s+/, /\A\#.*/, /\A\n/
         
     | 
| 
      
 9 
     | 
    
         
            +
                      #skip blank and comment
         
     | 
| 
      
 10 
     | 
    
         
            +
                    when /\A[0-9]+/
         
     | 
| 
      
 11 
     | 
    
         
            +
                      yield :NUM, $&
         
     | 
| 
      
 12 
     | 
    
         
            +
                    when /\A./
         
     | 
| 
      
 13 
     | 
    
         
            +
                      yield $&, $&
         
     | 
| 
      
 14 
     | 
    
         
            +
                    else
         
     | 
| 
      
 15 
     | 
    
         
            +
                      raise RuntimeError, "must not happen #{line}"
         
     | 
| 
      
 16 
     | 
    
         
            +
                    end
         
     | 
| 
      
 17 
     | 
    
         
            +
                    @line = $'
         
     | 
| 
      
 18 
     | 
    
         
            +
                  end
         
     | 
| 
      
 19 
     | 
    
         
            +
                end
         
     | 
| 
      
 20 
     | 
    
         
            +
                yield nil, nil
         
     | 
| 
      
 21 
     | 
    
         
            +
              end
         
     | 
| 
      
 22 
     | 
    
         
            +
            %}
         
     | 
| 
      
 23 
     | 
    
         
            +
            %%
         
     | 
| 
      
 24 
     | 
    
         
            +
             
     | 
| 
      
 25 
     | 
    
         
            +
            #begin-rule
         
     | 
| 
      
 26 
     | 
    
         
            +
              expr :
         
     | 
| 
      
 27 
     | 
    
         
            +
                  expr '+' term
         
     | 
| 
      
 28 
     | 
    
         
            +
                | expr '-' term
         
     | 
| 
      
 29 
     | 
    
         
            +
                | term
         
     | 
| 
      
 30 
     | 
    
         
            +
              ;
         
     | 
| 
      
 31 
     | 
    
         
            +
              term :
         
     | 
| 
      
 32 
     | 
    
         
            +
                  term '*' fact
         
     | 
| 
      
 33 
     | 
    
         
            +
                | term '/' fact
         
     | 
| 
      
 34 
     | 
    
         
            +
                | fact
         
     | 
| 
      
 35 
     | 
    
         
            +
              ;
         
     | 
| 
      
 36 
     | 
    
         
            +
              fact :
         
     | 
| 
      
 37 
     | 
    
         
            +
                  NUM
         
     | 
| 
      
 38 
     | 
    
         
            +
                | '(' expr ')'
         
     | 
| 
      
 39 
     | 
    
         
            +
              ;
         
     | 
| 
      
 40 
     | 
    
         
            +
            #end-rule
         
     | 
| 
      
 41 
     | 
    
         
            +
            %%
         
     | 
| 
      
 42 
     | 
    
         
            +
            parser = TinyCalc.create_decorated_parser()
         
     | 
| 
      
 43 
     | 
    
         
            +
            parser.parse(ARGF)
         
     | 
| 
         @@ -1,29 +1,29 @@ 
     | 
|
| 
       1 
     | 
    
         
            -
            %class TinyCalc
         
     | 
| 
       2 
     | 
    
         
            -
            %extend Lexer ('plugins/lex.rb')
         
     | 
| 
       3 
     | 
    
         
            -
            %%
         
     | 
| 
       4 
     | 
    
         
            -
             
     | 
| 
       5 
     | 
    
         
            -
            %LEX{
         
     | 
| 
       6 
     | 
    
         
            -
              /\s+/, /\#.*/, /\n/  { }
         
     | 
| 
       7 
     | 
    
         
            -
              /[1-9][0-9]*/        { yield token(:NUM, $&.to_i) }
         
     | 
| 
       8 
     | 
    
         
            -
              /./                  { yield token($&, $&) }
         
     | 
| 
       9 
     | 
    
         
            -
            %}
         
     | 
| 
       10 
     | 
    
         
            -
             
     | 
| 
       11 
     | 
    
         
            -
            #begin-rule
         
     | 
| 
       12 
     | 
    
         
            -
              expr :
         
     | 
| 
       13 
     | 
    
         
            -
                  expr '+' term
         
     | 
| 
       14 
     | 
    
         
            -
                | expr '-' term
         
     | 
| 
       15 
     | 
    
         
            -
                | term
         
     | 
| 
       16 
     | 
    
         
            -
              ;
         
     | 
| 
       17 
     | 
    
         
            -
              term :
         
     | 
| 
       18 
     | 
    
         
            -
                  term '*' fact
         
     | 
| 
       19 
     | 
    
         
            -
                | term '/' fact
         
     | 
| 
       20 
     | 
    
         
            -
                | fact
         
     | 
| 
       21 
     | 
    
         
            -
              ;
         
     | 
| 
       22 
     | 
    
         
            -
              fact :
         
     | 
| 
       23 
     | 
    
         
            -
                  NUM
         
     | 
| 
       24 
     | 
    
         
            -
                | '(' expr ')'
         
     | 
| 
       25 
     | 
    
         
            -
              ;
         
     | 
| 
       26 
     | 
    
         
            -
            #end-rule
         
     | 
| 
       27 
     | 
    
         
            -
            %%
         
     | 
| 
       28 
     | 
    
         
            -
            parser = TinyCalc. 
     | 
| 
       29 
     | 
    
         
            -
            parser.parse( 
     | 
| 
      
 1 
     | 
    
         
            +
            %class TinyCalc::Parser
         
     | 
| 
      
 2 
     | 
    
         
            +
            %extend Depager::Lexer ('depager/plugins/lex.rb')
         
     | 
| 
      
 3 
     | 
    
         
            +
            %%
         
     | 
| 
      
 4 
     | 
    
         
            +
             
     | 
| 
      
 5 
     | 
    
         
            +
            %LEX{
         
     | 
| 
      
 6 
     | 
    
         
            +
              /\s+/, /\#.*/, /\n/  { }
         
     | 
| 
      
 7 
     | 
    
         
            +
              /[1-9][0-9]*/        { yield token(:NUM, $&.to_i) }
         
     | 
| 
      
 8 
     | 
    
         
            +
              /./                  { yield token($&, $&) }
         
     | 
| 
      
 9 
     | 
    
         
            +
            %}
         
     | 
| 
      
 10 
     | 
    
         
            +
             
     | 
| 
      
 11 
     | 
    
         
            +
            #begin-rule
         
     | 
| 
      
 12 
     | 
    
         
            +
              expr :
         
     | 
| 
      
 13 
     | 
    
         
            +
                  expr '+' term
         
     | 
| 
      
 14 
     | 
    
         
            +
                | expr '-' term
         
     | 
| 
      
 15 
     | 
    
         
            +
                | term
         
     | 
| 
      
 16 
     | 
    
         
            +
              ;
         
     | 
| 
      
 17 
     | 
    
         
            +
              term :
         
     | 
| 
      
 18 
     | 
    
         
            +
                  term '*' fact
         
     | 
| 
      
 19 
     | 
    
         
            +
                | term '/' fact
         
     | 
| 
      
 20 
     | 
    
         
            +
                | fact
         
     | 
| 
      
 21 
     | 
    
         
            +
              ;
         
     | 
| 
      
 22 
     | 
    
         
            +
              fact :
         
     | 
| 
      
 23 
     | 
    
         
            +
                  NUM
         
     | 
| 
      
 24 
     | 
    
         
            +
                | '(' expr ')'
         
     | 
| 
      
 25 
     | 
    
         
            +
              ;
         
     | 
| 
      
 26 
     | 
    
         
            +
            #end-rule
         
     | 
| 
      
 27 
     | 
    
         
            +
            %%
         
     | 
| 
      
 28 
     | 
    
         
            +
            parser = TinyCalc.create_decorated_parser()
         
     | 
| 
      
 29 
     | 
    
         
            +
            parser.parse(ARGF)
         
     | 
| 
         @@ -1,31 +1,31 @@ 
     | 
|
| 
       1 
     | 
    
         
            -
            %class TinyCalc
         
     | 
| 
       2 
     | 
    
         
            -
            %extend Lexer ('plugins/lex.rb')
         
     | 
| 
       3 
     | 
    
         
            -
            %extend  
     | 
| 
       4 
     | 
    
         
            -
            %decorate @ 
     | 
| 
       5 
     | 
    
         
            -
            #%decorate ShiftReducePrinter ('plugins/srp.rb')
         
     | 
| 
       6 
     | 
    
         
            -
            %prec{
         
     | 
| 
       7 
     | 
    
         
            -
              left '*' '/'
         
     | 
| 
       8 
     | 
    
         
            -
              left '+' '-'
         
     | 
| 
       9 
     | 
    
         
            -
            %}
         
     | 
| 
       10 
     | 
    
         
            -
            %%
         
     | 
| 
       11 
     | 
    
         
            -
             
     | 
| 
       12 
     | 
    
         
            -
            %LEX{
         
     | 
| 
       13 
     | 
    
         
            -
              /\s+/, /\#.*/ { }
         
     | 
| 
       14 
     | 
    
         
            -
              /[1-9][0-9]*/ { yield token(:NUM, $&.to_i) }
         
     | 
| 
       15 
     | 
    
         
            -
              /./           { yield token($&, $&) }
         
     | 
| 
       16 
     | 
    
         
            -
            %}
         
     | 
| 
       17 
     | 
    
         
            -
             
     | 
| 
       18 
     | 
    
         
            -
            #begin-rule
         
     | 
| 
       19 
     | 
    
         
            -
              expr : 
         
     | 
| 
       20 
     | 
    
         
            -
                  expr-left '+' expr-right { _left + _right }
         
     | 
| 
       21 
     | 
    
         
            -
                | expr-left '-' expr-right { _left - _right }
         
     | 
| 
       22 
     | 
    
         
            -
                | expr-left '*' expr-right { _left * _right }
         
     | 
| 
       23 
     | 
    
         
            -
                | expr-left '/' expr-right { _left / _right }
         
     | 
| 
       24 
     | 
    
         
            -
                | '(' expr ')'  { _expr }
         
     | 
| 
       25 
     | 
    
         
            -
                | NUM           { _NUM.value }
         
     | 
| 
       26 
     | 
    
         
            -
              ;
         
     | 
| 
       27 
     | 
    
         
            -
            #end-rule
         
     | 
| 
       28 
     | 
    
         
            -
            %%
         
     | 
| 
       29 
     | 
    
         
            -
            parser =  
     | 
| 
       30 
     | 
    
         
            -
            r, = parser.parse( 
     | 
| 
       31 
     | 
    
         
            -
            puts r
         
     | 
| 
      
 1 
     | 
    
         
            +
            %class TinyCalc::Parser
         
     | 
| 
      
 2 
     | 
    
         
            +
            %extend Depager::Lexer ('depager/plugins/lex.rb')
         
     | 
| 
      
 3 
     | 
    
         
            +
            %extend Depager::Action ('depager/plugins/action.rb')
         
     | 
| 
      
 4 
     | 
    
         
            +
            %decorate @Action
         
     | 
| 
      
 5 
     | 
    
         
            +
            #%decorate Depager::LALR::ShiftReducePrinter ('depager/plugins/srp.rb')
         
     | 
| 
      
 6 
     | 
    
         
            +
            %prec{
         
     | 
| 
      
 7 
     | 
    
         
            +
              left '*' '/'
         
     | 
| 
      
 8 
     | 
    
         
            +
              left '+' '-'
         
     | 
| 
      
 9 
     | 
    
         
            +
            %}
         
     | 
| 
      
 10 
     | 
    
         
            +
            %%
         
     | 
| 
      
 11 
     | 
    
         
            +
             
     | 
| 
      
 12 
     | 
    
         
            +
            %LEX{
         
     | 
| 
      
 13 
     | 
    
         
            +
              /\s+/, /\#.*/ { }
         
     | 
| 
      
 14 
     | 
    
         
            +
              /[1-9][0-9]*/ { yield token(:NUM, $&.to_i) }
         
     | 
| 
      
 15 
     | 
    
         
            +
              /./           { yield token($&, $&) }
         
     | 
| 
      
 16 
     | 
    
         
            +
            %}
         
     | 
| 
      
 17 
     | 
    
         
            +
             
     | 
| 
      
 18 
     | 
    
         
            +
            #begin-rule
         
     | 
| 
      
 19 
     | 
    
         
            +
              expr : 
         
     | 
| 
      
 20 
     | 
    
         
            +
                  expr-left '+' expr-right { _left + _right }
         
     | 
| 
      
 21 
     | 
    
         
            +
                | expr-left '-' expr-right { _left - _right }
         
     | 
| 
      
 22 
     | 
    
         
            +
                | expr-left '*' expr-right { _left * _right }
         
     | 
| 
      
 23 
     | 
    
         
            +
                | expr-left '/' expr-right { _left / _right }
         
     | 
| 
      
 24 
     | 
    
         
            +
                | '(' expr ')'  { _expr }
         
     | 
| 
      
 25 
     | 
    
         
            +
                | NUM           { _NUM.value }
         
     | 
| 
      
 26 
     | 
    
         
            +
              ;
         
     | 
| 
      
 27 
     | 
    
         
            +
            #end-rule
         
     | 
| 
      
 28 
     | 
    
         
            +
            %%
         
     | 
| 
      
 29 
     | 
    
         
            +
            parser = TinyCalc.create_decorated_parser
         
     | 
| 
      
 30 
     | 
    
         
            +
            r, = parser.parse(ARGF)
         
     | 
| 
      
 31 
     | 
    
         
            +
            puts r
         
     | 
    
        data/lib/depager/cli.rb
    ADDED
    
    | 
         @@ -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
         
     |