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 """ |