Class: ReVIEW::LineInput
Constant Summary collapse
- INVALID_CHARACTER_PATTERN =
accept 0x09: TAB, 0x0a: LF, 0x0d: CR
/[\x00-\x08\x0b-\x0c\x0e-\x1f]/
Instance Attribute Summary collapse
-
#lineno ⇒ Object
readonly
Returns the value of attribute lineno.
Class Method Summary collapse
-
.from_string(string) ⇒ Object
Create LineInput from a string directly.
Instance Method Summary collapse
- #each ⇒ Object
- #eof? ⇒ Boolean
- #gets ⇒ Object
-
#initialize(f) ⇒ LineInput
constructor
A new instance of LineInput.
- #inspect ⇒ Object
- #next? ⇒ Boolean
- #peek ⇒ Object
- #skip_blank_lines ⇒ Object
- #skip_comment_lines ⇒ Object
- #until_match(re) ⇒ Object
- #while_match(re) ⇒ Object
Constructor Details
#initialize(f) ⇒ LineInput
Returns a new instance of LineInput.
19 20 21 22 23 24 |
# File 'lib/review/lineinput.rb', line 19 def initialize(f) @input = f @buf = [] @lineno = 0 @eof_p = false end |
Instance Attribute Details
#lineno ⇒ Object (readonly)
Returns the value of attribute lineno.
17 18 19 |
# File 'lib/review/lineinput.rb', line 17 def lineno @lineno end |
Class Method Details
.from_string(string) ⇒ Object
Create LineInput from a string directly
27 28 29 |
# File 'lib/review/lineinput.rb', line 27 def self.from_string(string) new(StringIO.new(string)) end |
Instance Method Details
#each ⇒ Object
91 92 93 94 95 |
# File 'lib/review/lineinput.rb', line 91 def each while line = gets yield line end end |
#eof? ⇒ Boolean
35 36 37 |
# File 'lib/review/lineinput.rb', line 35 def eof? @eof_p end |
#gets ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/review/lineinput.rb', line 51 def gets unless @buf.empty? @lineno += 1 return @buf.pop end return nil if @eof_p # to avoid ARGF blocking. line = @input.gets @eof_p = true unless line @lineno += 1 invalid_char = lookup_invalid_char(line) if invalid_char raise SyntaxError, "found invalid control-sequence character (#{sprintf('%#x', invalid_char.codepoints[0])})." end line end |
#inspect ⇒ Object
31 32 33 |
# File 'lib/review/lineinput.rb', line 31 def inspect "#<#{self.class} file=#{@input.inspect} line=#{lineno}>" end |
#next? ⇒ Boolean
75 76 77 |
# File 'lib/review/lineinput.rb', line 75 def next? peek ? true : false end |
#peek ⇒ Object
69 70 71 72 73 |
# File 'lib/review/lineinput.rb', line 69 def peek line = gets ungets(line) if line line end |
#skip_blank_lines ⇒ Object
79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/review/lineinput.rb', line 79 def skip_blank_lines n = 0 while line = gets unless line.strip.empty? ungets(line) return n end n += 1 end n end |
#skip_comment_lines ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/review/lineinput.rb', line 39 def skip_comment_lines n = 0 while line = gets unless /\A\#@/.match?(line.strip) ungets(line) return n end n += 1 end n end |
#until_match(re) ⇒ Object
108 109 110 111 112 113 114 115 116 117 |
# File 'lib/review/lineinput.rb', line 108 def until_match(re) while line = gets if re&.match?(line) ungets(line) return end yield line end nil end |
#while_match(re) ⇒ Object
97 98 99 100 101 102 103 104 105 106 |
# File 'lib/review/lineinput.rb', line 97 def while_match(re) while line = gets unless re&.match?(line) ungets(line) return end yield line end nil end |