Skip to content

Commit 80d738c

Browse files
authored
Add support for overlay/fragments in DevicetreeLexer and update example DTS file (#3021)
The DTS lexer had issues with recognizing overlay/fragment syntax, whereby in the following snippet: &i2c1 { serlcd@72 { compatible = "sparkfun,serlcd"; reg = <0x72>; columns = <16>; rows = <2>; command-delay-ms = <10>; special-command-delay-ms = <50>; }; }; the "i2c1" was being tokenized as a Name.Attribute instead of a Name.Function, causing the rest of the lexing to fail as it encountered the "@" character. This fixes the issue by adding proper support for overlay/fragment syntax. Signed-off-by: Benjamin Cabé <benjamin@zephyrproject.org>
1 parent bdd74eb commit 80d738c

3 files changed

Lines changed: 139 additions & 2 deletions

File tree

pygments/lexers/devicetree.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,10 @@ class DevicetreeLexer(RegexLexer):
7272
'root': [
7373
include('whitespace'),
7474
include('macro'),
75-
75+
# Overlay/fragment: &label { ... } or &{/path/to/node} { ... }
76+
(r'(&)(?:([A-Za-z_]\w*)|(\{)([^}]+)(\}))(' + _ws + r')(\{)',
77+
bygroups(Operator, Name.Function, Punctuation, Name.Namespace,
78+
Punctuation, Comment.Multiline, Punctuation), 'node'),
7679
# Nodes
7780
(r'([^/*@\s&]+|/)(@?)((?:0x)?[0-9a-fA-F,]*)(' + _ws + r')(\{)',
7881
bygroups(Name.Function, Operator, Number.Integer,
@@ -88,7 +91,10 @@ class DevicetreeLexer(RegexLexer):
8891
'node': [
8992
include('whitespace'),
9093
include('macro'),
91-
94+
# Overlay/fragment: &label { ... } or &{/path/to/node} { ... }
95+
(r'(&)(?:([A-Za-z_]\w*)|(\{)([^}]+)(\}))(' + _ws + r')(\{)',
96+
bygroups(Operator, Name.Function, Punctuation, Name.Namespace,
97+
Punctuation, Comment.Multiline, Punctuation), '#push'),
9298
(r'([^/*@\s&]+|/)(@?)((?:0x)?[0-9a-fA-F,]*)(' + _ws + r')(\{)',
9399
bygroups(Name.Function, Operator, Number.Integer,
94100
Comment.Multiline, Punctuation), '#push'),

tests/examplefiles/devicetree/example.dts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,21 @@
8787
};
8888
};
8989

90+
&i2c1 {
91+
serlcd@72 {
92+
compatible = "sparkfun,serlcd";
93+
reg = <0x72>;
94+
columns = <16>;
95+
rows = <2>;
96+
command-delay-ms = <10>;
97+
special-command-delay-ms = <50>;
98+
};
99+
};
100+
101+
&{/soc/i2c@e000d800} {
102+
status = "okay";
103+
};
104+
90105
&i2c3 {
91106
clock-frequency = <100000>;
92107
pinctrl-names = "default";

tests/examplefiles/devicetree/example.dts.output

Lines changed: 116 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)