Repository: alecthomas/chroma
Pull request #294 (2019-10-20 13:09:55)

Add J lexer
Generated with `_tools/pygments2chroma.py`.

I tested this out in a local Hugo instance with an example from https://code.jsoftware.com/wiki/Studio/IdiosyncraticIntroduction#Prefix, using the solarized-light theme:

<img width="447" alt="image" src="https://user-images.githubusercontent.com/1120328/67159930-fed0d200-f342-11e9-9f9f-bb251017dfe6.png">

alecthomas: Add a J sample to tests? See README in lexers directory.
smcgivern: Thanks @alecthomas, I totally missed that README. I've added a sample now from https://github.com/jsoftware/jsource/blob/j808-release/jlibrary/system/main/stdlib.ijs

Patch:
diff --git a/README.md b/README.md
index 499fe4f..ac51f49 100644
--- a/README.md
+++ b/README.md
@@ -44,7 +44,7 @@ F | Factor, Fish, Forth, Fortran, FSharp
 G | GAS, GDScript, Genshi, Genshi HTML, Genshi Text, GLSL, Gnuplot, Go, Go HTML Template, Go Text Template, GraphQL, Groovy
 H | Handlebars, Haskell, Haxe, HCL, Hexdump, HTML, HTTP, Hy
 I | Idris, INI, Io
-J | Java, JavaScript, JSON, Julia, Jungle
+J | J, Java, JavaScript, JSON, Julia, Jungle
 K | Kotlin
 L | Lighttpd configuration file, LLVM, Lua
 M | Mako, markdown, Mason, Mathematica, Matlab, MiniZinc, Modula-2, MonkeyC, MorrowindScript, Myghty, MySQL
diff --git a/lexers/j/j.go b/lexers/j/j.go
new file mode 100644
index 0000000..686e53b
--- /dev/null
+++ b/lexers/j/j.go
@@ -0,0 +1,73 @@
+package j
+
+import (
+	. "github.com/alecthomas/chroma" // nolint
+	"github.com/alecthomas/chroma/lexers/internal"
+)
+
+// J lexer.
+var J = internal.Register(MustNewLexer(
+	&Config{
+		Name:      "J",
+		Aliases:   []string{"j"},
+		Filenames: []string{"*.ijs"},
+		MimeTypes: []string{"text/x-j"},
+	},
+	Rules{
+		"root": {
+			{`#!.*$`, CommentPreproc, nil},
+			{`NB\..*`, CommentSingle, nil},
+			{`\n+\s*Note`, CommentMultiline, Push("comment")},
+			{`\s*Note.*`, CommentSingle, nil},
+			{`\s+`, Text, nil},
+			{`'`, LiteralString, Push("singlequote")},
+			{`0\s+:\s*0|noun\s+define\s*$`, NameEntity, Push("nounDefinition")},
+			{`(([1-4]|13)\s+:\s*0|(adverb|conjunction|dyad|monad|verb)\s+define)\b`, NameFunction, Push("explicitDefinition")},
+			{Words(``, `\b[a-zA-Z]\w*\.`, `for_`, `goto_`, `label_`), NameLabel, nil},
+			{Words(``, `\.`, `assert`, `break`, `case`, `catch`, `catchd`, `catcht`, `continue`, `do`, `else`, `elseif`, `end`, `fcase`, `for`, `if`, `return`, `select`, `throw`, `try`, `while`, `whilst`), NameLabel, nil},
+			{`\b[a-zA-Z]\w*`, NameVariable, nil},
+			{Words(``, ``, `ARGV`, `CR`, `CRLF`, `DEL`, `Debug`, `EAV`, `EMPTY`, `FF`, `JVERSION`, `LF`, `LF2`, `Note`, `TAB`, `alpha17`, `alpha27`, `apply`, `bind`, `boxopen`, `boxxopen`, `bx`, `clear`, `cutLF`, `cutopen`, `datatype`, `def`, `dfh`, `drop`, `each`, `echo`, `empty`, `erase`, `every`, `evtloop`, `exit`, `expand`, `fetch`, `file2url`, `fixdotdot`, `fliprgb`, `getargs`, `getenv`, `hfd`, `inv`, `inverse`, `iospath`, `isatty`, `isutf8`, `items`, `leaf`, `list`, `nameclass`, `namelist`, `names`, `nc`, `nl`, `on`, `pick`, `rows`, `script`, `scriptd`, `sign`, `sminfo`, `smoutput`, `sort`, `split`, `stderr`, `stdin`, `stdout`, `table`, `take`, `timespacex`, `timex`, `tmoutput`, `toCRLF`, `toHOST`, `toJ`, `tolower`, `toupper`, `type`, `ucp`, `ucpcount`, `usleep`, `utf8`, `uucp`), NameFunction, nil},
+			{`=[.:]`, Operator, nil},
+			{"[-=+*#$%@!~`^&\";:.,<>{}\\[\\]\\\\|/]", Operator, nil},
+			{`[abCdDeEfHiIjLMoprtT]\.`, KeywordReserved, nil},
+			{`[aDiLpqsStux]\:`, KeywordReserved, nil},
+			{`(_[0-9])\:`, KeywordConstant, nil},
+			{`\(`, Punctuation, Push("parentheses")},
+			Include("numbers"),
+		},
+		"comment": {
+			{`[^)]`, CommentMultiline, nil},
+			{`^\)`, CommentMultiline, Pop(1)},
+			{`[)]`, CommentMultiline, nil},
+		},
+		"explicitDefinition": {
+			{`\b[nmuvxy]\b`, NameDecorator, nil},
+			Include("root"),
+			{`[^)]`, Name, nil},
+			{`^\)`, NameLabel, Pop(1)},
+			{`[)]`, Name, nil},
+		},
+		"numbers": {
+			{`\b_{1,2}\b`, LiteralNumber, nil},
+			{`_?\d+(\.\d+)?(\s*[ejr]\s*)_?\d+(\.?=\d+)?`, LiteralNumber, nil},
+			{`_?\d+\.(?=\d+)`, LiteralNumberFloat, nil},
+			{`_?\d+x`, LiteralNumberIntegerLong, nil},
+			{`_?\d+`, LiteralNumberInteger, nil},
+		},
+		"nounDefinition": {
+			{`[^)]`, LiteralString, nil},
+			{`^\)`, NameLabel, Pop(1)},
+			{`[)]`, LiteralString, nil},
+		},
+		"parentheses": {
+			{`\)`, Punctuation, Pop(1)},
+			Include("explicitDefinition"),
+			Include("root"),
+		},
+		"singlequote": {
+			{`[^']`, LiteralString, nil},
+			{`''`, LiteralString, nil},
+			{`'`, LiteralString, Pop(1)},
+		},
+	},
+))


