Class: ANTLR3::Main::ParserMain

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 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(parser_class, options = {}) ⇒ ParserMain


368
369
370
371
372
373
374
375
376
377
378
379
380
# File 'lib/antlr3/main.rb', line 368

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

Instance Attribute Details

#lexer_classObject

Returns the value of attribute lexer_class


361
362
363
# File 'lib/antlr3/main.rb', line 361

def lexer_class
  @lexer_class
end

#lexer_class_nameObject

Returns the value of attribute lexer_class_name


361
362
363
# File 'lib/antlr3/main.rb', line 361

def lexer_class_name
  @lexer_class_name
end

#logObject

Returns the value of attribute log


361
362
363
# File 'lib/antlr3/main.rb', line 361

def log
  @log
end

#parser_classObject

Returns the value of attribute parser_class


361
362
363
# File 'lib/antlr3/main.rb', line 361

def parser_class
  @parser_class
end

#parser_ruleObject

Returns the value of attribute parser_rule


361
362
363
# File 'lib/antlr3/main.rb', line 361

def parser_rule
  @parser_rule
end

#portObject

Returns the value of attribute port


361
362
363
# File 'lib/antlr3/main.rb', line 361

def port
  @port
end

Instance Method Details

#present(return_value) ⇒ Object


457
458
459
460
461
462
463
464
465
466
467
468
469
# File 'lib/antlr3/main.rb', line 457

def present( return_value )
  ASTBuilder > @parser_class and return_value = return_value.tree
  if return_value
    text = 
      begin
        require 'pp'
        return_value.pretty_inspect
      rescue LoadError, NoMethodError
        return_value.inspect
      end
    puts( text )
  end
end

#recognize(in_stream) ⇒ Object


440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
# File 'lib/antlr3/main.rb', line 440

def recognize( in_stream )
  parser_options = {}
  if @debug
    if @trace
      parser_options[ :debug_listener ] = ANTLR3::Debug::RuleTracer.new
    else
      parser_options[ :port ] = @port
      parser_options[ :log ]  = @log
    end
  end
  lexer = @lexer_class.new( in_stream )
  # token_stream = CommonTokenStream.new( lexer )
  parser = @parser_class.new( lexer, parser_options )
  result = parser.send( @parser_rule ) and present( result )
  @profile and puts( parser.profile.generate_report )
end

#setupObject


422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
# File 'lib/antlr3/main.rb', line 422

def setup
  unless @lexer_class ||= fetch_class( @lexer_class_name )
    if @lexer_class_name
      fail( "unable to locate the lexer class ``#@lexer_class_name''" )
    else
      unless @lexer_class = @parser_class.associated_lexer
        fail( doc( <<-END ) )
        | no lexer class has been specified with the --lexer-name option
        | and #@parser_class does not appear to have an associated
        | lexer class
        END
      end
    end
  end
  @parser_rule ||= @parser_class.default_rule or
    fail( "a parser rule name must be specified via --rule NAME" )
end

#setup_options(opt) ⇒ Object


382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
# File 'lib/antlr3/main.rb', line 382

def setup_options( opt )
  super
  
  opt.separator ""
  opt.separator( "Parser Configuration:" )
  
  opt.on( '--lexer-name CLASS_NAME', "name of the lexer class to use" ) { |val|
    @lexer_class_name = val
    @lexer_class = nil
  }
  
  opt.on( '--lexer-file PATH_TO_LIBRARY', "path to library defining the lexer class" ) { |val|
    begin
      test( ?f, val ) ? load( val ) : require( val )
    rescue LoadError
      warn( "unable to load the library specified by --lexer-file: #{ $! }" )
    end
  }
  
  opt.on( '--rule NAME', "name of the parser rule to execute" ) { |val| @parser_rule = val }
  
  if @debug
    opt.separator ''
    opt.separator "Debug Mode Options:"
    
    opt.on( '--trace', '-t', "print rule trace instead of opening a debug socket" ) do
      @trace = true
    end
    
    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