...

Source file src/github.com/alecthomas/chroma/lexers/c/caddyfile.go

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

     1  package c
     2  
     3  import (
     4  	. "github.com/alecthomas/chroma" // nolint
     5  	"github.com/alecthomas/chroma/lexers/internal"
     6  )
     7  
     8  // caddyfileCommon are the rules common to both of the lexer variants
     9  func caddyfileCommonRules() Rules {
    10  	return Rules{
    11  		"site_block_common": {
    12  			// Import keyword
    13  			{`(import)(\s+)([^\s]+)`, ByGroups(Keyword, Text, NameVariableMagic), nil},
    14  			// Matcher definition
    15  			{`@[^\s]+(?=\s)`, NameDecorator, Push("matcher")},
    16  			// Matcher token stub for docs
    17  			{`\[\<matcher\>\]`, NameDecorator, Push("matcher")},
    18  			// These cannot have matchers but may have things that look like
    19  			// matchers in their arguments, so we just parse as a subdirective.
    20  			{`try_files`, Keyword, Push("subdirective")},
    21  			// These are special, they can nest more directives
    22  			{`handle_errors|handle|route|handle_path|not`, Keyword, Push("nested_directive")},
    23  			// Any other directive
    24  			{`[^\s#]+`, Keyword, Push("directive")},
    25  			Include("base"),
    26  		},
    27  		"matcher": {
    28  			{`\{`, Punctuation, Push("block")},
    29  			// Not can be one-liner
    30  			{`not`, Keyword, Push("deep_not_matcher")},
    31  			// Any other same-line matcher
    32  			{`[^\s#]+`, Keyword, Push("arguments")},
    33  			// Terminators
    34  			{`\n`, Text, Pop(1)},
    35  			{`\}`, Punctuation, Pop(1)},
    36  			Include("base"),
    37  		},
    38  		"block": {
    39  			{`\}`, Punctuation, Pop(2)},
    40  			// Not can be one-liner
    41  			{`not`, Keyword, Push("not_matcher")},
    42  			// Any other subdirective
    43  			{`[^\s#]+`, Keyword, Push("subdirective")},
    44  			Include("base"),
    45  		},
    46  		"nested_block": {
    47  			{`\}`, Punctuation, Pop(2)},
    48  			// Matcher definition
    49  			{`@[^\s]+(?=\s)`, NameDecorator, Push("matcher")},
    50  			// Something that starts with literally < is probably a docs stub
    51  			{`\<[^#]+\>`, Keyword, Push("nested_directive")},
    52  			// Any other directive
    53  			{`[^\s#]+`, Keyword, Push("nested_directive")},
    54  			Include("base"),
    55  		},
    56  		"not_matcher": {
    57  			{`\}`, Punctuation, Pop(2)},
    58  			{`\{(?=\s)`, Punctuation, Push("block")},
    59  			{`[^\s#]+`, Keyword, Push("arguments")},
    60  			{`\s+`, Text, nil},
    61  		},
    62  		"deep_not_matcher": {
    63  			{`\}`, Punctuation, Pop(2)},
    64  			{`\{(?=\s)`, Punctuation, Push("block")},
    65  			{`[^\s#]+`, Keyword, Push("deep_subdirective")},
    66  			{`\s+`, Text, nil},
    67  		},
    68  		"directive": {
    69  			{`\{(?=\s)`, Punctuation, Push("block")},
    70  			Include("matcher_token"),
    71  			Include("comments_pop_1"),
    72  			{`\n`, Text, Pop(1)},
    73  			Include("base"),
    74  		},
    75  		"nested_directive": {
    76  			{`\{(?=\s)`, Punctuation, Push("nested_block")},
    77  			Include("matcher_token"),
    78  			Include("comments_pop_1"),
    79  			{`\n`, Text, Pop(1)},
    80  			Include("base"),
    81  		},
    82  		"subdirective": {
    83  			{`\{(?=\s)`, Punctuation, Push("block")},
    84  			Include("comments_pop_1"),
    85  			{`\n`, Text, Pop(1)},
    86  			Include("base"),
    87  		},
    88  		"arguments": {
    89  			{`\{(?=\s)`, Punctuation, Push("block")},
    90  			Include("comments_pop_2"),
    91  			{`\\\n`, Text, nil}, // Skip escaped newlines
    92  			{`\n`, Text, Pop(2)},
    93  			Include("base"),
    94  		},
    95  		"deep_subdirective": {
    96  			{`\{(?=\s)`, Punctuation, Push("block")},
    97  			Include("comments_pop_3"),
    98  			{`\n`, Text, Pop(3)},
    99  			Include("base"),
   100  		},
   101  		"matcher_token": {
   102  			{`@[^\s]+`, NameDecorator, Push("arguments")},         // Named matcher
   103  			{`/[^\s]+`, NameDecorator, Push("arguments")},         // Path matcher
   104  			{`\*`, NameDecorator, Push("arguments")},              // Wildcard path matcher
   105  			{`\[\<matcher\>\]`, NameDecorator, Push("arguments")}, // Matcher token stub for docs
   106  		},
   107  		"comments": {
   108  			{`^#.*\n`, CommentSingle, nil},   // Comment at start of line
   109  			{`\s+#.*\n`, CommentSingle, nil}, // Comment preceded by whitespace
   110  		},
   111  		"comments_pop_1": {
   112  			{`^#.*\n`, CommentSingle, Pop(1)},   // Comment at start of line
   113  			{`\s+#.*\n`, CommentSingle, Pop(1)}, // Comment preceded by whitespace
   114  		},
   115  		"comments_pop_2": {
   116  			{`^#.*\n`, CommentSingle, Pop(2)},   // Comment at start of line
   117  			{`\s+#.*\n`, CommentSingle, Pop(2)}, // Comment preceded by whitespace
   118  		},
   119  		"comments_pop_3": {
   120  			{`^#.*\n`, CommentSingle, Pop(3)},   // Comment at start of line
   121  			{`\s+#.*\n`, CommentSingle, Pop(3)}, // Comment preceded by whitespace
   122  		},
   123  		"base": {
   124  			Include("comments"),
   125  			{`(on|off|first|last|before|after|internal|strip_prefix|strip_suffix|replace)\b`, NameConstant, nil},
   126  			{`(https?://)?([a-z0-9.-]+)(:)([0-9]+)`, ByGroups(Name, Name, Punctuation, LiteralNumberInteger), nil},
   127  			{`[a-z-]+/[a-z-+]+`, LiteralString, nil},
   128  			{`[0-9]+[km]?\b`, LiteralNumberInteger, nil},
   129  			{`\{[\w+.\$-]+\}`, LiteralStringEscape, nil}, // Placeholder
   130  			{`\[(?=[^#{}$]+\])`, Punctuation, nil},
   131  			{`\]|\|`, Punctuation, nil},
   132  			{`[^\s#{}$\]]+`, LiteralString, nil},
   133  			{`/[^\s#]*`, Name, nil},
   134  			{`\s+`, Text, nil},
   135  		},
   136  	}
   137  }
   138  
   139  // Caddyfile lexer.
   140  var Caddyfile = internal.Register(MustNewLazyLexer(
   141  	&Config{
   142  		Name:      "Caddyfile",
   143  		Aliases:   []string{"caddyfile", "caddy"},
   144  		Filenames: []string{"Caddyfile*"},
   145  		MimeTypes: []string{},
   146  	},
   147  	caddyfileRules,
   148  ))
   149  
   150  func caddyfileRules() Rules {
   151  	return Rules{
   152  		"root": {
   153  			Include("comments"),
   154  			// Global options block
   155  			{`^\s*(\{)\s*$`, ByGroups(Punctuation), Push("globals")},
   156  			// Snippets
   157  			{`(\([^\s#]+\))(\s*)(\{)`, ByGroups(NameVariableAnonymous, Text, Punctuation), Push("snippet")},
   158  			// Site label
   159  			{`[^#{(\s,]+`, GenericHeading, Push("label")},
   160  			// Site label with placeholder
   161  			{`\{[\w+.\$-]+\}`, LiteralStringEscape, Push("label")},
   162  			{`\s+`, Text, nil},
   163  		},
   164  		"globals": {
   165  			{`\}`, Punctuation, Pop(1)},
   166  			{`[^\s#]+`, Keyword, Push("directive")},
   167  			Include("base"),
   168  		},
   169  		"snippet": {
   170  			{`\}`, Punctuation, Pop(1)},
   171  			// Matcher definition
   172  			{`@[^\s]+(?=\s)`, NameDecorator, Push("matcher")},
   173  			// Any directive
   174  			{`[^\s#]+`, Keyword, Push("directive")},
   175  			Include("base"),
   176  		},
   177  		"label": {
   178  			// Allow multiple labels, comma separated, newlines after
   179  			// a comma means another label is coming
   180  			{`,\s*\n?`, Text, nil},
   181  			{` `, Text, nil},
   182  			// Site label with placeholder
   183  			{`\{[\w+.\$-]+\}`, LiteralStringEscape, nil},
   184  			// Site label
   185  			{`[^#{(\s,]+`, GenericHeading, nil},
   186  			// Comment after non-block label (hack because comments end in \n)
   187  			{`#.*\n`, CommentSingle, Push("site_block")},
   188  			// Note: if \n, we'll never pop out of the site_block, it's valid
   189  			{`\{(?=\s)|\n`, Punctuation, Push("site_block")},
   190  		},
   191  		"site_block": {
   192  			{`\}`, Punctuation, Pop(2)},
   193  			Include("site_block_common"),
   194  		},
   195  	}.Merge(caddyfileCommonRules())
   196  }
   197  
   198  // Caddyfile directive-only lexer.
   199  var CaddyfileDirectives = internal.Register(MustNewLazyLexer(
   200  	&Config{
   201  		Name:      "Caddyfile Directives",
   202  		Aliases:   []string{"caddyfile-directives", "caddyfile-d", "caddy-d"},
   203  		Filenames: []string{},
   204  		MimeTypes: []string{},
   205  	},
   206  	caddyfileDirectivesRules,
   207  ))
   208  
   209  func caddyfileDirectivesRules() Rules {
   210  	return Rules{
   211  		// Same as "site_block" in Caddyfile
   212  		"root": {
   213  			Include("site_block_common"),
   214  		},
   215  	}.Merge(caddyfileCommonRules())
   216  }
   217  

View as plain text