Syntax highlighting for ObjectScript
Did you know that Caché (and now InterSystems IRIS) has available parser for ObjectScript ?
Well, technically, lexer. It hides under the name of %SyntaxColor class. This class provides API to the DLL used by Studio to do syntax highlighting. As a result if works only under Windows.
Basic usage of this class is to generate HTML with syntax-highlighted ObjectScript.
For example, following program (taken from class reference for %SyntaxColor):
Set instr=##class(%Stream.TmpCharacter).%New()
Do instr.WriteLine(" set a = 1")
Do instr.WriteLine(" &sql(insert into sometable(A) values(:a))")
Set outstr=##class(%Stream.TmpCharacter).%New()
Set colorer=##class(%SyntaxColor).%New()
Set ret=colorer.Color(instr,outstr,"COS","PF",,,.langs,.coloringerrors)
If 'ret {Write "Fatal error: ",colorer.DLLResultCode,! Quit}
If coloringerrors {
Write "Syntax error(s)",!
}
Do outstr.Rewind()
While 'outstr.AtEnd {
Write outstr.ReadLine(),!
}
Generates following output:
USER>do ^test
<FONT COLOR="#0000ff">set </FONT><FONT COLOR="#800000">a </FONT><FONT COLOR="#000000">= 1<BR>
</FONT><FONT COLOR="#800080">&sql(</FONT><FONT COLOR="#0000ff">insert </FONT><FONT COLOR="#000080">into </FONT><FONT COLOR="#008000">sometable</FONT><FONT COLOR="#000000">(</FONT><FONT COLOR="#008000">A</FONT><FONT COLOR="#000000">) </FONT><FONT COLOR="#000080">values</FONT><FONT COLOR="#000000">(</FONT><FONT COLOR="#800000">:a</FONT><FONT COLOR="#000000">)</FONT><FONT COLOR="#800080">)</FONT>
That results in following HTML:
Here in that sample program instr is the stream with the source code we want to highlight, outstr – resulting stream. “COS” – the language, ObjectScript (other values are “JS”, ”HTML”). The most interesting part in the call is fourth argument – “PF”. Here “P” means “don’t add enclosing <PRE> element”, and “F” – “no enclosing element with background and foreground”. For more available options – see class reference for method Color.
There is a small test page available on InterSystems Open Exchange as cache-objectscript-syntax-colorer, that uses %SyntaxColor to create highlighted ObjectScript code.
The most interesting part of %SyntaxColor, however, is that it generates not only HTML, but also XML.
For example, if in previous program we replace “PF” with “Q=N”, then result would be following:
USER>do ^test
<color>
<line>
<WhiteSpace> </WhiteSpace>
<Command>set</Command>
<WhiteSpace> </WhiteSpace>
<Localvariable>a</Localvariable>
<WhiteSpace> </WhiteSpace>
<Operator>=</Operator>
<WhiteSpace> </WhiteSpace>
<Number>1</Number>
</line>
<line>
<WhiteSpace> </WhiteSpace>
<SQL>&</SQL>
<SQL>sql</SQL>
<SQL>(</SQL>
<Statementkeyword>insert</Statementkeyword>
<WhiteSpace> </WhiteSpace>
<Qualifierkeyword>into</Qualifierkeyword>
<WhiteSpace> </WhiteSpace>
<Identifier>sometable</Identifier>
<Delimiter>(</Delimiter>
<Identifier>A</Identifier>
<Delimiter>)</Delimiter>
<WhiteSpace> </WhiteSpace>
<Qualifierkeyword>values</Qualifierkeyword>
<Delimiter>(</Delimiter>
<Hostvariablename>:a</Hostvariablename>
<Delimiter>)</Delimiter>
<SQL>)</SQL>
</line>
And that is much more interesting, having this output we can analyze text of our ObjectScript programs. For example, find places in the code that violate Code guidelines and use shortcut names.
Another use case is determining if this particular line of ObjectScript is executable or just comment or whitespace. That can be useful for Unit Test coverage – see “Unit Test Coverage” presentation by @Timothy Leavitt from Global Summit 2018 (%SyntaxColor is mentioned at 31:10). Unit Test coverage tool is also available on Open Exchange.
If you create something interesting with this class – let us know in comments!