Another interesting syntax highlighting challenge

Discuss syntax highlighting or submit new syntax files.
Post Reply
jgodfrey
Posts: 471
Joined: 19 Aug 2011 23:02
Location: Missouri, USA

Another interesting syntax highlighting challenge

Post by jgodfrey »

I work with a lot of NC code (for guiding numerically controlled machinery). These machines tend to use a form of code that's generically referred to as G-Code. Though there are lots of variations, it mostly consists of key/value pairs. Each "key" is generally an alpha character and each "value" is some sort of number.

I've attached an image containing some sample code on the left, and then some syntax highlighting on the right (just done by me, by hand, for sake of discussion).

I attempted to set up a TextEd highlighter last night for this, but ran into an issue immediately because there are not spaces separating things (though there can be).

For instance, it's not possible to define "G00" (4th line from the bottom above, in green) as keyword, as it's not separated from the "X" that follows. Defining "X" as a "symbol" for use as a word separator is not really an option...

In the example, some key/value pairs have specific meaning. In that case, I've colorized them in green or pink (two different categories). In other cases, the value portion of a key/value pair is a coordinate in space, so it's unpredictable. For instance, the "31.1605" on the last line. Usually, X, Y, I, and J are associated with coordinate values, so I'd like to just pick them off on their own for visibility, while leaving the value just the standard text color.

The main challenge for a highlighter is to be able to pick off defined "words" without requiring white space separators. Also, it would need to be able to determine the difference between (for instance) "M10" and "M101". That is, it wouldn't be correct to "colorize" the "M10" portion of "M101" and leave the trailing 1 all by itself.

This type of code is fairly easy to pick apart using simple regular expressions, which might add a lot of power to TextEd's syntax highlighting. For instance, all key/value pairs in the file can be picked off using something like this:

Code: Select all

[A-Z]+[0-9.\-]+
or all of the "M" codes (the pink items) can be picked off with this

Code: Select all

M[0-9]+
That's probably enough for now. If further discussion is warranted, I can supply more details as necessary. It's certainly not critical that I'm able to highlight this type of file though it would be nice. Really, I just present this info in case it can be worked into some future highlighter enhancement.

Thanks,

Jeff
Attachments
G-Code syntax highlighting
G-Code syntax highlighting
GCodeSyntax.png (62.79 KiB) Viewed 25464 times
User avatar
pjj
Posts: 2109
Joined: 13 Oct 2009 13:48
Location: Kraków, Poland

Re: Another interesting syntax highlighting challenge

Post by pjj »

I know very little about inner RJ TextEd architecture, but one thing I know: it can run scripts (e.g. JavaScript--cfr. Zen coding in RJ TE!). So, why not integrate an existing JS for syntax highlighting? That would require, I think, writing a wrapper and not much more (I may be flat out wrong, naturally!) There are many open-source highlighters out there, among them certain SyntaxHighlighter by Alex Gorbatchev. This script:
* is actively maintained (now on GitHub)
* ...and versatile
* comes with many "official" (=bundled) syntaxes cfr. http://alexgorbatchev.com/SyntaxHighlig ... l/brushes/
* ...and also many "unofficial" http://www.undermyhat.org/blog/2009/09/ ... ighligher/ (Fortran is "work in progress")
It is based on regex, cfr. http://www.java2s.com/Open-Source/Javas ... phi.js.htm so you could contribute NC code highlighter to the world :wink: (And yes, there's YAML highlighter for me, too.)

Rickard, what do you think?
Alium tibi quaere fratrem; hic, quem tuum putas, meus est. Titus Flāvius Caesar Vespasiānus Augustus
User avatar
Rickard Johansson
Site Admin
Posts: 6577
Joined: 19 Jul 2006 14:29

Re: Another interesting syntax highlighting challenge

Post by Rickard Johansson »

I'll see if I can add support for regex in keyword identifying.

Maybe something like:

Code: Select all

[Keywords]
Normal keywords=AND|break|case|...
#Key/Value pair=[A-Z]+[0-9.\-]+
#M codes=M[0-9]+
User avatar
Rickard Johansson
Site Admin
Posts: 6577
Joined: 19 Jul 2006 14:29

Re: Another interesting syntax highlighting challenge

Post by Rickard Johansson »

Added to v7.63. Please test it in the upcoming beta release.

pjj
Rickard, what do you think?
The memo component in RJ TextEd is not a standard text component (like Rich Text Edit Control). Adding script support for highlighting would probably require some work...
jgodfrey
Posts: 471
Joined: 19 Aug 2011 23:02
Location: Missouri, USA

Re: Another interesting syntax highlighting challenge

Post by jgodfrey »

Rickard Johansson wrote:Added to v7.63. Please test it in the upcoming beta release.
I'd be happy to. Thanks!

Jeff
jgodfrey
Posts: 471
Joined: 19 Aug 2011 23:02
Location: Missouri, USA

Re: Another interesting syntax highlighting challenge

Post by jgodfrey »

Hi Rickard,

I'm working on the regex definitions for my G-Code file. First, these work exactly as expected:

Code: Select all

#Motion - G0*[0-3]
#M-Codes - M[0-9]+
However, this doesn't work as I'd expected:

Code: Select all

#Coordinates - [XYIJ]
I was hoping to pick off all X, Y, I, or J items in a line of code. For instance, this:

Code: Select all

X10.0Y20.0I22.6J15.4
It seems to stop scanning the line after the first match is found. So, above, it'll find the X, by not the Y, I, or J. Testing the same regex in the Find dialog, it reacts the same way. Is that working as expected?

Thanks,

Jeff
User avatar
Rickard Johansson
Site Admin
Posts: 6577
Joined: 19 Jul 2006 14:29

Re: Another interesting syntax highlighting challenge

Post by Rickard Johansson »

I can't reproduce it using the find dialog so that seems to work, but I'll try to fix the syntax highlighting issue.
jgodfrey
Posts: 471
Joined: 19 Aug 2011 23:02
Location: Missouri, USA

Re: Another interesting syntax highlighting challenge

Post by jgodfrey »

Rickard Johansson wrote:I can't reproduce it using the find dialog so that seems to work, but I'll try to fix the syntax highlighting issue.
Doh! You're right. I didn't look closely enough. I was using the find dialog to "Find All", and noticed that only 1 item was highlighted in each found line. However, I didn't notice that the line was repeated for each match. So, find does work as expected, though the highlighter does not.

Jeff
User avatar
Rickard Johansson
Site Admin
Posts: 6577
Joined: 19 Jul 2006 14:29

Re: Another interesting syntax highlighting challenge

Post by Rickard Johansson »

I've made a small change in the draw function and it seems to be working now.
jgodfrey
Posts: 471
Joined: 19 Aug 2011 23:02
Location: Missouri, USA

Re: Another interesting syntax highlighting challenge

Post by jgodfrey »

Thanks.
jgodfrey
Posts: 471
Joined: 19 Aug 2011 23:02
Location: Missouri, USA

Re: Another interesting syntax highlighting challenge

Post by jgodfrey »

Rickard Johansson wrote:I've made a small change in the draw function and it seems to be working now.
I assume this'll be part of 7.63 beta 2, right?

Jeff
jgodfrey
Posts: 471
Joined: 19 Aug 2011 23:02
Location: Missouri, USA

Re: Another interesting syntax highlighting challenge

Post by jgodfrey »

Rickard Johansson wrote:I've made a small change in the draw function and it seems to be working now.
Just tried this in 7.63 b2 and it's working great - thanks!

Jeff
Post Reply