usd.py 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. """
  2. pygments.lexers.usd
  3. ~~~~~~~~~~~~~~~~~~~
  4. The module that parses Pixar's Universal Scene Description file format.
  5. :copyright: Copyright 2006-2021 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. .. versionadded:: 2.6
  24. """
  25. name = "USD"
  26. aliases = ["usd", "usda"]
  27. filenames = ["*.usd", "*.usda"]
  28. tokens = {
  29. "root": [
  30. (r"(custom){_WHITESPACE}(uniform)(\s+){}(\s+){}(\s*)(=)".format(
  31. _TYPE, _BASE_ATTRIBUTE, _WHITESPACE=_WHITESPACE),
  32. bygroups(Keyword.Token, Whitespace, Keyword.Token, Whitespace,
  33. Keyword.Type, Whitespace, Name.Attribute, Text,
  34. Name.Keyword.Tokens, Whitespace, Operator)),
  35. (r"(custom){_WHITESPACE}{}(\s+){}(\s*)(=)".format(
  36. _TYPE, _BASE_ATTRIBUTE, _WHITESPACE=_WHITESPACE),
  37. bygroups(Keyword.Token, Whitespace, Keyword.Type, Whitespace,
  38. Name.Attribute, Text, Name.Keyword.Tokens, Whitespace,
  39. Operator)),
  40. (r"(uniform){_WHITESPACE}{}(\s+){}(\s*)(=)".format(
  41. _TYPE, _BASE_ATTRIBUTE, _WHITESPACE=_WHITESPACE),
  42. bygroups(Keyword.Token, Whitespace, Keyword.Type, Whitespace,
  43. Name.Attribute, Text, Name.Keyword.Tokens, Whitespace,
  44. Operator)),
  45. (r"{}{_WHITESPACE}{}(\s*)(=)".format(
  46. _TYPE, _BASE_ATTRIBUTE, _WHITESPACE=_WHITESPACE),
  47. bygroups(Keyword.Type, Whitespace, Name.Attribute, Text,
  48. Name.Keyword.Tokens, Whitespace, Operator)),
  49. ] +
  50. _keywords(KEYWORDS, Keyword.Tokens) +
  51. _keywords(SPECIAL_NAMES, Name.Builtins) +
  52. _keywords(COMMON_ATTRIBUTES, Name.Attribute) +
  53. [(r"\b\w+:[\w:]+\b", Name.Attribute)] +
  54. _keywords(OPERATORS, Operator) + # more attributes
  55. [(type_ + r"\[\]", Keyword.Type) for type_ in TYPES] +
  56. _keywords(TYPES, Keyword.Type) +
  57. [
  58. (r"[(){}\[\]]", Punctuation),
  59. ("#.*?$", Comment.Single),
  60. (",", Punctuation),
  61. (";", Punctuation), # ";"s are allowed to combine separate metadata lines
  62. ("=", Operator),
  63. (r"[-]*([0-9]*[.])?[0-9]+(?:e[+-]*\d+)?", Number),
  64. (r"'''(?:.|\n)*?'''", String),
  65. (r'"""(?:.|\n)*?"""', String),
  66. (r"'.*?'", String),
  67. (r'".*?"', String),
  68. (r"<(\.\./)*([\w/]+|[\w/]+\.\w+[\w:]*)>", Name.Namespace),
  69. (r"@.*?@", String.Interpol),
  70. (r'\(.*"[.\\n]*".*\)', String.Doc),
  71. (r"\A#usda .+$", Comment.Hashbang),
  72. (r"\s+", Whitespace),
  73. (r"\w+", Text),
  74. (r"[_:.]+", Punctuation),
  75. ],
  76. }