You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

go.go 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. package g
  2. import (
  3. "strings"
  4. . "github.com/alecthomas/chroma" // nolint
  5. "github.com/alecthomas/chroma/lexers/h"
  6. "github.com/alecthomas/chroma/lexers/internal"
  7. )
  8. // Go lexer.
  9. var Go = internal.Register(MustNewLexer(
  10. &Config{
  11. Name: "Go",
  12. Aliases: []string{"go", "golang"},
  13. Filenames: []string{"*.go"},
  14. MimeTypes: []string{"text/x-gosrc"},
  15. EnsureNL: true,
  16. },
  17. Rules{
  18. "root": {
  19. {`\n`, Text, nil},
  20. {`\s+`, Text, nil},
  21. {`\\\n`, Text, nil},
  22. {`//(.*?)\n`, CommentSingle, nil},
  23. {`/(\\\n)?[*](.|\n)*?[*](\\\n)?/`, CommentMultiline, nil},
  24. {`(import|package)\b`, KeywordNamespace, nil},
  25. {`(var|func|struct|map|chan|type|interface|const)\b`, KeywordDeclaration, nil},
  26. {Words(``, `\b`, `break`, `default`, `select`, `case`, `defer`, `go`, `else`, `goto`, `switch`, `fallthrough`, `if`, `range`, `continue`, `for`, `return`), Keyword, nil},
  27. {`(true|false|iota|nil)\b`, KeywordConstant, nil},
  28. {Words(``, `\b(\()`, `uint`, `uint8`, `uint16`, `uint32`, `uint64`, `int`, `int8`, `int16`, `int32`, `int64`, `float`, `float32`, `float64`, `complex64`, `complex128`, `byte`, `rune`, `string`, `bool`, `error`, `uintptr`, `print`, `println`, `panic`, `recover`, `close`, `complex`, `real`, `imag`, `len`, `cap`, `append`, `copy`, `delete`, `new`, `make`), ByGroups(NameBuiltin, Punctuation), nil},
  29. {Words(``, `\b`, `uint`, `uint8`, `uint16`, `uint32`, `uint64`, `int`, `int8`, `int16`, `int32`, `int64`, `float`, `float32`, `float64`, `complex64`, `complex128`, `byte`, `rune`, `string`, `bool`, `error`, `uintptr`), KeywordType, nil},
  30. {`\d+i`, LiteralNumber, nil},
  31. {`\d+\.\d*([Ee][-+]\d+)?i`, LiteralNumber, nil},
  32. {`\.\d+([Ee][-+]\d+)?i`, LiteralNumber, nil},
  33. {`\d+[Ee][-+]\d+i`, LiteralNumber, nil},
  34. {`\d+(\.\d+[eE][+\-]?\d+|\.\d*|[eE][+\-]?\d+)`, LiteralNumberFloat, nil},
  35. {`\.\d+([eE][+\-]?\d+)?`, LiteralNumberFloat, nil},
  36. {`0[0-7]+`, LiteralNumberOct, nil},
  37. {`0[xX][0-9a-fA-F]+`, LiteralNumberHex, nil},
  38. {`(0|[1-9][0-9]*)`, LiteralNumberInteger, nil},
  39. {`'(\\['"\\abfnrtv]|\\x[0-9a-fA-F]{2}|\\[0-7]{1,3}|\\u[0-9a-fA-F]{4}|\\U[0-9a-fA-F]{8}|[^\\])'`, LiteralStringChar, nil},
  40. {"(`)([^`]*)(`)", ByGroups(LiteralString, Using(TypeRemappingLexer(GoTextTemplate, TypeMapping{{Other, LiteralString, nil}})), LiteralString), nil},
  41. {`"(\\\\|\\"|[^"])*"`, LiteralString, nil},
  42. {`(<<=|>>=|<<|>>|<=|>=|&\^=|&\^|\+=|-=|\*=|/=|%=|&=|\|=|&&|\|\||<-|\+\+|--|==|!=|:=|\.\.\.|[+\-*/%&])`, Operator, nil},
  43. {`([a-zA-Z_]\w*)(\s*)(\()`, ByGroups(NameFunction, UsingSelf("root"), Punctuation), nil},
  44. {`[|^<>=!()\[\]{}.,;:]`, Punctuation, nil},
  45. {`[^\W\d]\w*`, NameOther, nil},
  46. },
  47. },
  48. ).SetAnalyser(func(text string) float32 {
  49. if strings.Contains(text, "fmt.") && strings.Contains(text, "package ") {
  50. return 0.5
  51. }
  52. if strings.Contains(text, "package ") {
  53. return 0.1
  54. }
  55. return 0.0
  56. }))
  57. var goTemplateRules = Rules{
  58. "root": {
  59. {`{{(- )?/\*(.|\n)*?\*/( -)?}}`, CommentMultiline, nil},
  60. {`{{[-]?`, CommentPreproc, Push("template")},
  61. {`[^{]+`, Other, nil},
  62. {`{`, Other, nil},
  63. },
  64. "template": {
  65. {`[-]?}}`, CommentPreproc, Pop(1)},
  66. {`(?=}})`, CommentPreproc, Pop(1)}, // Terminate the pipeline
  67. {`\(`, Operator, Push("subexpression")},
  68. {`"(\\\\|\\"|[^"])*"`, LiteralString, nil},
  69. Include("expression"),
  70. },
  71. "subexpression": {
  72. {`\)`, Operator, Pop(1)},
  73. Include("expression"),
  74. },
  75. "expression": {
  76. {`\s+`, Whitespace, nil},
  77. {`\(`, Operator, Push("subexpression")},
  78. {`(range|if|else|while|with|template|end|true|false|nil|and|call|html|index|js|len|not|or|print|printf|println|urlquery|eq|ne|lt|le|gt|ge)\b`, Keyword, nil},
  79. {`\||:?=|,`, Operator, nil},
  80. {`[$]?[^\W\d]\w*`, NameOther, nil},
  81. {`[$]?\.(?:[^\W\d]\w*)?`, NameAttribute, nil},
  82. {`"(\\\\|\\"|[^"])*"`, LiteralString, nil},
  83. {`-?\d+i`, LiteralNumber, nil},
  84. {`-?\d+\.\d*([Ee][-+]\d+)?i`, LiteralNumber, nil},
  85. {`\.\d+([Ee][-+]\d+)?i`, LiteralNumber, nil},
  86. {`-?\d+[Ee][-+]\d+i`, LiteralNumber, nil},
  87. {`-?\d+(\.\d+[eE][+\-]?\d+|\.\d*|[eE][+\-]?\d+)`, LiteralNumberFloat, nil},
  88. {`-?\.\d+([eE][+\-]?\d+)?`, LiteralNumberFloat, nil},
  89. {`-?0[0-7]+`, LiteralNumberOct, nil},
  90. {`-?0[xX][0-9a-fA-F]+`, LiteralNumberHex, nil},
  91. {`-?(0|[1-9][0-9]*)`, LiteralNumberInteger, nil},
  92. {`'(\\['"\\abfnrtv]|\\x[0-9a-fA-F]{2}|\\[0-7]{1,3}|\\u[0-9a-fA-F]{4}|\\U[0-9a-fA-F]{8}|[^\\])'`, LiteralStringChar, nil},
  93. {"`[^`]*`", LiteralString, nil},
  94. },
  95. }
  96. var GoHTMLTemplate = internal.Register(DelegatingLexer(h.HTML, MustNewLexer(
  97. &Config{
  98. Name: "Go HTML Template",
  99. Aliases: []string{"go-html-template"},
  100. },
  101. goTemplateRules,
  102. )))
  103. var GoTextTemplate = internal.Register(MustNewLexer(
  104. &Config{
  105. Name: "Go Text Template",
  106. Aliases: []string{"go-text-template"},
  107. },
  108. goTemplateRules,
  109. ))