Class: Baba::Scanner
- Inherits:
-
Object
- Object
- Baba::Scanner
- Defined in:
- lib/baba/scanner.rb
Instance Attribute Summary collapse
-
#source ⇒ Object
readonly
Returns the value of attribute source.
-
#tokens ⇒ Object
readonly
Returns the value of attribute tokens.
Instance Method Summary collapse
- #add_token(type, literal = nil) ⇒ Object
- #advance ⇒ Object
- #alpha?(c) ⇒ Boolean
- #alphanumeric?(c) ⇒ Boolean
- #digit?(c) ⇒ Boolean
- #eof? ⇒ Boolean
- #error(c) ⇒ Object
- #identifier ⇒ Object
-
#initialize(source) ⇒ Scanner
constructor
A new instance of Scanner.
- #match(expected) ⇒ Object
- #number ⇒ Object
- #peek ⇒ Object
- #peek_next ⇒ Object
- #scan_token ⇒ Object
- #scan_tokens ⇒ Object
- #string ⇒ Object
Constructor Details
#initialize(source) ⇒ Scanner
Returns a new instance of Scanner.
9 10 11 12 13 14 15 16 |
# File 'lib/baba/scanner.rb', line 9 def initialize(source) @source = source.strip @tokens = [] @line = 1 @current = 0 @start = 0 end |
Instance Attribute Details
#source ⇒ Object (readonly)
Returns the value of attribute source.
7 8 9 |
# File 'lib/baba/scanner.rb', line 7 def source @source end |
#tokens ⇒ Object (readonly)
Returns the value of attribute tokens.
7 8 9 |
# File 'lib/baba/scanner.rb', line 7 def tokens @tokens end |
Instance Method Details
#add_token(type, literal = nil) ⇒ Object
33 34 35 36 |
# File 'lib/baba/scanner.rb', line 33 def add_token(type, literal = nil) text = @source[@start...@current] @tokens << Token.new(type, text, literal, @line) end |
#advance ⇒ Object
38 39 40 41 |
# File 'lib/baba/scanner.rb', line 38 def advance @current += 1 return @source[@current - 1] end |
#alpha?(c) ⇒ Boolean
65 66 67 |
# File 'lib/baba/scanner.rb', line 65 def alpha?(c) !!(c =~ /([a-z]|[A-Z]|_)/) end |
#alphanumeric?(c) ⇒ Boolean
69 70 71 |
# File 'lib/baba/scanner.rb', line 69 def alphanumeric?(c) alpha?(c) || digit?(c) end |
#digit?(c) ⇒ Boolean
61 62 63 |
# File 'lib/baba/scanner.rb', line 61 def digit?(c) !!(c =~ /\d/) end |
#eof? ⇒ Boolean
18 19 20 |
# File 'lib/baba/scanner.rb', line 18 def eof? @current >= @source.length end |
#error(c) ⇒ Object
144 145 146 |
# File 'lib/baba/scanner.rb', line 144 def error(c) Baba.scanner_error(@line, "Unexpected character '#{c}'", @source, @current) end |
#identifier ⇒ Object
148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/baba/scanner.rb', line 148 def identifier() while alphanumeric?(peek()) advance() end text = @source[@start...@current] type = KEYWORDS[text] type = IDENTIFIER if type.nil? add_token(type) end |
#match(expected) ⇒ Object
43 44 45 46 47 48 49 |
# File 'lib/baba/scanner.rb', line 43 def match(expected) return false if eof? return false if @source[@current] != expected @current += 1 return true end |
#number ⇒ Object
176 177 178 179 180 181 182 183 184 185 186 187 188 189 |
# File 'lib/baba/scanner.rb', line 176 def number() while digit?(peek()) advance() end if peek() == "." && digit?(peek_next()) advance() while digit?(peek()) advance() end end add_token(NUMBER, @source[@start...@current].to_f) end |
#peek ⇒ Object
51 52 53 54 |
# File 'lib/baba/scanner.rb', line 51 def peek() return "\0" if eof? return source[@current] end |
#peek_next ⇒ Object
56 57 58 59 |
# File 'lib/baba/scanner.rb', line 56 def peek_next() return "\0" if (@current + 1 >= @source.length) @source[@current + 1] end |
#scan_token ⇒ Object
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 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/baba/scanner.rb', line 73 def scan_token c = advance() case c when "(" add_token(LEFT_PAREN) when ")" add_token(RIGHT_PAREN) when "," add_token(COMMA) when "-" add_token(MINUS) when "+" add_token(PLUS) when "*" add_token(STAR) when "/" add_token(SLASH) when ";" add_token(SEMICOLON) when ":" add_token(COLON) when "{" add_token(COLON) when "}" add_token(KEND) when "." add_token(DOT) when "!" add_token(match("=") ? NOT_EQUAL : NOT) when "<" add_token(match("=") ? LESS_EQUAL : LESS) when ">" add_token(match("=") ? GREATER_EQUAL : GREATER) when "=" add_token(match("=") ? EQUAL_EQUAL : EQUAL) when "&" if match("&") add_token(AND) else error(c) end when "|" if match("|") add_token(OR) else error(c) end when "#" while peek() != "\n" && !eof? advance() end when " " when "\r" @line += 1 when "\t" @line += 1 when "\n" @line += 1 when '"' string() else if digit?(c) number() elsif alpha?(c) identifier() else error(c) end end end |
#scan_tokens ⇒ Object
22 23 24 25 26 27 28 29 30 31 |
# File 'lib/baba/scanner.rb', line 22 def scan_tokens until eof? # We are at the next lexeme @start = @current scan_token() end add_token(EOF) return @tokens end |
#string ⇒ Object
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 |
# File 'lib/baba/scanner.rb', line 160 def string() while peek() != '"' && !eof? @line += 1 if peek() == '\n' advance() end if eof? Baba.scanner_error(@line, "Unterminated string", @source, @start) end advance() value = @source[(@start + 1)..(@current - 2)] add_token(STRING, value) end |