smalltalk.go

 1package s
 2
 3import (
 4	. "github.com/alecthomas/chroma" // nolint
 5	"github.com/alecthomas/chroma/lexers/internal"
 6)
 7
 8// Smalltalk lexer.
 9var Smalltalk = internal.Register(MustNewLexer(
10	&Config{
11		Name:      "Smalltalk",
12		Aliases:   []string{"smalltalk", "squeak", "st"},
13		Filenames: []string{"*.st"},
14		MimeTypes: []string{"text/x-smalltalk"},
15	},
16	Rules{
17		"root": {
18			{`(<)(\w+:)(.*?)(>)`, ByGroups(Text, Keyword, Text, Text), nil},
19			Include("squeak fileout"),
20			Include("whitespaces"),
21			Include("method definition"),
22			{`(\|)([\w\s]*)(\|)`, ByGroups(Operator, NameVariable, Operator), nil},
23			Include("objects"),
24			{`\^|:=|_`, Operator, nil},
25			{`[\]({}.;!]`, Text, nil},
26		},
27		"method definition": {
28			{`([a-zA-Z]+\w*:)(\s*)(\w+)`, ByGroups(NameFunction, Text, NameVariable), nil},
29			{`^(\b[a-zA-Z]+\w*\b)(\s*)$`, ByGroups(NameFunction, Text), nil},
30			{`^([-+*/\\~<>=|&!?,@%]+)(\s*)(\w+)(\s*)$`, ByGroups(NameFunction, Text, NameVariable, Text), nil},
31		},
32		"blockvariables": {
33			Include("whitespaces"),
34			{`(:)(\s*)(\w+)`, ByGroups(Operator, Text, NameVariable), nil},
35			{`\|`, Operator, Pop(1)},
36			Default(Pop(1)),
37		},
38		"literals": {
39			{`'(''|[^'])*'`, LiteralString, Push("afterobject")},
40			{`\$.`, LiteralStringChar, Push("afterobject")},
41			{`#\(`, LiteralStringSymbol, Push("parenth")},
42			{`\)`, Text, Push("afterobject")},
43			{`(\d+r)?-?\d+(\.\d+)?(e-?\d+)?`, LiteralNumber, Push("afterobject")},
44		},
45		"_parenth_helper": {
46			Include("whitespaces"),
47			{`(\d+r)?-?\d+(\.\d+)?(e-?\d+)?`, LiteralNumber, nil},
48			{`[-+*/\\~<>=|&#!?,@%\w:]+`, LiteralStringSymbol, nil},
49			{`'(''|[^'])*'`, LiteralString, nil},
50			{`\$.`, LiteralStringChar, nil},
51			{`#*\(`, LiteralStringSymbol, Push("inner_parenth")},
52		},
53		"parenth": {
54			{`\)`, LiteralStringSymbol, Push("root", "afterobject")},
55			Include("_parenth_helper"),
56		},
57		"inner_parenth": {
58			{`\)`, LiteralStringSymbol, Pop(1)},
59			Include("_parenth_helper"),
60		},
61		"whitespaces": {
62			{`\s+`, Text, nil},
63			{`"(""|[^"])*"`, Comment, nil},
64		},
65		"objects": {
66			{`\[`, Text, Push("blockvariables")},
67			{`\]`, Text, Push("afterobject")},
68			{`\b(self|super|true|false|nil|thisContext)\b`, NameBuiltinPseudo, Push("afterobject")},
69			{`\b[A-Z]\w*(?!:)\b`, NameClass, Push("afterobject")},
70			{`\b[a-z]\w*(?!:)\b`, NameVariable, Push("afterobject")},
71			{`#("(""|[^"])*"|[-+*/\\~<>=|&!?,@%]+|[\w:]+)`, LiteralStringSymbol, Push("afterobject")},
72			Include("literals"),
73		},
74		"afterobject": {
75			{`! !$`, Keyword, Pop(1)},
76			Include("whitespaces"),
77			{`\b(ifTrue:|ifFalse:|whileTrue:|whileFalse:|timesRepeat:)`, NameBuiltin, Pop(1)},
78			{`\b(new\b(?!:))`, NameBuiltin, nil},
79			{`:=|_`, Operator, Pop(1)},
80			{`\b[a-zA-Z]+\w*:`, NameFunction, Pop(1)},
81			{`\b[a-zA-Z]+\w*`, NameFunction, nil},
82			{`\w+:?|[-+*/\\~<>=|&!?,@%]+`, NameFunction, Pop(1)},
83			{`\.`, Punctuation, Pop(1)},
84			{`;`, Punctuation, nil},
85			{`[\])}]`, Text, nil},
86			{`[\[({]`, Text, Pop(1)},
87		},
88		"squeak fileout": {
89			{`^"(""|[^"])*"!`, Keyword, nil},
90			{`^'(''|[^'])*'!`, Keyword, nil},
91			{`^(!)(\w+)( commentStamp: )(.*?)( prior: .*?!\n)(.*?)(!)`, ByGroups(Keyword, NameClass, Keyword, LiteralString, Keyword, Text, Keyword), nil},
92			{`^(!)(\w+(?: class)?)( methodsFor: )('(?:''|[^'])*')(.*?!)`, ByGroups(Keyword, NameClass, Keyword, LiteralString, Keyword), nil},
93			{`^(\w+)( subclass: )(#\w+)(\s+instanceVariableNames: )(.*?)(\s+classVariableNames: )(.*?)(\s+poolDictionaries: )(.*?)(\s+category: )(.*?)(!)`, ByGroups(NameClass, Keyword, LiteralStringSymbol, Keyword, LiteralString, Keyword, LiteralString, Keyword, LiteralString, Keyword, LiteralString, Keyword), nil},
94			{`^(\w+(?: class)?)(\s+instanceVariableNames: )(.*?)(!)`, ByGroups(NameClass, Keyword, LiteralString, Keyword), nil},
95			{`(!\n)(\].*)(! !)$`, ByGroups(Keyword, Text, Keyword), nil},
96			{`! !$`, Keyword, nil},
97		},
98	},
99))