1
0
mirror of https://github.com/alecthomas/chroma.git synced 2025-10-30 23:57:49 +02:00

Moved lexers into alphabetical sub-packages.

This was done to speed up incremental compilation when working on
lexers. That is, modifying a single lexer will no longer require
recompiling all lexers.

This is a (slightly) backwards breaking change in that lexers are no
longer exported directly in the lexers package. The registry API is
"aliased" at the old location.
This commit is contained in:
Alec Thomas
2018-02-15 21:01:44 +11:00
parent 50a6b50a3b
commit 563aadc53c
158 changed files with 566 additions and 424 deletions

View File

@@ -23,7 +23,7 @@ func BenchmarkHTMLFormatter(b *testing.B) {
formatter := New()
b.ResetTimer()
for i := 0; i < b.N; i++ {
it, err := lexers.Go.Tokenise(nil, "package main\nfunc main()\n{\nprintln(`hello world`)\n}\n")
it, err := lexers.Get("go").Tokenise(nil, "package main\nfunc main()\n{\nprintln(`hello world`)\n}\n")
assert.NoError(b, err)
err = formatter.Format(ioutil.Discard, styles.Fallback, it)
assert.NoError(b, err)

View File

@@ -1,11 +1,12 @@
package lexers
package a
import (
. "github.com/alecthomas/chroma" // nolint
"github.com/alecthomas/chroma/lexers/internal"
)
// Abnf lexer.
var Abnf = Register(MustNewLexer(
var Abnf = internal.Register(MustNewLexer(
&Config{
Name: "ABNF",
Aliases: []string{"abnf"},

View File

@@ -1,11 +1,12 @@
package lexers
package a
import (
. "github.com/alecthomas/chroma" // nolint
"github.com/alecthomas/chroma/lexers/internal"
)
// Actionscript lexer.
var Actionscript = Register(MustNewLexer(
var Actionscript = internal.Register(MustNewLexer(
&Config{
Name: "ActionScript",
Aliases: []string{"as", "actionscript"},

View File

@@ -1,11 +1,12 @@
package lexers
package a
import (
. "github.com/alecthomas/chroma" // nolint
"github.com/alecthomas/chroma/lexers/internal"
)
// Actionscript 3 lexer.
var Actionscript3 = Register(MustNewLexer(
var Actionscript3 = internal.Register(MustNewLexer(
&Config{
Name: "ActionScript 3",
Aliases: []string{"as3", "actionscript3"},

View File

@@ -1,11 +1,12 @@
package lexers
package a
import (
. "github.com/alecthomas/chroma" // nolint
"github.com/alecthomas/chroma/lexers/internal"
)
// Ada lexer.
var Ada = Register(MustNewLexer(
var Ada = internal.Register(MustNewLexer(
&Config{
Name: "Ada",
Aliases: []string{"ada", "ada95", "ada2005"},

View File

@@ -1,11 +1,12 @@
package lexers
package a
import (
. "github.com/alecthomas/chroma" // nolint
"github.com/alecthomas/chroma/lexers/internal"
)
// Angular2 lexer.
var Angular2 = Register(MustNewLexer(
var Angular2 = internal.Register(MustNewLexer(
&Config{
Name: "Angular2",
Aliases: []string{"ng2"},

View File

@@ -1,11 +1,12 @@
package lexers
package a
import (
. "github.com/alecthomas/chroma" // nolint
"github.com/alecthomas/chroma/lexers/internal"
)
// ANTLR lexer.
var ANTLR = Register(MustNewLexer(
var ANTLR = internal.Register(MustNewLexer(
&Config{
Name: "ANTLR",
Aliases: []string{"antlr"},

View File

@@ -1,11 +1,12 @@
package lexers
package a
import (
. "github.com/alecthomas/chroma" // nolint
"github.com/alecthomas/chroma/lexers/internal"
)
// Apacheconf lexer.
var Apacheconf = Register(MustNewLexer(
var Apacheconf = internal.Register(MustNewLexer(
&Config{
Name: "ApacheConf",
Aliases: []string{"apacheconf", "aconf", "apache"},

View File

@@ -1,11 +1,12 @@
package lexers
package a
import (
. "github.com/alecthomas/chroma" // nolint
"github.com/alecthomas/chroma/lexers/internal"
)
// Apl lexer.
var Apl = Register(MustNewLexer(
var Apl = internal.Register(MustNewLexer(
&Config{
Name: "APL",
Aliases: []string{"apl"},

View File

@@ -1,11 +1,12 @@
package lexers
package a
import (
. "github.com/alecthomas/chroma" // nolint
"github.com/alecthomas/chroma/lexers/internal"
)
// Applescript lexer.
var Applescript = Register(MustNewLexer(
var Applescript = internal.Register(MustNewLexer(
&Config{
Name: "AppleScript",
Aliases: []string{"applescript"},

View File

@@ -1,11 +1,12 @@
package lexers
package a
import (
. "github.com/alecthomas/chroma" // nolint
"github.com/alecthomas/chroma/lexers/internal"
)
// Awk lexer.
var Awk = Register(MustNewLexer(
var Awk = internal.Register(MustNewLexer(
&Config{
Name: "Awk",
Aliases: []string{"awk", "gawk", "mawk", "nawk"},

View File

@@ -1,33 +0,0 @@
package lexers_test
import (
"io/ioutil"
"testing"
"github.com/alecthomas/assert"
"github.com/alecthomas/chroma/formatters"
"github.com/alecthomas/chroma/lexers"
"github.com/alecthomas/chroma/styles"
)
func TestCompileAllRegexes(t *testing.T) {
for _, lexer := range lexers.Registry.Lexers {
it, err := lexer.Tokenise(nil, "")
assert.NoError(t, err, "%s failed", lexer.Config().Name)
err = formatters.NoOp.Format(ioutil.Discard, styles.SwapOff, it)
assert.NoError(t, err, "%s failed", lexer.Config().Name)
}
}
func TestGet(t *testing.T) {
t.Run("ByName", func(t *testing.T) {
assert.Equal(t, lexers.Get("xml"), lexers.XML)
})
t.Run("ByAlias", func(t *testing.T) {
assert.Equal(t, lexers.Get("as"), lexers.Actionscript)
})
t.Run("ViaFilename", func(t *testing.T) {
assert.Equal(t, lexers.Get("svg"), lexers.XML)
})
}

View File

@@ -1,15 +1,16 @@
package lexers
package b
import (
"regexp"
. "github.com/alecthomas/chroma" // nolint
"github.com/alecthomas/chroma/lexers/internal"
)
var bashAnalyserRe = regexp.MustCompile(`(?m)^#!.*/bin/(?:env |)(?:bash|zsh|sh|ksh)`)
// Bash lexer.
var Bash = Register(MustNewLexer(
var Bash = internal.Register(MustNewLexer(
&Config{
Name: "Bash",
Aliases: []string{"bash", "sh", "ksh", "zsh", "shell"},

View File

@@ -1,11 +1,12 @@
package lexers
package b
import (
. "github.com/alecthomas/chroma" // nolint
"github.com/alecthomas/chroma/lexers/internal"
)
// Batchfile lexer.
var Batchfile = Register(MustNewLexer(
var Batchfile = internal.Register(MustNewLexer(
&Config{
Name: "Batchfile",
Aliases: []string{"bat", "batch", "dosbatch", "winbatch"},

View File

@@ -1,11 +1,12 @@
package lexers
package b
import (
. "github.com/alecthomas/chroma" // nolint
"github.com/alecthomas/chroma/lexers/internal"
)
// Blitzbasic lexer.
var Blitzbasic = Register(MustNewLexer(
var Blitzbasic = internal.Register(MustNewLexer(
&Config{
Name: "BlitzBasic",
Aliases: []string{"blitzbasic", "b3d", "bplus"},

View File

@@ -1,11 +1,12 @@
package lexers
package b
import (
. "github.com/alecthomas/chroma" // nolint
"github.com/alecthomas/chroma/lexers/internal"
)
// Bnf lexer.
var Bnf = Register(MustNewLexer(
var Bnf = internal.Register(MustNewLexer(
&Config{
Name: "BNF",
Aliases: []string{"bnf"},

View File

@@ -1,11 +1,12 @@
package lexers
package b
import (
. "github.com/alecthomas/chroma" // nolint
"github.com/alecthomas/chroma/lexers/internal"
)
// Brainfuck lexer.
var Brainfuck = Register(MustNewLexer(
var Brainfuck = internal.Register(MustNewLexer(
&Config{
Name: "Brainfuck",
Aliases: []string{"brainfuck", "bf"},

View File

@@ -1,11 +1,12 @@
package lexers
package c
import (
. "github.com/alecthomas/chroma" // nolint
"github.com/alecthomas/chroma/lexers/internal"
)
// C lexer.
var C = Register(MustNewLexer(
var C = internal.Register(MustNewLexer(
&Config{
Name: "C",
Aliases: []string{"c"},

View File

@@ -1,11 +1,12 @@
package lexers
package c
import (
. "github.com/alecthomas/chroma" // nolint
"github.com/alecthomas/chroma/lexers/internal"
)
// Cap'N'Proto Proto lexer.
var CapNProto = Register(MustNewLexer(
var CapNProto = internal.Register(MustNewLexer(
&Config{
Name: "Cap'n Proto",
Aliases: []string{"capnp"},

View File

@@ -1,11 +1,12 @@
package lexers
package c
import (
. "github.com/alecthomas/chroma" // nolint
"github.com/alecthomas/chroma/lexers/internal"
)
// Ceylon lexer.
var Ceylon = Register(MustNewLexer(
var Ceylon = internal.Register(MustNewLexer(
&Config{
Name: "Ceylon",
Aliases: []string{"ceylon"},

View File

@@ -1,11 +1,12 @@
package lexers
package c
import (
. "github.com/alecthomas/chroma" // nolint
"github.com/alecthomas/chroma/lexers/internal"
)
// Cfengine3 lexer.
var Cfengine3 = Register(MustNewLexer(
var Cfengine3 = internal.Register(MustNewLexer(
&Config{
Name: "CFEngine3",
Aliases: []string{"cfengine3", "cf3"},

View File

@@ -1,11 +1,12 @@
package lexers
package c
import (
. "github.com/alecthomas/chroma" // nolint
"github.com/alecthomas/chroma/lexers/internal"
)
// Chaiscript lexer.
var Chaiscript = Register(MustNewLexer(
var Chaiscript = internal.Register(MustNewLexer(
&Config{
Name: "ChaiScript",
Aliases: []string{"chai", "chaiscript"},

View File

@@ -1,11 +1,13 @@
package lexers
package c
import (
. "github.com/alecthomas/chroma" // nolint
"github.com/alecthomas/chroma/lexers/internal"
. "github.com/alecthomas/chroma/lexers/p" // nolint
)
// Cheetah lexer.
var Cheetah = Register(MustNewLexer(
var Cheetah = internal.Register(MustNewLexer(
&Config{
Name: "Cheetah",
Aliases: []string{"cheetah", "spitfire"},

View File

@@ -1,11 +1,12 @@
package lexers
package c
import (
. "github.com/alecthomas/chroma" // nolint
"github.com/alecthomas/chroma/lexers/internal"
)
// Common Lisp lexer.
var CommonLisp = Register(MustNewLexer(
var CommonLisp = internal.Register(MustNewLexer(
&Config{
Name: "Common Lisp",
Aliases: []string{"common-lisp", "cl", "lisp"},

View File

@@ -1,11 +1,12 @@
package lexers
package c
import (
. "github.com/alecthomas/chroma" // nolint
"github.com/alecthomas/chroma/lexers/internal"
)
// Clojure lexer.
var Clojure = Register(MustNewLexer(
var Clojure = internal.Register(MustNewLexer(
&Config{
Name: "Clojure",
Aliases: []string{"clojure", "clj"},

View File

@@ -1,11 +1,12 @@
package lexers
package c
import (
. "github.com/alecthomas/chroma" // nolint
"github.com/alecthomas/chroma/lexers/internal"
)
// Cmake lexer.
var Cmake = Register(MustNewLexer(
var Cmake = internal.Register(MustNewLexer(
&Config{
Name: "CMake",
Aliases: []string{"cmake"},

View File

@@ -1,11 +1,12 @@
package lexers
package c
import (
. "github.com/alecthomas/chroma" // nolint
"github.com/alecthomas/chroma/lexers/internal"
)
// Cobol lexer.
var Cobol = Register(MustNewLexer(
var Cobol = internal.Register(MustNewLexer(
&Config{
Name: "COBOL",
Aliases: []string{"cobol"},

View File

@@ -1,11 +1,12 @@
package lexers
package c
import (
. "github.com/alecthomas/chroma" // nolint
"github.com/alecthomas/chroma/lexers/internal"
)
// Coffeescript lexer.
var Coffeescript = Register(MustNewLexer(
var Coffeescript = internal.Register(MustNewLexer(
&Config{
Name: "CoffeeScript",
Aliases: []string{"coffee-script", "coffeescript", "coffee"},

View File

@@ -1,11 +1,12 @@
package lexers
package c
import (
. "github.com/alecthomas/chroma" // nolint
"github.com/alecthomas/chroma/lexers/internal"
)
// Cfstatement lexer.
var Cfstatement = Register(MustNewLexer(
var Cfstatement = internal.Register(MustNewLexer(
&Config{
Name: "cfstatement",
Aliases: []string{"cfs"},

View File

@@ -1,11 +1,12 @@
package lexers
package c
import (
. "github.com/alecthomas/chroma" // nolint
"github.com/alecthomas/chroma/lexers/internal"
)
// Coq lexer.
var Coq = Register(MustNewLexer(
var Coq = internal.Register(MustNewLexer(
&Config{
Name: "Coq",
Aliases: []string{"coq"},

View File

@@ -1,11 +1,12 @@
package lexers
package c
import (
. "github.com/alecthomas/chroma" // nolint
"github.com/alecthomas/chroma/lexers/internal"
)
// CPP lexer.
var CPP = Register(MustNewLexer(
var CPP = internal.Register(MustNewLexer(
&Config{
Name: "C++",
Aliases: []string{"cpp", "c++"},

View File

@@ -1,11 +1,12 @@
package lexers
package c
import (
. "github.com/alecthomas/chroma" // nolint
"github.com/alecthomas/chroma/lexers/internal"
)
// Crystal lexer.
var Crystal = Register(MustNewLexer(
var Crystal = internal.Register(MustNewLexer(
&Config{
Name: "Crystal",
Aliases: []string{"cr", "crystal"},

View File

@@ -1,11 +1,12 @@
package lexers
package c
import (
. "github.com/alecthomas/chroma" // nolint
"github.com/alecthomas/chroma/lexers/internal"
)
// CSharp lexer.
var CSharp = Register(MustNewLexer(
var CSharp = internal.Register(MustNewLexer(
&Config{
Name: "C#",
Aliases: []string{"csharp", "c#"},

View File

@@ -1,11 +1,12 @@
package lexers
package c
import (
. "github.com/alecthomas/chroma" // nolint
"github.com/alecthomas/chroma/lexers/internal"
)
// CSS lexer.
var CSS = Register(MustNewLexer(
var CSS = internal.Register(MustNewLexer(
&Config{
Name: "CSS",
Aliases: []string{"css"},

View File

@@ -1,11 +1,12 @@
package lexers
package c
import (
. "github.com/alecthomas/chroma" // nolint
"github.com/alecthomas/chroma/lexers/internal"
)
// Cython lexer.
var Cython = Register(MustNewLexer(
var Cython = internal.Register(MustNewLexer(
&Config{
Name: "Cython",
Aliases: []string{"cython", "pyx", "pyrex"},

View File

@@ -1,11 +1,12 @@
package lexers
package d
import (
. "github.com/alecthomas/chroma" // nolint
"github.com/alecthomas/chroma/lexers/internal"
)
// Dart lexer.
var Dart = Register(MustNewLexer(
var Dart = internal.Register(MustNewLexer(
&Config{
Name: "Dart",
Aliases: []string{"dart"},

View File

@@ -1,11 +1,12 @@
package lexers
package d
import (
. "github.com/alecthomas/chroma" // nolint
"github.com/alecthomas/chroma/lexers/internal"
)
// Diff lexer.
var Diff = Register(MustNewLexer(
var Diff = internal.Register(MustNewLexer(
&Config{
Name: "Diff",
Aliases: []string{"diff", "udiff"},

View File

@@ -1,11 +1,12 @@
package lexers
package d
import (
. "github.com/alecthomas/chroma" // nolint
"github.com/alecthomas/chroma/lexers/internal"
)
// Django/Jinja lexer.
var DjangoJinja = Register(MustNewLexer(
var DjangoJinja = internal.Register(MustNewLexer(
&Config{
Name: "Django/Jinja",
Aliases: []string{"django", "jinja"},

View File

@@ -1,11 +1,13 @@
package lexers
package d
import (
. "github.com/alecthomas/chroma" // nolint
. "github.com/alecthomas/chroma/lexers/b"
"github.com/alecthomas/chroma/lexers/internal"
)
// Docker lexer.
var Docker = Register(MustNewLexer(
var Docker = internal.Register(MustNewLexer(
&Config{
Name: "Docker",
Aliases: []string{"docker", "dockerfile"},

View File

@@ -1,11 +1,12 @@
package lexers
package d
import (
. "github.com/alecthomas/chroma" // nolint
"github.com/alecthomas/chroma/lexers/internal"
)
// Dtd lexer.
var Dtd = Register(MustNewLexer(
var Dtd = internal.Register(MustNewLexer(
&Config{
Name: "DTD",
Aliases: []string{"dtd"},

View File

@@ -1,20 +0,0 @@
package lexers
import (
"testing"
"github.com/alecthomas/assert"
"github.com/alecthomas/chroma"
)
func TestDiffLexerWithoutTralingNewLine(t *testing.T) {
diffLexer := Get("diff")
it, err := diffLexer.Tokenise(nil, "-foo\n+bar")
assert.NoError(t, err)
actual := it.Tokens()
expected := []*chroma.Token{
&chroma.Token{chroma.GenericDeleted, "-foo\n"},
&chroma.Token{chroma.GenericInserted, "+bar\n"},
}
assert.Equal(t, expected, actual)
}

View File

@@ -1,11 +1,12 @@
package lexers
package e
import (
. "github.com/alecthomas/chroma" // nolint
"github.com/alecthomas/chroma/lexers/internal"
)
// Ebnf lexer.
var Ebnf = Register(MustNewLexer(
var Ebnf = internal.Register(MustNewLexer(
&Config{
Name: "EBNF",
Aliases: []string{"ebnf"},

View File

@@ -1,11 +1,12 @@
package lexers
package e
import (
. "github.com/alecthomas/chroma" // nolint
"github.com/alecthomas/chroma/lexers/internal"
)
// Elixir lexer.
var Elixir = Register(MustNewLexer(
var Elixir = internal.Register(MustNewLexer(
&Config{
Name: "Elixir",
Aliases: []string{"elixir", "ex", "exs"},

View File

@@ -1,11 +1,12 @@
package lexers
package e
import (
. "github.com/alecthomas/chroma" // nolint
"github.com/alecthomas/chroma/lexers/internal"
)
// Elm lexer.
var Elm = Register(MustNewLexer(
var Elm = internal.Register(MustNewLexer(
&Config{
Name: "Elm",
Aliases: []string{"elm"},

View File

@@ -1,7 +1,8 @@
package lexers
package e
import (
. "github.com/alecthomas/chroma" // nolint
"github.com/alecthomas/chroma/lexers/internal"
)
var (
@@ -521,7 +522,7 @@ var (
)
// EmacsLisp lexer.
var EmacsLisp = Register(TypeRemappingLexer(MustNewLexer(
var EmacsLisp = internal.Register(TypeRemappingLexer(MustNewLexer(
&Config{
Name: "EmacsLisp",
Aliases: []string{"emacs", "elisp", "emacs-lisp"},

View File

@@ -1,11 +1,12 @@
package lexers
package e
import (
. "github.com/alecthomas/chroma" // nolint
"github.com/alecthomas/chroma/lexers/internal"
)
// Erlang lexer.
var Erlang = Register(MustNewLexer(
var Erlang = internal.Register(MustNewLexer(
&Config{
Name: "Erlang",
Aliases: []string{"erlang"},

View File

@@ -1,11 +1,12 @@
package lexers
package f
import (
. "github.com/alecthomas/chroma" // nolint
"github.com/alecthomas/chroma/lexers/internal"
)
// Factor lexer.
var Factor = Register(MustNewLexer(
var Factor = internal.Register(MustNewLexer(
&Config{
Name: "Factor",
Aliases: []string{"factor"},

View File

@@ -1,11 +1,12 @@
package lexers
package f
import (
. "github.com/alecthomas/chroma" // nolint
"github.com/alecthomas/chroma/lexers/internal"
)
// Fish lexer.
var Fish = Register(MustNewLexer(
var Fish = internal.Register(MustNewLexer(
&Config{
Name: "Fish",
Aliases: []string{"fish", "fishshell"},

View File

@@ -1,11 +1,12 @@
package lexers
package f
import (
. "github.com/alecthomas/chroma" // nolint
"github.com/alecthomas/chroma/lexers/internal"
)
// Forth lexer.
var Forth = Register(MustNewLexer(
var Forth = internal.Register(MustNewLexer(
&Config{
Name: "Forth",
Aliases: []string{"forth"},

View File

@@ -1,11 +1,12 @@
package lexers
package f
import (
. "github.com/alecthomas/chroma" // nolint
"github.com/alecthomas/chroma/lexers/internal"
)
// Fortran lexer.
var Fortran = Register(MustNewLexer(
var Fortran = internal.Register(MustNewLexer(
&Config{
Name: "Fortran",
Aliases: []string{"fortran"},

View File

@@ -1,11 +1,12 @@
package lexers
package f
import (
. "github.com/alecthomas/chroma" // nolint
"github.com/alecthomas/chroma/lexers/internal"
)
// Fsharp lexer.
var Fsharp = Register(MustNewLexer(
var Fsharp = internal.Register(MustNewLexer(
&Config{
Name: "FSharp",
Aliases: []string{"fsharp"},

View File

@@ -1,11 +1,12 @@
package lexers
package g
import (
. "github.com/alecthomas/chroma" // nolint
"github.com/alecthomas/chroma/lexers/internal"
)
// Gas lexer.
var Gas = Register(MustNewLexer(
var Gas = internal.Register(MustNewLexer(
&Config{
Name: "GAS",
Aliases: []string{"gas", "asm"},

View File

@@ -1,11 +1,12 @@
package lexers
package g
import (
. "github.com/alecthomas/chroma" // nolint
"github.com/alecthomas/chroma/lexers/internal"
)
// GDScript lexer.
var GDScript = Register(MustNewLexer(
var GDScript = internal.Register(MustNewLexer(
&Config{
Name: "GDScript",
Aliases: []string{"gdscript", "gd"},

View File

@@ -1,11 +1,13 @@
package lexers
package g
import (
. "github.com/alecthomas/chroma" // nolint
"github.com/alecthomas/chroma/lexers/internal"
. "github.com/alecthomas/chroma/lexers/p"
)
// Genshi Text lexer.
var GenshiText = Register(MustNewLexer(
var GenshiText = internal.Register(MustNewLexer(
&Config{
Name: "Genshi Text",
Aliases: []string{"genshitext"},
@@ -35,7 +37,7 @@ var GenshiText = Register(MustNewLexer(
))
// Html+Genshi lexer.
var GenshiHTMLTemplate = Register(MustNewLexer(
var GenshiHTMLTemplate = internal.Register(MustNewLexer(
&Config{
Name: "Genshi HTML",
Aliases: []string{"html+genshi", "html+kid"},
@@ -48,7 +50,7 @@ var GenshiHTMLTemplate = Register(MustNewLexer(
))
// Genshi lexer.
var Genshi = Register(MustNewLexer(
var Genshi = internal.Register(MustNewLexer(
&Config{
Name: "Genshi",
Aliases: []string{"genshi", "kid", "xml+genshi", "xml+kid"},

View File

@@ -1,11 +1,12 @@
package lexers
package g
import (
. "github.com/alecthomas/chroma" // nolint
"github.com/alecthomas/chroma/lexers/internal"
)
// GLSL lexer.
var GLSL = Register(MustNewLexer(
var GLSL = internal.Register(MustNewLexer(
&Config{
Name: "GLSL",
Aliases: []string{"glsl"},

View File

@@ -1,11 +1,12 @@
package lexers
package g
import (
. "github.com/alecthomas/chroma" // nolint
"github.com/alecthomas/chroma/lexers/internal"
)
// Gnuplot lexer.
var Gnuplot = Register(MustNewLexer(
var Gnuplot = internal.Register(MustNewLexer(
&Config{
Name: "Gnuplot",
Aliases: []string{"gnuplot"},

View File

@@ -1,13 +1,14 @@
package lexers
package g
import (
"strings"
. "github.com/alecthomas/chroma" // nolint
"github.com/alecthomas/chroma/lexers/internal"
)
// Go lexer.
var Go = Register(MustNewLexer(
var Go = internal.Register(MustNewLexer(
&Config{
Name: "Go",
Aliases: []string{"go", "golang"},

View File

@@ -1,11 +1,12 @@
package lexers
package g
import (
. "github.com/alecthomas/chroma" // nolint
"github.com/alecthomas/chroma/lexers/internal"
)
// Groovy lexer.
var Groovy = Register(MustNewLexer(
var Groovy = internal.Register(MustNewLexer(
&Config{
Name: "Groovy",
Aliases: []string{"groovy"},

View File

@@ -1,11 +1,12 @@
package lexers
package h
import (
. "github.com/alecthomas/chroma" // nolint
"github.com/alecthomas/chroma/lexers/internal"
)
// Handlebars lexer.
var Handlebars = Register(MustNewLexer(
var Handlebars = internal.Register(MustNewLexer(
&Config{
Name: "Handlebars",
Aliases: []string{"handlebars"},

View File

@@ -1,11 +1,12 @@
package lexers
package h
import (
. "github.com/alecthomas/chroma" // nolint
"github.com/alecthomas/chroma/lexers/internal"
)
// Haskell lexer.
var Haskell = Register(MustNewLexer(
var Haskell = internal.Register(MustNewLexer(
&Config{
Name: "Haskell",
Aliases: []string{"haskell", "hs"},

View File

@@ -1,11 +1,12 @@
package lexers
package h
import (
. "github.com/alecthomas/chroma" // nolint
"github.com/alecthomas/chroma/lexers/internal"
)
// Haxe lexer.
var Haxe = Register(MustNewLexer(
var Haxe = internal.Register(MustNewLexer(
&Config{
Name: "Haxe",
Aliases: []string{"hx", "haxe", "hxsl"},

View File

@@ -1,11 +1,12 @@
package lexers
package h
import (
. "github.com/alecthomas/chroma" // nolint
"github.com/alecthomas/chroma/lexers/internal"
)
// Hexdump lexer.
var Hexdump = Register(MustNewLexer(
var Hexdump = internal.Register(MustNewLexer(
&Config{
Name: "Hexdump",
Aliases: []string{"hexdump"},

View File

@@ -1,11 +1,14 @@
package lexers
package h
import (
. "github.com/alecthomas/chroma" // nolint
. "github.com/alecthomas/chroma" // nolint
. "github.com/alecthomas/chroma/lexers/c" // nolint
"github.com/alecthomas/chroma/lexers/internal"
. "github.com/alecthomas/chroma/lexers/j" // nolint
)
// HTML lexer.
var HTML = Register(MustNewLexer(
var HTML = internal.Register(MustNewLexer(
&Config{
Name: "HTML",
Aliases: []string{"html"},

View File

@@ -1,12 +1,14 @@
package lexers
package h
import (
. "github.com/alecthomas/chroma" // nolint
"strings"
. "github.com/alecthomas/chroma" // nolint
"github.com/alecthomas/chroma/lexers/internal"
)
// HTTP lexer.
var HTTP = Register(httpBodyContentTypeLexer(MustNewLexer(
var HTTP = internal.Register(httpBodyContentTypeLexer(MustNewLexer(
&Config{
Name: "HTTP",
Aliases: []string{"http"},
@@ -91,7 +93,7 @@ func (d *httpBodyContentTyper) Tokenise(options *TokeniseOptions, text string) (
}
case token.Type == Generic && contentType != "":
{
lexer := MatchMimeType(contentType)
lexer := internal.MatchMimeType(contentType)
// application/calendar+xml can be treated as application/xml
// if there's not a better match.
@@ -99,7 +101,7 @@ func (d *httpBodyContentTyper) Tokenise(options *TokeniseOptions, text string) (
slashPos := strings.Index(contentType, "/")
plusPos := strings.LastIndex(contentType, "+")
contentType = contentType[:slashPos+1] + contentType[plusPos+1:]
lexer = MatchMimeType(contentType)
lexer = internal.MatchMimeType(contentType)
}
if lexer == nil {

View File

@@ -1,11 +1,12 @@
package lexers
package h
import (
. "github.com/alecthomas/chroma" // nolint
"github.com/alecthomas/chroma/lexers/internal"
)
// Hy lexer.
var Hy = Register(MustNewLexer(
var Hy = internal.Register(MustNewLexer(
&Config{
Name: "Hy",
Aliases: []string{"hylang"},

View File

@@ -1,11 +1,12 @@
package lexers
package i
import (
. "github.com/alecthomas/chroma" // nolint
"github.com/alecthomas/chroma/lexers/internal"
)
// Idris lexer.
var Idris = Register(MustNewLexer(
var Idris = internal.Register(MustNewLexer(
&Config{
Name: "Idris",
Aliases: []string{"idris", "idr"},

View File

@@ -1,11 +1,12 @@
package lexers
package i
import (
. "github.com/alecthomas/chroma" // nolint
"github.com/alecthomas/chroma/lexers/internal"
)
// Ini lexer.
var Ini = Register(MustNewLexer(
var Ini = internal.Register(MustNewLexer(
&Config{
Name: "INI",
Aliases: []string{"ini", "cfg", "dosini"},

View File

@@ -1,11 +1,12 @@
package lexers
package i
import (
. "github.com/alecthomas/chroma" // nolint
"github.com/alecthomas/chroma/lexers/internal"
)
// Io lexer.
var Io = Register(MustNewLexer(
var Io = internal.Register(MustNewLexer(
&Config{
Name: "Io",
Aliases: []string{"io"},

View File

@@ -1,4 +1,4 @@
package lexers
package internal
import (
"path/filepath"

View File

@@ -1,11 +1,12 @@
package lexers
package j
import (
. "github.com/alecthomas/chroma" // nolint
"github.com/alecthomas/chroma/lexers/internal"
)
// Java lexer.
var Java = Register(MustNewLexer(
var Java = internal.Register(MustNewLexer(
&Config{
Name: "Java",
Aliases: []string{"java"},

View File

@@ -1,7 +1,8 @@
package lexers
package j
import (
. "github.com/alecthomas/chroma" // nolint
"github.com/alecthomas/chroma/lexers/internal"
)
var JavascriptRules = Rules{
@@ -58,7 +59,7 @@ var JavascriptRules = Rules{
}
// Javascript lexer.
var Javascript = Register(MustNewLexer(
var Javascript = internal.Register(MustNewLexer(
&Config{
Name: "JavaScript",
Aliases: []string{"js", "javascript"},

View File

@@ -1,11 +1,12 @@
package lexers
package j
import (
. "github.com/alecthomas/chroma" // nolint
"github.com/alecthomas/chroma/lexers/internal"
)
// Json lexer.
var Json = Register(MustNewLexer(
var Json = internal.Register(MustNewLexer(
&Config{
Name: "JSON",
Aliases: []string{"json"},

View File

@@ -1,7 +1,8 @@
package lexers
package j
import (
. "github.com/alecthomas/chroma" // nolint
"github.com/alecthomas/chroma/lexers/internal"
)
var JSXRules = func() Rules {
@@ -29,7 +30,7 @@ var JSXRules = func() Rules {
}()
// JSX lexer.
var JSX = Register(MustNewLexer(
var JSX = internal.Register(MustNewLexer(
&Config{
Name: "JSX",
Aliases: []string{"react"},

View File

@@ -1,11 +1,12 @@
package lexers
package j
import (
. "github.com/alecthomas/chroma" // nolint
"github.com/alecthomas/chroma/lexers/internal"
)
// Julia lexer.
var Julia = Register(MustNewLexer(
var Julia = internal.Register(MustNewLexer(
&Config{
Name: "Julia",
Aliases: []string{"julia", "jl"},

File diff suppressed because one or more lines are too long

View File

@@ -1,11 +1,12 @@
package lexers
package l
import (
. "github.com/alecthomas/chroma" // nolint
"github.com/alecthomas/chroma/lexers/internal"
)
// Lighttpd Configuration File lexer.
var Lighttpd = Register(MustNewLexer(
var Lighttpd = internal.Register(MustNewLexer(
&Config{
Name: "Lighttpd configuration file",
Aliases: []string{"lighty", "lighttpd"},

View File

@@ -1,11 +1,12 @@
package lexers
package l
import (
. "github.com/alecthomas/chroma" // nolint
"github.com/alecthomas/chroma/lexers/internal"
)
// Llvm lexer.
var Llvm = Register(MustNewLexer(
var Llvm = internal.Register(MustNewLexer(
&Config{
Name: "LLVM",
Aliases: []string{"llvm"},

View File

@@ -1,11 +1,12 @@
package lexers
package l
import (
. "github.com/alecthomas/chroma" // nolint
"github.com/alecthomas/chroma/lexers/internal"
)
// Lua lexer.
var Lua = Register(MustNewLexer(
var Lua = internal.Register(MustNewLexer(
&Config{
Name: "Lua",
Aliases: []string{"lua"},

View File

@@ -1,9 +1,10 @@
package lexers
package lexers_test
import (
"testing"
"github.com/alecthomas/assert"
"github.com/alecthomas/chroma/lexers/g"
)
const lexerBenchSource = `package chroma
@@ -29,7 +30,7 @@ func (f FormatterFunc) Format(w io.Writer, s *Style) (func(*Token), error) {
func Benchmark(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
it, err := Go.Tokenise(nil, lexerBenchSource)
it, err := g.Go.Tokenise(nil, lexerBenchSource)
assert.NoError(b, err)
for t := it(); t != nil; t = it() {
}

58
lexers/lexers.go Normal file
View File

@@ -0,0 +1,58 @@
// Package lexers contains the registry of all lexers.
//
// Sub-packages contain lexer implementations.
package lexers
// nolint: golint
import (
"github.com/alecthomas/chroma"
_ "github.com/alecthomas/chroma/lexers/a"
_ "github.com/alecthomas/chroma/lexers/b"
_ "github.com/alecthomas/chroma/lexers/c"
_ "github.com/alecthomas/chroma/lexers/d"
_ "github.com/alecthomas/chroma/lexers/e"
_ "github.com/alecthomas/chroma/lexers/f"
_ "github.com/alecthomas/chroma/lexers/g"
_ "github.com/alecthomas/chroma/lexers/h"
_ "github.com/alecthomas/chroma/lexers/i"
"github.com/alecthomas/chroma/lexers/internal"
_ "github.com/alecthomas/chroma/lexers/j"
_ "github.com/alecthomas/chroma/lexers/k"
_ "github.com/alecthomas/chroma/lexers/l"
_ "github.com/alecthomas/chroma/lexers/m"
_ "github.com/alecthomas/chroma/lexers/n"
_ "github.com/alecthomas/chroma/lexers/o"
_ "github.com/alecthomas/chroma/lexers/p"
_ "github.com/alecthomas/chroma/lexers/q"
_ "github.com/alecthomas/chroma/lexers/r"
_ "github.com/alecthomas/chroma/lexers/s"
_ "github.com/alecthomas/chroma/lexers/t"
_ "github.com/alecthomas/chroma/lexers/v"
_ "github.com/alecthomas/chroma/lexers/w"
_ "github.com/alecthomas/chroma/lexers/x"
_ "github.com/alecthomas/chroma/lexers/y"
)
// Registry of Lexers.
var Registry = internal.Registry
// Names of all lexers, optionally including aliases.
func Names(withAliases bool) []string { return internal.Names(withAliases) }
// Get a Lexer by name, alias or file extension.
func Get(name string) chroma.Lexer { return internal.Get(name) }
// MatchMimeType attempts to find a lexer for the given MIME type.
func MatchMimeType(mimeType string) chroma.Lexer { return internal.MatchMimeType(mimeType) }
// Match returns the first lexer matching filename.
func Match(filename string) chroma.Lexer { return internal.Match(filename) }
// Analyse text content and return the "best" lexer..
func Analyse(text string) chroma.Lexer { return internal.Analyse(text) }
// Register a Lexer with the global registry.
func Register(lexer chroma.Lexer) chroma.Lexer { return internal.Register(lexer) }
// Fallback lexer if no other is found.
var Fallback = internal.Fallback

View File

@@ -1,63 +1,35 @@
package lexers
package lexers_test
import (
"encoding/json"
"io/ioutil"
"os"
"path/filepath"
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/alecthomas/assert"
"github.com/alecthomas/chroma"
"github.com/alecthomas/chroma/formatters"
"github.com/alecthomas/chroma/lexers"
"github.com/alecthomas/chroma/lexers/a"
"github.com/alecthomas/chroma/lexers/x"
"github.com/alecthomas/chroma/styles"
)
// Test source files are in the form <key>.<key> and validation data is in the form <key>.<key>.expected.
func TestLexers(t *testing.T) {
files, err := ioutil.ReadDir("testdata")
require.NoError(t, err)
for _, file := range files {
ext := filepath.Ext(file.Name())[1:]
if ext != "actual" {
continue
}
lexer := Get(strings.TrimSuffix(file.Name(), filepath.Ext(file.Name())))
if !assert.NotNil(t, lexer) {
continue
}
filename := filepath.Join("testdata", file.Name())
expectedFilename := strings.TrimSuffix(filename, filepath.Ext(filename)) + ".expected"
lexer = chroma.Coalesce(lexer)
t.Run(lexer.Config().Name, func(t *testing.T) {
// Read and tokenise source text.
actualText, err := ioutil.ReadFile(filename)
if !assert.NoError(t, err) {
return
}
actual, err := chroma.Tokenise(lexer, nil, string(actualText))
if !assert.NoError(t, err) {
return
}
// Read expected JSON into token slice.
expected := []*chroma.Token{}
r, err := os.Open(expectedFilename)
if !assert.NoError(t, err) {
return
}
err = json.NewDecoder(r).Decode(&expected)
if !assert.NoError(t, err) {
return
}
// Equal?
assert.Equal(t, expected, actual)
})
func TestCompileAllRegexes(t *testing.T) {
for _, lexer := range lexers.Registry.Lexers {
it, err := lexer.Tokenise(nil, "")
assert.NoError(t, err, "%s failed", lexer.Config().Name)
err = formatters.NoOp.Format(ioutil.Discard, styles.SwapOff, it)
assert.NoError(t, err, "%s failed", lexer.Config().Name)
}
}
func TestGet(t *testing.T) {
t.Run("ByName", func(t *testing.T) {
assert.Equal(t, lexers.Get("xml"), x.XML)
})
t.Run("ByAlias", func(t *testing.T) {
assert.Equal(t, lexers.Get("as"), a.Actionscript)
})
t.Run("ViaFilename", func(t *testing.T) {
assert.Equal(t, lexers.Get("svg"), x.XML)
})
}

View File

@@ -1,11 +1,13 @@
package lexers
package m
import (
. "github.com/alecthomas/chroma" // nolint
. "github.com/alecthomas/chroma" // nolint
. "github.com/alecthomas/chroma/lexers/b" // nolint
"github.com/alecthomas/chroma/lexers/internal"
)
// Makefile lexer.
var Makefile = Register(MustNewLexer(
var Makefile = internal.Register(MustNewLexer(
&Config{
Name: "Base Makefile",
Aliases: []string{"make", "makefile", "mf", "bsdmake"},

View File

@@ -1,11 +1,13 @@
package lexers
package m
import (
. "github.com/alecthomas/chroma" // nolint
"github.com/alecthomas/chroma/lexers/internal"
. "github.com/alecthomas/chroma/lexers/p" // nolint
)
// Mako lexer.
var Mako = Register(MustNewLexer(
var Mako = internal.Register(MustNewLexer(
&Config{
Name: "Mako",
Aliases: []string{"mako"},

View File

@@ -1,11 +1,12 @@
package lexers
package m
import (
. "github.com/alecthomas/chroma" // nolint
"github.com/alecthomas/chroma/lexers/internal"
)
// Markdown lexer.
var Markdown = Register(MustNewLexer(
var Markdown = internal.Register(MustNewLexer(
&Config{
Name: "markdown",
Aliases: []string{"md", "mkd"},
@@ -46,7 +47,7 @@ func markdownCodeBlock(groups []string, lexer Lexer) Iterator {
{Text, groups[3]},
}
code := groups[4]
lexer = Get(groups[2])
lexer = internal.Get(groups[2])
if lexer == nil {
tokens = append(tokens, &Token{String, code})
iterators = append(iterators, Literator(tokens...))

View File

@@ -1,11 +1,14 @@
package lexers
package m
import (
. "github.com/alecthomas/chroma" // nolint
. "github.com/alecthomas/chroma" // nolint
. "github.com/alecthomas/chroma/lexers/h" // nolint
"github.com/alecthomas/chroma/lexers/internal"
. "github.com/alecthomas/chroma/lexers/p" // nolint
)
// Mason lexer.
var Mason = Register(MustNewLexer(
var Mason = internal.Register(MustNewLexer(
&Config{
Name: "Mason",
Aliases: []string{"mason"},

View File

@@ -1,11 +1,12 @@
package lexers
package m
import (
. "github.com/alecthomas/chroma" // nolint
"github.com/alecthomas/chroma/lexers/internal"
)
// Mathematica lexer.
var Mathematica = Register(MustNewLexer(
var Mathematica = internal.Register(MustNewLexer(
&Config{
Name: "Mathematica",
Aliases: []string{"mathematica", "mma", "nb"},

View File

@@ -1,11 +1,12 @@
package lexers
package m
import (
. "github.com/alecthomas/chroma" // nolint
"github.com/alecthomas/chroma/lexers/internal"
)
// MiniZinc lexer.
var MZN = Register(MustNewLexer(
var MZN = internal.Register(MustNewLexer(
&Config{
Name: "MiniZinc",
Aliases: []string{"minizinc", "MZN", "mzn"},

View File

@@ -1,11 +1,12 @@
package lexers
package m
import (
. "github.com/alecthomas/chroma" // nolint
"github.com/alecthomas/chroma/lexers/internal"
)
// Modula-2 lexer.
var Modula2 = Register(MustNewLexer(
var Modula2 = internal.Register(MustNewLexer(
&Config{
Name: "Modula-2",
Aliases: []string{"modula2", "m2"},

View File

@@ -1,11 +1,12 @@
package lexers
package m
import (
. "github.com/alecthomas/chroma" // nolint
"github.com/alecthomas/chroma/lexers/internal"
)
// MorrowindScript lexer.
var MorrowindScript = Register(MustNewLexer(
var MorrowindScript = internal.Register(MustNewLexer(
&Config{
Name: "MorrowindScript",
Aliases: []string{"morrowind", "mwscript"},

View File

@@ -1,11 +1,13 @@
package lexers
package m
import (
. "github.com/alecthomas/chroma" // nolint
"github.com/alecthomas/chroma/lexers/internal"
. "github.com/alecthomas/chroma/lexers/p" // nolint
)
// Myghty lexer.
var Myghty = Register(MustNewLexer(
var Myghty = internal.Register(MustNewLexer(
&Config{
Name: "Myghty",
Aliases: []string{"myghty"},

View File

@@ -1,11 +1,12 @@
package lexers
package m
import (
. "github.com/alecthomas/chroma" // nolint
"github.com/alecthomas/chroma/lexers/internal"
)
// MySQL lexer.
var MySQL = Register(MustNewLexer(
var MySQL = internal.Register(MustNewLexer(
&Config{
Name: "MySQL",
Aliases: []string{"mysql"},

View File

@@ -1,11 +1,12 @@
package lexers
package n
import (
. "github.com/alecthomas/chroma" // nolint
"github.com/alecthomas/chroma/lexers/internal"
)
// Nasm lexer.
var Nasm = Register(MustNewLexer(
var Nasm = internal.Register(MustNewLexer(
&Config{
Name: "NASM",
Aliases: []string{"nasm"},

View File

@@ -1,11 +1,12 @@
package lexers
package n
import (
. "github.com/alecthomas/chroma" // nolint
"github.com/alecthomas/chroma/lexers/internal"
)
// Newspeak lexer.
var Newspeak = Register(MustNewLexer(
var Newspeak = internal.Register(MustNewLexer(
&Config{
Name: "Newspeak",
Aliases: []string{"newspeak"},

View File

@@ -1,11 +1,12 @@
package lexers
package n
import (
. "github.com/alecthomas/chroma" // nolint
"github.com/alecthomas/chroma/lexers/internal"
)
// Nginx Configuration File lexer.
var Nginx = Register(MustNewLexer(
var Nginx = internal.Register(MustNewLexer(
&Config{
Name: "Nginx configuration file",
Aliases: []string{"nginx"},

View File

@@ -1,11 +1,12 @@
package lexers
package n
import (
. "github.com/alecthomas/chroma" // nolint
"github.com/alecthomas/chroma/lexers/internal"
)
// Nim lexer.
var Nim = Register(MustNewLexer(
var Nim = internal.Register(MustNewLexer(
&Config{
Name: "Nim",
Aliases: []string{"nim", "nimrod"},

View File

@@ -1,16 +1,17 @@
package lexers
package n
import (
"strings"
. "github.com/alecthomas/chroma" // nolint
"github.com/alecthomas/chroma/lexers/internal"
)
// nixb matches right boundary of a nix word. Use it instead of \b.
const nixb = `(?![a-zA-Z0-9_'-])`
// Nix lexer.
var Nix = Register(MustNewLexer(
var Nix = internal.Register(MustNewLexer(
&Config{
Name: "Nix",
Aliases: []string{"nixos", "nix"},

View File

@@ -1,11 +1,12 @@
package lexers
package o
import (
. "github.com/alecthomas/chroma" // nolint
"github.com/alecthomas/chroma/lexers/internal"
)
// Objective-C lexer.
var ObjectiveC = Register(MustNewLexer(
var ObjectiveC = internal.Register(MustNewLexer(
&Config{
Name: "Objective-C",
Aliases: []string{"objective-c", "objectivec", "obj-c", "objc"},

View File

@@ -1,11 +1,12 @@
package lexers
package o
import (
. "github.com/alecthomas/chroma" // nolint
"github.com/alecthomas/chroma/lexers/internal"
)
// Ocaml lexer.
var Ocaml = Register(MustNewLexer(
var Ocaml = internal.Register(MustNewLexer(
&Config{
Name: "OCaml",
Aliases: []string{"ocaml"},

View File

@@ -1,11 +1,12 @@
package lexers
package o
import (
. "github.com/alecthomas/chroma" // nolint
"github.com/alecthomas/chroma/lexers/internal"
)
// Octave lexer.
var Octave = Register(MustNewLexer(
var Octave = internal.Register(MustNewLexer(
&Config{
Name: "Octave",
Aliases: []string{"octave"},

View File

@@ -1,11 +1,12 @@
package lexers
package p
import (
. "github.com/alecthomas/chroma" // nolint
"github.com/alecthomas/chroma/lexers/internal"
)
// Pacmanconf lexer.
var Pacmanconf = Register(MustNewLexer(
var Pacmanconf = internal.Register(MustNewLexer(
&Config{
Name: "PacmanConf",
Aliases: []string{"pacmanconf"},

Some files were not shown because too many files have changed in this diff Show More