1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 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 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 143 144 145 146 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 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 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 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 262 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 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 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 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 421 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 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 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 633 634 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
|
# frozen_string_literal: true
# encoding: UTF-8
$DEBUG = true if ENV["DEBUG"]
class RubyLexer
# :stopdoc:
EOF = :eof_haha!
ESCAPES = {
"a" => "\007",
"b" => "\010",
"e" => "\033",
"f" => "\f",
"n" => "\n",
"r" => "\r",
"s" => " ",
"t" => "\t",
"v" => "\13",
"\\" => '\\',
"\n" => "",
"C-\?" => 127.chr,
"c\?" => 127.chr,
}
HAS_ENC = "".respond_to? :encoding
BTOKENS = {
".." => :tBDOT2,
"..." => :tBDOT3,
}
TOKENS = {
"!" => :tBANG,
"!=" => :tNEQ,
"!@" => :tBANG,
"!~" => :tNMATCH,
"," => :tCOMMA,
".." => :tDOT2,
"..." => :tDOT3,
"=" => :tEQL,
"==" => :tEQ,
"===" => :tEQQ,
"=>" => :tASSOC,
"=~" => :tMATCH,
"->" => :tLAMBDA,
}
PERCENT_END = {
"(" => ")",
"[" => "]",
"{" => "}",
"<" => ">",
}
SIMPLE_RE_META = /[\$\*\+\.\?\^\|\)\]\}\>]/
@@regexp_cache = Hash.new { |h, k| h[k] = Regexp.new(Regexp.escape(k)) }
@@regexp_cache[nil] = nil
def regexp_cache
@@regexp_cache
end
if $DEBUG then
attr_reader :lex_state
def lex_state= o
return if @lex_state == o
from = ""
if ENV["VERBOSE"]
path = caller[0]
path = caller[1] if path =~ /result/
path, line, *_ = path.split(/:/)
path.delete_prefix! File.dirname File.dirname __FILE__
from = " at .%s:%s" % [path, line]
end
warn "lex_state: %p -> %p%s" % [lex_state, o, from]
@lex_state = o
end
end
# :startdoc:
attr_accessor :lex_state unless $DEBUG
attr_accessor :brace_nest
attr_accessor :cmdarg
attr_accessor :command_start
attr_accessor :cmd_state # temporary--ivar to avoid passing everywhere
attr_accessor :last_state
attr_accessor :cond
attr_accessor :old_ss
attr_accessor :old_lineno
# these are generated via ruby_lexer.rex: ss, lineno
##
# Additional context surrounding tokens that both the lexer and
# grammar use.
attr_accessor :lex_strterm
attr_accessor :lpar_beg
attr_accessor :paren_nest
attr_accessor :parser # HACK for very end of lexer... *sigh*
attr_accessor :space_seen
attr_accessor :string_buffer
attr_accessor :string_nest
# Last token read via next_token.
attr_accessor :token
attr_writer :comments
def initialize _ = nil
@lex_state = nil # remove one warning under $DEBUG
self.lex_state = EXPR_NONE
self.cond = RubyParserStuff::StackState.new(:cond, $DEBUG)
self.cmdarg = RubyParserStuff::StackState.new(:cmdarg, $DEBUG)
self.ss = RPStringScanner.new ""
reset
end
def arg_ambiguous
self.warning "Ambiguous first argument. make sure."
end
def arg_state
is_after_operator? ? EXPR_ARG : EXPR_BEG
end
def ignore_body_comments
@comments.clear
end
def comments # TODO: remove this... maybe comment_string + attr_accessor
c = @comments.join
@comments.clear
c
end
def debug n
raise "debug #{n}"
end
def expr_dot?
lex_state =~ EXPR_DOT
end
def expr_fname? # REFACTOR
lex_state =~ EXPR_FNAME
end
def expr_result token, text
cond.push false
cmdarg.push false
result EXPR_BEG, token, text
end
def in_fname? # REFACTOR
lex_state =~ EXPR_FNAME
end
def int_with_base base
rb_compile_error "Invalid numeric format" if matched =~ /__/
text = matched
case
when text.end_with?("ri")
result EXPR_NUM, :tIMAGINARY, Complex(0, Rational(text.chop.chop.to_i(base)))
when text.end_with?("r")
result EXPR_NUM, :tRATIONAL, Rational(text.chop.to_i(base))
when text.end_with?("i")
result EXPR_NUM, :tIMAGINARY, Complex(0, text.chop.to_i(base))
else
result EXPR_NUM, :tINTEGER, text.to_i(base)
end
end
def is_after_operator?
lex_state =~ EXPR_FNAME|EXPR_DOT
end
def is_arg?
lex_state =~ EXPR_ARG_ANY
end
def is_beg?
lex_state =~ EXPR_BEG_ANY || lex_state == EXPR_LAB # yes, == EXPR_LAB
end
def is_end?
lex_state =~ EXPR_END_ANY
end
def is_label_possible?
(lex_state =~ EXPR_LABEL|EXPR_ENDFN && !cmd_state) || is_arg?
end
def is_label_suffix?
check(/:(?!:)/)
end
def is_space_arg? c = "x"
is_arg? and space_seen and c !~ /\s/
end
def lambda_beginning?
lpar_beg && lpar_beg == paren_nest
end
def is_local_id id
# maybe just make this false for now
self.parser.env[id.to_sym] == :lvar # HACK: this isn't remotely right
end
def lvar_defined? id
# TODO: (dyna_in_block? && dvar_defined?(id)) || local_id?(id)
self.parser.env[id.to_sym] == :lvar
end
def not_end?
not is_end?
end
def possibly_escape_string text, check
content = match[1]
if text =~ check then
unescape_string content
else
content.gsub(/\\\\/, "\\").gsub(/\\\'/, "'")
end
end
def process_amper text
token = if is_arg? && space_seen && !check(/\s/) then
warning("`&' interpreted as argument prefix")
:tAMPER
elsif lex_state =~ EXPR_BEG|EXPR_MID then
:tAMPER
else
:tAMPER2
end
result :arg_state, token, "&"
end
def process_backref text
token = match[1].to_sym
# TODO: can't do lineno hack w/ symbol
result EXPR_END, :tBACK_REF, token
end
def process_begin text
@comments << matched
unless scan(/.*?\n=end( |\t|\f)*[^\n]*(\n|\z)/m) then
@comments.clear
rb_compile_error("embedded document meets end of file")
end
@comments << matched
self.lineno += matched.count("\n") # HACK?
nil # TODO
end
def process_brace_close text
case matched
when "}" then
self.brace_nest -= 1
return :tSTRING_DEND, matched if brace_nest < 0
end
# matching compare/parse26.y:8099
cond.pop
cmdarg.pop
case matched
when "}" then
self.lex_state = ruby24minus? ? EXPR_ENDARG : EXPR_END
return :tRCURLY, matched
when "]" then
self.paren_nest -= 1
self.lex_state = ruby24minus? ? EXPR_ENDARG : EXPR_END
return :tRBRACK, matched
when ")" then
self.paren_nest -= 1
self.lex_state = EXPR_ENDFN
return :tRPAREN, matched
else
raise "Unknown bracing: #{matched.inspect}"
end
end
def process_brace_open text
# matching compare/parse23.y:8694
self.brace_nest += 1
if lambda_beginning? then
self.lpar_beg = nil
self.paren_nest -= 1 # close arg list when lambda opens body
return expr_result(:tLAMBEG, "{")
end
token = case
when lex_state =~ EXPR_LABELED then
:tLBRACE # hash
when lex_state =~ EXPR_ARG_ANY|EXPR_END|EXPR_ENDFN then
:tLCURLY # block (primary) "{" in parse.y
when lex_state =~ EXPR_ENDARG then
:tLBRACE_ARG # block (expr)
else
:tLBRACE # hash
end
state = token == :tLBRACE_ARG ? EXPR_BEG : EXPR_PAR
self.command_start = true if token != :tLBRACE
cond.push false
cmdarg.push false
result state, token, text
end
def process_colon1 text
# ?: / then / when
if is_end? || check(/\s/) then
return result EXPR_BEG, :tCOLON, text
end
case
when scan(/\'/) then
string STR_SSYM, matched
when scan(/\"/) then
string STR_DSYM, matched
end
result EXPR_FNAME, :tSYMBEG, text
end
def process_colon2 text
if is_beg? || lex_state =~ EXPR_CLASS || is_space_arg? then
result EXPR_BEG, :tCOLON3, text
else
result EXPR_DOT, :tCOLON2, text
end
end
def process_dots text
tokens = ruby27plus? && is_beg? ? BTOKENS : TOKENS
result EXPR_BEG, tokens[text], text
end
def process_float text
rb_compile_error "Invalid numeric format" if text =~ /__/
case
when text.end_with?("ri")
result EXPR_NUM, :tIMAGINARY, Complex(0, Rational(text.chop.chop))
when text.end_with?("i")
result EXPR_NUM, :tIMAGINARY, Complex(0, text.chop.to_f)
when text.end_with?("r")
result EXPR_NUM, :tRATIONAL, Rational(text.chop)
else
result EXPR_NUM, :tFLOAT, text.to_f
end
end
def process_gvar text
if parser.class.version > 20 && text == "$-" then
rb_compile_error "unexpected $undefined"
end
result EXPR_END, :tGVAR, text
end
def process_gvar_oddity text
rb_compile_error "#{text.inspect} is not allowed as a global variable name"
end
def process_ivar text
tok_id = text =~ /^@@/ ? :tCVAR : :tIVAR
result EXPR_END, tok_id, text
end
def process_label text
symbol = possibly_escape_string text, /^\"/
result EXPR_LAB, :tLABEL, symbol
end
def process_label_or_string text
if @was_label && text =~ /:\Z/ then
@was_label = nil
return process_label text
elsif text =~ /:\Z/ then
self.pos -= 1 # put back ":"
text = text[0..-2]
end
orig_line = lineno
str = text[1..-2].gsub(/\\\\/, "\\").gsub(/\\\'/, "\'")
self.lineno += str.count("\n")
result EXPR_END, :tSTRING, str, orig_line
end
def process_lchevron text
if (lex_state !~ EXPR_DOT|EXPR_CLASS &&
!is_end? &&
(!is_arg? || lex_state =~ EXPR_LABELED || space_seen)) then
tok = self.heredoc_identifier
return tok if tok
end
if is_after_operator? then
self.lex_state = EXPR_ARG
else
self.command_start = true if lex_state =~ EXPR_CLASS
self.lex_state = EXPR_BEG
end
result lex_state, :tLSHFT, "\<\<"
end
def process_newline_or_comment text # ../compare/parse30.y:9126 ish
c = matched
if c == "#" then
self.pos -= 1
while scan(/\s*\#.*(\n+|\z)/) do
self.lineno += matched.count "\n"
@comments << matched.gsub(/^ +#/, "#").gsub(/^ +$/, "")
end
return nil if end_of_stream?
end
c = (lex_state =~ EXPR_BEG|EXPR_CLASS|EXPR_FNAME|EXPR_DOT &&
lex_state !~ EXPR_LABELED)
if c || self.lex_state == EXPR_LAB then # yes, == EXPR_LAB
# ignore if !fallthrough?
if !c && parser.in_kwarg then
# normal newline
self.command_start = true
return result EXPR_BEG, :tNL, nil
else
maybe_pop_stack
return # goto retry
end
end
if scan(/[\ \t\r\f\v]+/) then
self.space_seen = true
end
if check(/#/) then
return # goto retry
elsif check(/&\.|\.(?!\.)/) then # C version is a hellish obfuscated xnor
return # goto retry
end
self.command_start = true
result EXPR_BEG, :tNL, nil
end
def process_nthref text
# TODO: can't do lineno hack w/ number
result EXPR_END, :tNTH_REF, match[1].to_i
end
def process_paren text
token = if is_beg? then
:tLPAREN
elsif !space_seen then
# foo( ... ) => method call, no ambiguity
:tLPAREN2
elsif is_space_arg? then
:tLPAREN_ARG
elsif lex_state =~ EXPR_ENDFN && !lambda_beginning? then
# TODO:
# warn("parentheses after method name is interpreted as " \
# "an argument list, not a decomposed argument")
:tLPAREN2
else
:tLPAREN2 # plain "(" in parse.y
end
self.paren_nest += 1
cond.push false
cmdarg.push false
result EXPR_PAR, token, text
end
def process_percent text
case
when is_beg? then
process_percent_quote
when scan(/\=/)
result EXPR_BEG, :tOP_ASGN, "%"
when is_space_arg?(check(/\s/)) || (lex_state =~ EXPR_FITEM && check(/s/))
process_percent_quote
else
result :arg_state, :tPERCENT, "%"
end
end
def process_plus_minus text
sign = matched
utype, type = if sign == "+" then
[:tUPLUS, :tPLUS]
else
[:tUMINUS, :tMINUS]
end
if is_after_operator? then
if scan(/@/) then
return result(EXPR_ARG, utype, "#{sign}@")
else
return result(EXPR_ARG, type, sign)
end
end
return result(EXPR_BEG, :tOP_ASGN, sign) if scan(/\=/)
if is_beg? || (is_arg? && space_seen && !check(/\s/)) then
arg_ambiguous if is_arg?
if check(/\d/) then
return nil if utype == :tUPLUS
return result EXPR_BEG, :tUMINUS_NUM, sign
end
return result EXPR_BEG, utype, sign
end
result EXPR_BEG, type, sign
end
def process_questionmark text
if is_end? then
return result EXPR_BEG, :tEH, "?"
end
if end_of_stream? then
rb_compile_error "incomplete character syntax: parsed #{text.inspect}"
end
if check(/\s|\v/) then
unless is_arg? then
c2 = { " " => "s",
"\n" => "n",
"\t" => "t",
"\v" => "v",
"\r" => "r",
"\f" => "f" }[matched]
if c2 then
warning("invalid character syntax; use ?\\" + c2)
end
end
# ternary
return result EXPR_BEG, :tEH, "?"
elsif check(/\w(?=\w)/) then # ternary, also
return result EXPR_BEG, :tEH, "?"
end
c = if scan(/\\/) then
self.read_escape
else
getch
end
result EXPR_END, :tSTRING, c
end
def process_simple_string text
orig_line = lineno
self.lineno += text.count("\n")
str = unescape_string text[1..-2]
result EXPR_END, :tSTRING, str, orig_line
end
def process_slash text
if is_beg? then
string STR_REGEXP, matched
return result nil, :tREGEXP_BEG, "/"
end
if scan(/\=/) then
return result(EXPR_BEG, :tOP_ASGN, "/")
end
if is_arg? && space_seen then
unless scan(/\s/) then
arg_ambiguous
string STR_REGEXP, "/"
return result(nil, :tREGEXP_BEG, "/")
end
end
result :arg_state, :tDIVIDE, "/"
end
def process_square_bracket text
self.paren_nest += 1
token = nil
if is_after_operator? then
case
when scan(/\]\=/) then
self.paren_nest -= 1 # HACK? I dunno, or bug in MRI
return result EXPR_ARG, :tASET, "[]="
when scan(/\]/) then
self.paren_nest -= 1 # HACK? I dunno, or bug in MRI
return result EXPR_ARG, :tAREF, "[]"
else
rb_compile_error "unexpected '['"
end
elsif is_beg? then
token = :tLBRACK
elsif is_arg? && (space_seen || lex_state =~ EXPR_LABELED) then
token = :tLBRACK
else
token = :tLBRACK2
end
cond.push false
cmdarg.push false
result EXPR_PAR, token, text
end
def process_symbol text
symbol = possibly_escape_string text, /^:\"/ # stupid emacs
result EXPR_LIT, :tSYMBOL, symbol
end
def process_token text
# matching: parse_ident in compare/parse23.y:7989
# FIX: remove: self.last_state = lex_state
token = self.token = text
token << matched if scan(/[\!\?](?!=)/)
tok_id =
case
when token =~ /[!?]$/ then
:tFID
when lex_state =~ EXPR_FNAME && scan(/=(?:(?![~>=])|(?==>))/) then
# ident=, not =~ => == or followed by =>
# TODO test lexing of a=>b vs a==>b
token << matched
:tIDENTIFIER
when token =~ /^[A-Z]/ then
:tCONSTANT
else
:tIDENTIFIER
end
if is_label_possible? and is_label_suffix? then
scan(/:/)
return result EXPR_LAB, :tLABEL, token
end
# TODO: mb == ENC_CODERANGE_7BIT && lex_state !~ EXPR_DOT
if lex_state !~ EXPR_DOT then
# See if it is a reserved word.
keyword = RubyParserStuff::Keyword.keyword token
return process_token_keyword keyword if keyword
end
# matching: compare/parse30.y:9039
state = if lex_state =~ EXPR_BEG_ANY|EXPR_ARG_ANY|EXPR_DOT then
cmd_state ? EXPR_CMDARG : EXPR_ARG
elsif lex_state =~ EXPR_FNAME then
EXPR_ENDFN
else
EXPR_END
end
self.lex_state = state
tok_id = :tIDENTIFIER if tok_id == :tCONSTANT && is_local_id(token)
if last_state !~ EXPR_DOT|EXPR_FNAME and
(tok_id == :tIDENTIFIER) and # not EXPR_FNAME, not attrasgn
lvar_defined?(token) then
state = EXPR_END|EXPR_LABEL
end
result state, tok_id, token
end
def process_token_keyword keyword
# matching MIDDLE of parse_ident in compare/parse23.y:8046
state = lex_state
return result(EXPR_ENDFN, keyword.id0, token) if lex_state =~ EXPR_FNAME
self.lex_state = keyword.state
self.command_start = true if lex_state =~ EXPR_BEG
case
when keyword.id0 == :kDO then # parse26.y line 7591
case
when lambda_beginning? then
self.lpar_beg = nil # lambda_beginning? == FALSE in the body of "-> do ... end"
self.paren_nest -= 1 # TODO: question this?
result lex_state, :kDO_LAMBDA, token
when cond.is_in_state then
result lex_state, :kDO_COND, token
when cmdarg.is_in_state && state != EXPR_CMDARG then
result lex_state, :kDO_BLOCK, token
else
result lex_state, :kDO, token
end
when state =~ EXPR_PAD then
result lex_state, keyword.id0, token
when keyword.id0 != keyword.id1 then
result EXPR_PAR, keyword.id1, token
else
result lex_state, keyword.id1, token
end
end
def process_underscore text
self.unscan # put back "_"
if beginning_of_line? && scan(/\__END__(\r?\n|\Z)/) then
ss.terminate
[RubyLexer::EOF, RubyLexer::EOF]
elsif scan(/#{IDENT_CHAR}+/) then
process_token matched
end
end
def rb_compile_error msg
msg += ". near line #{self.lineno}: #{self.rest[/^.*/].inspect}"
raise RubyParser::SyntaxError, msg
end
def reset
self.lineno = 1
self.brace_nest = 0
self.command_start = true
self.comments = []
self.lex_state = EXPR_NONE
self.lex_strterm = nil
self.lpar_beg = nil
self.paren_nest = 0
self.space_seen = false
self.string_nest = 0
self.token = nil
self.string_buffer = []
self.old_ss = nil
self.old_lineno = nil
self.cond.reset
self.cmdarg.reset
end
def result new_state, token, text, line = self.lineno # :nodoc:
new_state = self.arg_state if new_state == :arg_state
self.lex_state = new_state if new_state
[token, [text, line]]
end
def ruby22_label?
ruby22plus? and is_label_possible?
end
def ruby22plus?
parser.class.version >= 22
end
def ruby23plus?
parser.class.version >= 23
end
def ruby24minus?
parser.class.version <= 24
end
def ruby27plus?
parser.class.version >= 27
end
def space_vs_beginning space_type, beg_type, fallback
if is_space_arg? check(/./m) then
warning "`**' interpreted as argument prefix"
space_type
elsif is_beg? then
beg_type
else
# TODO: warn_balanced("**", "argument prefix");
fallback
end
end
def unescape_string str
str = str.gsub(ESC) { unescape($1).b.force_encoding Encoding::UTF_8 }
if str.valid_encoding?
str
else
str.b
end
end
def unescape s
r = ESCAPES[s]
return r if r
x = case s
when /^[0-7]{1,3}/ then
($&.to_i(8) & 0xFF).chr
when /^x([0-9a-fA-F]{1,2})/ then
$1.to_i(16).chr
when /^M-(.)/ then
($1[0].ord | 0x80).chr
when /^(C-|c)(.)/ then
($2[0].ord & 0x9f).chr
when /^[89a-f]/i then # bad octal or hex... ignore? that's what MRI does :(
s
when /^[McCx0-9]/ then
rb_compile_error("Invalid escape character syntax")
when /u(\h{4})/ then
[$1.delete("{}").to_i(16)].pack("U")
when /u(\h{1,3})/ then
rb_compile_error("Invalid escape character syntax")
when /u\{(\h+(?:\s+\h+)*)\}/ then
$1.split.map { |cp| cp.to_i(16) }.pack("U*")
else
s
end
x
end
def warning s
# do nothing for now
end
def was_label?
@was_label = ruby22_label?
true
end
class State
attr_accessor :n
attr_accessor :names
# TODO: take a shared hash of strings for inspect/to_s
def initialize o, names
raise ArgumentError, "bad state: %p" % [o] unless Integer === o # TODO: remove
self.n = o
self.names = names
end
def == o
self.equal?(o) || (o.class == self.class && o.n == self.n)
end
def =~ v
(self.n & v.n) != 0
end
def | v
raise ArgumentError, "Incompatible State: %p vs %p" % [self, v] unless
self.names == v.names
self.class.new(self.n | v.n, self.names)
end
def inspect
return "Value(0)" if n.zero? # HACK?
names.map { |v, k| k if self =~ v }.
compact.
join("|").
gsub(/(?:EXPR_|STR_(?:FUNC_)?)/, "")
end
alias to_s inspect
module Values
expr_names = {}
EXPR_NONE = State.new 0x0, expr_names
EXPR_BEG = State.new 0x1, expr_names
EXPR_END = State.new 0x2, expr_names
EXPR_ENDARG = State.new 0x4, expr_names
EXPR_ENDFN = State.new 0x8, expr_names
EXPR_ARG = State.new 0x10, expr_names
EXPR_CMDARG = State.new 0x20, expr_names
EXPR_MID = State.new 0x40, expr_names
EXPR_FNAME = State.new 0x80, expr_names
EXPR_DOT = State.new 0x100, expr_names
EXPR_CLASS = State.new 0x200, expr_names
EXPR_LABEL = State.new 0x400, expr_names
EXPR_LABELED = State.new 0x800, expr_names
EXPR_FITEM = State.new 0x1000, expr_names
EXPR_BEG_ANY = EXPR_BEG | EXPR_MID | EXPR_CLASS
EXPR_ARG_ANY = EXPR_ARG | EXPR_CMDARG
EXPR_END_ANY = EXPR_END | EXPR_ENDARG | EXPR_ENDFN
# extra fake lex_state names to make things a bit cleaner
EXPR_LAB = EXPR_ARG|EXPR_LABELED
EXPR_LIT = EXPR_END|EXPR_ENDARG
EXPR_PAR = EXPR_BEG|EXPR_LABEL
EXPR_PAD = EXPR_BEG|EXPR_LABELED
EXPR_NUM = EXPR_LIT
expr_names.merge!(EXPR_NONE => "EXPR_NONE",
EXPR_BEG => "EXPR_BEG",
EXPR_END => "EXPR_END",
EXPR_ENDARG => "EXPR_ENDARG",
EXPR_ENDFN => "EXPR_ENDFN",
EXPR_ARG => "EXPR_ARG",
EXPR_CMDARG => "EXPR_CMDARG",
EXPR_MID => "EXPR_MID",
EXPR_FNAME => "EXPR_FNAME",
EXPR_DOT => "EXPR_DOT",
EXPR_CLASS => "EXPR_CLASS",
EXPR_LABEL => "EXPR_LABEL",
EXPR_LABELED => "EXPR_LABELED",
EXPR_FITEM => "EXPR_FITEM")
# ruby constants for strings
str_func_names = {}
STR_FUNC_BORING = State.new 0x00, str_func_names
STR_FUNC_ESCAPE = State.new 0x01, str_func_names
STR_FUNC_EXPAND = State.new 0x02, str_func_names
STR_FUNC_REGEXP = State.new 0x04, str_func_names
STR_FUNC_QWORDS = State.new 0x08, str_func_names
STR_FUNC_SYMBOL = State.new 0x10, str_func_names
STR_FUNC_INDENT = State.new 0x20, str_func_names # <<-HEREDOC
STR_FUNC_LABEL = State.new 0x40, str_func_names
STR_FUNC_LIST = State.new 0x4000, str_func_names
STR_FUNC_TERM = State.new 0x8000, str_func_names
STR_FUNC_DEDENT = State.new 0x10000, str_func_names # <<~HEREDOC
# TODO: check parser25.y on how they do STR_FUNC_INDENT
STR_SQUOTE = STR_FUNC_BORING
STR_DQUOTE = STR_FUNC_EXPAND
STR_XQUOTE = STR_FUNC_EXPAND
STR_REGEXP = STR_FUNC_REGEXP | STR_FUNC_ESCAPE | STR_FUNC_EXPAND
STR_SWORD = STR_FUNC_QWORDS | STR_FUNC_LIST
STR_DWORD = STR_FUNC_QWORDS | STR_FUNC_EXPAND | STR_FUNC_LIST
STR_SSYM = STR_FUNC_SYMBOL
STR_DSYM = STR_FUNC_SYMBOL | STR_FUNC_EXPAND
STR_LABEL = STR_FUNC_LABEL
str_func_names.merge!(STR_FUNC_ESCAPE => "STR_FUNC_ESCAPE",
STR_FUNC_EXPAND => "STR_FUNC_EXPAND",
STR_FUNC_REGEXP => "STR_FUNC_REGEXP",
STR_FUNC_QWORDS => "STR_FUNC_QWORDS",
STR_FUNC_SYMBOL => "STR_FUNC_SYMBOL",
STR_FUNC_INDENT => "STR_FUNC_INDENT",
STR_FUNC_LABEL => "STR_FUNC_LABEL",
STR_FUNC_LIST => "STR_FUNC_LIST",
STR_FUNC_TERM => "STR_FUNC_TERM",
STR_FUNC_DEDENT => "STR_FUNC_DEDENT",
STR_SQUOTE => "STR_SQUOTE")
end
include Values
end
include State::Values
end
class RubyLexer
module SSWrapper
def string= s
ss.string= s
end
def beginning_of_line?
ss.bol?
end
alias bol? beginning_of_line? # to make .rex file more readable
def check re
maybe_pop_stack
ss.check re
end
def end_of_stream?
ss.eos?
end
alias eos? end_of_stream?
def getch
c = ss.getch
c = ss.getch if c == "\r" && ss.peek(1) == "\n"
c
end
def match
ss
end
def matched
ss.matched
end
def in_heredoc?
!!self.old_ss
end
def maybe_pop_stack
if ss.eos? && in_heredoc? then
self.ss_pop
self.lineno_pop
end
end
def pos
ss.pos
end
def pos= n
ss.pos = n
end
def rest
ss.rest
end
def scan re
maybe_pop_stack
ss.scan re
end
def scanner_class # TODO: design this out of oedipus_lex. or something.
RPStringScanner
end
def ss_string
ss.string
end
def ss_string= s
raise "Probably not"
ss.string = s
end
def unscan
ss.unscan
end
end
include SSWrapper
end
class RubyLexer
module SSStackish
def lineno_push new_lineno
self.old_lineno = self.lineno
self.lineno = new_lineno
end
def lineno_pop
self.lineno = self.old_lineno
self.old_lineno = nil
end
def ss= o
raise "Clearing ss while in heredoc!?!" if in_heredoc?
@old_ss = nil
super
end
def ss_push new_ss
@old_ss = self.ss
@ss = new_ss
end
def ss_pop
@ss = self.old_ss
@old_ss = nil
end
end
prepend SSStackish
end
if ENV["RP_STRTERM_DEBUG"] then
class RubyLexer
def d o
$stderr.puts o.inspect
end
alias old_lex_strterm= lex_strterm=
def lex_strterm= o
self.old_lex_strterm= o
where = caller.first.split(/:/).first(2).join(":")
$stderr.puts
d :lex_strterm => [o, where]
end
end
end
require_relative "./ruby_lexer.rex.rb"
require_relative "./ruby_lexer_strings.rb"
if ENV["RP_LINENO_DEBUG"] then
class RubyLexer
def d o
$stderr.puts o.inspect
end
alias old_lineno= lineno=
def lineno= n
self.old_lineno= n
where = caller.first.split(/:/).first(2).join(":")
$stderr.puts
d :lineno => [n, where]
end
end
end
|