1. package lexers
    
  2. 
    
  3. import (
    
  4. 	. "github.com/alecthomas/chroma/v2" // nolint
    
  5. )
    
  6. 
    
  7. // Svelte lexer.
    
  8. var Svelte = Register(DelegatingLexer(HTML, MustNewLexer(
    
  9. 	&Config{
    
  10. 		Name:      "Svelte",
    
  11. 		Aliases:   []string{"svelte"},
    
  12. 		Filenames: []string{"*.svelte"},
    
  13. 		MimeTypes: []string{"application/x-svelte"},
    
  14. 		DotAll:    true,
    
  15. 	},
    
  16. 	svelteRules,
    
  17. )))
    
  18. 
    
  19. func svelteRules() Rules {
    
  20. 	return Rules{
    
  21. 		"root": {
    
  22. 			// Let HTML handle the comments, including comments containing script and style tags
    
  23. 			{`<!--`, Other, Push("comment")},
    
  24. 			{
    
  25. 				// Highlight script and style tags based on lang attribute
    
  26. 				// and allow attributes besides lang
    
  27. 				`(<\s*(?:script|style).*?lang\s*=\s*['"])` +
    
  28. 					`(.+?)(['"].*?>)` +
    
  29. 					`(.+?)` +
    
  30. 					`(<\s*/\s*(?:script|style)\s*>)`,
    
  31. 				UsingByGroup(2, 4, Other, Other, Other, Other, Other),
    
  32. 				nil,
    
  33. 			},
    
  34. 			{
    
  35. 				// Make sure `{` is not inside script or style tags
    
  36. 				`(?<!<\s*(?:script|style)(?:(?!(?:script|style)\s*>).)*?)` +
    
  37. 					`{` +
    
  38. 					`(?!(?:(?!<\s*(?:script|style)).)*?(?:script|style)\s*>)`,
    
  39. 				Punctuation,
    
  40. 				Push("templates"),
    
  41. 			},
    
  42. 			// on:submit|preventDefault
    
  43. 			{`(?<=\s+on:\w+(?:\|\w+)*)\|(?=\w+)`, Operator, nil},
    
  44. 			{`.+?`, Other, nil},
    
  45. 		},
    
  46. 		"comment": {
    
  47. 			{`-->`, Other, Pop(1)},
    
  48. 			{`.+?`, Other, nil},
    
  49. 		},
    
  50. 		"templates": {
    
  51. 			{`}`, Punctuation, Pop(1)},
    
  52. 			// Let TypeScript handle strings and the curly braces inside them
    
  53. 			{`(?<!(?<!\\)\\)(['"` + "`])" + `.*?(?<!(?<!\\)\\)\1`, Using("TypeScript"), nil},
    
  54. 			// If there is another opening curly brace push to templates again
    
  55. 			{"{", Punctuation, Push("templates")},
    
  56. 			{`@(debug|html)\b`, Keyword, nil},
    
  57. 			{
    
  58. 				`(#await)(\s+)(\w+)(\s+)(then|catch)(\s+)(\w+)`,
    
  59. 				ByGroups(Keyword, Text, Using("TypeScript"), Text,
    
  60. 					Keyword, Text, Using("TypeScript"),
    
  61. 				),
    
  62. 				nil,
    
  63. 			},
    
  64. 			{`(#|/)(await|each|if|key)\b`, Keyword, nil},
    
  65. 			{`(:else)(\s+)(if)?\b`, ByGroups(Keyword, Text, Keyword), nil},
    
  66. 			{`:(catch|then)\b`, Keyword, nil},
    
  67. 			{`[^{}]+`, Using("TypeScript"), nil},
    
  68. 		},
    
  69. 	}
    
  70. }