2017-06-01 16:17:21 +02:00
|
|
|
package chroma
|
|
|
|
|
2017-07-20 08:51:16 +02:00
|
|
|
//go:generate stringer -type TokenType
|
|
|
|
|
2017-06-01 16:17:21 +02:00
|
|
|
// TokenType is the type of token to highlight.
|
|
|
|
//
|
|
|
|
// It is also an Emitter, emitting a single token of itself
|
|
|
|
type TokenType int
|
|
|
|
|
|
|
|
// Set of TokenTypes.
|
|
|
|
//
|
|
|
|
// Categories of types are grouped in ranges of 1000, while sub-categories are in ranges of 100. For
|
|
|
|
// example, the literal category is in the range 3000-3999. The sub-category for literal strings is
|
|
|
|
// in the range 3100-3199.
|
2017-07-20 08:51:16 +02:00
|
|
|
|
|
|
|
// Meta token types.
|
2017-06-01 16:17:21 +02:00
|
|
|
const (
|
2017-09-20 06:15:06 +02:00
|
|
|
// Default background style.
|
2017-07-20 08:51:16 +02:00
|
|
|
Background TokenType = -1 - iota
|
2017-09-20 06:15:06 +02:00
|
|
|
// Line numbers in output.
|
2017-09-20 05:30:46 +02:00
|
|
|
LineNumbers
|
2017-09-20 06:15:06 +02:00
|
|
|
// Line higlight style.
|
|
|
|
LineHighlight
|
|
|
|
// Input that could not be tokenised.
|
2017-06-01 16:17:21 +02:00
|
|
|
Error
|
2017-09-20 06:15:06 +02:00
|
|
|
// Other is used by the Delegate lexer to indicate which tokens should be handled by the delegate.
|
2017-06-01 16:17:21 +02:00
|
|
|
Other
|
2017-09-20 06:15:06 +02:00
|
|
|
// No highlighting.
|
2017-09-18 03:16:44 +02:00
|
|
|
None
|
2017-06-01 16:17:21 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// Keywords.
|
|
|
|
const (
|
|
|
|
Keyword TokenType = 1000 + iota
|
|
|
|
KeywordConstant
|
|
|
|
KeywordDeclaration
|
|
|
|
KeywordNamespace
|
|
|
|
KeywordPseudo
|
|
|
|
KeywordReserved
|
|
|
|
KeywordType
|
|
|
|
)
|
|
|
|
|
|
|
|
// Names.
|
|
|
|
const (
|
|
|
|
Name TokenType = 2000 + iota
|
|
|
|
NameAttribute
|
|
|
|
NameBuiltin
|
|
|
|
NameBuiltinPseudo
|
|
|
|
NameClass
|
|
|
|
NameConstant
|
|
|
|
NameDecorator
|
|
|
|
NameEntity
|
|
|
|
NameException
|
|
|
|
NameFunction
|
|
|
|
NameFunctionMagic
|
2017-09-18 03:16:44 +02:00
|
|
|
NameKeyword
|
2017-06-01 16:17:21 +02:00
|
|
|
NameLabel
|
|
|
|
NameNamespace
|
2017-07-20 08:51:16 +02:00
|
|
|
NameOperator
|
2017-09-18 03:16:44 +02:00
|
|
|
NameOther
|
|
|
|
NamePseudo
|
|
|
|
NameProperty
|
2017-06-01 16:17:21 +02:00
|
|
|
NameTag
|
|
|
|
NameVariable
|
2017-09-18 03:16:44 +02:00
|
|
|
NameVariableAnonymous
|
2017-06-01 16:17:21 +02:00
|
|
|
NameVariableClass
|
|
|
|
NameVariableGlobal
|
|
|
|
NameVariableInstance
|
|
|
|
NameVariableMagic
|
|
|
|
)
|
|
|
|
|
|
|
|
// Literals.
|
|
|
|
const (
|
|
|
|
Literal TokenType = 3000 + iota
|
|
|
|
LiteralDate
|
2017-09-18 03:16:44 +02:00
|
|
|
LiteralOther
|
2017-06-01 16:17:21 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// Strings.
|
|
|
|
const (
|
|
|
|
LiteralString TokenType = 3100 + iota
|
|
|
|
LiteralStringAffix
|
2017-09-18 03:16:44 +02:00
|
|
|
LiteralStringAtom
|
2017-06-01 16:17:21 +02:00
|
|
|
LiteralStringBacktick
|
2017-09-18 03:16:44 +02:00
|
|
|
LiteralStringBoolean
|
2017-06-01 16:17:21 +02:00
|
|
|
LiteralStringChar
|
|
|
|
LiteralStringDelimiter
|
|
|
|
LiteralStringDoc
|
|
|
|
LiteralStringDouble
|
|
|
|
LiteralStringEscape
|
|
|
|
LiteralStringHeredoc
|
|
|
|
LiteralStringInterpol
|
2017-09-18 03:16:44 +02:00
|
|
|
LiteralStringName
|
2017-06-01 16:17:21 +02:00
|
|
|
LiteralStringOther
|
|
|
|
LiteralStringRegex
|
|
|
|
LiteralStringSingle
|
|
|
|
LiteralStringSymbol
|
|
|
|
)
|
|
|
|
|
|
|
|
// Literals.
|
|
|
|
const (
|
|
|
|
LiteralNumber TokenType = 3200 + iota
|
|
|
|
LiteralNumberBin
|
|
|
|
LiteralNumberFloat
|
|
|
|
LiteralNumberHex
|
|
|
|
LiteralNumberInteger
|
|
|
|
LiteralNumberIntegerLong
|
|
|
|
LiteralNumberOct
|
|
|
|
)
|
|
|
|
|
|
|
|
// Operators.
|
|
|
|
const (
|
|
|
|
Operator TokenType = 4000 + iota
|
|
|
|
OperatorWord
|
|
|
|
)
|
|
|
|
|
|
|
|
// Punctuation.
|
|
|
|
const (
|
|
|
|
Punctuation TokenType = 5000 + iota
|
|
|
|
)
|
|
|
|
|
|
|
|
// Comments.
|
|
|
|
const (
|
|
|
|
Comment TokenType = 6000 + iota
|
|
|
|
CommentHashbang
|
|
|
|
CommentMultiline
|
|
|
|
CommentSingle
|
|
|
|
CommentSpecial
|
|
|
|
)
|
|
|
|
|
2017-06-04 14:18:35 +02:00
|
|
|
// Preprocessor "comments".
|
|
|
|
const (
|
|
|
|
CommentPreproc TokenType = 6100 + iota
|
|
|
|
CommentPreprocFile
|
|
|
|
)
|
|
|
|
|
2017-06-01 16:17:21 +02:00
|
|
|
// Generic tokens.
|
|
|
|
const (
|
|
|
|
Generic TokenType = 7000 + iota
|
|
|
|
GenericDeleted
|
|
|
|
GenericEmph
|
|
|
|
GenericError
|
|
|
|
GenericHeading
|
|
|
|
GenericInserted
|
|
|
|
GenericOutput
|
|
|
|
GenericPrompt
|
|
|
|
GenericStrong
|
|
|
|
GenericSubheading
|
|
|
|
GenericTraceback
|
2017-06-04 14:18:35 +02:00
|
|
|
GenericUnderline
|
2017-06-01 16:17:21 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// Text.
|
|
|
|
const (
|
|
|
|
Text TokenType = 8000 + iota
|
|
|
|
TextWhitespace
|
2017-09-18 03:16:44 +02:00
|
|
|
TextSymbol
|
|
|
|
TextPunctuation
|
2017-06-01 16:17:21 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// Aliases.
|
|
|
|
const (
|
|
|
|
Whitespace = TextWhitespace
|
|
|
|
|
|
|
|
Date = LiteralDate
|
|
|
|
|
|
|
|
String = LiteralString
|
|
|
|
StringAffix = LiteralStringAffix
|
|
|
|
StringBacktick = LiteralStringBacktick
|
|
|
|
StringChar = LiteralStringChar
|
|
|
|
StringDelimiter = LiteralStringDelimiter
|
|
|
|
StringDoc = LiteralStringDoc
|
|
|
|
StringDouble = LiteralStringDouble
|
|
|
|
StringEscape = LiteralStringEscape
|
|
|
|
StringHeredoc = LiteralStringHeredoc
|
|
|
|
StringInterpol = LiteralStringInterpol
|
|
|
|
StringOther = LiteralStringOther
|
|
|
|
StringRegex = LiteralStringRegex
|
|
|
|
StringSingle = LiteralStringSingle
|
|
|
|
StringSymbol = LiteralStringSymbol
|
|
|
|
|
|
|
|
Number = LiteralNumber
|
|
|
|
NumberBin = LiteralNumberBin
|
|
|
|
NumberFloat = LiteralNumberFloat
|
|
|
|
NumberHex = LiteralNumberHex
|
|
|
|
NumberInteger = LiteralNumberInteger
|
|
|
|
NumberIntegerLong = LiteralNumberIntegerLong
|
|
|
|
NumberOct = LiteralNumberOct
|
|
|
|
)
|
|
|
|
|
|
|
|
func (t TokenType) Category() TokenType {
|
|
|
|
return t / 1000 * 1000
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t TokenType) SubCategory() TokenType {
|
|
|
|
return t / 100 * 100
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t TokenType) InCategory(other TokenType) bool {
|
|
|
|
return t/1000 == other/1000
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t TokenType) InSubCategory(other TokenType) bool {
|
|
|
|
return t/100 == other/100
|
|
|
|
}
|
|
|
|
|
2017-09-20 14:19:36 +02:00
|
|
|
func (t TokenType) Emit(groups []string, lexer Lexer) Iterator {
|
|
|
|
return Literator(&Token{Type: t, Value: groups[0]})
|
2017-06-01 16:17:21 +02:00
|
|
|
}
|