Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve Java properties lexer #2404

Merged
merged 1 commit into from Apr 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
52 changes: 34 additions & 18 deletions pygments/lexers/configs.py
Expand Up @@ -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),
],
}

Expand Down Expand Up @@ -1154,7 +1170,7 @@ class UnixConfigLexer(RegexLexer):
* ``/etc/group``
* ``/etc/passwd``
* ``/etc/shadow``

.. versionadded:: 2.12
"""

Expand Down
13 changes: 12 additions & 1 deletion tests/examplefiles/properties/java.properties
Expand Up @@ -4,7 +4,7 @@
Truth:Beauty
Truth Beauty
Truth :Beauty

! line continuations and escapes
fruits apple, banana, pear, \
cantaloupe, watermelon, \
Expand All @@ -14,6 +14,8 @@ key = \
and value2\\
key\ 2 = value
key\\ 3 = value3
key \
= value

! empty keys and edge cases
key1 =
Expand All @@ -22,3 +24,12 @@ key3 the value3
key4 the:value4
key5 the=value5
key6=the value6

! escapes in keys
key\ with\ spaces = value
key\nwith\nnewlines = value\nwith\nnewlines

! indented comment

! line continuations do \
not = work for comments
110 changes: 93 additions & 17 deletions tests/examplefiles/properties/java.properties.output

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion tests/snippets/properties/test_escaped_space_in_value.txt
Expand Up @@ -6,5 +6,7 @@ key = doubleword\ value
' ' Text.Whitespace
'=' Operator
' ' Text.Whitespace
'doubleword\\ value' Literal.String
'doubleword' Literal.String
'\\ ' Literal.String.Escape
'value' Literal.String
'\n' Text.Whitespace
4 changes: 3 additions & 1 deletion tests/snippets/properties/test_just_key_with_space.txt
Expand Up @@ -2,5 +2,7 @@
just\ key

---tokens---
'just\\ key' Name.Attribute
'just' Name.Attribute
'\\ ' Literal.String.Escape
'key' Name.Attribute
'\n' Text.Whitespace