jow- / ucode

JavaScript-like language with optional templating
ISC License
87 stars 24 forks source link

Syntax highlighting for ucode #178

Open eric-j-ason opened 8 months ago

eric-j-ason commented 8 months ago

I'd like to get support for ucode in syntax highlighters such as Chroma (which is used by Gitea) and Pygments, perhaps by starting with their Javascript definitions and modifying them. Is there a specification of what keywords and other token types exist in ucode?

For reference:

eric-j-ason commented 8 months ago

And in nano, of course, included in OpenWrt!

eric-j-ason commented 8 months ago

A pull request for introducing ucode in Chroma has been created.

jow- commented 8 months ago

I added a pull request to include syntax highlighting in OpenWrt's nano package: https://github.com/openwrt/packages/pull/22623

jow- commented 8 months ago

@eric-j-ason - I suggest to use the nano highlighting I submitted in the other PR as authoritative source for the supported syntax keywords.

See also https://github.com/jow-/ucode/blob/master/lexer.c#L47

There's some special cases like as and from which are syntax elements in import statements but otherwise no reserved identifier (see recently fixed bug at 1468cc406bf9b46ae7942791eaa78f74a185062e).

Another complication is that ucode may run in template mode where by default the file source is uninterpreted plain text and actual ucode logic only appears in {{ ... }} or {% ... %} expression blocks. (See the grey text in screenshot https://user-images.githubusercontent.com/2528802/281428096-478dddef-fe44-4c01-ac3e-63fa6c605f6e.png for illustration)

eric-j-ason commented 8 months ago

@eric-j-ason - I suggest to use the nano highlighting I submitted in the other PR as authoritative source for the supported syntax keywords.

See also https://github.com/jow-/ucode/blob/master/lexer.c#L47

Yes, good idea. Those lines in lexer.c was actually what I used to figure out the list of keywords.

There's some special cases like as and from which are syntax elements in import statements but otherwise no reserved identifier (see recently fixed bug at 1468cc4).

Oh, I see. Does it work the same in JavaScript, or is it different? In what I did for Chroma, I just took JavaScript as the starting point, and adjusted the list of keywords. I'm afraid I don't know how to do anything more complicated in Chroma or any other highlighter.

Another complication is that ucode may run in template mode where by default the file source is uninterpreted plain text and actual ucode logic only appears in {{ ... }} or {% ... %} expression blocks. (See the grey text in screenshot https://user-images.githubusercontent.com/2528802/281428096-478dddef-fe44-4c01-ac3e-63fa6c605f6e.png for illustration)

Yes, also this I didn't know how to do, so I had to leave it out. It's nice that you did it for nano.

I haven't quite figured out how highlighting works on GitHub. Perhaps they use a complete parser, enabling additional features such as getting the definition of a variable by clicking it. This is what they say: "We use Linguist to perform language detection and to select third-party grammars for syntax highlighting. You can find out which keywords are valid in the languages YAML file."

eric-j-ason commented 7 months ago

I added a pull request to include syntax highlighting in OpenWrt's nano package: openwrt/packages#22623

I have noticed that these highlighting rules assume that the file is a template, and breaks on a pure ucode file.