gitea

Development moved to Codeberg

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
  13. 13
  14. 14
  15. 15
  16. 16
  17. 17
  18. 18
  19. 19
  20. 20
  21. 21
  22. 22
  23. 23
  24. 24
  25. 25
  26. 26
  27. 27
  28. 28
  29. 29
  30. 30
  31. 31
  32. 32
  33. 33
  34. 34
  35. 35
  36. 36
  37. 37
  38. 38
  39. 39
  40. 40
  41. 41
  42. 42
  43. 43
  44. 44
  45. 45
  46. 46
  47. 47
  48. 48
  49. 49
  50. 50
  51. 51
  52. 52
  53. 53
  54. 54
  55. 55
  56. 56
  57. 57
  58. 58
  59. 59
  60. 60
  61. 61
  62. 62
  63. 63
  64. 64
  65. 65
  66. 66
  67. 67
  68. 68
  69. 69
  70. 70
  71. 71
package y

import (
	. "github.com/alecthomas/chroma" // nolint
	"github.com/alecthomas/chroma/lexers/internal"
)

var YANG = internal.Register(MustNewLazyLexer(
	&Config{
		Name:      "YANG",
		Aliases:   []string{"yang"},
		Filenames: []string{"*.yang"},
		MimeTypes: []string{"application/yang"},
	},
	yangRules,
))

func yangRules() Rules {
	return Rules{
		"root": {
			{`\s+`, Whitespace, nil},
			{`[\{\}\;]+`, Punctuation, nil},
			{`(?<![\-\w])(and|or|not|\+|\.)(?![\-\w])`, Operator, nil},

			{`"(?:\\"|[^"])*?"`, StringDouble, nil},
			{`'(?:\\'|[^'])*?'`, StringSingle, nil},

			{`/\*`, CommentMultiline, Push("comments")},
			{`//.*?$`, CommentSingle, nil},

			// match BNF stmt for `node-identifier` with [ prefix ":"]
			{`(?:^|(?<=[\s{};]))([\w.-]+)(:)([\w.-]+)(?=[\s{};])`, ByGroups(KeywordNamespace, Punctuation, Text), nil},

			// match BNF stmt `date-arg-str`
			{`([0-9]{4}\-[0-9]{2}\-[0-9]{2})(?=[\s\{\}\;])`, LiteralDate, nil},
			{`([0-9]+\.[0-9]+)(?=[\s\{\}\;])`, NumberFloat, nil},
			{`([0-9]+)(?=[\s\{\}\;])`, NumberInteger, nil},

			// TOP_STMTS_KEYWORDS
			{Words(``, `(?=[^\w\-\:])`, `module`, `submodule`), Keyword, nil},
			// MODULE_HEADER_STMT_KEYWORDS
			{Words(``, `(?=[^\w\-\:])`, `belongs-to`, `namespace`, `prefix`, `yang-version`), Keyword, nil},
			// META_STMT_KEYWORDS
			{Words(``, `(?=[^\w\-\:])`, `contact`, `description`, `organization`, `reference`, `revision`), Keyword, nil},
			// LINKAGE_STMTS_KEYWORDS
			{Words(``, `(?=[^\w\-\:])`, `import`, `include`, `revision-date`), Keyword, nil},
			// BODY_STMT_KEYWORDS
			{Words(``, `(?=[^\w\-\:])`, `action`, `argument`, `augment`, `deviation`, `extension`, `feature`, `grouping`, `identity`, `if-feature`, `input`, `notification`, `output`, `rpc`, `typedef`), Keyword, nil},
			// DATA_DEF_STMT_KEYWORDS
			{Words(``, `(?=[^\w\-\:])`, `anydata`, `anyxml`, `case`, `choice`, `config`, `container`, `deviate`, `leaf`, `leaf-list`, `list`, `must`, `presence`, `refine`, `uses`, `when`), Keyword, nil},
			// TYPE_STMT_KEYWORDS
			{Words(``, `(?=[^\w\-\:])`, `base`, `bit`, `default`, `enum`, `error-app-tag`, `error-message`, `fraction-digits`, `length`, `max-elements`, `min-elements`, `modifier`, `ordered-by`, `path`, `pattern`, `position`, `range`, `require-instance`, `status`, `type`, `units`, `value`, `yin-element`), Keyword, nil},
			// LIST_STMT_KEYWORDS
			{Words(``, `(?=[^\w\-\:])`, `key`, `mandatory`, `unique`), Keyword, nil},

			// CONSTANTS_KEYWORDS - RFC7950 other keywords
			{Words(``, `(?=[^\w\-\:])`, `add`, `current`, `delete`, `deprecated`, `false`, `invert-match`, `max`, `min`, `not-supported`, `obsolete`, `replace`, `true`, `unbounded`, `user`), NameClass, nil},

			// RFC7950 Built-In Types
			{Words(``, `(?=[^\w\-\:])`, `binary`, `bits`, `boolean`, `decimal64`, `empty`, `enumeration`, `identityref`, `instance-identifier`, `int16`, `int32`, `int64`, `int8`, `leafref`, `string`, `uint16`, `uint32`, `uint64`, `uint8`, `union`), NameClass, nil},

			{`[^;{}\s\'\"]+`, Text, nil},
		},
		"comments": {
			{`[^*/]`, CommentMultiline, nil},
			{`/\*`, CommentMultiline, Push("comment")},
			{`\*/`, CommentMultiline, Pop(1)},
			{`[*/]`, CommentMultiline, nil},
		},
	}
}