diff options
-rw-r--r-- | pygments/lexers/ruby.py | 11 | ||||
-rw-r--r-- | tests/test_ruby.py | 12 |
2 files changed, 21 insertions, 2 deletions
diff --git a/pygments/lexers/ruby.py b/pygments/lexers/ruby.py index 52ba23ad..88cbafec 100644 --- a/pygments/lexers/ruby.py +++ b/pygments/lexers/ruby.py @@ -22,6 +22,11 @@ __all__ = ['RubyLexer', 'RubyConsoleLexer', 'FancyLexer'] line_re = re.compile('.*?\n') +RUBY_OPERATORS = ( + '*', '**', '-', '+', '-@', '+@', '/', '%', '&', '|', '^', '`', '~', + '[]', '[]=', '<<', '>>', '<', '<>', '<=>', '>', '>=', '==', '===' +) + class RubyLexer(ExtendedRegexLexer): """ For `Ruby <http://www.ruby-lang.org>`_ source code. @@ -100,8 +105,8 @@ class RubyLexer(ExtendedRegexLexer): states = {} states['strings'] = [ # easy ones - (r'\:@{0,2}([a-zA-Z_]\w*[\!\?]?|\*\*?|[-+]@?|' - r'[/%&|^`~]|\[\]=?|<<|>>|<=?>|>=?|===?)', String.Symbol), + (r'\:@{0,2}[a-zA-Z_]\w*[\!\?]?', String.Symbol), + (words(RUBY_OPERATORS, prefix=r'\:@{0,2}'), String.Symbol), (r":'(\\\\|\\'|[^'])*'", String.Symbol), (r"'(\\\\|\\'|[^'])*'", String.Single), (r':"', String.Symbol, 'simple-sym'), @@ -306,6 +311,8 @@ class RubyLexer(ExtendedRegexLexer): (r'[A-Z]\w+', Name.Constant), # this is needed because ruby attributes can look # like keywords (class) or like this: ` ?!? + (words(RUBY_OPERATORS, prefix=r'(\.|::)'), + bygroups(Operator, Name.Operator)), (r'(\.|::)([a-zA-Z_]\w*[\!\?]?|[*%&^`~+\-/\[<>=])', bygroups(Operator, Name)), (r'[a-zA-Z_]\w*[\!\?]?', Name), diff --git a/tests/test_ruby.py b/tests/test_ruby.py index 8420826e..9c6bc085 100644 --- a/tests/test_ruby.py +++ b/tests/test_ruby.py @@ -17,6 +17,7 @@ class RubyTest(unittest.TestCase): def setUp(self): self.lexer = RubyLexer() + self.maxDiff = None def testRangeSyntax1(self): fragment = u'1..3\n' @@ -111,3 +112,14 @@ class RubyTest(unittest.TestCase): ] self.assertEqual(tokens, list(self.lexer.get_tokens(fragment))) + def testOperatorMethods(self): + fragment = u'x.==4\n' + tokens = [ + (Token.Name, u'x'), + (Token.Operator, u'.'), + (Token.Name.Operator, u'=='), + (Token.Literal.Number.Integer, u'4'), + (Token.Text, u'\n'), + ] + self.assertEqual(tokens, list(self.lexer.get_tokens(fragment))) + |