sgf.py 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. """
  2. pygments.lexers.sgf
  3. ~~~~~~~~~~~~~~~~~~~
  4. Lexer for Smart Game Format (sgf) file format.
  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 Name, Literal, String, Punctuation, Whitespace
  10. __all__ = ["SmartGameFormatLexer"]
  11. class SmartGameFormatLexer(RegexLexer):
  12. """
  13. Lexer for Smart Game Format (sgf) file format.
  14. The format is used to store game records of board games for two players
  15. (mainly Go game).
  16. .. versionadded:: 2.4
  17. """
  18. name = 'SmartGameFormat'
  19. url = 'https://www.red-bean.com/sgf/'
  20. aliases = ['sgf']
  21. filenames = ['*.sgf']
  22. tokens = {
  23. 'root': [
  24. (r'[():;]+', Punctuation),
  25. # tokens:
  26. (r'(A[BW]|AE|AN|AP|AR|AS|[BW]L|BM|[BW]R|[BW]S|[BW]T|CA|CH|CP|CR|'
  27. r'DD|DM|DO|DT|EL|EV|EX|FF|FG|G[BW]|GC|GM|GN|HA|HO|ID|IP|IT|IY|KM|'
  28. r'KO|LB|LN|LT|L|MA|MN|M|N|OB|OM|ON|OP|OT|OV|P[BW]|PC|PL|PM|RE|RG|'
  29. r'RO|RU|SO|SC|SE|SI|SL|SO|SQ|ST|SU|SZ|T[BW]|TC|TE|TM|TR|UC|US|VW|'
  30. r'V|[BW]|C)',
  31. Name.Builtin),
  32. # number:
  33. (r'(\[)([0-9.]+)(\])',
  34. bygroups(Punctuation, Literal.Number, Punctuation)),
  35. # date:
  36. (r'(\[)([0-9]{4}-[0-9]{2}-[0-9]{2})(\])',
  37. bygroups(Punctuation, Literal.Date, Punctuation)),
  38. # point:
  39. (r'(\[)([a-z]{2})(\])',
  40. bygroups(Punctuation, String, Punctuation)),
  41. # double points:
  42. (r'(\[)([a-z]{2})(:)([a-z]{2})(\])',
  43. bygroups(Punctuation, String, Punctuation, String, Punctuation)),
  44. (r'(\[)([\w\s#()+,\-.:?]+)(\])',
  45. bygroups(Punctuation, String, Punctuation)),
  46. (r'(\[)(\s.*)(\])',
  47. bygroups(Punctuation, Whitespace, Punctuation)),
  48. (r'\s+', Whitespace)
  49. ],
  50. }