Syntax Highlighting - NetLogo/NetLogo GitHub Wiki

Want to build a NetLogo syntax highlighter for your favorite text editor or formatting engine? The following information should help.

Note that if you're on the JVM, you might want to just call our routines directly, rather than rolling your equivalents.

Existing highlighters and formatters

see the Tools section of http://ccl.northwestern.edu/netlogo/resources.shtml

Background

You'll want to have read this:

Lexical syntax

The lexical syntax of NetLogo is here:

The actual lexer is generated from this specification using JFlex.

To classify identifiers, the lexer calls out to this class:

which has methods like isConstant(), isKeyword(), and so on.

The isCommand() and isReporter() methods consult this file, which lists all of the primitive commands and reporters:

The actual colors used for syntax-highlighting are here:

Indenting

NetLogo's auto-indenter is here:

Test cases are here:

The indenter does an okay job, but it could do a lot better. See indent.txt for a number of commented-out test cases corresponding to potential improvements.

Parsing (advanced)

If you just want to do syntax-highlighting the same way the NetLogo application does it, you don't need to parse the code. The coloring is based solely on the lexical syntax.

If you do have some reason to want to parse, well... parsing NetLogo is rather complicated. We don't have a formal specification on how it's done, or even an informal one (we do have a description of the various phases involved in parsing. Have a look here) We only have the Scala code at:

StructureParser processes declarations and splits the program body up into procedures. The 5.x StructureParser is a bunch of ad hoc code. The StructureParser on 6.x and headless uses parser combinators and cleanly separates the specification of program structure from implementation specifics, so the grammar it parses can easily be seen in the code.

Procedure bodies are parsed by ExpressionParser. Unit tests for ExpressionParser are here:

ExpressionParser is guided by the Syntax objects for each primitive. The particular syntax for each primitive is in the source file for that primitive, but all the syntaxes are also shown together as text in the following test:

For guidance on the meaning of the different syntaxes, see the comments in this source file:

⚠️ **GitHub.com Fallback** ⚠️