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