Class: Nasl::Tokenizer
- Inherits:
-
Object
- Object
- Nasl::Tokenizer
- Defined in:
- lib/nasl/tokenizer.rb
Constant Summary collapse
- @@initialized =
false
- @@keywords =
{ 'break' => :BREAK, 'continue' => :CONTINUE, 'else' => :ELSE, 'export' => :EXPORT, 'for' => :FOR, 'foreach' => :FOREACH, 'function' => :FUNCTION, 'global_var' => :GLOBAL, 'if' => :IF, 'import' => :IMPORT, 'include' => :INCLUDE, 'local_var' => :LOCAL, 'repeat' => :REPEAT, 'return' => :RETURN, 'until' => :UNTIL, 'x' => :REP, 'while' => :WHILE, 'FALSE' => :FALSE, 'NULL' => :UNDEF, 'TRUE' => :TRUE }
- @@operators =
[ ["><", :SUBSTR_EQ], [">!<", :SUBSTR_NE], ["=~", :REGEX_EQ], ["!~", :REGEX_NE], ["==", :CMP_EQ], ["!=", :CMP_NE], ["<=", :CMP_LE], [">=", :CMP_GE], ["=", :ASS_EQ], ["+=", :ADD_EQ], ["-=", :SUB_EQ], ["*=", :MUL_EQ], ["/=", :DIV_EQ], ["%=", :MOD_EQ], [">>=", :SRL_EQ], [">>>=", :SRA_EQ], ["<<=", :SLL_EQ], ["||", :OR], ["&&", :AND], ["!", :NOT], ["|", :BIT_OR], ["^", :BIT_XOR], ["&", :BIT_AND], [">>>", :BIT_SRA], [">>", :BIT_SRL], ["<<", :BIT_SLL], ["<", :CMP_LT], [">", :CMP_GT], ["++", :INCR], ["--", :DECR], ["**", :EXP], ["+", :ADD], ["-", :SUB], ["*", :MUL], ["/", :DIV], ["%", :MOD], ["~", :BIT_NOT], [".", :PERIOD], [",", :COMMA], [":", :COLON], [";", :SEMICOLON], ["(", :LPAREN], [")", :RPAREN], ["[", :LBRACK], ["]", :RBRACK], ["{", :LBRACE], ["}", :RBRACE] ]
- @@annotated =
[ :EXPORT, :FUNCTION, :GLOBAL ]
Instance Method Summary collapse
- #consume(num = 1) ⇒ Object
- #die(msg) ⇒ Object
- #get_comment ⇒ Object
- #get_identifier ⇒ Object
- #get_integer ⇒ Object
- #get_operator ⇒ Object
- #get_string ⇒ Object
- #get_token ⇒ Object
- #get_tokens ⇒ Object
-
#initialize(code, path) ⇒ Tokenizer
constructor
A new instance of Tokenizer.
- #initialize! ⇒ Object
- #reset ⇒ Object
- #skip ⇒ Object
Constructor Details
#initialize(code, path) ⇒ Tokenizer
Returns a new instance of Tokenizer.
136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/nasl/tokenizer.rb', line 136 def initialize(code, path) @code = code # Perform one-time initialization of tokenizer data structures. initialize! # Create a context object that will be shared amongst all tokens for this # code. @ctx = Context.new(@code, path) reset end |
Instance Method Details
#consume(num = 1) ⇒ Object
149 150 151 152 153 154 155 156 157 158 159 160 161 |
# File 'lib/nasl/tokenizer.rb', line 149 def consume(num=1) # Update the index of the character we're currently looking at. @point += num # Update the flag that indicates whether we've reached the file's end. @eof = (@point >= @code.length) # Update the the character we're examining currently. @char = @code[@point] # Extract the remainder of the line. @line = @code[@point..@ctx.eol(@point)] end |
#die(msg) ⇒ Object
187 188 189 190 191 192 193 194 195 |
# File 'lib/nasl/tokenizer.rb', line 187 def die(msg) # We want the default context for token errors to be all lines that # contain the region. region = @ctx.bol(@mark)..@ctx.eol(@point) bt = @ctx.context(@mark..@point + 1, region) # Raise an exception with the context as our backtrace. raise TokenException, msg, bt end |
#get_comment ⇒ Object
289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 |
# File 'lib/nasl/tokenizer.rb', line 289 def get_comment # Remember the column the comment begins in. col = @ctx.col(@point) # Consume all of the comments in the block. block = [] begin prev = @ctx.row(@point) comment = @line[/^#.*$/] break if comment.nil? block << comment consume(comment.length) skip cur = @ctx.row(@point) end while @ctx.col(@point) == col && cur == prev + 1 return [:COMMENT, block.join("\n")] end |
#get_identifier ⇒ Object
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 |
# File 'lib/nasl/tokenizer.rb', line 197 def get_identifier # Identifiers are composed of letters, digits, and underscores. ident = @line[/^[_a-z][_a-z0-9]*/i] consume(ident.length) # Assume that we've got an identifier until proven otherwise. type = :IDENT # Identifiers may be prefixed with keywords. One example of a valid # identifier is "break_". To ensure that we catch these cases, we # initially parse all keywords as identifiers and then convert them as # needed. type = @@keywords[ident] if @@keywords.has_key? ident return [type, ident] end |
#get_integer ⇒ Object
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 |
# File 'lib/nasl/tokenizer.rb', line 214 def get_integer # Try and parse the integer in any of three bases. if @line =~ /^0x/i # Hex integers start with "0x". type = :INT_HEX name = "hex" regex1 = /^0x\w+/i regex2 = /^0x[a-f0-9]+/i elsif @line =~ /^0\w+/ # Octal integers start with "0". type = :INT_OCT name = "octal" regex1 = /^0\w+/ regex2 = /^0[0-7]+/ else # Anything else is a decimal integer. type = :INT_DEC name = "decimal" regex1 = /^\w*/ regex2 = /^[0-9]+/ end # First match with an overly permissive regex, and then match with the # proper regex. If the permissive and restrictive versions don't match, # then there's an error in the input. permissive = @line[regex1] restrictive = @line[regex2] if permissive.nil? || restrictive.nil? || permissive != restrictive # NASL interprets integers with a leading zero as octal if the only # contain octal digits, and considers the integers as decimal otherwise. type = :INT_DEC regex2 = /^[0-9]+/ restrictive = @line[regex2] end if permissive.nil? || restrictive.nil? || permissive != restrictive die("Invalid #{name} literal") end # If there was no problem, we use the restrictive version as the body of # our integer. integer = restrictive consume(integer.length) return [type, integer] end |
#get_operator ⇒ Object
308 309 310 311 312 313 314 315 316 317 318 |
# File 'lib/nasl/tokenizer.rb', line 308 def get_operator # These are all of the operators defined in NASL. Their order is vitally # important. @@operators.each do |regex, op, type| next if @line !~ regex consume(op.length) return [type, op] end return nil end |
#get_string ⇒ Object
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 |
# File 'lib/nasl/tokenizer.rb', line 263 def get_string unparsed = @code[@point..-1] if @char == "'" type = :DATA # Single-quoted strings cannot have single-quotes stuffed inside them. contents = unparsed[/\A'(\\.|[^'\\])*'/m] die("Unterminated single-quoted string") if contents.nil? else type = :STRING # Double-quoted strings cannot have double quotes stuffed inside them. contents = unparsed[/\A"[^"]*"/m] die("Unterminated double-quoted string") if contents.nil? end # Move the point forward over the string. consume(contents.length) # Remove the bounding quotes. contents = contents[1..-2] return [type, contents] end |
#get_token ⇒ Object
320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 |
# File 'lib/nasl/tokenizer.rb', line 320 def get_token # If we deferred a token, emit it now. unless @deferred.nil? token = @deferred @deferred = nil return token end # Make sure we're not at the end of the file. return [false, Token.new(:EOF, "$", @point...@point, @ctx)] if @eof # Save our starting point, which to use Emacs terminology is called the # 'mark'. @mark = @point # Try to parse token at the point. token = if @char =~ /[_a-z]/i get_identifier elsif @char =~ /['"]/ get_string elsif @char =~ /[0-9]/ get_integer elsif @char == '#' get_comment else get_operator end # Everything in the language is enumerated by the above functions, so if # we get here without a token parsed, the input file is invalid. die("Invalid character ('#@char')") if token.nil? # Consume all whitespace after the token, and create an object with # context. skip token = [token.first, Token.new(*token, @mark...@point, @ctx)] # If a comment is the first token in a file, or is followed by certain # tokens, then it is considered significant. Such tokens will appear in # the grammar so that it can be made visible to nasldoc. if token.first == :COMMENT if @previous.nil? @previous = [:DUMMY, ""] else @previous = token token = get_token end elsif !@previous.nil? && @previous.first == :COMMENT && @@annotated.include?(token.first) @deferred = token token = @previous @previous = @deferred else @previous = token end return token end |
#get_tokens ⇒ Object
378 379 380 381 382 383 384 385 386 |
# File 'lib/nasl/tokenizer.rb', line 378 def get_tokens tokens = [] begin tokens << get_token end while not tokens.last.last.type == :EOF return tokens end |
#initialize! ⇒ Object
125 126 127 128 129 130 131 132 133 134 |
# File 'lib/nasl/tokenizer.rb', line 125 def initialize! return if @@initialized # Convert the operators into a regex-compatible form. @@operators = @@operators.map do |op, type| [Regexp.new("^#{Regexp.escape(op)}"), op, type] end @@initialized = true end |
#reset ⇒ Object
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 |
# File 'lib/nasl/tokenizer.rb', line 163 def reset # We need to remember the last token so we only emit comments significant # to nasldoc. @previous = nil @deferred = nil # Set tokenizer to initial state, ready to tokenize the code from the # start. @point = 0 consume(0) skip # Return tokenizer to allow method chaining. self end |
#skip ⇒ Object
179 180 181 182 183 184 185 |
# File 'lib/nasl/tokenizer.rb', line 179 def skip while true do whitespace = @line[/^\s+/] return if whitespace.nil? consume(whitespace.length) end end |