graphviz.py 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. """
  2. pygments.lexers.graphviz
  3. ~~~~~~~~~~~~~~~~~~~~~~~~
  4. Lexer for the DOT language (graphviz).
  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.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. aliases = ['graphviz', 'dot']
  19. filenames = ['*.gv', '*.dot']
  20. mimetypes = ['text/x-graphviz', 'text/vnd.graphviz']
  21. tokens = {
  22. 'root': [
  23. (r'\s+', Whitespace),
  24. (r'(#|//).*?$', Comment.Single),
  25. (r'/(\\\n)?[*](.|\n)*?[*](\\\n)?/', Comment.Multiline),
  26. (r'(?i)(node|edge|graph|digraph|subgraph|strict)\b', Keyword),
  27. (r'--|->', Operator),
  28. (r'[{}[\]:;,]', Punctuation),
  29. (r'(\b\D\w*)(\s*)(=)(\s*)',
  30. bygroups(Name.Attribute, Whitespace, Punctuation, Whitespace),
  31. 'attr_id'),
  32. (r'\b(n|ne|e|se|s|sw|w|nw|c|_)\b', Name.Builtin),
  33. (r'\b\D\w*', Name.Tag), # node
  34. (r'[-]?((\.[0-9]+)|([0-9]+(\.[0-9]*)?))', Number),
  35. (r'"(\\"|[^"])*?"', Name.Tag), # quoted node
  36. (r'<', Punctuation, 'xml'),
  37. ],
  38. 'attr_id': [
  39. (r'\b\D\w*', String, '#pop'),
  40. (r'[-]?((\.[0-9]+)|([0-9]+(\.[0-9]*)?))', Number, '#pop'),
  41. (r'"(\\"|[^"])*?"', String.Double, '#pop'),
  42. (r'<', Punctuation, ('#pop', 'xml')),
  43. ],
  44. 'xml': [
  45. (r'<', Punctuation, '#push'),
  46. (r'>', Punctuation, '#pop'),
  47. (r'\s+', Whitespace),
  48. (r'[^<>\s]', Name.Tag),
  49. ]
  50. }