class AdLint::StringContent

DESCRIPTION

Object represents the whole content of the string.

Public Class Methods

new(str, tab_width = 8, fpath = nil, line_no = 1, column_no = 1) click to toggle source

DESCRIPTION

Constructs the content object of the string.

PARAMETER

str

String – Target string.

fpath

Pathname – File path name contains the target string.

line_no

Integer – Initial line-no of the target string.

# File lib/adlint/lexer.rb, line 84
def initialize(str, tab_width = 8, fpath = nil, line_no = 1, column_no = 1)
  @scanner = StringScanner.new(str)
  @tab_width = tab_width
  @fpath, @line_no, @column_no = fpath, line_no, column_no
  @appearance_column_no = column_no
end

Public Instance Methods

_debug_inspect() click to toggle source
# File lib/adlint/lexer.rb, line 139
def _debug_inspect
  @scanner.rest
end
check(regexp) click to toggle source
# File lib/adlint/lexer.rb, line 114
def check(regexp)
  @scanner.check(regexp)
end
eat!(len = 1) click to toggle source

DESCRIPTION

Skips a content.

PARAMETER

len

Integer – Skipping content length.

RETURN VALUE

String – Eaten string.

# File lib/adlint/lexer.rb, line 126
def eat!(len = 1)
  self.scan(/.{#{len}}/m)
end
empty?() click to toggle source

DESCRIPTION

Checks whether this content is currently empty or not.

RETURN VALUE

Boolean – Returns true if already empty.

# File lib/adlint/lexer.rb, line 135
def empty?
  @scanner.eos?
end
location() click to toggle source
# File lib/adlint/lexer.rb, line 91
def location
  Location.new(@fpath, @line_no, @column_no, @appearance_column_no)
end
scan(regexp) click to toggle source

DESCRIPTION

Scans a token.

PARAMETER

regexp

Regexp – Token pattern.

RETURN VALUE

StringToken string or nil.

# File lib/adlint/lexer.rb, line 104
def scan(regexp)
  tok = @scanner.scan(regexp)
  if tok
    update_location(tok)
    tok
  else
    nil
  end
end

Private Instance Methods

update_location(tok) click to toggle source
# File lib/adlint/lexer.rb, line 144
def update_location(tok)
  if (nl_cnt = tok.count("\n")) > 0
    @line_no += nl_cnt
    lst_line = tok[tok.rindex("\n")..-1]
    @column_no = lst_line.length
    @appearance_column_no = lst_line.gsub(/\t/, " " * @tab_width).length
  else
    @column_no += tok.length
    @appearance_column_no += tok.gsub(/\t/, " " * @tab_width).length
  end
  forget_memo_of__location
end