sgf.py 2.0 KB

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