...

Source file src/github.com/alecthomas/chroma/lexers/t/typescript.go

Documentation: github.com/alecthomas/chroma/lexers/t

     1  package t
     2  
     3  import (
     4  	. "github.com/alecthomas/chroma" // nolint
     5  	"github.com/alecthomas/chroma/lexers/internal"
     6  )
     7  
     8  // TypeScript lexer.
     9  var TypeScript = internal.Register(MustNewLazyLexer(
    10  	&Config{
    11  		Name:      "TypeScript",
    12  		Aliases:   []string{"ts", "tsx", "typescript"},
    13  		Filenames: []string{"*.ts", "*.tsx"},
    14  		MimeTypes: []string{"text/x-typescript"},
    15  		DotAll:    true,
    16  		EnsureNL:  true,
    17  	},
    18  	typeScriptRules,
    19  ))
    20  
    21  func typeScriptRules() Rules {
    22  	return Rules{
    23  		"commentsandwhitespace": {
    24  			{`\s+`, Text, nil},
    25  			{`<!--`, Comment, nil},
    26  			{`//.*?\n`, CommentSingle, nil},
    27  			{`/\*.*?\*/`, CommentMultiline, nil},
    28  		},
    29  		"slashstartsregex": {
    30  			Include("commentsandwhitespace"),
    31  			{`/(\\.|[^[/\\\n]|\[(\\.|[^\]\\\n])*])+/([gim]+\b|\B)`, LiteralStringRegex, Pop(1)},
    32  			{`(?=/)`, Text, Push("#pop", "badregex")},
    33  			Default(Pop(1)),
    34  		},
    35  		"badregex": {
    36  			{`\n`, Text, Pop(1)},
    37  		},
    38  		"root": {
    39  			Include("jsx"),
    40  			{`^(?=\s|/|<!--)`, Text, Push("slashstartsregex")},
    41  			Include("commentsandwhitespace"),
    42  			{`\+\+|--|~|&&|\?|:|\|\||\\(?=\n)|(<<|>>>?|==?|!=?|[-<>+*%&|^/])=?`, Operator, Push("slashstartsregex")},
    43  			{`[{(\[;,]`, Punctuation, Push("slashstartsregex")},
    44  			{`[})\].]`, Punctuation, nil},
    45  			{`(for|in|of|while|do|break|return|yield|continue|switch|case|default|if|else|throw|try|catch|finally|new|delete|typeof|instanceof|keyof|asserts|is|infer|await|void|this)\b`, Keyword, Push("slashstartsregex")},
    46  			{`(var|let|with|function)\b`, KeywordDeclaration, Push("slashstartsregex")},
    47  			{`(abstract|async|boolean|class|const|debugger|enum|export|extends|from|get|global|goto|implements|import|interface|namespace|package|private|protected|public|readonly|require|set|static|super|type)\b`, KeywordReserved, nil},
    48  			{`(true|false|null|NaN|Infinity|undefined)\b`, KeywordConstant, nil},
    49  			{`(Array|Boolean|Date|Error|Function|Math|Number|Object|Packages|RegExp|String|decodeURI|decodeURIComponent|encodeURI|encodeURIComponent|eval|isFinite|isNaN|parseFloat|parseInt|document|this|window)\b`, NameBuiltin, nil},
    50  			{`\b(module)(\s*)(\s*[\w?.$][\w?.$]*)(\s*)`, ByGroups(KeywordReserved, Text, NameOther, Text), Push("slashstartsregex")},
    51  			{`\b(string|bool|number|any|never|object|symbol|unique|unknown|bigint)\b`, KeywordType, nil},
    52  			{`\b(constructor|declare|interface|as)\b`, KeywordReserved, nil},
    53  			{`(super)(\s*)(\([\w,?.$\s]+\s*\))`, ByGroups(KeywordReserved, Text), Push("slashstartsregex")},
    54  			{`([a-zA-Z_?.$][\w?.$]*)\(\) \{`, NameOther, Push("slashstartsregex")},
    55  			{`([\w?.$][\w?.$]*)(\s*:\s*)([\w?.$][\w?.$]*)`, ByGroups(NameOther, Text, KeywordType), nil},
    56  			{`[$a-zA-Z_]\w*`, NameOther, nil},
    57  			{`[0-9][0-9]*\.[0-9]+([eE][0-9]+)?[fd]?`, LiteralNumberFloat, nil},
    58  			{`0x[0-9a-fA-F]+`, LiteralNumberHex, nil},
    59  			{`[0-9]+`, LiteralNumberInteger, nil},
    60  			{`"(\\\\|\\"|[^"])*"`, LiteralStringDouble, nil},
    61  			{`'(\\\\|\\'|[^'])*'`, LiteralStringSingle, nil},
    62  			{"`", LiteralStringBacktick, Push("interp")},
    63  			{`@\w+`, KeywordDeclaration, nil},
    64  		},
    65  		"interp": {
    66  			{"`", LiteralStringBacktick, Pop(1)},
    67  			{`\\\\`, LiteralStringBacktick, nil},
    68  			{"\\\\`", LiteralStringBacktick, nil},
    69  			{`\$\{`, LiteralStringInterpol, Push("interp-inside")},
    70  			{`\$`, LiteralStringBacktick, nil},
    71  			{"[^`\\\\$]+", LiteralStringBacktick, nil},
    72  		},
    73  		"interp-inside": {
    74  			{`\}`, LiteralStringInterpol, Pop(1)},
    75  			Include("root"),
    76  		},
    77  		"jsx": {
    78  			{`(<)(/?)(>)`, ByGroups(Punctuation, Punctuation, Punctuation), nil},
    79  			{`(<)([\w\.]+)`, ByGroups(Punctuation, NameTag), Push("tag")},
    80  			{`(<)(/)([\w\.]*)(>)`, ByGroups(Punctuation, Punctuation, NameTag, Punctuation), nil},
    81  		},
    82  		"tag": {
    83  			{`\s+`, Text, nil},
    84  			{`([\w]+\s*)(=)(\s*)`, ByGroups(NameAttribute, Operator, Text), Push("attr")},
    85  			{`[{}]+`, Punctuation, nil},
    86  			{`[\w\.]+`, NameAttribute, nil},
    87  			{`(/?)(\s*)(>)`, ByGroups(Punctuation, Text, Punctuation), Pop(1)},
    88  		},
    89  		"attr": {
    90  			{`{`, Punctuation, Push("expression")},
    91  			{`".*?"`, LiteralString, Pop(1)},
    92  			{`'.*?'`, LiteralString, Pop(1)},
    93  			Default(Pop(1)),
    94  		},
    95  		"expression": {
    96  			{`{`, Punctuation, Push()},
    97  			{`}`, Punctuation, Pop(1)},
    98  			Include("root"),
    99  		},
   100  	}
   101  }
   102  

View as plain text