1
0
mirror of https://github.com/alecthomas/chroma.git synced 2025-10-30 23:57:49 +02:00

Add HCL lexer.

This commit is contained in:
Alec Thomas
2019-06-17 21:43:15 +10:00
parent ea14dd8660
commit 1a13c19115
4 changed files with 197 additions and 11 deletions

View File

@@ -33,29 +33,30 @@ Prefix | Language
:----: | --------
A | ABNF, ActionScript, ActionScript 3, Ada, Angular2, ANTLR, ApacheConf, APL, AppleScript, Arduino, Awk
B | Ballerina, Base Makefile, Bash, Batchfile, BlitzBasic, BNF, Brainfuck
C | C, C#, C++, Cassandra CQL, CFEngine3, cfstatement/ColdFusion, CMake, COBOL, CSS, Cap'n Proto, Ceylon, ChaiScript, Cheetah, Clojure, CoffeeScript, Common Lisp, Coq, Crystal, Cython
D | Dart, Diff, Django/Jinja, Docker, DTD
C | C, C#, C++, Cap'n Proto, Cassandra CQL, Ceylon, CFEngine3, cfstatement, ChaiScript, Cheetah, Clojure, CMake, COBOL, CoffeeScript, Common Lisp, Coq, Crystal, CSS, Cython
D | D, Dart, Diff, Django/Jinja, Docker, DTD
E | EBNF, Elixir, Elm, EmacsLisp, Erlang
F | Factor, Fish, Forth, Fortran, FSharp
G | GAS, GDScript, GLSL, Genshi, Genshi HTML, Genshi Text, Gnuplot, Go, Go HTML Template, Go Text Template, GraphQL, Groovy
H | Handlebars, Haskell, Haxe, Hexdump, HTML, HTTP, Hy
G | GAS, GDScript, Genshi, Genshi HTML, Genshi Text, GLSL, Gnuplot, Go, Go HTML Template, Go Text Template, GraphQL, Groovy
H | Handlebars, Haskell, Haxe, HCL, Hexdump, HTML, HTTP, Hy
I | Idris, INI, Io
J | Java, JavaScript, JSON, Jsx, Julia, Jungle
J | Java, JavaScript, JSON, Julia, Jungle
K | Kotlin
L | Lighttpd configuration file, LLVM, Lua
M | Mako, Markdown, Mason, Mathematica, MiniZinc, Modula-2, MonkeyC, MorrowindScript, Myghty, MySQL
M | Mako, markdown, Mason, Mathematica, Matlab, MiniZinc, Modula-2, MonkeyC, MorrowindScript, Myghty, MySQL
N | NASM, Newspeak, Nginx configuration file, Nim, Nix
O | Objective-C, OCaml, Octave, OpenSCAD, Org Mode
P | PacmanConf, Perl, PHP, Pig, PkgConfig, Plaintext, PL/pgSQL, PostgreSQL SQL dialect, PostScript, POVRay, PowerShell, Prolog, Protocol Buffer, Puppet, Python, Python 3
P | PacmanConf, Perl, PHP, Pig, PkgConfig, PL/pgSQL, plaintext, PostgreSQL SQL dialect, PostScript, POVRay, PowerShell, Prolog, Protocol Buffer, Puppet, Python, Python 3
Q | QBasic
R | R, Racket, Ragel, reg, reStructuredText, Rexx, Ruby, Rust
S | Sass, Scala, Scheme, Scilab, SCSS, Smalltalk, Smarty, Snobol, Solidity, SPARQL, SQL, SquidConf, Swift, systemd, Systemverilog
T | TASM, Tcl, Tcsh, Termcap, Terminfo, Terraform, TeX, Thrift, TOML, TradingView, Transact-SQL, Turtle, Twig, TypeScript, TypoScript, TypoScriptCssData, TypoScriptHtmlData
V | verilog, VHDL, VimL
R | R, Racket, Ragel, react, reg, reStructuredText, Rexx, Ruby, Rust
S | Sass, Scala, Scheme, Scilab, SCSS, Smalltalk, Smarty, Snobol, Solidity, SPARQL, SQL, SquidConf, Swift, SYSTEMD, systemverilog
T | TASM, Tcl, Tcsh, Termcap, Terminfo, Terraform, TeX, Thrift, TOML, TradingView, Transact-SQL, Turing, Turtle, Twig, TypeScript, TypoScript, TypoScriptCssData, TypoScriptHtmlData
V | VB.net, verilog, VHDL, VimL, vue
W | WDTE
X | XML, Xorg
Y | YAML
_I will attempt to keep this section up to date, but an authoritative list can be
displayed with `chroma --list`._

