Syntax Highlighting for HAL in Nano

More
15 Jun 2023 23:16 - 16 Jun 2023 16:09 #273660 by MrRekt
I made this as a learning exercise as I read through the HAL man pages. I figured someone here might make use of it - perhaps even help me improve it.
Of course, it'll look different depending on how you have your terminal set up, but on mine it looks like this:


If you haven't already, you'll need to enable syntax highlighting for Nano. One way to do this is by opening /etc/nanorc and finding and un-commenting the second line below:
## To include most of the existing syntax definitions, you can do:
# include "/usr/share/nano/*.nanorc"
Then save the following to /usr/share/nano/hal.nanorc :
## Syntax highlighting for HAL files.
# Note if editing: Precedence of regex impacts how highlighting is displayed.

syntax hal "\.[hH]{1}[aA]{1}[lL]{1}$"
comment "#"

# Formatting for precedence.
color italic,normal "^loadrt.*"

# Read/write names.
color lightmagenta "(read|write)"

# Thread names.
color blue "(servo|base)\-thread"

# Commands.
color italic,blue "^(addf|alias|delf|delsig|getp|gets|ptype|stype|help|linkpp|linkps|inksp|list|loadrt|loadusr|lock|net|newsig|save|setp|ets|show|source|start|status|stop|unalias|unlinkp|unload|nloadrt|unloadusr|unlock|waitusr)"

# Components.
color lightred "([[:space:]]|\,|=)(motion|iocontrol|classicladder|halui|stepgen|encoder|pid|siggen|supply)"

# Axis labels and numaric IDs.
color lightblue "(\.|-|[[:space:]])(x|y|z|a|b|c|u|v|w|[[:digit:]]{1,2})(\.|\,|-|[[:space:]]|$)"

# Formatting for precedence/correction.
color normal "(\.|-|_|,|=)"

# INI references.
color italic,lightyellow "\[\S+]\(\S+\)"
color italic,lightgreen "\[\S+\]"
color lightyellow "\][A-Z0-9\_]+"
color cyan "(\[|\]|\(|\))"

# Thread names.
color blue "(servo|base)\-thread"

# Strings.
color lightcyan start="\"" end="\""

# Numbers and booleans.
color lightmagenta "\s\-?[[:digit:]]+(\.[[:digit:]]+)?"
color lightmagenta "(true|false)"

# Comments.
color lightblack "\#.*"

# Reminders.
color bold,brightblack,yellow "\<FIXME|TODO|XXX)\>"

As you can see, Nano's highlighting rules are limited to regex, but this should be fine for the purposes of a declarative language.
All feedback is welcome - even if it's telling me I'm barking up the wrong tree.
Attachments:
Last edit: 16 Jun 2023 16:09 by MrRekt. Reason: Removed useless duplicate code-block; removed forum-post editing artifacts.
The following user(s) said Thank You: tommylight, beefy, Aciera

Please Log in or Create an account to join the conversation.

More
18 Jun 2023 23:13 #273815 by beefy
That's very good to know.

I often increase the spacing so it's much clearer to my eye.

With this syntax highlighting there won't be as much need to do this.

Please Log in or Create an account to join the conversation.

More
18 Jun 2023 23:27 #273817 by tommylight
OK, i juts gave it a try and it works:

Thank you.
Attachments:

Please Log in or Create an account to join the conversation.

More
20 Jun 2023 17:56 - 20 Jun 2023 18:12 #273952 by MrRekt
A couple of quick fixes:
  • Regex fix where "unload" would match before "unloadrt", which wouldn't highlight the whole word (and similar cases)
  • Highlighted axis/numeric parts of names will now be highlighted if only separated by a "." (as in tommylight's screenshot)
  • Everything following commands starting with "load" or "unload" will now be italicized unless otherwise formatted (as opposed to just "loadrt")
  • Added a couple more components I missed.
    ## Syntax highlighting for HAL files.
    # Note if editing: Precedence of regex impacts how highlighting is displayed.
    
    syntax hal "\.[hH]{1}[aA]{1}[lL]{1}$"
    comment "#"
    
    # Formatting for precedence.
    color italic,normal "^(un)?load.*"
    
    # Read/write names.
    color lightmagenta "(read|write)"
    
    # Thread names.
    color blue "(servo|base)\-thread"
    
    # Commands.
    color italic,blue "^(unalias|unlinkp|unload|unloadrt|unloadusr|unlock|addf|alias|delf|delsig|getp|gets|ptype|stype|help|linkpp|linkps|inksp|list|loadrt|loadusr|lock|net|newsig|save|setp|sets|show|source|start|status|stop|waitusr)"
    
    # Components.
    color lightred "([[:space:]]|\.|\,|=)(motion|iocontrol|classicladder|halui|stepgen|encoder|parport|pid|siggen|spindle|stepgen|supply)"
    # add hal_.+[[:space:]] ?
    
    
    # Axis labels and numaric IDs.
    color lightblue "((\.|-|[[:space:]])(x|y|z|a|b|c|u|v|w|s|[[:digit:]]{1,2}))+(\.|\,|-|[[:space:]]|$)"
    
    # Formatting for precedence/correction.
    color normal "(\.|-|_|,|=)"
    
    # INI references.
    color italic,lightyellow "\[\S+]\(\S+\)"
    color italic,lightgreen "\[\S+\]"
    color lightyellow "\][A-Z0-9\_]+"
    color cyan "(\[|\]|\(|\))"
    
    # Thread names.
    color blue "(servo|base)\-thread"
    
    # Strings.
    color lightcyan start="\"" end="\""
    
    # Numbers and booleans.
    color lightmagenta "\s\-?[[:digit:]]+(\.[[:digit:]]+)?"
    color lightmagenta "(true|false)"
    
    # Comments.
    color lightblack "\#.*"
    
    # Reminders.
    color bold,brightblack,yellow "\<;(FIXME|TODO|XXX)\>"

    Let me know as you find other things to be improved.
Last edit: 20 Jun 2023 18:12 by MrRekt. Reason: Accidentally deleted the "s" in "sets" under commands.
The following user(s) said Thank You: tommylight, Aciera

Please Log in or Create an account to join the conversation.

Time to create page: 0.693 seconds
Powered by Kunena Forum