Skip to content

Commit 1e983e7

Browse files
authored
lexers/cue: support CUE attributes (#961)
Currently the following CUE results in the chroma lexer producing an error token for the '@': value: string @go(Value) This code is, however, valid CUE. '@go' is an attributes. This change adds lexer support for attributes in CUE.
1 parent 9347b55 commit 1e983e7

File tree

3 files changed

+117
-1
lines changed

3 files changed

+117
-1
lines changed

lexers/embedded/cue.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
<rule pattern="(true|false|null|_)\b">
5050
<token type="KeywordConstant"/>
5151
</rule>
52-
<rule pattern="#?[_a-zA-Z$]\w*">
52+
<rule pattern="[@#]?[_a-zA-Z$]\w*">
5353
<token type="Name"/>
5454
</rule>
5555
</state>

lexers/testdata/cue.actual

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,23 @@ _hidden: int
3434
regular: int
3535
$id: int
3636
#definition: int
37+
38+
@protobuf(proto3)
39+
40+
myStruct1: {
41+
// Struct attribute:
42+
@jsonschema(id="https://example.org/mystruct1.json")
43+
44+
// Field attributes
45+
field: string @go(Field)
46+
attr: int @xml(,attr) @go(Attr)
47+
}
48+
49+
myStruct2: {
50+
field: string @go(Field)
51+
attr: int @xml(a1,attr) @go(Attr)
52+
}
53+
54+
Combined: myStruct1 & myStruct2
55+
// field: string @go(Field)
56+
// attr: int @xml(,attr) @xml(a1,attr) @go(Attr)

lexers/testdata/cue.expected

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,5 +126,101 @@
126126
{"type":"Operator","value":":"},
127127
{"type":"Text","value":" "},
128128
{"type":"KeywordType","value":"int"},
129+
{"type":"Text","value":"\n\n"},
130+
{"type":"Name","value":"@protobuf"},
131+
{"type":"Punctuation","value":"("},
132+
{"type":"Name","value":"proto3"},
133+
{"type":"Punctuation","value":")"},
134+
{"type":"Text","value":"\n\n"},
135+
{"type":"Name","value":"myStruct1"},
136+
{"type":"Operator","value":":"},
137+
{"type":"Text","value":" "},
138+
{"type":"Punctuation","value":"{"},
139+
{"type":"Text","value":"\n "},
140+
{"type":"CommentSingle","value":"// Struct attribute:"},
141+
{"type":"Text","value":"\n "},
142+
{"type":"Name","value":"@jsonschema"},
143+
{"type":"Punctuation","value":"("},
144+
{"type":"Name","value":"id"},
145+
{"type":"Operator","value":"="},
146+
{"type":"LiteralString","value":"\"https://example.org/mystruct1.json\""},
147+
{"type":"Punctuation","value":")"},
148+
{"type":"Text","value":"\n\n "},
149+
{"type":"CommentSingle","value":"// Field attributes"},
150+
{"type":"Text","value":"\n "},
151+
{"type":"Name","value":"field"},
152+
{"type":"Operator","value":":"},
153+
{"type":"Text","value":" "},
154+
{"type":"KeywordType","value":"string"},
155+
{"type":"Text","value":" "},
156+
{"type":"Name","value":"@go"},
157+
{"type":"Punctuation","value":"("},
158+
{"type":"Name","value":"Field"},
159+
{"type":"Punctuation","value":")"},
160+
{"type":"Text","value":"\n "},
161+
{"type":"Name","value":"attr"},
162+
{"type":"Operator","value":":"},
163+
{"type":"Text","value":" "},
164+
{"type":"KeywordType","value":"int"},
165+
{"type":"Text","value":" "},
166+
{"type":"Name","value":"@xml"},
167+
{"type":"Punctuation","value":"("},
168+
{"type":"Operator","value":","},
169+
{"type":"Name","value":"attr"},
170+
{"type":"Punctuation","value":")"},
171+
{"type":"Text","value":" "},
172+
{"type":"Name","value":"@go"},
173+
{"type":"Punctuation","value":"("},
174+
{"type":"Name","value":"Attr"},
175+
{"type":"Punctuation","value":")"},
176+
{"type":"Text","value":"\n"},
177+
{"type":"Punctuation","value":"}"},
178+
{"type":"Text","value":"\n\n"},
179+
{"type":"Name","value":"myStruct2"},
180+
{"type":"Operator","value":":"},
181+
{"type":"Text","value":" "},
182+
{"type":"Punctuation","value":"{"},
183+
{"type":"Text","value":"\n "},
184+
{"type":"Name","value":"field"},
185+
{"type":"Operator","value":":"},
186+
{"type":"Text","value":" "},
187+
{"type":"KeywordType","value":"string"},
188+
{"type":"Text","value":" "},
189+
{"type":"Name","value":"@go"},
190+
{"type":"Punctuation","value":"("},
191+
{"type":"Name","value":"Field"},
192+
{"type":"Punctuation","value":")"},
193+
{"type":"Text","value":"\n "},
194+
{"type":"Name","value":"attr"},
195+
{"type":"Operator","value":":"},
196+
{"type":"Text","value":" "},
197+
{"type":"KeywordType","value":"int"},
198+
{"type":"Text","value":" "},
199+
{"type":"Name","value":"@xml"},
200+
{"type":"Punctuation","value":"("},
201+
{"type":"Name","value":"a1"},
202+
{"type":"Operator","value":","},
203+
{"type":"Name","value":"attr"},
204+
{"type":"Punctuation","value":")"},
205+
{"type":"Text","value":" "},
206+
{"type":"Name","value":"@go"},
207+
{"type":"Punctuation","value":"("},
208+
{"type":"Name","value":"Attr"},
209+
{"type":"Punctuation","value":")"},
210+
{"type":"Text","value":"\n"},
211+
{"type":"Punctuation","value":"}"},
212+
{"type":"Text","value":"\n\n"},
213+
{"type":"Name","value":"Combined"},
214+
{"type":"Operator","value":":"},
215+
{"type":"Text","value":" "},
216+
{"type":"Name","value":"myStruct1"},
217+
{"type":"Text","value":" "},
218+
{"type":"Operator","value":"\u0026"},
219+
{"type":"Text","value":" "},
220+
{"type":"Name","value":"myStruct2"},
221+
{"type":"Text","value":"\n"},
222+
{"type":"CommentSingle","value":"// field: string @go(Field)"},
223+
{"type":"Text","value":"\n"},
224+
{"type":"CommentSingle","value":"// attr: int @xml(,attr) @xml(a1,attr) @go(Attr)"},
129225
{"type":"Text","value":"\n"}
130226
]

0 commit comments

Comments
 (0)