Class: Bijou::Parse::StringLexer

Inherits:
Lexer
  • Object
show all
Defined in:
lib/bijou/lexer.rb

Overview

The string lexer is used to switch the parser into a mode in which it can intelligently ignore (or process) Ruby strings, including strings which have embedded tag close sequences like ‘>’ and ‘%>’.

Defined Under Namespace

Classes: Type

Instance Attribute Summary

Attributes inherited from Lexer

#input

Instance Method Summary collapse

Methods inherited from Lexer

#column, #diagnostic, #error, #errors, #getc, #is_token_buffered, #line, #peek_token, #pop, #pop_token, #prev_token, #set_string_token, #shift_token, #text, #token, #ungetc, #warning, #warnings

Constructor Details

#initialize(input) ⇒ StringLexer

Returns a new instance of StringLexer.



446
447
448
449
# File 'lib/bijou/lexer.rb', line 446

def initialize(input)
  super(input)
  @string = ""
end

Instance Method Details

#next_tokenObject



485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
# File 'lib/bijou/lexer.rb', line 485

def next_token()
  if is_token_buffered
    return shift_token
  end

  while ch0 = getc
    ch = ch0.chr

    if ch == "\\"
      push_token(Token::Char, ch)
      ch1 = getc
      return push_token(Token::Char, ch1.chr)
    elsif ch == '"'
      if @type == Type::Double
        return push_token(Token::String, ch)
      end
    elsif ch == "'"
      if @type == Type::Single
        return push_token(Token::String, ch)
      end
    end
    return push_token(Token::Char, ch)
  end
  nil
end

#push_token(token, text) ⇒ Object



477
478
479
480
481
482
483
# File 'lib/bijou/lexer.rb', line 477

def push_token(token, text)
  if token == Token::Char
#      print "(#{token}, #{text})"
    @string << text
  end
  super(token, text)
end

#raw_stringObject



469
470
471
472
473
474
475
# File 'lib/bijou/lexer.rb', line 469

def raw_string
  if type == StringLexer::Type::Double
    "\"#{@string}\""
  else
    "'#{@string}'"
  end
end

#typeObject



461
462
463
# File 'lib/bijou/lexer.rb', line 461

def type
  @type
end

#type=(t) ⇒ Object



456
457
458
459
# File 'lib/bijou/lexer.rb', line 456

def type=(t)
  @type = t
  @string = '' # reinitialize
end

#unquoted_stringObject



465
466
467
# File 'lib/bijou/lexer.rb', line 465

def unquoted_string
  @string
end