diff options
author | Jean Abou-Samra <jean@abou-samra.fr> | 2023-04-17 20:14:42 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-04-17 20:14:42 +0200 |
commit | fdf182a7af85b1deeeb637ca970d31935e7c9d52 (patch) | |
tree | 1319d3dfc5591185a4e13e0d593b3b8fcfe41173 /pygments | |
parent | c5a2b23adaaadc08a7586a5eda72e9f7d6171012 (diff) | |
download | pygments-git-fdf182a7af85b1deeeb637ca970d31935e7c9d52.tar.gz |
Improve Java properties lexer (#2404)
Use special lexer rules for escapes; fixes catastrophic backtracking,
and highlights them too.
Fixes #2356
Diffstat (limited to 'pygments')
-rw-r--r-- | pygments/lexers/configs.py | 52 |
1 files changed, 34 insertions, 18 deletions
diff --git a/pygments/lexers/configs.py b/pygments/lexers/configs.py index 3b1f9b4d..17842150 100644 --- a/pygments/lexers/configs.py +++ b/pygments/lexers/configs.py @@ -129,26 +129,42 @@ class PropertiesLexer(RegexLexer): tokens = { 'root': [ - (r'\s+', Whitespace), + # comments (r'[!#].*|/{2}.*', Comment.Single), - # search for first separator - (r'([^\\\n]|\\.)*?(?=[ \f\t=:])', Name.Attribute, "separator"), - # empty key - (r'.+?$', Name.Attribute), + # ending a comment or whitespace-only line + (r'\n', Whitespace), + # eat whitespace at the beginning of a line + (r'^[^\S\n]+', Whitespace), + # start lexing a key + default('key'), ], - 'separator': [ - # search for line continuation escape - (r'([ \f\t]*)([=:]*)([ \f\t]*)(.*(?<!\\)(?:\\{2})*)(\\)(?!\\)$', - bygroups(Whitespace, Operator, Whitespace, String, Text), "value", "#pop"), - (r'([ \f\t]*)([=:]*)([ \f\t]*)(.*)', - bygroups(Whitespace, Operator, Whitespace, String), "#pop"), + 'key': [ + # non-escaped key characters + (r'[^\\:=\s]+', Name.Attribute), + # escapes + include('escapes'), + # separator is the first non-escaped whitespace or colon or '=' on the line; + # if it's whitespace, = and : are gobbled after it + (r'([^\S\n]*)([:=])([^\S\n]*)', + bygroups(Whitespace, Operator, Whitespace), + ('#pop', 'value')), + (r'[^\S\n]+', Whitespace, ('#pop', 'value')), + # maybe we got no value after all + (r'\n', Whitespace, '#pop'), ], - 'value': [ # line continuation - (r'\s+', Whitespace), - # search for line continuation escape - (r'(\s*)(.*(?<!\\)(?:\\{2})*)(\\)(?!\\)([ \t]*)', - bygroups(Whitespace, String, Text, Whitespace)), - (r'.*$', String, "#pop"), + 'value': [ + # non-escaped value characters + (r'[^\\\n]+', String), + # escapes + include('escapes'), + # end the value on an unescaped newline + (r'\n', Whitespace, '#pop'), + ], + 'escapes': [ + # line continuations; these gobble whitespace at the beginning of the next line + (r'(\\\n)([^\S\n]*)', bygroups(String.Escape, Whitespace)), + # other escapes + (r'\\(.|\n)', String.Escape), ], } @@ -1154,7 +1170,7 @@ class UnixConfigLexer(RegexLexer): * ``/etc/group`` * ``/etc/passwd`` * ``/etc/shadow`` - + .. versionadded:: 2.12 """ |