Skip to content

Commit d233cd8

Browse files
committed
Add support for overlay/fragments in DevicetreeLexer and update example DTS file
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 28ec10c commit d233cd8

3 files changed

Lines changed: 117 additions & 0 deletions

File tree

pygments/lexers/devicetree.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,13 @@ class DevicetreeLexer(RegexLexer):
7272
include('whitespace'),
7373
include('macro'),
7474

75+
# Overlay / fragment: &label { ... };
76+
(r'(&)([A-Za-z_]\w*)(' + _ws + r')(\{)',
77+
bygroups(Operator, Name.Function, Comment.Multiline, Punctuation), 'node'),
78+
# Overlay / fragment by path: &{/path/to/node} { ... };
79+
(r'(&)(\{)([^}]+)(\})(' + _ws + r')(\{)',
80+
bygroups(Operator, Punctuation, Name.Namespace, Punctuation,
81+
Comment.Multiline, Punctuation), 'node'),
7582
# Nodes
7683
(r'([^/*@\s&]+|/)(@?)((?:0x)?[0-9a-fA-F,]*)(' + _ws + r')(\{)',
7784
bygroups(Name.Function, Operator, Number.Integer,
@@ -88,6 +95,14 @@ class DevicetreeLexer(RegexLexer):
8895
include('whitespace'),
8996
include('macro'),
9097

98+
# Overlay / fragment nested: &label { ... };
99+
(r'(&)([A-Za-z_]\w*)(' + _ws + r')(\{)',
100+
bygroups(Operator, Name.Function, Comment.Multiline, Punctuation), '#push'),
101+
# Overlay / fragment by path nested: &{/path/to/node} { ... };
102+
(r'(&)(\{)([^}]+)(\})(' + _ws + r')(\{)',
103+
bygroups(Operator, Punctuation, Name.Namespace, Punctuation,
104+
Comment.Multiline, Punctuation), '#push'),
105+
91106
(r'([^/*@\s&]+|/)(@?)((?:0x)?[0-9a-fA-F,]*)(' + _ws + r')(\{)',
92107
bygroups(Name.Function, Operator, Number.Integer,
93108
Comment.Multiline, Punctuation), '#push'),

tests/examplefiles/devicetree/example.dts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,17 @@
7979
};
8080
};
8181

82+
&i2c1 {
83+
serlcd@72 {
84+
compatible = "sparkfun,serlcd";
85+
reg = <0x72>;
86+
columns = <16>;
87+
rows = <2>;
88+
command-delay-ms = <10>;
89+
special-command-delay-ms = <50>;
90+
};
91+
};
92+
8293
&i2c3 {
8394
clock-frequency = <100000>;
8495
pinctrl-names = "default";

tests/examplefiles/devicetree/example.dts.output

Lines changed: 91 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)