blueprint.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. """
  2. pygments.lexers.blueprint
  3. ~~~~~~~~~~~~~~~~~~~~~~~~~
  4. Lexer for the Blueprint UI markup language.
  5. :copyright: Copyright 2006-2024 by the Pygments team, see AUTHORS.
  6. :license: BSD, see LICENSE for details.
  7. """
  8. import re
  9. from pygments.lexer import RegexLexer, include, bygroups, words
  10. from pygments.token import (
  11. Comment,
  12. Operator,
  13. Keyword,
  14. Name,
  15. String,
  16. Number,
  17. Punctuation,
  18. Whitespace,
  19. )
  20. __all__ = ["BlueprintLexer"]
  21. class BlueprintLexer(RegexLexer):
  22. """
  23. For Blueprint UI markup.
  24. """
  25. name = "Blueprint"
  26. aliases = ["blueprint"]
  27. filenames = ["*.blp"]
  28. mimetypes = ["text/x-blueprint"]
  29. url = "https://gitlab.gnome.org/jwestman/blueprint-compiler"
  30. version_added = '2.16'
  31. flags = re.IGNORECASE
  32. tokens = {
  33. "root": [
  34. include("block-content"),
  35. ],
  36. "type": [
  37. (r"\$\s*[a-z_][a-z0-9_\-]*", Name.Class),
  38. (r"(?:([a-z_][a-z0-9_\-]*)(\s*)(\.)(\s*))?([a-z_][a-z0-9_\-]*)",
  39. bygroups(Name.Namespace, Whitespace, Punctuation, Whitespace, Name.Class)),
  40. ],
  41. "whitespace": [
  42. (r"\s+", Whitespace),
  43. (r"//.*?\n", Comment.Single),
  44. (r"/\*", Comment.Multiline, "comment-multiline"),
  45. ],
  46. "comment-multiline": [
  47. (r"\*/", Comment.Multiline, "#pop"),
  48. (r"[^*]+", Comment.Multiline),
  49. (r"\*", Comment.Multiline),
  50. ],
  51. "value": [
  52. (r"(typeof)(\s*)(<)", bygroups(Keyword, Whitespace, Punctuation), "typeof"),
  53. (words(("true", "false", "null")), Keyword.Constant),
  54. (r"[a-z_][a-z0-9_\-]*", Name.Variable),
  55. (r"\|", Operator),
  56. (r'".*?"', String.Double),
  57. (r"\'.*?\'", String.Single),
  58. (r"0x[\d_]*", Number.Hex),
  59. (r"[0-9_]+", Number.Integer),
  60. (r"\d[\d\.a-z_]*", Number),
  61. ],
  62. "typeof": [
  63. include("whitespace"),
  64. include("type"),
  65. (r">", Punctuation, "#pop"),
  66. ],
  67. "content": [
  68. include("whitespace"),
  69. # Keywords
  70. (words(("after", "bidirectional", "bind-property", "bind", "default",
  71. "destructive", "disabled", "inverted", "no-sync-create",
  72. "suggested", "swapped", "sync-create", "template")),
  73. Keyword),
  74. # Translated strings
  75. (r"(C?_)(\s*)(\()",
  76. bygroups(Name.Function.Builtin, Whitespace, Punctuation),
  77. "paren-content"),
  78. # Cast expressions
  79. (r"(as)(\s*)(<)", bygroups(Keyword, Whitespace, Punctuation), "typeof"),
  80. # Closures
  81. (r"(\$?[a-z_][a-z0-9_\-]*)(\s*)(\()",
  82. bygroups(Name.Function, Whitespace, Punctuation),
  83. "paren-content"),
  84. # Objects
  85. (r"(?:(\$\s*[a-z_][a-z0-9_\-]+)|(?:([a-z_][a-z0-9_\-]*)(\s*)(\.)(\s*))?([a-z_][a-z0-9_\-]*))(?:(\s+)([a-z_][a-z0-9_\-]*))?(\s*)(\{)",
  86. bygroups(Name.Class, Name.Namespace, Whitespace, Punctuation, Whitespace,
  87. Name.Class, Whitespace, Name.Variable, Whitespace, Punctuation),
  88. "brace-block"),
  89. # Misc
  90. include("value"),
  91. (r",|\.", Punctuation),
  92. ],
  93. "block-content": [
  94. # Import statements
  95. (r"(using)(\s+)([a-z_][a-z0-9_\-]*)(\s+)(\d[\d\.]*)(;)",
  96. bygroups(Keyword, Whitespace, Name.Namespace, Whitespace,
  97. Name.Namespace, Punctuation)),
  98. # Menus
  99. (r"(menu|section|submenu)(?:(\s+)([a-z_][a-z0-9_\-]*))?(\s*)(\{)",
  100. bygroups(Keyword, Whitespace, Name.Variable, Whitespace, Punctuation),
  101. "brace-block"),
  102. (r"(item)(\s*)(\{)",
  103. bygroups(Keyword, Whitespace, Punctuation),
  104. "brace-block"),
  105. (r"(item)(\s*)(\()",
  106. bygroups(Keyword, Whitespace, Punctuation),
  107. "paren-block"),
  108. # Templates
  109. (r"template", Keyword.Declaration, "template"),
  110. # Nested blocks. When extensions are added, this is where they go.
  111. (r"(responses|items|mime-types|patterns|suffixes|marks|widgets|strings|styles)(\s*)(\[)",
  112. bygroups(Keyword, Whitespace, Punctuation),
  113. "bracket-block"),
  114. (r"(accessibility|setters|layout|item)(\s*)(\{)",
  115. bygroups(Keyword, Whitespace, Punctuation),
  116. "brace-block"),
  117. (r"(condition|mark|item)(\s*)(\()",
  118. bygroups(Keyword, Whitespace, Punctuation),
  119. "paren-content"),
  120. (r"\[", Punctuation, "child-type"),
  121. # Properties and signals
  122. (r"([a-z_][a-z0-9_\-]*(?:::[a-z0-9_]+)?)(\s*)(:|=>)",
  123. bygroups(Name.Property, Whitespace, Punctuation),
  124. "statement"),
  125. include("content"),
  126. ],
  127. "paren-block": [
  128. include("block-content"),
  129. (r"\)", Punctuation, "#pop"),
  130. ],
  131. "paren-content": [
  132. include("content"),
  133. (r"\)", Punctuation, "#pop"),
  134. ],
  135. "bracket-block": [
  136. include("block-content"),
  137. (r"\]", Punctuation, "#pop"),
  138. ],
  139. "brace-block": [
  140. include("block-content"),
  141. (r"\}", Punctuation, "#pop"),
  142. ],
  143. "statement": [
  144. include("content"),
  145. (r";", Punctuation, "#pop"),
  146. ],
  147. "child-type": [
  148. include("whitespace"),
  149. (r"(action)(\s+)(response)(\s*)(=)(\s*)",
  150. bygroups(Keyword, Whitespace, Name.Attribute, Whitespace,
  151. Punctuation, Whitespace)),
  152. (words(("default", "internal-child", "response")), Keyword),
  153. (r"[a-z_][a-z0-9_\-]*", Name.Decorator),
  154. include("value"),
  155. (r"=", Punctuation),
  156. (r"\]", Punctuation, "#pop"),
  157. ],
  158. "template": [
  159. include("whitespace"),
  160. include("type"),
  161. (r":", Punctuation),
  162. (r"\{", Punctuation, ("#pop", "brace-block")),
  163. ],
  164. }