Skip to content

Commit c59856c

Browse files
docs: better syntax highlighting (#652)
### Summary of Changes Add rules for [all applicable builtin tokens of Pygments](https://pygments.org/docs/tokens/) to the lexer to improve syntax highlighting in the grammar. In particular, syntactic elements now get highlighted similarly to their Python equivalent, where possible. --------- Co-authored-by: megalinter-bot <129584137+megalinter-bot@users.noreply.github.com>
1 parent b5615c0 commit c59856c

File tree

2 files changed

+86
-21
lines changed

2 files changed

+86
-21
lines changed

docs/lexer/safe_ds_lexer/_safe_ds_lexer.py

+85-20
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,51 @@
11
from pygments.lexer import RegexLexer, words
22
from pygments.token import Comment, Keyword, Name, Number, Operator, String, Whitespace
33

4-
constants = (
4+
keywords_annotation = ("annotation",)
5+
6+
keywords_class = (
7+
"class",
8+
"enum",
9+
)
10+
11+
keywords_constant = (
12+
"attr",
13+
"val",
14+
)
15+
16+
keywords_function = (
17+
"fun",
18+
"pipeline",
19+
"segment",
20+
)
21+
22+
keywords_literal = (
523
"false",
624
"null",
725
"true",
826
)
927

10-
keywords = (
11-
"annotation",
28+
keywords_namespace = (
29+
"from",
30+
"package",
31+
)
32+
33+
keywords_generic = (
1234
"as",
13-
"attr",
14-
"class",
1535
"const",
16-
"enum",
17-
"fun",
36+
"import",
1837
"in",
1938
"internal",
2039
"literal",
2140
"out",
22-
"pipeline",
2341
"private",
2442
"schema",
25-
"segment",
2643
"static",
2744
"union",
28-
"val",
2945
"where",
3046
"yield",
3147
)
3248

33-
namespace = (
34-
"from",
35-
"import",
36-
"package",
37-
)
38-
3949
operators = (
4050
"and",
4151
"not",
@@ -44,6 +54,21 @@
4454
"super",
4555
)
4656

57+
builtins = (
58+
"Any",
59+
"Nothing",
60+
"Boolean",
61+
"Number",
62+
"Int",
63+
"Float",
64+
"ListMap",
65+
"String",
66+
)
67+
68+
identifier_fragment = r"[_a-zA-Z][_a-zA-Z0-9]*"
69+
identifier_regex = rf"{identifier_fragment}|`{identifier_fragment}`"
70+
qualified_name_regex = rf"({identifier_regex})(\.({identifier_regex}))*"
71+
4772

4873
class SafeDsLexer(RegexLexer):
4974
name = "safe-ds"
@@ -59,17 +84,57 @@ class SafeDsLexer(RegexLexer):
5984

6085
tokens = {
6186
"root": [
87+
# Literals
6288
(r"\b([0-9]+(\.[0-9]+)?([eE][+-]?[0-9]+)?)\b", Number),
6389
(r'"|}}', String, "string"),
64-
(words(constants, prefix=r"\b", suffix=r"\b"), Keyword.Constant),
65-
(words(keywords, prefix=r"\b", suffix=r"\b"), Keyword),
66-
(words(namespace, prefix=r"\b", suffix=r"\b"), Keyword.Namespace),
90+
# Keywords
91+
(
92+
words(keywords_annotation, prefix=r"\b", suffix=r"\b"),
93+
Keyword,
94+
"annotation",
95+
),
96+
(words(keywords_class, prefix=r"\b", suffix=r"\b"), Keyword, "class"),
97+
(
98+
words(keywords_constant, prefix=r"\b", suffix=r"\b"),
99+
Keyword.Declaration,
100+
"placeholder",
101+
),
102+
(words(keywords_function, prefix=r"\b", suffix=r"\b"), Keyword, "function"),
103+
(words(keywords_literal, prefix=r"\b", suffix=r"\b"), Keyword.Constant),
104+
(
105+
words(keywords_namespace, prefix=r"\b", suffix=r"\b"),
106+
Keyword.Namespace,
107+
"namespace",
108+
),
109+
(words(keywords_generic, prefix=r"\b", suffix=r"\b"), Keyword),
110+
# Operators
67111
(words(operators, prefix=r"\b", suffix=r"\b"), Operator.Word),
68-
(r"`[_a-zA-Z][_a-zA-Z0-9]*`", Name),
112+
# Builtins
113+
(words(builtins, prefix=r"\b", suffix=r"\b"), Name.Builtin),
114+
# Identifiers
115+
(rf"@{identifier_regex}", Name.Decorator),
116+
(identifier_regex, Name),
117+
# Comments
69118
(r"//.+?$", Comment.Single),
70119
(r"/\*[\s\S]*?\*/", Comment.Multiline),
120+
# Whitespace
71121
(r"\s+", Whitespace),
72122
],
123+
"annotation": [
124+
(identifier_regex, Name.Decorator, "#pop"),
125+
],
126+
"class": [
127+
(identifier_regex, Name.Class, "#pop"),
128+
],
129+
"function": [
130+
(identifier_regex, Name.Function, "#pop"),
131+
],
132+
"namespace": [
133+
(qualified_name_regex, Name.Namespace, "#pop"),
134+
],
135+
"placeholder": [
136+
(identifier_regex, Name.Constant, "#pop"),
137+
],
73138
"string": [
74139
(r'([^"{]|\{(?!\{))+', String),
75140
(r'\{\{|"', String, "#pop"),

tests/resources/validation/other/argument lists/missing required parameter/dont show this error if the argument list is missing altogether.sdstest

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package tests.validation.other.argumentLists.missingRequiredParameter
33
// $TEST$ no error r"The parameters? .* must be set here\."
44

55
@MyAnnotation
6-
segment mySegment2(
6+
segment mySegment3(
77
myCallableType: (a: Int, b: Int, c: Int = 0) -> ()
88
) {
99
val myBlockLambda = (a: Int, b: Int, c: Int = 0) {};

0 commit comments

Comments
 (0)