Class: ANTLR3::Main::WalkerMain

Inherits:
Main
  • Object
show all
Defined in:
lib/antlr3/main.rb

Overview

A class which implements a handy test script which is executed whenever an ANTLR generated tree walker (tree parser) file is run directly from the command line.

Instance Attribute Summary collapse

Attributes inherited from Main

#error, #output

Attributes included from Options

#debug_socket, #encoding, #input, #interactive, #no_output, #profile, #ruby_prof

Instance Method Summary collapse

Methods inherited from Main

#execute

Methods included from Util

parse_version, silence_warnings, snake_case, tidy

Methods included from Options

#parse_options

Constructor Details

#initialize(walker_class, options = {}) ⇒ WalkerMain

Returns a new instance of WalkerMain


483
484
485
486
487
488
489
490
491
492
493
494
# File 'lib/antlr3/main.rb', line 483

def initialize( walker_class, options = {} )
  super( options )
  @walker_class = walker_class
  @lexer_class_name = options[ :lexer_class_name ]
  @lexer_class  = options[ :lexer_class ]
  @parser_class_name = options[ :parser_class_name ]
  @parser_class = options[ :parser_class ]
  if @debug = ( @parser_class.debug? rescue false )
    @port = options.fetch( :port, ANTLR3::Debug::DEFAULT_PORT )
    @log  = options.fetch( :log, @error )
  end
end

Instance Attribute Details

#lexer_classObject

Returns the value of attribute lexer_class


481
482
483
# File 'lib/antlr3/main.rb', line 481

def lexer_class
  @lexer_class
end

#parser_classObject

Returns the value of attribute parser_class


481
482
483
# File 'lib/antlr3/main.rb', line 481

def parser_class
  @parser_class
end

#walker_classObject

Returns the value of attribute walker_class


481
482
483
# File 'lib/antlr3/main.rb', line 481

def walker_class
  @walker_class
end

Instance Method Details

#recognize(in_stream) ⇒ Object


556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
# File 'lib/antlr3/main.rb', line 556

def recognize( in_stream )
  walker_options = {}
  if @debug
    walker_options[ :port ] = @port
    walker_options[ :log ] = @log
  end
  @lexer = @lexer_class.new( in_stream )
  @token_stream = ANTLR3::CommonTokenStream.new( @lexer )
  @parser = @parser_class.new( @token_stream )
  if result = @parser.send( @parser_rule )
    result.respond_to?( :tree ) or fail( "Parser did not return an AST for rule #@parser_rule" )
    @node_stream = ANTLR3::CommonTreeNodeStream.new( result.tree )
    @node_stream.token_stream = @token_stream
    @walker = @walker_class.new( @node_stream, walker_options )
    if result = @walker.send( @walker_rule )
      out = result.tree.inspect rescue result.inspect
      puts( out )
    else puts!( "walker.#@walker_rule returned nil" )
    end
  else puts!( "parser.#@parser_rule returned nil" )
  end
end

#setupObject

TODO: finish the Main modules


547
548
549
550
551
552
553
554
# File 'lib/antlr3/main.rb', line 547

def setup
  unless @lexer_class ||= fetch_class( @lexer_class_name )
    fail( "unable to locate the lexer class #@lexer_class_name" )
  end
  unless @parser_class ||= fetch_class( @parser_class_name )
    fail( "unable to locate the parser class #@parser_class_name" )
  end
end

#setup_options(opt) ⇒ Object


496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
# File 'lib/antlr3/main.rb', line 496

def setup_options( opt )
  super
  
  opt.separator ''
  opt.separator "Tree Parser Configuration:"
  
  opt.on( '--lexer-name CLASS_NAME', 'full name of the lexer class to use' ) { |val| @lexer_class_name = val }
  opt.on(
    '--lexer-file PATH_TO_LIBRARY',
    'path to load to make the lexer class available'
  ) { |val|
    begin
      test( ?f, val ) ? load( val ) : require( val )
    rescue LoadError
      warn( "unable to load the library `#{ val }' specified by --lexer-file: #{ $! }" )
    end
  }
  
  opt.on(
    '--parser-name CLASS_NAME',
    'full name of the parser class to use'
  ) { |val| @parser_class_name = val }
  opt.on(
    '--parser-file PATH_TO_LIBRARY',
    'path to load to make the parser class available'
  ) { |val|
    begin
      test( ?f, val ) ? load( val ) : require( val )
    rescue LoadError
      warn( "unable to load the library specified by --parser-file: #{ $! }" )
    end
  }
  
  opt.on( '--parser-rule NAME', "name of the parser rule to use on the input" ) { |val| @parser_rule = val }
  opt.on( '--rule NAME', "name of the rule to invoke in the tree parser" ) { |val| @walker_rule = val }
  
  if @debug
    opt.separator ''
    opt.separator "Debug Mode Options:"
    
    opt.on( '--port NUMBER', Integer, "port number to use for the debug socket" ) do |number|
      @port = number
    end
    opt.on( '--log PATH', "path of file to use to record socket activity",
           "(stderr by default)" ) do |path|
      @log = open( path, 'w' )
    end
  end
end