ThirdParty/Pygments/pygments/lexer.py

branch
Py2 comp.
changeset 2669
11a6696ff868
parent 2525
8b507a9a2d40
child 3079
0233bbe9a9c4
equal deleted inserted replaced
2607:e5115553185a 2669:11a6696ff868
6 Base lexer classes. 6 Base lexer classes.
7 7
8 :copyright: Copyright 2006-2013 by the Pygments team, see AUTHORS. 8 :copyright: Copyright 2006-2013 by the Pygments team, see AUTHORS.
9 :license: BSD, see LICENSE for details. 9 :license: BSD, see LICENSE for details.
10 """ 10 """
11 from __future__ import unicode_literals # __IGNORE_WARNING__
12 try: 11 try:
13 str = unicode 12 str = unicode # __IGNORE_WARNING__
14 except (NameError): 13 except (NameError):
15 pass 14 pass
16 15
17 import re, itertools 16 import re, itertools
18 17
35 ('\xfe\xff', 'utf-16be')] 34 ('\xfe\xff', 'utf-16be')]
36 35
37 _default_analyse = staticmethod(lambda x: 0.0) 36 _default_analyse = staticmethod(lambda x: 0.0)
38 37
39 38
39 def with_metaclass(meta, base=object):
40 """
41 Python independent version to create a base class with a metaclass.
42 Taken from six 1.3.0 (http://pythonhosted.org/six)
43 """
44 return meta("NewBase", (base,), {})
45
46
40 class LexerMeta(type): 47 class LexerMeta(type):
41 """ 48 """
42 This metaclass automagically converts ``analyse_text`` methods into 49 This metaclass automagically converts ``analyse_text`` methods into
43 static methods which always return float values. 50 static methods which always return float values.
44 """ 51 """
47 if 'analyse_text' in d: 54 if 'analyse_text' in d:
48 d['analyse_text'] = make_analysator(d['analyse_text']) 55 d['analyse_text'] = make_analysator(d['analyse_text'])
49 return type.__new__(cls, name, bases, d) 56 return type.__new__(cls, name, bases, d)
50 57
51 58
52 class Lexer(object, metaclass=LexerMeta): 59 class Lexer(with_metaclass(LexerMeta, object)):
53 """ 60 """
54 Lexer for a specific language. 61 Lexer for a specific language.
55 62
56 Basic options recognized: 63 Basic options recognized:
57 ``stripnl`` 64 ``stripnl``
536 cls._tokens = cls.process_tokendef('', cls.get_tokendefs()) 543 cls._tokens = cls.process_tokendef('', cls.get_tokendefs())
537 544
538 return type.__call__(cls, *args, **kwds) 545 return type.__call__(cls, *args, **kwds)
539 546
540 547
541 class RegexLexer(Lexer, metaclass=RegexLexerMeta): 548 class RegexLexer(with_metaclass(RegexLexerMeta, Lexer)):
542 """ 549 """
543 Base for simple stateful regular expression-based lexers. 550 Base for simple stateful regular expression-based lexers.
544 Simplifies the lexing process so that you need only 551 Simplifies the lexing process so that you need only
545 provide a list of states and regular expressions. 552 provide a list of states and regular expressions.
546 """ 553 """

eric ide

mercurial