Skip to content

Commit

Permalink
lexers: devicetree: allow node label without root node (#1755)
Browse files Browse the repository at this point in the history
* lexers: devicetree: allow node label without root node

It is sometimes useful to render DeviceTree snippets such as:

foo: bar@1234 {
    foo = "bar";
};

However the snipper shown above does not render properly unless it is
enclosed on a root node, i.e.

/{
    foo: bar@1234 {
        foo = "bar";
    };
};

Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>

* tests: add devicetree lexer test for fragments out of root node

Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
  • Loading branch information
gmarull committed Apr 5, 2021
1 parent 64db02e commit 1417210
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pygments/lexers/devicetree.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class DevicetreeLexer(RegexLexer):
(r'(L?)(")', bygroups(String.Affix, String), 'string'),
(r'0x[0-9a-fA-F]+', Number.Hex),
(r'\d+', Number.Integer),
(r'([^\s{}/*]*)(\s*)(:)', bygroups(Name.Label, Text, Punctuation)),
(r'([^\s{}/*]*)(\s*)(:)', bygroups(Name.Label, Text, Punctuation), '#pop'),
(words(('compatible', 'model', 'phandle', 'status', '#address-cells',
'#size-cells', 'reg', 'virtual-reg', 'ranges', 'dma-ranges',
'device_type', 'name'), suffix=r'\b'), Keyword.Reserved),
Expand Down
32 changes: 32 additions & 0 deletions tests/test_devicetree_lexer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
"""
Devicetree Lexer Tests
~~~~~~~~~~~~~~~~~~~~~~
:copyright: Copyright 2006-2021 by the Pygments team, see AUTHORS.
:license: BSD, see LICENSE for details.
"""

import pytest

from pygments.lexers.devicetree import DevicetreeLexer
from pygments.token import Token


@pytest.fixture(scope="module")
def lexer():
yield DevicetreeLexer()


@pytest.mark.parametrize(
"fragment",
(
'nodelabel: node@0 { foo = "bar"; };',
'nodelabel: node { foo = "bar"; };',
'nodelabel0: nodelabel1: node@0 { foo = "bar"; };',
),
)
def test_fragment_out_of_root_node(lexer, fragment):
"""Validate that a devicetree fragment out of a root node is parsed correctly."""

tokens = list(lexer.get_tokens(fragment))
assert all(x[0] != Token.Error for x in tokens)

0 comments on commit 1417210

Please sign in to comment.