diff options
author | Gaurav Jain <gaurav@gauravjain.org> | 2014-05-17 22:28:30 -0400 |
---|---|---|
committer | Gaurav Jain <gaurav@gauravjain.org> | 2014-05-17 22:28:30 -0400 |
commit | 4358daadcd3a9d4b228be54094218560aacfd108 (patch) | |
tree | 562fcc25e656c7ba2836504c211c3f05d0995aa9 /pygments/lexer.py | |
parent | a62cdcfaa5a260274303cb93b92a3f2e2ce3be98 (diff) | |
parent | b4328f0a0cd095ce34eb4614cfd3267d695ddf8f (diff) | |
download | pygments-4358daadcd3a9d4b228be54094218560aacfd108.tar.gz |
Merged birkenfeld/pygments-main into default
Diffstat (limited to 'pygments/lexer.py')
-rw-r--r-- | pygments/lexer.py | 27 |
1 files changed, 21 insertions, 6 deletions
diff --git a/pygments/lexer.py b/pygments/lexer.py index 567e85f8..205b7495 100644 --- a/pygments/lexer.py +++ b/pygments/lexer.py @@ -18,7 +18,7 @@ from pygments.util import get_bool_opt, get_int_opt, get_list_opt, \ __all__ = ['Lexer', 'RegexLexer', 'ExtendedRegexLexer', 'DelegatingLexer', - 'LexerContext', 'include', 'inherit', 'bygroups', 'using', 'this'] + 'LexerContext', 'include', 'inherit', 'bygroups', 'using', 'this', 'default'] _encoding_map = [(b'\xef\xbb\xbf', 'utf-8'), @@ -383,6 +383,16 @@ def using(_other, **kwargs): return callback +class default: + """ + Indicates a state or state action (e.g. #pop) to apply. + For example default('#pop') is equivalent to ('', Token, '#pop') + Note that state tuples may be used as well + """ + def __init__(self, state): + self.state = state + + class RegexLexerMeta(LexerMeta): """ Metaclass for RegexLexer, creates the self._tokens attribute from @@ -452,6 +462,10 @@ class RegexLexerMeta(LexerMeta): if isinstance(tdef, _inherit): # processed already continue + if isinstance(tdef, default): + new_state = cls._process_new_state(tdef.state, unprocessed, processed) + tokens.append((re.compile('').match, None, new_state)) + continue assert type(tdef) is tuple, "wrong rule def %r" % tdef @@ -582,11 +596,12 @@ class RegexLexer(Lexer): for rexmatch, action, new_state in statetokens: m = rexmatch(text, pos) if m: - if type(action) is _TokenType: - yield pos, action, m.group() - else: - for item in action(self, m): - yield item + if action is not None: + if type(action) is _TokenType: + yield pos, action, m.group() + else: + for item in action(self, m): + yield item pos = m.end() if new_state is not None: # state transition |