Class: Rucc::FileIO

Inherits:
Object
  • Object
show all
Defined in:
lib/rucc/file_io.rb

Constant Summary collapse

EOF =
nil

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(io, name) ⇒ FileIO

Returns a new instance of FileIO.

Parameters:

  • io (IO)
  • name (String)


7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/rucc/file_io.rb', line 7

def initialize(io, name)
  @io = io
  @name = name
  @line = 1
  @column = 1
  @ntok = 0  # Token counter
  @buflen = 0
  @buf = Array.new(3)
  @last = Object.new  # NOTE: Not use nil because nil means EOF. Set dummy data at first.

  # TODO(south37) Set @mtime
  # struct stat st;
  # if (fstat(fileno(file), &st) == -1)
  #     error("fstat failed: %s", strerror(errno));
  # r->mtime = st.st_mtime;
end

Instance Attribute Details

#columnObject (readonly)

TODO(south37) Set @mtime struct stat st; if (fstat(fileno(file), &st) == -1)

error("fstat failed: %s", strerror(errno));

r->mtime = st.st_mtime;



23
24
25
# File 'lib/rucc/file_io.rb', line 23

def column
  @column
end

#lineObject

TODO(south37) Set @mtime struct stat st; if (fstat(fileno(file), &st) == -1)

error("fstat failed: %s", strerror(errno));

r->mtime = st.st_mtime;



23
24
25
# File 'lib/rucc/file_io.rb', line 23

def line
  @line
end

#nameObject

TODO(south37) Set @mtime struct stat st; if (fstat(fileno(file), &st) == -1)

error("fstat failed: %s", strerror(errno));

r->mtime = st.st_mtime;



23
24
25
# File 'lib/rucc/file_io.rb', line 23

def name
  @name
end

#ntokObject (readonly)

TODO(south37) Set @mtime struct stat st; if (fstat(fileno(file), &st) == -1)

error("fstat failed: %s", strerror(errno));

r->mtime = st.st_mtime;



23
24
25
# File 'lib/rucc/file_io.rb', line 23

def ntok
  @ntok
end

Instance Method Details

#closeObject



63
64
65
# File 'lib/rucc/file_io.rb', line 63

def close
  @io.close
end

#incr_ntok!Object



59
60
61
# File 'lib/rucc/file_io.rb', line 59

def incr_ntok!
  @ntok += 1
end

#mtimeObject



67
68
69
70
# File 'lib/rucc/file_io.rb', line 67

def mtime
  Util.assert!{ @io.is_a?(File) }
  File.mtime(@io)
end

#readcChar, NilClass

Returns nil at EOF.

Returns:

  • (Char, NilClass)

    nil at EOF



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/rucc/file_io.rb', line 27

def readc
  while true do
    c = get
    if c == EOF
      return c
    end
    if (c != "\\")
      return c
    end
    c2 = get
    if c2 == "\n"
      next
    end
    unreadc(c2)
    return c
  end
end

#unreadc(c) ⇒ Object

Parameters:

  • c (Char)


46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/rucc/file_io.rb', line 46

def unreadc(c)
  return if c == EOF
  Util.assert!{ @buflen < @buf.size }
  @buf[@buflen] = c
  @buflen += 1
  if c == "\n"
    @column = 1
    @line -= 1
  else
    @column -= 1
  end
end