Class: Lexer
- Inherits:
-
Object
- Object
- Lexer
- Defined in:
- lib/xmk/lexer.rb
Instance Method Summary collapse
- #advance(n = 1) ⇒ Object
- #consume ⇒ Object
- #end_line ⇒ Object
- #find(c) ⇒ Object
- #get_value(t) ⇒ Object
- #handle_comment_backslash(c) ⇒ Object
- #handle_label ⇒ Object
-
#initialize ⇒ Lexer
constructor
A new instance of Lexer.
- #peek ⇒ Object
- #push_back(c) ⇒ Object
- #scan ⇒ Object
- #terminates?(c) ⇒ Boolean
Constructor Details
#initialize ⇒ Lexer
Returns a new instance of Lexer.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
# File 'lib/xmk/lexer.rb', line 3 def initialize @tokens = Array.new @start = 0 @current = -1 @line = 0 @string = "" @file = nil @in_label = false @assignment_type = "" if File.exists?("xmkfile") @file = File.open("xmkfile", "r") else puts "No xmkfile found" end end |
Instance Method Details
#advance(n = 1) ⇒ Object
176 177 178 179 180 181 182 183 184 185 186 187 |
# File 'lib/xmk/lexer.rb', line 176 def advance(n = 1) c = "" i = n @current += n loop do c = self.consume @string << c if c i -= 1 break if i == 0 end c end |
#consume ⇒ Object
195 196 197 198 199 200 |
# File 'lib/xmk/lexer.rb', line 195 def consume if self.terminates?(@string[@current]) @line += 1 end @file.getc end |
#end_line ⇒ Object
216 217 218 219 220 221 222 |
# File 'lib/xmk/lexer.rb', line 216 def end_line # strips away characters until line break loop do c = self.advance break if self.terminates?(c) end end |
#find(c) ⇒ Object
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/xmk/lexer.rb', line 100 def find(c) # check if char occurs before comment or newline res = false i = 0 backup = Array.new loop do d = self.consume backup.push(d) i += 1 res = true if d == c break if d == "#" break if self.terminates?(d) end i.times do |j| self.push_back(backup[i-j-1]) end res end |
#get_value(t) ⇒ Object
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/xmk/lexer.rb', line 119 def get_value(t) if self.terminates?(@string[@current]) return @tokens.push(Token.new(Token::LBR, "", @line)) end @start = @current loop do c = self.advance c = self.handle_comment_backslash(c) break if self.terminates?(c) end str = @string[@start .. @current-1].rstrip if t == Token::VAL && @assignment_type == "assign" str = str.lstrip end @tokens.push(Token.new(t, str, @line)) end |
#handle_comment_backslash(c) ⇒ Object
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 |
# File 'lib/xmk/lexer.rb', line 147 def handle_comment_backslash(c) if c == "\\" loop do if self.terminates?(self.peek) @string[@current] = self.consume break else @string[@current] = self.consume end end while self.terminates?(@string[@current]) @string[@current] = self.consume end while @string[@current] == " " @string[@current] = self.consume end elsif c == "#" loop do @string[@current] = self.consume if self.terminates?(self.peek) @string[@current] = self.consume c = @string[@current] break end end end c end |
#handle_label ⇒ Object
136 137 138 139 140 141 142 143 144 145 |
# File 'lib/xmk/lexer.rb', line 136 def handle_label @in_label = true loop do self.advance break if self.peek == ":" end @tokens.push(Token.new(Token::LAB, @string[@start .. @current], @line)) self.end_line end |
#peek ⇒ Object
189 190 191 192 193 |
# File 'lib/xmk/lexer.rb', line 189 def peek c = self.consume self.push_back(c) c end |
#push_back(c) ⇒ Object
202 203 204 205 206 207 |
# File 'lib/xmk/lexer.rb', line 202 def push_back(c) if self.terminates?(c) @line -= 1 end @file.ungetc(c) end |
#scan ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/xmk/lexer.rb', line 20 def scan while (c = self.advance) != nil @start = @current case c when "#" # comment self.end_line when "+" # append/declaration if self.peek == "=" @assignment_type = "append" @tokens.push(Token.new(Token::ADD, '+=', @line)) self.advance(2) self.get_value(Token::VAL) end when "=" # declaration @assignment_type = "assign" @tokens.push(Token.new(Token::SET, '=', @line)) self.advance self.get_value(Token::VAL) when "@" # target loop do c = self.advance c = self.handle_comment_backslash(c) break if self.terminates?(c) end @tokens.push(Token.new(Token::TAR, @string[@start+1 .. @current].strip, @line)) when "$" # cmd self.get_value(Token::CMD) when "_" # label self.handle_label else if self.terminates?(c) @in_label = false @tokens.push(Token.new(Token::LBR, c, @line)) elsif c.match?(/[A-Z]/) # var loop do self.advance break unless self.peek.match?(/[A-Z_]/) end @tokens.push(Token.new( Token::VAR, @string[@start .. @current].strip, @line)) elsif c.match?(/[a-z]/) # cmd / label / platform if @in_label # cmd self.get_value(Token::CMD) else # label || platform if self.find("@") # platform loop do self.advance break if self.peek == ":" end @tokens.push(Token.new(Token::PLAT, @string[@start .. @current], @line)) parse_target = false loop do c = self.advance if c == "@" self.advance parse_target = true @start = @current end if (c == " " || self.terminates?(c)) && parse_target parse_target = false @tokens.push(Token.new(Token::TARLINK, @string[@start .. @current-1], @line)) end break if self.terminates?(c) end else self.handle_label end end end end end @file.close @tokens.push(Token.new(Token::LBR, "", @line)) @tokens.push(Token.new(Token::EOF, "end_of_file", @line)) @tokens end |
#terminates?(c) ⇒ Boolean
209 210 211 212 213 214 |
# File 'lib/xmk/lexer.rb', line 209 def terminates?(c) return true if c == "\n" return true if c.nil? return true if c == "\r" return false end |