lua.go

 1package l
 2
 3import (
 4	. "github.com/alecthomas/chroma" // nolint
 5	"github.com/alecthomas/chroma/lexers/internal"
 6)
 7
 8// Lua lexer.
 9var Lua = internal.Register(MustNewLexer(
10	&Config{
11		Name:      "Lua",
12		Aliases:   []string{"lua"},
13		Filenames: []string{"*.lua", "*.wlua"},
14		MimeTypes: []string{"text/x-lua", "application/x-lua"},
15	},
16	Rules{
17		"root": {
18			{`#!.*`, CommentPreproc, nil},
19			Default(Push("base")),
20		},
21		"ws": {
22			{`(?:--\[(=*)\[[\w\W]*?\](\1)\])`, CommentMultiline, nil},
23			{`(?:--.*$)`, CommentSingle, nil},
24			{`(?:\s+)`, Text, nil},
25		},
26		"base": {
27			Include("ws"),
28			{`(?i)0x[\da-f]*(\.[\da-f]*)?(p[+-]?\d+)?`, LiteralNumberHex, nil},
29			{`(?i)(\d*\.\d+|\d+\.\d*)(e[+-]?\d+)?`, LiteralNumberFloat, nil},
30			{`(?i)\d+e[+-]?\d+`, LiteralNumberFloat, nil},
31			{`\d+`, LiteralNumberInteger, nil},
32			{`(?s)\[(=*)\[.*?\]\1\]`, LiteralString, nil},
33			{`::`, Punctuation, Push("label")},
34			{`\.{3}`, Punctuation, nil},
35			{`[=<>|~&+\-*/%#^]+|\.\.`, Operator, nil},
36			{`[\[\]{}().,:;]`, Punctuation, nil},
37			{`(and|or|not)\b`, OperatorWord, nil},
38			{`(break|do|else|elseif|end|for|if|in|repeat|return|then|until|while)\b`, KeywordReserved, nil},
39			{`goto\b`, KeywordReserved, Push("goto")},
40			{`(local)\b`, KeywordDeclaration, nil},
41			{`(true|false|nil)\b`, KeywordConstant, nil},
42			{`(function)\b`, KeywordReserved, Push("funcname")},
43			{`[A-Za-z_]\w*(\.[A-Za-z_]\w*)?`, Name, nil},
44			{`'`, LiteralStringSingle, Combined("stringescape", "sqs")},
45			{`"`, LiteralStringDouble, Combined("stringescape", "dqs")},
46		},
47		"funcname": {
48			Include("ws"),
49			{`[.:]`, Punctuation, nil},
50			{`(?:[^\W\d]\w*)(?=(?:(?:--\[(=*)\[[\w\W]*?\](\2)\])|(?:--.*$)|(?:\s+))*[.:])`, NameClass, nil},
51			{`(?:[^\W\d]\w*)`, NameFunction, Pop(1)},
52			{`\(`, Punctuation, Pop(1)},
53		},
54		"goto": {
55			Include("ws"),
56			{`(?:[^\W\d]\w*)`, NameLabel, Pop(1)},
57		},
58		"label": {
59			Include("ws"),
60			{`::`, Punctuation, Pop(1)},
61			{`(?:[^\W\d]\w*)`, NameLabel, nil},
62		},
63		"stringescape": {
64			{`\\([abfnrtv\\"\']|[\r\n]{1,2}|z\s*|x[0-9a-fA-F]{2}|\d{1,3}|u\{[0-9a-fA-F]+\})`, LiteralStringEscape, nil},
65		},
66		"sqs": {
67			{`'`, LiteralStringSingle, Pop(1)},
68			{`[^\\']+`, LiteralStringSingle, nil},
69		},
70		"dqs": {
71			{`"`, LiteralStringDouble, Pop(1)},
72			{`[^\\"]+`, LiteralStringDouble, nil},
73		},
74	},
75))