graphviz.py 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. """
  2. pygments.lexers.graphviz
  3. ~~~~~~~~~~~~~~~~~~~~~~~~
  4. Lexer for the DOT language (graphviz).
  5. :copyright: Copyright 2006-2023 by the Pygments team, see AUTHORS.
  6. :license: BSD, see LICENSE for details.
  7. """
  8. from pygments.lexer import RegexLexer, bygroups
  9. from pygments.token import Comment, Keyword, Operator, Name, String, Number, \
  10. Punctuation, Whitespace
  11. __all__ = ['GraphvizLexer']
  12. class GraphvizLexer(RegexLexer):
  13. """
  14. For graphviz DOT graph description language.
  15. .. versionadded:: 2.8
  16. """
  17. name = 'Graphviz'
  18. url = 'https://www.graphviz.org/doc/info/lang.html'
  19. aliases = ['graphviz', 'dot']
  20. filenames = ['*.gv', '*.dot']
  21. mimetypes = ['text/x-graphviz', 'text/vnd.graphviz']
  22. tokens = {
  23. 'root': [
  24. (r'\s+', Whitespace),
  25. (r'(#|//).*?$', Comment.Single),
  26. (r'/(\\\n)?[*](.|\n)*?[*](\\\n)?/', Comment.Multiline),
  27. (r'(?i)(node|edge|graph|digraph|subgraph|strict)\b', Keyword),
  28. (r'--|->', Operator),
  29. (r'[{}[\]:;,]', Punctuation),
  30. (r'(\b\D\w*)(\s*)(=)(\s*)',
  31. bygroups(Name.Attribute, Whitespace, Punctuation, Whitespace),
  32. 'attr_id'),
  33. (r'\b(n|ne|e|se|s|sw|w|nw|c|_)\b', Name.Builtin),
  34. (r'\b\D\w*', Name.Tag), # node
  35. (r'[-]?((\.[0-9]+)|([0-9]+(\.[0-9]*)?))', Number),
  36. (r'"(\\"|[^"])*?"', Name.Tag), # quoted node
  37. (r'<', Punctuation, 'xml'),
  38. ],
  39. 'attr_id': [
  40. (r'\b\D\w*', String, '#pop'),
  41. (r'[-]?((\.[0-9]+)|([0-9]+(\.[0-9]*)?))', Number, '#pop'),
  42. (r'"(\\"|[^"])*?"', String.Double, '#pop'),
  43. (r'<', Punctuation, ('#pop', 'xml')),
  44. ],
  45. 'xml': [
  46. (r'<', Punctuation, '#push'),
  47. (r'>', Punctuation, '#pop'),
  48. (r'\s+', Whitespace),
  49. (r'[^<>\s]', Name.Tag),
  50. ]
  51. }