Skip to content

Commit

Permalink
lexers/cue: support CUE attributes (#961)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
myitcv committed Apr 28, 2024
1 parent 9347b55 commit 1e983e7
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lexers/embedded/cue.xml
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
<rule pattern="(true|false|null|_)\b">
<token type="KeywordConstant"/>
</rule>
<rule pattern="#?[_a-zA-Z$]\w*">
<rule pattern="[@#]?[_a-zA-Z$]\w*">
<token type="Name"/>
</rule>
</state>
Expand Down
20 changes: 20 additions & 0 deletions lexers/testdata/cue.actual
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,23 @@ _hidden: int
regular: int
$id: int
#definition: int

@protobuf(proto3)

myStruct1: {
// Struct attribute:
@jsonschema(id="https://example.org/mystruct1.json")

// Field attributes
field: string @go(Field)
attr: int @xml(,attr) @go(Attr)
}

myStruct2: {
field: string @go(Field)
attr: int @xml(a1,attr) @go(Attr)
}

Combined: myStruct1 & myStruct2
// field: string @go(Field)
// attr: int @xml(,attr) @xml(a1,attr) @go(Attr)
96 changes: 96 additions & 0 deletions lexers/testdata/cue.expected
Original file line number Diff line number Diff line change
Expand Up @@ -126,5 +126,101 @@
{"type":"Operator","value":":"},
{"type":"Text","value":" "},
{"type":"KeywordType","value":"int"},
{"type":"Text","value":"\n\n"},
{"type":"Name","value":"@protobuf"},
{"type":"Punctuation","value":"("},
{"type":"Name","value":"proto3"},
{"type":"Punctuation","value":")"},
{"type":"Text","value":"\n\n"},
{"type":"Name","value":"myStruct1"},
{"type":"Operator","value":":"},
{"type":"Text","value":" "},
{"type":"Punctuation","value":"{"},
{"type":"Text","value":"\n "},
{"type":"CommentSingle","value":"// Struct attribute:"},
{"type":"Text","value":"\n "},
{"type":"Name","value":"@jsonschema"},
{"type":"Punctuation","value":"("},
{"type":"Name","value":"id"},
{"type":"Operator","value":"="},
{"type":"LiteralString","value":"\"https://example.org/mystruct1.json\""},
{"type":"Punctuation","value":")"},
{"type":"Text","value":"\n\n "},
{"type":"CommentSingle","value":"// Field attributes"},
{"type":"Text","value":"\n "},
{"type":"Name","value":"field"},
{"type":"Operator","value":":"},
{"type":"Text","value":" "},
{"type":"KeywordType","value":"string"},
{"type":"Text","value":" "},
{"type":"Name","value":"@go"},
{"type":"Punctuation","value":"("},
{"type":"Name","value":"Field"},
{"type":"Punctuation","value":")"},
{"type":"Text","value":"\n "},
{"type":"Name","value":"attr"},
{"type":"Operator","value":":"},
{"type":"Text","value":" "},
{"type":"KeywordType","value":"int"},
{"type":"Text","value":" "},
{"type":"Name","value":"@xml"},
{"type":"Punctuation","value":"("},
{"type":"Operator","value":","},
{"type":"Name","value":"attr"},
{"type":"Punctuation","value":")"},
{"type":"Text","value":" "},
{"type":"Name","value":"@go"},
{"type":"Punctuation","value":"("},
{"type":"Name","value":"Attr"},
{"type":"Punctuation","value":")"},
{"type":"Text","value":"\n"},
{"type":"Punctuation","value":"}"},
{"type":"Text","value":"\n\n"},
{"type":"Name","value":"myStruct2"},
{"type":"Operator","value":":"},
{"type":"Text","value":" "},
{"type":"Punctuation","value":"{"},
{"type":"Text","value":"\n "},
{"type":"Name","value":"field"},
{"type":"Operator","value":":"},
{"type":"Text","value":" "},
{"type":"KeywordType","value":"string"},
{"type":"Text","value":" "},
{"type":"Name","value":"@go"},
{"type":"Punctuation","value":"("},
{"type":"Name","value":"Field"},
{"type":"Punctuation","value":")"},
{"type":"Text","value":"\n "},
{"type":"Name","value":"attr"},
{"type":"Operator","value":":"},
{"type":"Text","value":" "},
{"type":"KeywordType","value":"int"},
{"type":"Text","value":" "},
{"type":"Name","value":"@xml"},
{"type":"Punctuation","value":"("},
{"type":"Name","value":"a1"},
{"type":"Operator","value":","},
{"type":"Name","value":"attr"},
{"type":"Punctuation","value":")"},
{"type":"Text","value":" "},
{"type":"Name","value":"@go"},
{"type":"Punctuation","value":"("},
{"type":"Name","value":"Attr"},
{"type":"Punctuation","value":")"},
{"type":"Text","value":"\n"},
{"type":"Punctuation","value":"}"},
{"type":"Text","value":"\n\n"},
{"type":"Name","value":"Combined"},
{"type":"Operator","value":":"},
{"type":"Text","value":" "},
{"type":"Name","value":"myStruct1"},
{"type":"Text","value":" "},
{"type":"Operator","value":"\u0026"},
{"type":"Text","value":" "},
{"type":"Name","value":"myStruct2"},
{"type":"Text","value":"\n"},
{"type":"CommentSingle","value":"// field: string @go(Field)"},
{"type":"Text","value":"\n"},
{"type":"CommentSingle","value":"// attr: int @xml(,attr) @xml(a1,attr) @go(Attr)"},
{"type":"Text","value":"\n"}
]

0 comments on commit 1e983e7

Please sign in to comment.