69
lexers/h/hcl.go Normal file
View File

@@ -0,0 +1,69 @@
package h
import (
. "github.com/alecthomas/chroma" // nolint
"github.com/alecthomas/chroma/lexers/internal"
)
// HCL lexer.
var HCL = internal.Register(MustNewLexer(
&Config{
Name: "HCL",
Aliases: []string{"hcl"},
Filenames: []string{"*.hcl"},
MimeTypes: []string{"application/x-hcl"},
},
Rules{
"root": {
Include("string"),
Include("punctuation"),
Include("curly"),
Include("basic"),
Include("whitespace"),
{`[0-9]+`, LiteralNumber, nil},
},
"basic": {
{Words(`\b`, `\b`, `true`, `false`), KeywordType, nil},
{`\s*/\*`, CommentMultiline, Push("comment")},
{`\s*#.*\n`, CommentSingle, nil},
{`(.*?)(\s*)(=)`, ByGroups(Name, Text, Operator), nil},
{`\d+`, Number, nil},
{`\b\w+\b`, Keyword, nil},
{`\$\{`, LiteralStringInterpol, Push("var_builtin")},
},
"function": {
{`(\s+)(".*")(\s+)`, ByGroups(Text, LiteralString, Text), nil},
Include("punctuation"),
Include("curly"),
},
"var_builtin": {
{`\$\{`, LiteralStringInterpol, Push()},
{Words(`\b`, `\b`, `concat`, `file`, `join`, `lookup`, `element`), NameBuiltin, nil},
Include("string"),
Include("punctuation"),
{`\s+`, Text, nil},
{`\}`, LiteralStringInterpol, Pop(1)},
},
"string": {
{`(".*")`, ByGroups(LiteralStringDouble), nil},
},
"punctuation": {
{`[\[\](),.]`, Punctuation, nil},
},
"curly": {
{`\{`, TextPunctuation, nil},
{`\}`, TextPunctuation, nil},
},
"comment": {
{`[^*/]`, CommentMultiline, nil},
{`/\*`, CommentMultiline, Push()},
{`\*/`, CommentMultiline, Pop(1)},
{`[*/]`, CommentMultiline, nil},
},
"whitespace": {
{`\n`, Text, nil},
{`\s+`, Text, nil},
{`\\\n`, Text, nil},
},
},
))

15
table.py Executable file
View File

@@ -0,0 +1,15 @@
#!/usr/bin/env python3
from collections import defaultdict
from subprocess import check_output
lines = check_output(["go", "run", "./cmd/chroma/main.go", "--list"]).decode('utf-8').splitlines()
lines = [line.strip() for line in lines if line.startswith(" ") and not line.startswith(" ")]
lines = sorted(lines, key=lambda l: l.lower())
table = defaultdict(list)
for line in lines:
table[line[0].upper()].append(line)
for key, value in table.items():
print("{} | {}".format(key, ', '.join(value)))

View File

