Class: ANTLR3::Main::LexerMain

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 lexer file is run directly from the command line.

Instance Attribute Summary

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

Returns a new instance of LexerMain


313
314
315
316
# File 'lib/antlr3/main.rb', line 313

def initialize( lexer_class, options = {} )
  super( options )
  @lexer_class = lexer_class
end

Instance Method Details

#display_token(token) ⇒ Object


334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
# File 'lib/antlr3/main.rb', line 334

def display_token( token )
  case token.channel
  when ANTLR3::DEFAULT_CHANNEL
    prefix = '-->'
    suffix = ''
  when ANTLR3::HIDDEN_CHANNEL
    prefix = '#  '
    suffix = ' (hidden)'
  else
    prefix = '~~>'
    suffix = ' (channel %p)' % token.channel
  end
  
  printf( "%s %-15s %-15p @ line %-3i col %-3i%s\n",
         prefix, token.name, token.text,
         token.line, token.column, suffix )
end

#recognize(in_stream) ⇒ Object


318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
# File 'lib/antlr3/main.rb', line 318

def recognize( in_stream )
  lexer = @lexer_class.new( in_stream )
  
  loop do
    begin
      token = lexer.next_token
      if token.nil? || token.type == ANTLR3::EOF then break
      else display_token( token )
      end
    rescue ANTLR3::RecognitionError => error
      report_error( error )
      break
    end
  end
end