diff options
-rw-r--r-- | pygments/lexer.py | 10 | ||||
-rw-r--r-- | pygments/lexers/web.py | 47 |
2 files changed, 44 insertions, 13 deletions
diff --git a/pygments/lexer.py b/pygments/lexer.py index fa236d28..092870ce 100644 --- a/pygments/lexer.py +++ b/pygments/lexer.py @@ -275,8 +275,16 @@ def using(_other, **kwargs): """ if _other is this: def callback(lexer, match, ctx=None): + # if keyword arguments are given the callback + # function has to create a new lexer instance + if kwargs: + # XXX: cache that somehow + kwargs.update(lexer.options) + lx = lexer.__class__(**kwargs) + else: + lx = lexer s = match.start() - for i, t, v in lexer.get_tokens_unprocessed(match.group()): + for i, t, v in lx.get_tokens_unprocessed(match.group()): yield i + s, t, v if ctx: ctx.pos = match.end() diff --git a/pygments/lexers/web.py b/pygments/lexers/web.py index b00e84c0..10e34cac 100644 --- a/pygments/lexers/web.py +++ b/pygments/lexers/web.py @@ -16,9 +16,9 @@ try: except NameError: from sets import Set as set -from pygments.lexer import Lexer, RegexLexer, bygroups, using, include +from pygments.lexer import Lexer, RegexLexer, bygroups, using, include, this from pygments.token import \ - Text, Comment, Operator, Keyword, Name, String, Number, Other + Text, Comment, Operator, Keyword, Name, String, Number, Other, Punctuation from pygments.util import get_bool_opt, get_list_opt, looks_like_xml, \ html_doctype_matches @@ -40,7 +40,8 @@ class JavascriptLexer(RegexLexer): (r'//.*?\n', Comment), (r'/\*.*?\*/', Comment), (r'/(\\\\|\\/|[^/\n])*/[gim]*', String.Regex), - (r'[~\^\*!%&\[\]\{\}\(\)<>\|+=:;,./?-]', Operator), + (r'[~\^\*!%&<>\|+=:;,/?-\\]+', Operator), + (r'[{}\[\]();.]+', Punctuation), (r'(for|in|while|do|break|return|continue|if|else|throw|try|' r'catch|var|with|const|label|function|new|typeof|' r'instanceof|this)\b', Keyword), @@ -67,13 +68,13 @@ class CssLexer(RegexLexer): tokens = { 'root': [ (r'(@media)(\s+)(\w+)(\s*)({)', bygroups(Keyword, Text, String, - Text, Operator), 'media'), + Text, Punctuation), 'media'), include('basics'), ], 'basics': [ (r'\s+', Text), (r'/\*(?:.|\n)*?\*/', Comment), - (r'{', Operator, 'content'), + (r'{', Punctuation, 'content'), (r'\:[a-zA-Z0-9_-]+', Name.Decorator), (r'\.[a-zA-Z0-9_-]+', Name.Class), (r'\#[a-zA-Z0-9_-]+', Name.Function), @@ -84,11 +85,11 @@ class CssLexer(RegexLexer): ], 'media': [ include('basics'), - (r'}', Operator, '#pop') + (r'}', Punctuation, '#pop') ], 'content': [ (r'\s+', Text), - (r'}', Operator, '#pop'), + (r'}', Punctuation, '#pop'), (r'url\(.*?\)', String.Other), (r'^@.*?$', Comment.Preproc), (r'(azimuth|background-attachment|background-color|' @@ -178,7 +179,8 @@ class CssLexer(RegexLexer): (r'\#[a-zA-Z0-9]{1,6}', Number), (r'[\.-]?[0-9]*[\.]?[0-9]+(em|px|\%|pt|pc|in|mm|cm|ex)', Number), (r'-?[0-9]+', Number), - (r'[~\^\*!%&\[\]\(\)<>\|+=@:;,./?-]', Operator), + (r'[~\^\*!%&<>\|+=@:,./?-]+', Operator), + (r'[\[\]();]+', Punctuation), (r'"(\\\\|\\"|[^"])*"', String.Double), (r"'(\\\\|\\'|[^'])*'", String.Single), (r'[a-zA-Z][a-zA-Z0-9]+', Name) @@ -257,7 +259,8 @@ class PhpLexer(RegexLexer): (r'/\*.*?\*/', Comment), (r'(->|::)(\s*)([a-zA-Z_][a-zA-Z0-9_]*)', bygroups(Operator, Text, Name.Attribute)), - (r'[~!%^&*()+=|\[\]:;,.<>/?{}@-]', Text), + (r'[~!%^&*+=|:.<>/?@-]+', Operator), + (r'[\[\]{}();,]+', Punctuation), (r'(class)(\s+)', bygroups(Keyword, Text), 'classname'), (r'(function)(\s+)(&?)(\s*)', bygroups(Keyword, Text, Operator, Text), 'functionname'), @@ -276,15 +279,31 @@ class PhpLexer(RegexLexer): ('[a-zA-Z_][a-zA-Z0-9_]*', Name.Other), (r"[0-9](\.[0-9]*)?(eE[+-][0-9])?[flFLdD]?|" r"0[xX][0-9a-fA-F]+[Ll]?", Number), - (r'"(\\\\|\\"|[^"])*"', String.Double), - (r"'(\\\\|\\'|[^'])*'", String.Single), - (r'`(\\\\|\\`|[^`])*`', String.Backtick) + (r"'([^'\\]*(?:\\.[^'\\]*)*)'", String.Single), + (r'`([^`\\]*(?:\\.[^`\\]*)*)`', String.Backtick), + (r'"', String.Double, 'string'), ], 'classname': [ (r'[a-zA-Z_][a-zA-Z0-9_]*', Name.Class, '#pop') ], 'functionname': [ (r'[a-zA-Z_][a-zA-Z0-9_]*', Name.Function, '#pop') + ], + 'string': [ + (r'"', String.Double, '#pop'), + (r'[^{$"\\]+', String.Double), + (r'\\([nrt\"$]|[0-7]{1,3}|x[0-9A-Fa-f]{1,2})', String.Escape), + (r'\$[a-zA-Z_][a-zA-Z0-9_]*(\[\S+\]|->[a-zA-Z_][a-zA-Z0-9_]*)?', + String.Interpol), + (r'(\{\$\{)(.*?)(\}\})', + bygroups(String.Interpol, using(this, _startinline=True), + String.Interpol)), + (r'(\{)(\$.*?)(\})', + bygroups(String.Interpol, using(this, _startinline=True), + String.Interpol)), + (r'(\$\{)(\S+)(\})', + bygroups(String.Interpol, Name.Variable, String.Interpol)), + (r'[${\\]+', String.Double) ] } @@ -294,6 +313,10 @@ class PhpLexer(RegexLexer): self.disabledmodules = get_list_opt( options, 'disabledmodules', ['unknown']) self.startinline = get_bool_opt(options, 'startinline', False) + + # private option argument for the lexer itself + if '_startinline' in options: + self.startinline = options.pop('_startinline') # collect activated functions in a set self._functions = set() |