usd.py 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. """
  2. pygments.lexers.usd
  3. ~~~~~~~~~~~~~~~~~~~
  4. The module that parses Pixar's Universal Scene Description file format.
  5. :copyright: Copyright 2006-2024 by the Pygments team, see AUTHORS.
  6. :license: BSD, see LICENSE for details.
  7. """
  8. from pygments.lexer import RegexLexer, bygroups
  9. from pygments.lexer import words as words_
  10. from pygments.lexers._usd_builtins import COMMON_ATTRIBUTES, KEYWORDS, \
  11. OPERATORS, SPECIAL_NAMES, TYPES
  12. from pygments.token import Comment, Keyword, Name, Number, Operator, \
  13. Punctuation, String, Text, Whitespace
  14. __all__ = ["UsdLexer"]
  15. def _keywords(words, type_):
  16. return [(words_(words, prefix=r"\b", suffix=r"\b"), type_)]
  17. _TYPE = r"(\w+(?:\[\])?)"
  18. _BASE_ATTRIBUTE = r"(\w+(?:\:\w+)*)(?:(\.)(timeSamples))?"
  19. _WHITESPACE = r"([ \t]+)"
  20. class UsdLexer(RegexLexer):
  21. """
  22. A lexer that parses Pixar's Universal Scene Description file format.
  23. """
  24. name = "USD"
  25. url = 'https://graphics.pixar.com/usd/release/index.html'
  26. aliases = ["usd", "usda"]
  27. filenames = ["*.usd", "*.usda"]
  28. version_added = '2.6'
  29. tokens = {
  30. "root": [
  31. (rf"(custom){_WHITESPACE}(uniform)(\s+){_TYPE}(\s+){_BASE_ATTRIBUTE}(\s*)(=)",
  32. bygroups(Keyword.Token, Whitespace, Keyword.Token, Whitespace,
  33. Keyword.Type, Whitespace, Name.Attribute, Text,
  34. Name.Keyword.Tokens, Whitespace, Operator)),
  35. (rf"(custom){_WHITESPACE}{_TYPE}(\s+){_BASE_ATTRIBUTE}(\s*)(=)",
  36. bygroups(Keyword.Token, Whitespace, Keyword.Type, Whitespace,
  37. Name.Attribute, Text, Name.Keyword.Tokens, Whitespace,
  38. Operator)),
  39. (rf"(uniform){_WHITESPACE}{_TYPE}(\s+){_BASE_ATTRIBUTE}(\s*)(=)",
  40. bygroups(Keyword.Token, Whitespace, Keyword.Type, Whitespace,
  41. Name.Attribute, Text, Name.Keyword.Tokens, Whitespace,
  42. Operator)),
  43. (rf"{_TYPE}{_WHITESPACE}{_BASE_ATTRIBUTE}(\s*)(=)",
  44. bygroups(Keyword.Type, Whitespace, Name.Attribute, Text,
  45. Name.Keyword.Tokens, Whitespace, Operator)),
  46. ] +
  47. _keywords(KEYWORDS, Keyword.Tokens) +
  48. _keywords(SPECIAL_NAMES, Name.Builtins) +
  49. _keywords(COMMON_ATTRIBUTES, Name.Attribute) +
  50. [(r"\b\w+:[\w:]+\b", Name.Attribute)] +
  51. _keywords(OPERATORS, Operator) + # more attributes
  52. [(type_ + r"\[\]", Keyword.Type) for type_ in TYPES] +
  53. _keywords(TYPES, Keyword.Type) +
  54. [
  55. (r"[(){}\[\]]", Punctuation),
  56. ("#.*?$", Comment.Single),
  57. (",", Punctuation),
  58. (";", Punctuation), # ";"s are allowed to combine separate metadata lines
  59. ("=", Operator),
  60. (r"[-]*([0-9]*[.])?[0-9]+(?:e[+-]*\d+)?", Number),
  61. (r"'''(?:.|\n)*?'''", String),
  62. (r'"""(?:.|\n)*?"""', String),
  63. (r"'.*?'", String),
  64. (r'".*?"', String),
  65. (r"<(\.\./)*([\w/]+|[\w/]+\.\w+[\w:]*)>", Name.Namespace),
  66. (r"@.*?@", String.Interpol),
  67. (r'\(.*"[.\\n]*".*\)', String.Doc),
  68. (r"\A#usda .+$", Comment.Hashbang),
  69. (r"\s+", Whitespace),
  70. (r"\w+", Text),
  71. (r"[_:.]+", Punctuation),
  72. ],
  73. }