Skip to content

Commit

Permalink
Add test to pre-compile all regexes.
Browse files Browse the repository at this point in the history
  • Loading branch information
alecthomas committed Sep 19, 2017
1 parent 87183b3 commit a729603
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 67 deletions.
36 changes: 36 additions & 0 deletions _tools/exercise/main.go
@@ -0,0 +1,36 @@
package main

import (
"fmt"
"io/ioutil"

"github.com/alecthomas/chroma/formatters"
"github.com/alecthomas/chroma/lexers"
"github.com/alecthomas/chroma/styles"
"gopkg.in/alecthomas/kingpin.v3-unstable"
)

var (
filesArgs = kingpin.Arg("file", "Files to use to exercise lexers.").Required().ExistingFiles()
)

func main() {
kingpin.CommandLine.Help = "Exercise linters against a list of files."
kingpin.Parse()

writer, err := formatters.NoOp.Format(ioutil.Discard, styles.SwapOff)
kingpin.FatalIfError(err, "")

for _, file := range *filesArgs {
lexer := lexers.Match(file)
if lexer == nil {
fmt.Printf("warning: could not find lexer for %q\n", file)
continue
}
text, err := ioutil.ReadFile(file)
kingpin.FatalIfError(err, "")
err = lexer.Tokenise(nil, string(text), writer)
kingpin.FatalIfError(err, "%s failed to tokenise %q", lexer.Config().Name, file)
fmt.Printf("ok: %q\n", file)
}
}
8 changes: 6 additions & 2 deletions lexer.go
Expand Up @@ -133,6 +133,8 @@ func (e EmitterFunc) Emit(groups []string, lexer Lexer, out func(*Token)) { e(gr
// ByGroups emits a token for each matching group in the rule's regex.
func ByGroups(emitters ...Emitter) Emitter {
return EmitterFunc(func(groups []string, lexer Lexer, out func(*Token)) {
// NOTE: If this line panics, there is a mismatch with groups. Uncomment the following line to debug.
// fmt.Printf("%s %#v\n", emitters, groups[1:])
for i, group := range groups[1:] {
emitters[i].Emit([]string{group}, lexer, out)
}
Expand Down Expand Up @@ -212,6 +214,8 @@ func NewLexer(config *Config, rules Rules) (*RegexLexer, error) {
}

// A CompiledRule is a Rule with a pre-compiled regex.
//
// Note that regular expressions are lazily compiled on first use of the lexer.
type CompiledRule struct {
Rule
Regexp *regexp2.Regexp
Expand Down Expand Up @@ -274,12 +278,12 @@ func (r *RegexLexer) maybeCompile() (err error) {
if r.compiled {
return nil
}
for _, rules := range r.rules {
for state, rules := range r.rules {
for i, rule := range rules {
if rule.Regexp == nil {
rule.Regexp, err = regexp2.Compile("^(?"+rule.flags+")(?:"+rule.Pattern+")", 0)
if err != nil {
return err
return fmt.Errorf("failed to compile rule %s.%d: %s", state, i, err)
}
}
rules[i] = rule
Expand Down
21 changes: 21 additions & 0 deletions lexers/api_test.go
@@ -0,0 +1,21 @@
package lexers_test

import (
"io/ioutil"
"testing"

"github.com/stretchr/testify/assert"

"github.com/alecthomas/chroma/formatters"
"github.com/alecthomas/chroma/lexers"
"github.com/alecthomas/chroma/styles"
)

func TestCompileAllRegexes(t *testing.T) {
writer, err := formatters.NoOp.Format(ioutil.Discard, styles.SwapOff)
assert.NoError(t, err)
for _, lexer := range lexers.Registry.Lexers {
err = lexer.Tokenise(nil, "", writer)
assert.NoError(t, err, "%s failed", lexer.Config().Name)
}
}
4 changes: 2 additions & 2 deletions lexers/handlebars.go
Expand Up @@ -9,7 +9,7 @@ var Handlebars = Register(MustNewLexer(
&Config{
Name: "Handlebars",
Aliases: []string{"handlebars"},
Filenames: []string{},
Filenames: []string{"*.handlebars"},
MimeTypes: []string{},
},
Rules{
Expand All @@ -23,7 +23,7 @@ var Handlebars = Register(MustNewLexer(
{`\s+`, Text, nil},
{`\}\}\}`, CommentSpecial, Pop(1)},
{`\}\}`, CommentPreproc, Pop(1)},
{`([#/]*)(each|if|unless|else|with|log|in(line)?)`, ByGroups(Keyword, Keyword), nil},
{`([#/]*)(each|if|unless|else|with|log|in(?:line)?)`, ByGroups(Keyword, Keyword), nil},
{`#\*inline`, Keyword, nil},
{`([#/])([\w-]+)`, ByGroups(NameFunction, NameFunction), nil},
{`([\w-]+)(=)`, ByGroups(NameAttribute, Operator), nil},
Expand Down
20 changes: 10 additions & 10 deletions lexers/racket.go

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions lexers/smalltalk.go
Expand Up @@ -20,7 +20,7 @@ var Smalltalk = Register(MustNewLexer(
Include("method definition"),
{`(\|)([\w\s]*)(\|)`, ByGroups(Operator, NameVariable, Operator), nil},
Include("objects"),
{`\^|\:=|\_`, Operator, nil},
{`\^|:=|_`, Operator, nil},
{`[\]({}.;!]`, Text, nil},
},
"method definition": {
Expand Down Expand Up @@ -75,7 +75,7 @@ var Smalltalk = Register(MustNewLexer(
Include("whitespaces"),
{`\b(ifTrue:|ifFalse:|whileTrue:|whileFalse:|timesRepeat:)`, NameBuiltin, Pop(1)},
{`\b(new\b(?!:))`, NameBuiltin, nil},
{`\:=|\_`, Operator, Pop(1)},
{`:=|_`, Operator, Pop(1)},
{`\b[a-zA-Z]+\w*:`, NameFunction, Pop(1)},
{`\b[a-zA-Z]+\w*`, NameFunction, nil},
{`\w+:?|[-+*/\\~<>=|&!?,@%]+`, NameFunction, Pop(1)},
Expand Down
51 changes: 0 additions & 51 deletions styles/default.go

This file was deleted.

0 comments on commit a729603

Please sign in to comment.