Test patch:
diff --git a/lexers/testdata/j.actual b/lexers/testdata/j.actual
new file mode 100644
index 0000000..75e6449
--- /dev/null
+++ b/lexers/testdata/j.actual
@@ -0,0 +1,6 @@
+NB. from stdlib.ijs
+toupper=: 3 : 0`((((65+i.26){a.)(97+i.26)}a.) {~ a. i. ])@.(2 = 3!:0)
+x=. I. 26 > n=. ((97+i.26){a.) i. t=. ,y
+($y) $ ((x{n) { (65+i.26){a.) x}t
+)
+
diff --git a/lexers/testdata/j.expected b/lexers/testdata/j.expected
new file mode 100644
index 0000000..afe2472
--- /dev/null
+++ b/lexers/testdata/j.expected
@@ -0,0 +1,119 @@
+[
+  {"type":"CommentSingle","value":"NB. from stdlib.ijs"},
+  {"type":"Text","value":"\n"},
+  {"type":"NameVariable","value":"toupper"},
+  {"type":"Operator","value":"=:"},
+  {"type":"Text","value":" "},
+  {"type":"NameFunction","value":"3 : 0"},
+  {"type":"Operator","value":"`"},
+  {"type":"Punctuation","value":"(((("},
+  {"type":"LiteralNumberInteger","value":"65"},
+  {"type":"Operator","value":"+"},
+  {"type":"NameVariable","value":"i"},
+  {"type":"Operator","value":"."},
+  {"type":"LiteralNumberInteger","value":"26"},
+  {"type":"Punctuation","value":")"},
+  {"type":"Operator","value":"{"},
+  {"type":"NameVariable","value":"a"},
+  {"type":"Operator","value":"."},
+  {"type":"Punctuation","value":")("},
+  {"type":"LiteralNumberInteger","value":"97"},
+  {"type":"Operator","value":"+"},
+  {"type":"NameVariable","value":"i"},
+  {"type":"Operator","value":"."},
+  {"type":"LiteralNumberInteger","value":"26"},
+  {"type":"Punctuation","value":")"},
+  {"type":"Operator","value":"}"},
+  {"type":"NameVariable","value":"a"},
+  {"type":"Operator","value":"."},
+  {"type":"Punctuation","value":")"},
+  {"type":"Text","value":" "},
+  {"type":"Operator","value":"{~"},
+  {"type":"Text","value":" "},
+  {"type":"NameVariable","value":"a"},
+  {"type":"Operator","value":"."},
+  {"type":"Text","value":" "},
+  {"type":"NameVariable","value":"i"},
+  {"type":"Operator","value":"."},
+  {"type":"Text","value":" "},
+  {"type":"Operator","value":"]"},
+  {"type":"Punctuation","value":")"},
+  {"type":"Operator","value":"@."},
+  {"type":"Punctuation","value":"("},
+  {"type":"LiteralNumberInteger","value":"2"},
+  {"type":"Text","value":" "},
+  {"type":"Operator","value":"="},
+  {"type":"Text","value":" "},
+  {"type":"LiteralNumberInteger","value":"3"},
+  {"type":"Operator","value":"!:"},
+  {"type":"LiteralNumberInteger","value":"0"},
+  {"type":"Punctuation","value":")"},
+  {"type":"Text","value":"\n"},
+  {"type":"NameDecorator","value":"x"},
+  {"type":"Operator","value":"=."},
+  {"type":"Text","value":" "},
+  {"type":"NameVariable","value":"I"},
+  {"type":"Operator","value":"."},
+  {"type":"Text","value":" "},
+  {"type":"LiteralNumberInteger","value":"26"},
+  {"type":"Text","value":" "},
+  {"type":"Operator","value":"\u003e"},
+  {"type":"Text","value":" "},
+  {"type":"NameDecorator","value":"n"},
+  {"type":"Operator","value":"=."},
+  {"type":"Text","value":" "},
+  {"type":"Punctuation","value":"(("},
+  {"type":"LiteralNumberInteger","value":"97"},
+  {"type":"Operator","value":"+"},
+  {"type":"NameVariable","value":"i"},
+  {"type":"Operator","value":"."},
+  {"type":"LiteralNumberInteger","value":"26"},
+  {"type":"Punctuation","value":")"},
+  {"type":"Operator","value":"{"},
+  {"type":"NameVariable","value":"a"},
+  {"type":"Operator","value":"."},
+  {"type":"Punctuation","value":")"},
+  {"type":"Text","value":" "},
+  {"type":"NameVariable","value":"i"},
+  {"type":"Operator","value":"."},
+  {"type":"Text","value":" "},
+  {"type":"NameVariable","value":"t"},
+  {"type":"Operator","value":"=."},
+  {"type":"Text","value":" "},
+  {"type":"Operator","value":","},
+  {"type":"NameDecorator","value":"y"},
+  {"type":"Text","value":"\n"},
+  {"type":"Punctuation","value":"("},
+  {"type":"Operator","value":"$"},
+  {"type":"NameDecorator","value":"y"},
+  {"type":"Punctuation","value":")"},
+  {"type":"Text","value":" "},
+  {"type":"Operator","value":"$"},
+  {"type":"Text","value":" "},
+  {"type":"Punctuation","value":"(("},
+  {"type":"NameDecorator","value":"x"},
+  {"type":"Operator","value":"{"},
+  {"type":"NameDecorator","value":"n"},
+  {"type":"Punctuation","value":")"},
+  {"type":"Text","value":" "},
+  {"type":"Operator","value":"{"},
+  {"type":"Text","value":" "},
+  {"type":"Punctuation","value":"("},
+  {"type":"LiteralNumberInteger","value":"65"},
+  {"type":"Operator","value":"+"},
+  {"type":"NameVariable","value":"i"},
+  {"type":"Operator","value":"."},
+  {"type":"LiteralNumberInteger","value":"26"},
+  {"type":"Punctuation","value":")"},
+  {"type":"Operator","value":"{"},
+  {"type":"NameVariable","value":"a"},
+  {"type":"Operator","value":"."},
+  {"type":"Punctuation","value":")"},
+  {"type":"Text","value":" "},
+  {"type":"NameDecorator","value":"x"},
+  {"type":"Operator","value":"}"},
+  {"type":"NameVariable","value":"t"},
+  {"type":"Text","value":"\n"},
+  {"type":"NameLabel","value":")"},
+  {"type":"Text","value":"\n\n"}
+]