@@ -4,6 +4,107 @@ package chroma
import "strconv"
func _() {
// An "invalid array index" compiler error signifies that the constant values have changed.
// Re-run the stringer command to generate them again.
var x [1]struct{}
_ = x[Background - -1]
_ = x[LineNumbers - -2]
_ = x[LineNumbersTable - -3]
_ = x[LineHighlight - -4]
_ = x[LineTable - -5]
_ = x[LineTableTD - -6]
_ = x[Error - -7]
_ = x[Other - -8]
_ = x[None - -9]
_ = x[EOFType-0]
_ = x[Keyword-1000]
_ = x[KeywordConstant-1001]
_ = x[KeywordDeclaration-1002]
_ = x[KeywordNamespace-1003]
_ = x[KeywordPseudo-1004]
_ = x[KeywordReserved-1005]
_ = x[KeywordType-1006]
_ = x[Name-2000]
_ = x[NameAttribute-2001]
_ = x[NameBuiltin-2002]
_ = x[NameBuiltinPseudo-2003]
_ = x[NameClass-2004]
_ = x[NameConstant-2005]
_ = x[NameDecorator-2006]
_ = x[NameEntity-2007]
_ = x[NameException-2008]
_ = x[NameFunction-2009]
_ = x[NameFunctionMagic-2010]
_ = x[NameKeyword-2011]
_ = x[NameLabel-2012]
_ = x[NameNamespace-2013]
_ = x[NameOperator-2014]
_ = x[NameOther-2015]
_ = x[NamePseudo-2016]
_ = x[NameProperty-2017]
_ = x[NameTag-2018]
_ = x[NameVariable-2019]
_ = x[NameVariableAnonymous-2020]
_ = x[NameVariableClass-2021]
_ = x[NameVariableGlobal-2022]
_ = x[NameVariableInstance-2023]
_ = x[NameVariableMagic-2024]
_ = x[Literal-3000]
_ = x[LiteralDate-3001]
_ = x[LiteralOther-3002]
_ = x[LiteralString-3100]
_ = x[LiteralStringAffix-3101]
_ = x[LiteralStringAtom-3102]
_ = x[LiteralStringBacktick-3103]
_ = x[LiteralStringBoolean-3104]
_ = x[LiteralStringChar-3105]
_ = x[LiteralStringDelimiter-3106]
_ = x[LiteralStringDoc-3107]
_ = x[LiteralStringDouble-3108]
_ = x[LiteralStringEscape-3109]
_ = x[LiteralStringHeredoc-3110]
_ = x[LiteralStringInterpol-3111]
_ = x[LiteralStringName-3112]
_ = x[LiteralStringOther-3113]
_ = x[LiteralStringRegex-3114]
_ = x[LiteralStringSingle-3115]
_ = x[LiteralStringSymbol-3116]
_ = x[LiteralNumber-3200]
_ = x[LiteralNumberBin-3201]
_ = x[LiteralNumberFloat-3202]
_ = x[LiteralNumberHex-3203]
_ = x[LiteralNumberInteger-3204]
_ = x[LiteralNumberIntegerLong-3205]
_ = x[LiteralNumberOct-3206]
_ = x[Operator-4000]
_ = x[OperatorWord-4001]
_ = x[Punctuation-5000]
_ = x[Comment-6000]
_ = x[CommentHashbang-6001]
_ = x[CommentMultiline-6002]
_ = x[CommentSingle-6003]
_ = x[CommentSpecial-6004]
_ = x[CommentPreproc-6100]
_ = x[CommentPreprocFile-6101]
_ = x[Generic-7000]
_ = x[GenericDeleted-7001]
_ = x[GenericEmph-7002]
_ = x[GenericError-7003]
_ = x[GenericHeading-7004]
_ = x[GenericInserted-7005]
_ = x[GenericOutput-7006]
_ = x[GenericPrompt-7007]
_ = x[GenericStrong-7008]
_ = x[GenericSubheading-7009]
_ = x[GenericTraceback-7010]
_ = x[GenericUnderline-7011]
_ = x[Text-8000]
_ = x[TextWhitespace-8001]
_ = x[TextSymbol-8002]
_ = x[TextPunctuation-8003]
}
const _TokenType_name = "NoneOtherErrorLineTableTDLineTableLineHighlightLineNumbersTableLineNumbersBackgroundEOFTypeKeywordKeywordConstantKeywordDeclarationKeywordNamespaceKeywordPseudoKeywordReservedKeywordTypeNameNameAttributeNameBuiltinNameBuiltinPseudoNameClassNameConstantNameDecoratorNameEntityNameExceptionNameFunctionNameFunctionMagicNameKeywordNameLabelNameNamespaceNameOperatorNameOtherNamePseudoNamePropertyNameTagNameVariableNameVariableAnonymousNameVariableClassNameVariableGlobalNameVariableInstanceNameVariableMagicLiteralLiteralDateLiteralOtherLiteralStringLiteralStringAffixLiteralStringAtomLiteralStringBacktickLiteralStringBooleanLiteralStringCharLiteralStringDelimiterLiteralStringDocLiteralStringDoubleLiteralStringEscapeLiteralStringHeredocLiteralStringInterpolLiteralStringNameLiteralStringOtherLiteralStringRegexLiteralStringSingleLiteralStringSymbolLiteralNumberLiteralNumberBinLiteralNumberFloatLiteralNumberHexLiteralNumberIntegerLiteralNumberIntegerLongLiteralNumberOctOperatorOperatorWordPunctuationCommentCommentHashbangCommentMultilineCommentSingleCommentSpecialCommentPreprocCommentPreprocFileGenericGenericDeletedGenericEmphGenericErrorGenericHeadingGenericInsertedGenericOutputGenericPromptGenericStrongGenericSubheadingGenericTracebackGenericUnderlineTextTextWhitespaceTextSymbolTextPunctuation"
var _TokenType_map = map[TokenType]string{