2010-03-16 23:29:12 +03:00
|
|
|
# encoding: utf-8
|
2007-08-16 00:20:18 +04:00
|
|
|
module CodeRay
|
|
|
|
module Scanners
|
|
|
|
|
|
|
|
module Ruby::Patterns # :nodoc:
|
|
|
|
|
|
|
|
RESERVED_WORDS = %w[
|
|
|
|
and def end in or unless begin
|
|
|
|
defined? ensure module redo super until
|
|
|
|
BEGIN break do next rescue then
|
|
|
|
when END case else for retry
|
|
|
|
while alias class elsif if not return
|
|
|
|
undef yield
|
|
|
|
]
|
|
|
|
|
|
|
|
DEF_KEYWORDS = %w[ def ]
|
|
|
|
UNDEF_KEYWORDS = %w[ undef ]
|
2010-03-16 23:29:12 +03:00
|
|
|
ALIAS_KEYWORDS = %w[ alias ]
|
2011-01-22 16:18:01 +03:00
|
|
|
MODULE_KEYWORDS = %w[ class module ]
|
2007-08-16 00:20:18 +04:00
|
|
|
DEF_NEW_STATE = WordList.new(:initial).
|
|
|
|
add(DEF_KEYWORDS, :def_expected).
|
|
|
|
add(UNDEF_KEYWORDS, :undef_expected).
|
2010-03-16 23:29:12 +03:00
|
|
|
add(ALIAS_KEYWORDS, :alias_expected).
|
2007-08-16 00:20:18 +04:00
|
|
|
add(MODULE_KEYWORDS, :module_expected)
|
|
|
|
|
|
|
|
PREDEFINED_CONSTANTS = %w[
|
|
|
|
nil true false self
|
2011-01-22 16:18:01 +03:00
|
|
|
DATA ARGV ARGF
|
|
|
|
__FILE__ __LINE__ __ENCODING__
|
2007-08-16 00:20:18 +04:00
|
|
|
]
|
|
|
|
|
|
|
|
IDENT_KIND = WordList.new(:ident).
|
|
|
|
add(RESERVED_WORDS, :reserved).
|
|
|
|
add(PREDEFINED_CONSTANTS, :pre_constant)
|
|
|
|
|
2011-01-22 16:18:01 +03:00
|
|
|
if /\w/u === '∑'
|
|
|
|
# MRI 1.8.6, 1.8.7
|
|
|
|
IDENT = /[^\W\d]\w*/
|
|
|
|
else
|
|
|
|
if //.respond_to? :encoding
|
|
|
|
# MRI 1.9.1, 1.9.2
|
|
|
|
IDENT = Regexp.new '[\p{L}\p{M}\p{Pc}\p{Sm}&&[^\x00-\x40\x5b-\x5e\x60\x7b-\x7f]][\p{L}\p{M}\p{N}\p{Pc}\p{Sm}&&[^\x00-\x2f\x3a-\x40\x5b-\x5e\x60\x7b-\x7f]]*'
|
|
|
|
else
|
|
|
|
# JRuby, Rubinius
|
|
|
|
IDENT = /[^\x00-\x40\x5b-\x5e\x60\x7b-\x7f][^\x00-\x2f\x3a-\x40\x5b-\x5e\x60\x7b-\x7f]*/
|
|
|
|
end
|
|
|
|
end
|
2007-08-16 00:20:18 +04:00
|
|
|
|
|
|
|
METHOD_NAME = / #{IDENT} [?!]? /ox
|
|
|
|
METHOD_NAME_OPERATOR = /
|
|
|
|
\*\*? # multiplication and power
|
2010-03-16 23:29:12 +03:00
|
|
|
| [-+~]@? # plus, minus, tilde with and without at sign
|
|
|
|
| [\/%&|^`] # division, modulo or format strings, and, or, xor, system
|
2007-08-16 00:20:18 +04:00
|
|
|
| \[\]=? # array getter and setter
|
|
|
|
| << | >> # append or shift left, shift right
|
|
|
|
| <=?>? | >=? # comparison, rocket operator
|
2010-03-16 23:29:12 +03:00
|
|
|
| ===? | =~ # simple equality, case equality, match
|
|
|
|
| ![~=@]? # negation with and without at sign, not-equal and not-match
|
2007-08-16 00:20:18 +04:00
|
|
|
/ox
|
|
|
|
METHOD_NAME_EX = / #{IDENT} (?:[?!]|=(?!>))? | #{METHOD_NAME_OPERATOR} /ox
|
|
|
|
INSTANCE_VARIABLE = / @ #{IDENT} /ox
|
|
|
|
CLASS_VARIABLE = / @@ #{IDENT} /ox
|
|
|
|
OBJECT_VARIABLE = / @@? #{IDENT} /ox
|
|
|
|
GLOBAL_VARIABLE = / \$ (?: #{IDENT} | [1-9]\d* | 0\w* | [~&+`'=\/,;_.<>!@$?*":\\] | -[a-zA-Z_0-9] ) /ox
|
2010-03-16 23:29:12 +03:00
|
|
|
PREFIX_VARIABLE = / #{GLOBAL_VARIABLE} | #{OBJECT_VARIABLE} /ox
|
2007-08-16 00:20:18 +04:00
|
|
|
VARIABLE = / @?@? #{IDENT} | #{GLOBAL_VARIABLE} /ox
|
|
|
|
|
|
|
|
QUOTE_TO_TYPE = {
|
|
|
|
'`' => :shell,
|
|
|
|
'/'=> :regexp,
|
|
|
|
}
|
|
|
|
QUOTE_TO_TYPE.default = :string
|
|
|
|
|
|
|
|
REGEXP_MODIFIERS = /[mixounse]*/
|
2010-03-16 23:29:12 +03:00
|
|
|
REGEXP_SYMBOLS = /[|?*+(){}\[\].^$]/
|
2007-08-16 00:20:18 +04:00
|
|
|
|
|
|
|
DECIMAL = /\d+(?:_\d+)*/
|
|
|
|
OCTAL = /0_?[0-7]+(?:_[0-7]+)*/
|
|
|
|
HEXADECIMAL = /0x[0-9A-Fa-f]+(?:_[0-9A-Fa-f]+)*/
|
|
|
|
BINARY = /0b[01]+(?:_[01]+)*/
|
|
|
|
|
|
|
|
EXPONENT = / [eE] [+-]? #{DECIMAL} /ox
|
|
|
|
FLOAT_SUFFIX = / #{EXPONENT} | \. #{DECIMAL} #{EXPONENT}? /ox
|
|
|
|
FLOAT_OR_INT = / #{DECIMAL} (?: #{FLOAT_SUFFIX} () )? /ox
|
2010-03-16 23:29:12 +03:00
|
|
|
NUMERIC = / (?: (?=0) (?: #{OCTAL} | #{HEXADECIMAL} | #{BINARY} ) | #{FLOAT_OR_INT} ) /ox
|
2007-08-16 00:20:18 +04:00
|
|
|
|
|
|
|
SYMBOL = /
|
|
|
|
:
|
|
|
|
(?:
|
|
|
|
#{METHOD_NAME_EX}
|
|
|
|
| #{PREFIX_VARIABLE}
|
|
|
|
| ['"]
|
|
|
|
)
|
|
|
|
/ox
|
2010-03-16 23:29:12 +03:00
|
|
|
METHOD_NAME_OR_SYMBOL = / #{METHOD_NAME_EX} | #{SYMBOL} /ox
|
2007-08-16 00:20:18 +04:00
|
|
|
|
2010-03-16 23:29:12 +03:00
|
|
|
SIMPLE_ESCAPE = /
|
2007-08-16 00:20:18 +04:00
|
|
|
[abefnrstv]
|
|
|
|
| [0-7]{1,3}
|
|
|
|
| x[0-9A-Fa-f]{1,2}
|
2010-03-16 23:29:12 +03:00
|
|
|
| .?
|
2007-08-16 00:20:18 +04:00
|
|
|
/mx
|
2010-03-16 23:29:12 +03:00
|
|
|
|
|
|
|
CONTROL_META_ESCAPE = /
|
|
|
|
(?: M-|C-|c )
|
|
|
|
(?: \\ (?: M-|C-|c ) )*
|
|
|
|
(?: [^\\] | \\ #{SIMPLE_ESCAPE} )?
|
|
|
|
/mox
|
|
|
|
|
|
|
|
ESCAPE = /
|
|
|
|
#{CONTROL_META_ESCAPE} | #{SIMPLE_ESCAPE}
|
|
|
|
/mox
|
|
|
|
|
2007-08-16 00:20:18 +04:00
|
|
|
CHARACTER = /
|
|
|
|
\?
|
|
|
|
(?:
|
|
|
|
[^\s\\]
|
|
|
|
| \\ #{ESCAPE}
|
|
|
|
)
|
2010-03-16 23:29:12 +03:00
|
|
|
/mox
|
2007-08-16 00:20:18 +04:00
|
|
|
|
|
|
|
# NOTE: This is not completely correct, but
|
|
|
|
# nobody needs heredoc delimiters ending with \n.
|
2011-01-22 16:18:01 +03:00
|
|
|
# Also, delimiters starting with numbers are allowed.
|
|
|
|
# but they are more often than not a false positive.
|
2007-08-16 00:20:18 +04:00
|
|
|
HEREDOC_OPEN = /
|
|
|
|
<< (-)? # $1 = float
|
|
|
|
(?:
|
2011-01-22 16:18:01 +03:00
|
|
|
( #{IDENT} ) # $2 = delim
|
2007-08-16 00:20:18 +04:00
|
|
|
|
|
|
|
|
( ["'`\/] ) # $3 = quote, type
|
|
|
|
( [^\n]*? ) \3 # $4 = delim
|
|
|
|
)
|
|
|
|
/mx
|
|
|
|
|
|
|
|
RUBYDOC = /
|
|
|
|
=begin (?!\S)
|
|
|
|
.*?
|
|
|
|
(?: \Z | ^=end (?!\S) [^\n]* )
|
|
|
|
/mx
|
|
|
|
|
|
|
|
DATA = /
|
|
|
|
__END__$
|
|
|
|
.*?
|
|
|
|
(?: \Z | (?=^\#CODE) )
|
|
|
|
/mx
|
|
|
|
|
|
|
|
# Checks for a valid value to follow. This enables
|
2010-03-16 23:29:12 +03:00
|
|
|
# value_expected in method calls without parentheses.
|
2007-08-16 00:20:18 +04:00
|
|
|
VALUE_FOLLOWS = /
|
2010-03-16 23:29:12 +03:00
|
|
|
(?>[ \t\f\v]+)
|
2007-08-16 00:20:18 +04:00
|
|
|
(?:
|
|
|
|
[%\/][^\s=]
|
2010-03-16 23:29:12 +03:00
|
|
|
| <<-?\S
|
|
|
|
| [-+] \d
|
|
|
|
| #{CHARACTER}
|
2007-08-16 00:20:18 +04:00
|
|
|
)
|
|
|
|
/x
|
2010-03-16 23:29:12 +03:00
|
|
|
KEYWORDS_EXPECTING_VALUE = WordList.new.add(%w[
|
|
|
|
and end in or unless begin
|
|
|
|
defined? ensure redo super until
|
|
|
|
break do next rescue then
|
|
|
|
when case else for retry
|
|
|
|
while elsif if not return
|
|
|
|
yield
|
|
|
|
])
|
2007-08-16 00:20:18 +04:00
|
|
|
|
|
|
|
RUBYDOC_OR_DATA = / #{RUBYDOC} | #{DATA} /xo
|
|
|
|
|
|
|
|
RDOC_DATA_START = / ^=begin (?!\S) | ^__END__$ /x
|
|
|
|
|
|
|
|
FANCY_START_CORRECT = / % ( [qQwWxsr] | (?![a-zA-Z0-9]) ) ([^a-zA-Z0-9]) /mx
|
|
|
|
|
|
|
|
FancyStringType = {
|
|
|
|
'q' => [:string, false],
|
|
|
|
'Q' => [:string, true],
|
|
|
|
'r' => [:regexp, true],
|
|
|
|
's' => [:symbol, false],
|
|
|
|
'x' => [:shell, true]
|
|
|
|
}
|
|
|
|
FancyStringType['w'] = FancyStringType['q']
|
|
|
|
FancyStringType['W'] = FancyStringType[''] = FancyStringType['Q']
|
|
|
|
|
|
|
|
class StringState < Struct.new :type, :interpreted, :delim, :heredoc,
|
|
|
|
:paren, :paren_depth, :pattern, :next_state
|
|
|
|
|
|
|
|
CLOSING_PAREN = Hash[ *%w[
|
|
|
|
( )
|
|
|
|
[ ]
|
|
|
|
< >
|
|
|
|
{ }
|
|
|
|
] ]
|
|
|
|
|
2010-03-16 23:29:12 +03:00
|
|
|
CLOSING_PAREN.each { |k,v| k.freeze; v.freeze } # debug, if I try to change it with <<
|
2007-08-16 00:20:18 +04:00
|
|
|
OPENING_PAREN = CLOSING_PAREN.invert
|
|
|
|
|
2010-03-16 23:29:12 +03:00
|
|
|
STRING_PATTERN = Hash.new do |h, k|
|
2007-08-16 00:20:18 +04:00
|
|
|
delim, interpreted = *k
|
2010-03-16 23:29:12 +03:00
|
|
|
delim_pattern = Regexp.escape(delim.dup) # dup: workaround for old Ruby
|
2007-08-16 00:20:18 +04:00
|
|
|
if closing_paren = CLOSING_PAREN[delim]
|
2010-03-16 23:29:12 +03:00
|
|
|
delim_pattern = delim_pattern[0..-1] if defined? JRUBY_VERSION # JRuby fix
|
2007-08-16 00:20:18 +04:00
|
|
|
delim_pattern << Regexp.escape(closing_paren)
|
|
|
|
end
|
2010-03-16 23:29:12 +03:00
|
|
|
delim_pattern << '\\\\' unless delim == '\\'
|
|
|
|
|
2007-08-16 00:20:18 +04:00
|
|
|
special_escapes =
|
|
|
|
case interpreted
|
|
|
|
when :regexp_symbols
|
|
|
|
'| ' + REGEXP_SYMBOLS.source
|
|
|
|
when :words
|
|
|
|
'| \s'
|
|
|
|
end
|
2010-03-16 23:29:12 +03:00
|
|
|
|
2007-08-16 00:20:18 +04:00
|
|
|
h[k] =
|
|
|
|
if interpreted and not delim == '#'
|
2010-03-16 23:29:12 +03:00
|
|
|
/ (?= [#{delim_pattern}] | \# [{$@] #{special_escapes} ) /mx
|
2007-08-16 00:20:18 +04:00
|
|
|
else
|
2010-03-16 23:29:12 +03:00
|
|
|
/ (?= [#{delim_pattern}] #{special_escapes} ) /mx
|
2007-08-16 00:20:18 +04:00
|
|
|
end
|
2010-03-16 23:29:12 +03:00
|
|
|
end
|
2007-08-16 00:20:18 +04:00
|
|
|
|
2010-03-16 23:29:12 +03:00
|
|
|
HEREDOC_PATTERN = Hash.new do |h, k|
|
2007-08-16 00:20:18 +04:00
|
|
|
delim, interpreted, indented = *k
|
2010-03-16 23:29:12 +03:00
|
|
|
delim_pattern = Regexp.escape(delim.dup) # dup: workaround for old Ruby
|
2007-08-16 00:20:18 +04:00
|
|
|
delim_pattern = / \n #{ '(?>[\ \t]*)' if indented } #{ Regexp.new delim_pattern } $ /x
|
|
|
|
h[k] =
|
|
|
|
if interpreted
|
|
|
|
/ (?= #{delim_pattern}() | \\ | \# [{$@] ) /mx # $1 set == end of heredoc
|
|
|
|
else
|
|
|
|
/ (?= #{delim_pattern}() | \\ ) /mx
|
|
|
|
end
|
2010-03-16 23:29:12 +03:00
|
|
|
end
|
2007-08-16 00:20:18 +04:00
|
|
|
|
|
|
|
def initialize kind, interpreted, delim, heredoc = false
|
|
|
|
if heredoc
|
|
|
|
pattern = HEREDOC_PATTERN[ [delim, interpreted, heredoc == :indented] ]
|
2010-03-16 23:29:12 +03:00
|
|
|
delim = nil
|
2007-08-16 00:20:18 +04:00
|
|
|
else
|
|
|
|
pattern = STRING_PATTERN[ [delim, interpreted] ]
|
|
|
|
if paren = CLOSING_PAREN[delim]
|
|
|
|
delim, paren = paren, delim
|
|
|
|
paren_depth = 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
super kind, interpreted, delim, heredoc, paren, paren_depth, pattern, :initial
|
|
|
|
end
|
|
|
|
end unless defined? StringState
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|