Module: CodeRay::Scanners::Ruby::Patterns

Defined in:
lib/coderay/scanners/ruby/patterns.rb

Overview

:nodoc:

Defined Under Namespace

Classes: StringState

Constant Summary collapse

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 ]
ALIAS_KEYWORDS =
%w[ alias ]
MODULE_KEYWORDS =
%w[ class module ]
DEF_NEW_STATE =
WordList.new(:initial).
add(DEF_KEYWORDS, :def_expected).
add(UNDEF_KEYWORDS, :undef_expected).
add(ALIAS_KEYWORDS, :alias_expected).
add(MODULE_KEYWORDS, :module_expected)
PREDEFINED_CONSTANTS =
%w[
  nil true false self
  DATA ARGV ARGF
  __FILE__ __LINE__ __ENCODING__
]
IDENT_KIND =
WordList.new(:ident).
add(RESERVED_WORDS, :reserved).
add(PREDEFINED_CONSTANTS, :pre_constant)
IDENT =
'ä'[/[[:alpha:]]/] == 'ä' ? /[[:alpha:]_][[:alnum:]_]*/ : /[^\W\d]\w*/
METHOD_NAME =
/ #{IDENT} [?!]? /ox
METHOD_NAME_OPERATOR =
/
  \*\*?           # multiplication and power
  | [-+~]@?       # plus, minus, tilde with and without at sign
  | [\/%&|^`]     # division, modulo or format strings, and, or, xor, system
  | \[\]=?        # array getter and setter
  | << | >>       # append or shift left, shift right
  | <=?>? | >=?   # comparison, rocket operator
  | ===? | =~     # simple equality, case equality, match
  | ![~=@]?       # negation with and without at sign, not-equal and not-match
/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
PREFIX_VARIABLE =
/ #{GLOBAL_VARIABLE} | #{OBJECT_VARIABLE} /ox
VARIABLE =
/ @?@? #{IDENT} | #{GLOBAL_VARIABLE} /ox
QUOTE_TO_TYPE =
{
  '`' => :shell,
  '/'=> :regexp,
}
REGEXP_MODIFIERS =
/[mixounse]*/
REGEXP_SYMBOLS =
/[|?*+(){}\[\].^$]/
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
NUMERIC =
/ (?: (?=0) (?: #{OCTAL} | #{HEXADECIMAL} | #{BINARY} ) | #{FLOAT_OR_INT} ) /ox
SYMBOL =
/
  :
  (?:
    #{METHOD_NAME_EX}
  | #{PREFIX_VARIABLE}
  | ['"]
  )
/ox
METHOD_NAME_OR_SYMBOL =
/ #{METHOD_NAME_EX} | #{SYMBOL} /ox
SIMPLE_ESCAPE =
/
    [abefnrstv]
  |  [0-7]{1,3}
  | x[0-9A-Fa-f]{1,2}
  | .?
/mx
CONTROL_META_ESCAPE =
/
  (?: M-|C-|c )
  (?: \\ (?: M-|C-|c ) )*
  (?: [^\\] | \\ #{SIMPLE_ESCAPE} )?
/mox
ESCAPE =
/
  #{CONTROL_META_ESCAPE} | #{SIMPLE_ESCAPE}
/mox
CHARACTER =
/
  \?
  (?:
    [^\s\\]
  | \\ #{ESCAPE}
  )
/mox
HEREDOC_OPEN =

NOTE: This is not completely correct, but nobody needs heredoc delimiters ending with n. Also, delimiters starting with numbers are allowed. but they are more often than not a false positive.

/
  << (-)?              # $1 = float
  (?:
    ( #{IDENT} )       # $2 = delim
  |
    ( ["'`\/] )        # $3 = quote, type
    ( [^\n]*? ) \3     # $4 = delim
  )
/mx
RUBYDOC =
/
  =begin (?!\S)
  .*?
  (?: \Z | ^=end (?!\S) [^\n]* )
/mx
DATA =
/
  __END__$
  .*?
  (?: \Z | (?=^\#CODE) )
/mx
VALUE_FOLLOWS =

Checks for a valid value to follow. This enables value_expected in method calls without parentheses.

/
  (?>[ \t\f\v]+)
  (?:
    [%\/][^\s=]
  | <<-?\S
  | [-+] \d
  | #{CHARACTER}
  )
/x
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
])
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]
}