Class: Opal::Lexer
Overview
Lexer is used by Parser to step through ruby code, and returning tokens representing each chunk of ruby code.
Tokens are in the form:
[token, [value, location]]
where location
is in the form [line_number, column_number]
. The location
data can be used to produce source maps in the compiler. Tokens are
generally ruby symbols, and the value will always be a string value.
The main method used by the parser is #next_token, which is called
repeatedly until a token of value false
is returned, which indicated the
EOF has been reached.
Generally this class is only used by Parser directly.
Constant Summary
- STR_FUNC_ESCAPE =
0x01
- STR_FUNC_EXPAND =
0x02
- STR_FUNC_REGEXP =
0x04
- STR_FUNC_QWORDS =
0x08
- STR_FUNC_SYMBOL =
0x10
- STR_FUNC_INDENT =
0x20
- STR_FUNC_XQUOTE =
0x40
- STR_SQUOTE =
0x00
- STR_DQUOTE =
STR_FUNC_EXPAND
- STR_XQUOTE =
STR_FUNC_EXPAND | STR_FUNC_XQUOTE
- STR_REGEXP =
STR_FUNC_REGEXP | STR_FUNC_ESCAPE | STR_FUNC_EXPAND
- STR_SWORD =
STR_FUNC_QWORDS
- STR_DWORD =
STR_FUNC_QWORDS | STR_FUNC_EXPAND
- STR_SSYM =
STR_FUNC_SYMBOL
- STR_DSYM =
STR_FUNC_SYMBOL | STR_FUNC_EXPAND
Instance Attribute Summary collapse
-
#column ⇒ Object
readonly
Returns the value of attribute column.
-
#eof_content ⇒ Object
readonly
Returns the value of attribute eof_content.
-
#lex_state ⇒ Object
Returns the value of attribute lex_state.
-
#line ⇒ Object
Returns the value of attribute line.
-
#parser ⇒ Object
Returns the value of attribute parser.
-
#scanner ⇒ Object
Returns the value of attribute scanner.
-
#scope ⇒ Object
readonly
Returns the value of attribute scope.
-
#strterm ⇒ Object
Returns the value of attribute strterm.
-
#yylval ⇒ Object
Returns the value of attribute yylval.
Instance Method Summary collapse
- #add_string_content(str_buffer, str_parse) ⇒ Object
- #after_operator? ⇒ Boolean
- #arg? ⇒ Boolean
- #beg? ⇒ Boolean
- #check(regexp) ⇒ Object
- #cmdarg? ⇒ Boolean
- #cmdarg_lexpop ⇒ Object
- #cmdarg_pop ⇒ Object
- #cmdarg_push(n) ⇒ Object
- #cond? ⇒ Boolean
- #cond_lexpop ⇒ Object
- #cond_pop ⇒ Object
- #cond_push(n) ⇒ Object
- #end? ⇒ Boolean
- #has_local?(local) ⇒ Boolean
- #here_document(str_parse) ⇒ Object
- #heredoc_identifier ⇒ Object
-
#initialize(source, file) ⇒ Lexer
constructor
Create a new instance using the given ruby code and filename for reference.
- #label_state? ⇒ Boolean
- #matched ⇒ Object
- #new_op_asgn(value) ⇒ Object
- #new_strterm(func, term, paren) ⇒ Object
- #new_strterm2(func, term, paren) ⇒ Object
-
#next_token ⇒ Array
Returns next token from source input stream.
- #parse_string ⇒ Object
- #peek_variable_name ⇒ Object
- #process_identifier(matched, cmd_start) ⇒ Object
- #process_numeric ⇒ Object
- #pushback(n) ⇒ Object
- #read_escape ⇒ Object
- #scan(regexp) ⇒ Object
- #set_arg_state ⇒ Object
- #skip(regexp) ⇒ Object
- #space? ⇒ Boolean
- #spcarg? ⇒ Boolean
-
#yylex ⇒ Object
Does the heavy lifting for
next_token
.
Constructor Details
#initialize(source, file) ⇒ Lexer
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'opal/lib/opal/parser/lexer.rb', line 58 def initialize(source, file) @lex_state = :expr_beg @cond = 0 @cmdarg = 0 @line = 1 @tok_line = 1 @column = 0 @tok_column = 0 @file = file @scanner = StringScanner.new(source) @scanner_stack = [@scanner] @case_stmt = nil @start_of_lambda = nil end |
Instance Attribute Details
#column ⇒ Object (readonly)
Returns the value of attribute column
41 42 43 |
# File 'opal/lib/opal/parser/lexer.rb', line 41 def column @column end |
#eof_content ⇒ Object (readonly)
Returns the value of attribute eof_content
43 44 45 |
# File 'opal/lib/opal/parser/lexer.rb', line 43 def eof_content @eof_content end |
#lex_state ⇒ Object
Returns the value of attribute lex_state
45 46 47 |
# File 'opal/lib/opal/parser/lexer.rb', line 45 def lex_state @lex_state end |
#line ⇒ Object
Returns the value of attribute line
41 42 43 |
# File 'opal/lib/opal/parser/lexer.rb', line 41 def line @line end |
#parser ⇒ Object
Returns the value of attribute parser
49 50 51 |
# File 'opal/lib/opal/parser/lexer.rb', line 49 def parser @parser end |
#scanner ⇒ Object
Returns the value of attribute scanner
47 48 49 |
# File 'opal/lib/opal/parser/lexer.rb', line 47 def scanner @scanner end |
#scope ⇒ Object (readonly)
Returns the value of attribute scope
42 43 44 |
# File 'opal/lib/opal/parser/lexer.rb', line 42 def scope @scope end |
#strterm ⇒ Object
Returns the value of attribute strterm
46 47 48 |
# File 'opal/lib/opal/parser/lexer.rb', line 46 def strterm @strterm end |
#yylval ⇒ Object
Returns the value of attribute yylval
48 49 50 |
# File 'opal/lib/opal/parser/lexer.rb', line 48 def yylval @yylval end |
Instance Method Details
#add_string_content(str_buffer, str_parse) ⇒ Object
422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 |
# File 'opal/lib/opal/parser/lexer.rb', line 422 def add_string_content(str_buffer, str_parse) func = str_parse[:func] end_str_re = Regexp.new(Regexp.escape(str_parse[:term])) qwords = (func & STR_FUNC_QWORDS) != 0 = (func & STR_FUNC_EXPAND) != 0 regexp = (func & STR_FUNC_REGEXP) != 0 escape = (func & STR_FUNC_ESCAPE) != 0 xquote = (func == STR_XQUOTE) until scanner.eos? c = nil handled = true if check end_str_re # eos # if its just balancing, add it ass normal content.. if str_parse[:balance] && (str_parse[:nesting] != 0) # we only checked above, so actually scan it scan end_str_re c = scanner.matched str_parse[:nesting] -= 1 else # not balancing, so break (eos!) break end elsif str_parse[:balance] and scan Regexp.new(Regexp.escape(str_parse[:paren])) str_parse[:nesting] += 1 c = scanner.matched elsif qwords && scan(/\s/) pushback(1) break elsif && check(/#(?=[\$\@\{])/) break elsif qwords and scan(/\s/) pushback(1) break elsif scan(/\\/) if xquote # opal - treat xstrings as dquotes? forces us to double escape c = "\\" + scan(/./) elsif qwords and scan(/\n/) str_buffer << "\n" next elsif and scan(/\n/) next elsif qwords and scan(/\s/) c = ' ' elsif regexp if scan(/(.)/) c = "\\" + scanner.matched end elsif c = self.read_escape elsif scan(/\n/) # nothing.. elsif scan(/\\/) if escape c = "\\\\" else c = scanner.matched end else # \\ unless scan(end_str_re) str_buffer << "\\" else #c = scanner.matched end end else handled = false end unless handled reg = if qwords Regexp.new("[^#{Regexp.escape str_parse[:term]}\#\0\n\ \\\\]+|.") elsif str_parse[:balance] Regexp.new("[^#{Regexp.escape str_parse[:term]}#{Regexp.escape str_parse[:paren]}\#\0\\\\]+|.") else Regexp.new("[^#{Regexp.escape str_parse[:term]}\#\0\\\\]+|.") end scan reg c = scanner.matched end c ||= scanner.matched str_buffer << c end raise "reached EOF while in string" if scanner.eos? end |
#after_operator? ⇒ Boolean
142 143 144 |
# File 'opal/lib/opal/parser/lexer.rb', line 142 def after_operator? [:expr_fname, :expr_dot].include? @lex_state end |
#arg? ⇒ Boolean
130 131 132 |
# File 'opal/lib/opal/parser/lexer.rb', line 130 def arg? [:expr_arg, :expr_cmdarg].include? @lex_state end |
#beg? ⇒ Boolean
138 139 140 |
# File 'opal/lib/opal/parser/lexer.rb', line 138 def beg? [:expr_beg, :expr_value, :expr_mid, :expr_class].include? @lex_state end |
#check(regexp) ⇒ Object
180 181 182 |
# File 'opal/lib/opal/parser/lexer.rb', line 180 def check(regexp) @scanner.check regexp end |
#cmdarg? ⇒ Boolean
126 127 128 |
# File 'opal/lib/opal/parser/lexer.rb', line 126 def cmdarg? (@cmdarg & 1) != 0 end |
#cmdarg_lexpop ⇒ Object
122 123 124 |
# File 'opal/lib/opal/parser/lexer.rb', line 122 def cmdarg_lexpop @cmdarg = (@cmdarg >> 1) | (@cmdarg & 1) end |
#cmdarg_pop ⇒ Object
118 119 120 |
# File 'opal/lib/opal/parser/lexer.rb', line 118 def cmdarg_pop @cmdarg = @cmdarg >> 1 end |
#cmdarg_push(n) ⇒ Object
114 115 116 |
# File 'opal/lib/opal/parser/lexer.rb', line 114 def cmdarg_push(n) @cmdarg = (@cmdarg << 1) | (n & 1) end |
#cond? ⇒ Boolean
110 111 112 |
# File 'opal/lib/opal/parser/lexer.rb', line 110 def cond? (@cond & 1) != 0 end |
#cond_lexpop ⇒ Object
106 107 108 |
# File 'opal/lib/opal/parser/lexer.rb', line 106 def cond_lexpop @cond = (@cond >> 1) | (@cond & 1) end |
#cond_pop ⇒ Object
102 103 104 |
# File 'opal/lib/opal/parser/lexer.rb', line 102 def cond_pop @cond = @cond >> 1 end |
#cond_push(n) ⇒ Object
98 99 100 |
# File 'opal/lib/opal/parser/lexer.rb', line 98 def cond_push(n) @cond = (@cond << 1) | (n & 1) end |
#end? ⇒ Boolean
134 135 136 |
# File 'opal/lib/opal/parser/lexer.rb', line 134 def end? [:expr_end, :expr_endarg, :expr_endfn].include? @lex_state end |
#has_local?(local) ⇒ Boolean
94 95 96 |
# File 'opal/lib/opal/parser/lexer.rb', line 94 def has_local?(local) parser.scope.has_local?(local.to_sym) end |
#here_document(str_parse) ⇒ Object
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 |
# File 'opal/lib/opal/parser/lexer.rb', line 281 def here_document(str_parse) eos_regx = /[ \t]*#{Regexp.escape(str_parse[:term])}(\r*\n|$)/ = true # Don't escape single-quoted heredoc identifiers escape = str_parse[:func] != STR_SQUOTE if check(eos_regx) scan(/[ \t]*#{Regexp.escape(str_parse[:term])}/) if str_parse[:scanner] @scanner_stack << str_parse[:scanner] @scanner = str_parse[:scanner] end return :tSTRING_END end str_buffer = [] if scan(/#/) if tok = peek_variable_name return tok end str_buffer << '#' end until check(eos_regx) && scanner.bol? if scanner.eos? raise "reached EOF while in heredoc" end if scan(/\n/) str_buffer << scanner.matched elsif && check(/#(?=[\$\@\{])/) break elsif scan(/\\/) str_buffer << (escape ? self.read_escape : scanner.matched) else reg = Regexp.new("[^\#\0\\\\\n]+|.") scan reg str_buffer << scanner.matched end end complete_str = str_buffer.join '' @line += complete_str.count("\n") self.yylval = complete_str return :tSTRING_CONTENT end |
#heredoc_identifier ⇒ Object
517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 |
# File 'opal/lib/opal/parser/lexer.rb', line 517 def heredoc_identifier if scan(/(-?)(['"])?(\w+)\2?/) escape_method = (@scanner[2] == "'") ? STR_SQUOTE : STR_DQUOTE heredoc = @scanner[3] self.strterm = new_strterm(escape_method, heredoc, heredoc) self.strterm[:type] = :heredoc # if ruby code at end of line after heredoc, we have to store it to # parse after heredoc is finished parsing end_of_line = scan(/.*\n/) self.strterm[:scanner] = StringScanner.new(end_of_line) if end_of_line != "\n" self.line += 1 self.yylval = heredoc return :tSTRING_BEG end end |
#label_state? ⇒ Boolean
146 147 148 |
# File 'opal/lib/opal/parser/lexer.rb', line 146 def label_state? [:expr_beg, :expr_endfn].include?(@lex_state) or arg? end |
#matched ⇒ Object
188 189 190 |
# File 'opal/lib/opal/parser/lexer.rb', line 188 def matched @scanner.matched end |
#new_op_asgn(value) ⇒ Object
206 207 208 209 |
# File 'opal/lib/opal/parser/lexer.rb', line 206 def new_op_asgn(value) self.yylval = value :tOP_ASGN end |
#new_strterm(func, term, paren) ⇒ Object
197 198 199 |
# File 'opal/lib/opal/parser/lexer.rb', line 197 def new_strterm(func, term, paren) { :type => :string, :func => func, :term => term, :paren => paren } end |
#new_strterm2(func, term, paren) ⇒ Object
201 202 203 204 |
# File 'opal/lib/opal/parser/lexer.rb', line 201 def new_strterm2(func, term, paren) term = new_strterm(func, term, paren) term.merge({ :balance => true, :nesting => 0 }) end |
#next_token ⇒ Array
Returns next token from source input stream.
Token in form:
[token, [value, [source_line, source_column]]]
82 83 84 85 86 87 88 89 90 91 92 |
# File 'opal/lib/opal/parser/lexer.rb', line 82 def next_token token = self.yylex value = self.yylval location = [@tok_line, @tok_column] # once location is stored, ensure next token starts in correct place @tok_column = @column @tok_line = @line [token, [value, location]] end |
#parse_string ⇒ Object
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 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 |
# File 'opal/lib/opal/parser/lexer.rb', line 335 def parse_string str_parse = self.strterm func = str_parse[:func] space = false qwords = (func & STR_FUNC_QWORDS) != 0 = (func & STR_FUNC_EXPAND) != 0 regexp = (func & STR_FUNC_REGEXP) != 0 space = true if qwords and scan(/\s+/) # if not end of string, so we must be parsing contents str_buffer = [] if scan Regexp.new(Regexp.escape(str_parse[:term])) if qwords && !str_parse[:done_last_space]#&& space str_parse[:done_last_space] = true pushback(1) self.yylval = ' ' return :tSPACE end if str_parse[:balance] if str_parse[:nesting] == 0 if regexp self.yylval = scan(/\w+/) return :tREGEXP_END end return :tSTRING_END else str_buffer << scanner.matched str_parse[:nesting] -= 1 self.strterm = str_parse end elsif regexp @lex_state = :expr_end self.yylval = scan(/\w+/) return :tREGEXP_END else if str_parse[:scanner] @scanner_stack << str_parse[:scanner] @scanner = str_parse[:scanner] end return :tSTRING_END end end if space self.yylval = ' ' return :tSPACE end if str_parse[:balance] and scan Regexp.new(Regexp.escape(str_parse[:paren])) str_buffer << scanner.matched str_parse[:nesting] += 1 elsif check(/#[@$]/) scan(/#/) if return :tSTRING_DVAR else str_buffer << scanner.matched end elsif scan(/#\{/) if return :tSTRING_DBEG else str_buffer << scanner.matched end # causes error, so we will just collect it later on with other text elsif scan(/\#/) str_buffer << '#' end add_string_content str_buffer, str_parse complete_str = str_buffer.join '' @line += complete_str.count("\n") self.yylval = complete_str return :tSTRING_CONTENT end |
#peek_variable_name ⇒ Object
273 274 275 276 277 278 279 |
# File 'opal/lib/opal/parser/lexer.rb', line 273 def peek_variable_name if check(/[@$]/) :tSTRING_DVAR elsif scan(/\{/) :tSTRING_DBEG end end |
#process_identifier(matched, cmd_start) ⇒ Object
536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 |
# File 'opal/lib/opal/parser/lexer.rb', line 536 def process_identifier(matched, cmd_start) last_state = @lex_state if label_state? and !check(/::/) and scan(/:/) @lex_state = :expr_beg self.yylval = matched return :tLABEL end if matched == 'defined?' if after_operator? @lex_state = :expr_end return :tIDENTIFIER end @lex_state = :expr_arg return :kDEFINED end if matched.end_with? '?', '!' result = :tIDENTIFIER else if @lex_state == :expr_fname if !check(/\=\>/) and scan(/\=/) result = :tIDENTIFIER matched += scanner.matched end elsif matched =~ /^[A-Z]/ result = :tCONSTANT else result = :tIDENTIFIER end end if @lex_state != :expr_dot and kw = Keywords.keyword(matched) old_state = @lex_state @lex_state = kw.state if old_state == :expr_fname self.yylval = kw.name return kw.id[0] end if @lex_state == :expr_beg cmd_start = true end if matched == "do" if after_operator? @lex_state = :expr_end return :tIDENTIFIER end if @start_of_lambda @start_of_lambda = false @lex_state = :expr_beg return :kDO_LAMBDA elsif cond? @lex_state = :expr_beg return :kDO_COND elsif cmdarg? && @lex_state != :expr_cmdarg @lex_state = :expr_beg return :kDO_BLOCK elsif @lex_state == :expr_endarg return :kDO_BLOCK else @lex_state = :expr_beg return :kDO end else if old_state == :expr_beg or old_state == :expr_value self.yylval = matched return kw.id[0] else if kw.id[0] != kw.id[1] @lex_state = :expr_beg end self.yylval = matched return kw.id[1] end end end if [:expr_beg, :expr_dot, :expr_mid, :expr_arg, :expr_cmdarg].include? @lex_state @lex_state = cmd_start ? :expr_cmdarg : :expr_arg else @lex_state = :expr_end end if ![:expr_dot, :expr_fname].include?(last_state) and has_local?(matched) @lex_state = :expr_end end return matched =~ /^[A-Z]/ ? :tCONSTANT : :tIDENTIFIER end |
#process_numeric ⇒ Object
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 |
# File 'opal/lib/opal/parser/lexer.rb', line 211 def process_numeric @lex_state = :expr_end if scan(/[\d_]+\.[\d_]+\b|[\d_]+(\.[\d_]+)?[eE][-+]?[\d_]+\b/) # FLOATS self.yylval = scanner.matched.gsub(/_/, '').to_f return :tFLOAT elsif scan(/([^0][\d_]*|0)\b/) # BASE 10 self.yylval = scanner.matched.gsub(/_/, '').to_i return :tINTEGER elsif scan(/0[bB](0|1|_)+/) # BASE 2 self.yylval = scanner.matched.to_i(2) return :tINTEGER elsif scan(/0[xX](\d|[a-f]|[A-F]|_)+/) # BASE 16 self.yylval = scanner.matched.to_i(16) return :tINTEGER elsif scan(/0[oO]?([0-7]|_)+/) # BASE 8 self.yylval = scanner.matched.to_i(8) return :tINTEGER elsif scan(/0[dD]([0-9]|_)+/) # BASE 10 self.yylval = scanner.matched.gsub(/_/, '').to_i return :tINTEGER else raise "Lexing error on numeric type: `#{scanner.peek 5}`" end end |
#pushback(n) ⇒ Object
184 185 186 |
# File 'opal/lib/opal/parser/lexer.rb', line 184 def pushback(n) @scanner.pos -= n end |
#read_escape ⇒ Object
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 262 263 264 265 266 267 268 269 270 271 |
# File 'opal/lib/opal/parser/lexer.rb', line 237 def read_escape if scan(/\\/) "\\" elsif scan(/n/) "\n" elsif scan(/t/) "\t" elsif scan(/r/) "\r" elsif scan(/f/) "\f" elsif scan(/v/) "\v" elsif scan(/a/) "\a" elsif scan(/e/) "\e" elsif scan(/s/) " " elsif scan(/[0-7]{1,3}/) (matched.to_i(8) % 0x100).chr elsif scan(/x([0-9a-fA-F]{1,2})/) scanner[1].to_i(16).chr elsif scan(/u([0-9a-zA-Z]{1,4})/) if defined?(::Encoding) scanner[1].to_i(16).chr(Encoding::UTF_8) else # FIXME: no encoding on 1.8 "" end else # escaped char doesnt need escaping, so just return it scan(/./) end end |
#scan(regexp) ⇒ Object
162 163 164 165 166 167 168 169 |
# File 'opal/lib/opal/parser/lexer.rb', line 162 def scan(regexp) if result = @scanner.scan(regexp) @column += result.length @yylval += @scanner.matched end result end |
#set_arg_state ⇒ Object
158 159 160 |
# File 'opal/lib/opal/parser/lexer.rb', line 158 def set_arg_state @lex_state = after_operator? ? :expr_arg : :expr_beg end |
#skip(regexp) ⇒ Object
171 172 173 174 175 176 177 178 |
# File 'opal/lib/opal/parser/lexer.rb', line 171 def skip(regexp) if result = @scanner.scan(regexp) @column += result.length @tok_column = @column end result end |
#space? ⇒ Boolean
154 155 156 |
# File 'opal/lib/opal/parser/lexer.rb', line 154 def space? @scanner.check(/\s/) end |
#spcarg? ⇒ Boolean
150 151 152 |
# File 'opal/lib/opal/parser/lexer.rb', line 150 def spcarg? arg? and @space_seen and !space? end |
#yylex ⇒ Object
Does the heavy lifting for next_token
.
635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 |
# File 'opal/lib/opal/parser/lexer.rb', line 635 def yylex @yylval = '' @space_seen = false cmd_start = false c = '' if self.strterm if self.strterm[:type] == :heredoc token = here_document(self.strterm) else token = parse_string end if token == :tSTRING_END or token == :tREGEXP_END self.strterm = nil @lex_state = :expr_end end return token end while true if skip(/\ |\t|\r/) @space_seen = true next elsif skip(/(\n|#)/) c = scanner.matched if c == '#' skip(/(.*)/) else self.line += 1 end skip(/(\n+)/) if scanner.matched self.line += scanner.matched.length end next if [:expr_beg, :expr_dot].include? @lex_state if skip(/([\ \t\r\f\v]*)\./) @space_seen = true unless scanner[1].empty? pushback(1) next unless check(/\.\./) end cmd_start = true @lex_state = :expr_beg self.yylval = '\\n' return :tNL elsif scan(/\;/) @lex_state = :expr_beg return :tSEMI elsif check(/\*/) if scan(/\*\*\=/) @lex_state = :expr_beg return new_op_asgn('**') elsif scan(/\*\*/) self.set_arg_state return :tPOW elsif scan(/\*\=/) @lex_state = :expr_beg return new_op_asgn('*') else scan(/\*/) if after_operator? @lex_state = :expr_arg return :tSTAR2 elsif @space_seen && check(/\S/) @lex_state = :expr_beg return :tSTAR elsif [:expr_beg, :expr_mid].include? @lex_state @lex_state = :expr_beg return :tSTAR else @lex_state = :expr_beg return :tSTAR2 end end elsif scan(/\!/) if after_operator? @lex_state = :expr_arg if scan(/@/) return :tBANG, '!' end else @lex_state = :expr_beg end if scan(/\=/) return :tNEQ elsif scan(/\~/) return :tNMATCH end return :tBANG elsif scan(/\=/) if @lex_state == :expr_beg and !@space_seen if scan(/begin/) and space? scan(/(.*)/) # end of line line_count = 0 while true if scanner.eos? raise "embedded document meets end of file" end if scan(/\=end/) and space? @line += line_count return yylex end if scan(/\n/) line_count += 1 next end scan(/(.*)/) end end end self.set_arg_state if scan(/\=/) if scan(/\=/) return :tEQQ end return :tEQ end if scan(/\~/) return :tMATCH elsif scan(/\>/) return :tASSOC end return :tEQL elsif scan(/\"/) self.strterm = new_strterm(STR_DQUOTE, '"', "\0") return :tSTRING_BEG elsif scan(/\'/) self.strterm = new_strterm(STR_SQUOTE, "'", "\0") return :tSTRING_BEG elsif scan(/\`/) self.strterm = new_strterm(STR_XQUOTE, "`", "\0") return :tXSTRING_BEG elsif scan(/\&/) if scan(/\&/) @lex_state = :expr_beg if scan(/\=/) return new_op_asgn('&&') end return :tANDOP elsif scan(/\=/) @lex_state = :expr_beg return new_op_asgn('&') end if spcarg? #puts "warning: `&' interpreted as argument prefix" result = :tAMPER elsif beg? result = :tAMPER else #puts "warn_balanced: & argument prefix" result = :tAMPER2 end self.set_arg_state return result elsif scan(/\|/) if scan(/\|/) @lex_state = :expr_beg if scan(/\=/) return new_op_asgn('||') end return :tOROP elsif scan(/\=/) return new_op_asgn('|') end self.set_arg_state return :tPIPE elsif scan(/\%[QqWwixrs]/) str_type = scanner.matched[1, 1] paren = term = scan(/./) case term when '(' then term = ')' when '[' then term = ']' when '{' then term = '}' when '<' then term = '>' else paren = "\0" end token, func = case str_type when 'Q' [:tSTRING_BEG, STR_DQUOTE] when 'q' [:tSTRING_BEG, STR_SQUOTE] when 'W' skip(/\s*/) [:tWORDS_BEG, STR_DWORD] when 'w', 'i' skip(/\s*/) [:tAWORDS_BEG, STR_SWORD] when 'x' [:tXSTRING_BEG, STR_XQUOTE] when 'r' [:tREGEXP_BEG, STR_REGEXP] when 's' [:tSTRING_BEG, STR_SQUOTE] end self.strterm = new_strterm2(func, term, paren) return token elsif scan(/\//) if beg? self.strterm = new_strterm(STR_REGEXP, '/', '/') return :tREGEXP_BEG elsif scan(/\=/) @lex_state = :expr_beg return new_op_asgn('/') elsif after_operator? @lex_state = :expr_arg elsif arg? if !check(/\s/) && @space_seen self.strterm = new_strterm(STR_REGEXP, '/', '/') return :tREGEXP_BEG end else @lex_state = :expr_beg end return :tDIVIDE elsif scan(/\%/) if scan(/\=/) @lex_state = :expr_beg return new_op_asgn('%') elsif check(/[^\s]/) if @lex_state == :expr_beg or (@lex_state == :expr_arg && @space_seen) start_word = scan(/./) end_word = { '(' => ')', '[' => ']', '{' => '}' }[start_word] || start_word self.strterm = new_strterm2(STR_DQUOTE, end_word, start_word) return :tSTRING_BEG end end self.set_arg_state return :tPERCENT elsif scan(/\\/) if scan(/\r?\n/) @space_seen = true next end raise SyntaxError, "backslash must appear before newline :#{@file}:#{@line}" elsif scan(/\(/) result = scanner.matched if beg? result = :tLPAREN elsif @space_seen && arg? result = :tLPAREN_ARG else result = :tLPAREN2 end @lex_state = :expr_beg cond_push 0 cmdarg_push 0 return result elsif scan(/\)/) cond_lexpop cmdarg_lexpop @lex_state = :expr_end return :tRPAREN elsif scan(/\[/) result = scanner.matched if after_operator? @lex_state = :expr_arg if scan(/\]=/) return :tASET elsif scan(/\]/) return :tAREF else raise "Unexpected '[' token" end elsif beg? result = :tLBRACK elsif arg? && @space_seen result = :tLBRACK else result = :tLBRACK2 end @lex_state = :expr_beg cond_push 0 cmdarg_push 0 return result elsif scan(/\]/) cond_lexpop cmdarg_lexpop @lex_state = :expr_end return :tRBRACK elsif scan(/\}/) cond_lexpop cmdarg_lexpop @lex_state = :expr_end return :tRCURLY elsif scan(/\.\.\./) @lex_state = :expr_beg return :tDOT3 elsif scan(/\.\./) @lex_state = :expr_beg return :tDOT2 elsif scan(/\./) @lex_state = :expr_dot unless @lex_state == :expr_fname return :tDOT elsif scan(/\:\:/) if beg? @lex_state = :expr_beg return :tCOLON3 elsif spcarg? @lex_state = :expr_beg return :tCOLON3 end @lex_state = :expr_dot return :tCOLON2 elsif scan(/\:/) if end? || check(/\s/) unless check(/\w/) @lex_state = :expr_beg return :tCOLON end @lex_state = :expr_fname return :tSYMBEG end if scan(/\'/) self.strterm = new_strterm(STR_SSYM, "'", "\0") elsif scan(/\"/) self.strterm = new_strterm(STR_DSYM, '"', "\0") end @lex_state = :expr_fname return :tSYMBEG elsif scan(/\^\=/) @lex_state = :expr_beg return new_op_asgn('^') elsif scan(/\^/) self.set_arg_state return :tCARET elsif check(/\</) if scan(/\<\<\=/) @lex_state = :expr_beg return new_op_asgn('<<') elsif scan(/\<\</) if after_operator? @lex_state = :expr_arg return :tLSHFT elsif !after_operator? && !end? && (!arg? || @space_seen) if token = heredoc_identifier return token end @lex_state = :expr_beg return :tLSHFT end @lex_state = :expr_beg return :tLSHFT elsif scan(/\<\=\>/) if after_operator? @lex_state = :expr_arg else if @lex_state == :expr_class cmd_start = true end @lex_state = :expr_beg end return :tCMP elsif scan(/\<\=/) self.set_arg_state return :tLEQ elsif scan(/\</) self.set_arg_state return :tLT end elsif check(/\>/) if scan(/\>\>\=/) return new_op_asgn('>>') elsif scan(/\>\>/) self.set_arg_state return :tRSHFT elsif scan(/\>\=/) self.set_arg_state return :tGEQ elsif scan(/\>/) self.set_arg_state return :tGT end elsif scan(/->/) # FIXME: # should be :expr_arg, but '(' breaks it... @lex_state = :expr_end @start_of_lambda = true return :tLAMBDA elsif scan(/[+-]/) matched = scanner.matched sign, utype = if matched == '+' [:tPLUS, :tUPLUS] else [:tMINUS, :tUMINUS] end if beg? @lex_state = :expr_mid self.yylval = matched if scanner.peek(1) =~ /\d/ and return utype == :tUMINUS ? '-@NUM' : '+@NUM' else return utype end elsif after_operator? @lex_state = :expr_arg if scan(/@/) self.yylval = matched + '@' return :tIDENTIFIER end self.yylval = matched return sign end if scan(/\=/) @lex_state = :expr_beg return new_op_asgn(matched) end if spcarg? @lex_state = :expr_mid self.yylval = matched return utype end @lex_state = :expr_beg self.yylval = matched return sign elsif scan(/\?/) if end? @lex_state = :expr_beg return :tEH end if check(/\ |\t|\r|\s/) @lex_state = :expr_beg return :tEH elsif scan(/\\/) @lex_state = :expr_end self.yylval = self.read_escape return :tSTRING end @lex_state = :expr_end self.yylval = scan(/./) return :tSTRING elsif scan(/\~/) self.set_arg_state return :tTILDE elsif check(/\$/) if scan(/\$([1-9]\d*)/) @lex_state = :expr_end self.yylval = scanner.matched.sub('$', '') return :tNTH_REF elsif scan(/(\$_)(\w+)/) @lex_state = :expr_end return :tGVAR elsif scan(/\$[\+\'\`\&!@\"~*$?\/\\:;=.,<>_]/) @lex_state = :expr_end return :tGVAR elsif scan(/\$\w+/) @lex_state = :expr_end return :tGVAR else raise "Bad gvar name: #{scanner.peek(5).inspect}" end elsif scan(/\$\w+/) @lex_state = :expr_end return :tGVAR elsif scan(/\@\@\w*/) @lex_state = :expr_end return :tCVAR elsif scan(/\@\w*/) @lex_state = :expr_end return :tIVAR elsif scan(/\,/) @lex_state = :expr_beg return :tCOMMA elsif scan(/\{/) if @start_of_lambda @start_of_lambda = false @lex_state = :expr_beg return :tLAMBEG elsif arg? or @lex_state == :expr_end result = :tLCURLY elsif @lex_state == :expr_endarg result = :LBRACE_ARG else result = :tLBRACE end @lex_state = :expr_beg cond_push 0 cmdarg_push 0 return result elsif scanner.bol? and skip(/\__END__(\n|$)/) while true if scanner.eos? @eof_content = self.yylval return false end scan(/(.*)/) scan(/\n/) end elsif check(/[0-9]/) return process_numeric elsif scan(/(\w)+[\?\!]?/) return process_identifier scanner.matched, cmd_start end if scanner.eos? if @scanner_stack.size == 1 # our main scanner, we cant pop this self.yylval = false return false else # we were probably parsing a heredoc, so pop that parser and continue @scanner_stack.pop @scanner = @scanner_stack.last return yylex end end raise "Unexpected content in parsing stream `#{scanner.peek 5}` :#{@file}:#{@line}" end end |