Class: LanguageParser::CT_NormalState

Inherits:
CT_State
  • Object
show all
Defined in:
lib/cgialib/lp/CTokenizer.rb

Overview

class : CT_NormalState

The default state machine to which all of the other states return.

Instance Method Summary collapse

Constructor Details

#initialize(newstate, addtoken) ⇒ CT_NormalState

initialize( newstate, addtoken )

newstate - A method to be called to change state addtoken - The method to be called to add a token



334
335
336
337
338
339
340
341
342
343
344
# File 'lib/cgialib/lp/CTokenizer.rb', line 334

def initialize( newstate, addtoken )
  
  super( newstate, addtoken )
  
  # This normal state handles adding CodeTokens in the 
  # basic stream (e.g. not in a string). So we have a
  # text buffer.
  
  @text = ""
  
end

Instance Method Details

#next(ch) ⇒ Object

next( ch )

ch - The character

Handles the character in the parsing stream



352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
# File 'lib/cgialib/lp/CTokenizer.rb', line 352

def next( ch )
  
  if @@specials[ch]
  
    # If this is a special character (e.g. ;,*,+, etc.)
    # then dump the current token and add the special  
    # characer token
  
    @addtoken.call( CodeToken.new( @text ) )
    @text = ""
  
    @addtoken.call( CodeToken.new( ch ) )
  
  elsif ch == "\""
  
    # Start the double quote state if we see a
    # double quote
  
    @addtoken.call( CodeToken.new( @text ) )
    @newstate.call( CT_DoubleQuote )
  
  elsif ch == "/"
  
    # Start the comment switcher state if we 
    # see a slash
  
    @addtoken.call( CodeToken.new( @text ) )
    @newstate.call( CT_WaitingForComment )
  
  elsif ch =~ /\s/

    # Move into the whitespace state if we 
    # see whitespace.  Return true to re-run
    # the parser on this character.
  
    @addtoken.call( CodeToken.new( @text ) )
    @newstate.call( CT_WhitespaceTokenizer )
    return false
  
  else
  
    # Otherwise add this character to the buffer
  
    @text += ch
  
  end
  
  # Continue onto the next character
  
  true
  
end