typst.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. """
  2. pygments.lexers.typst
  3. ~~~~~~~~~~~~~~~~~~~~~
  4. Lexers for Typst language.
  5. :copyright: Copyright 2006-2024 by the Pygments team, see AUTHORS.
  6. :license: BSD, see LICENSE for details.
  7. """
  8. from pygments.lexer import RegexLexer, words, bygroups, include
  9. from pygments.token import Comment, Keyword, Name, String, Punctuation, \
  10. Whitespace, Generic, Operator, Number, Text
  11. __all__ = ['TypstLexer']
  12. class TypstLexer(RegexLexer):
  13. """
  14. For Typst code.
  15. """
  16. name = 'Typst'
  17. aliases = ['typst']
  18. filenames = ['*.typ']
  19. mimetypes = ['text/x-typst']
  20. url = 'https://typst.app'
  21. version_added = '2.18'
  22. tokens = {
  23. 'root': [
  24. include('markup'),
  25. ],
  26. 'common': [
  27. (r'[ \t]+', Whitespace),
  28. (r'((?!=[*_$`\-+0-9/<@\\#\[]|https?://).)+', Text),
  29. ],
  30. 'markup': [
  31. include('comment'),
  32. (r'^\s*=+.*$', Generic.Heading),
  33. (r'[*][^*]*[*]', Generic.Strong),
  34. (r'_[^_]*_', Generic.Emph),
  35. (r'\$', Punctuation, 'maths'),
  36. (r'`[^`]*`', String.Backtick), # inline code
  37. (r'^\s*-', Punctuation), # unnumbered list
  38. (r'^\s*\+', Punctuation), # numbered list
  39. (r'^\s*[0-9.]+', Punctuation), # numbered list variant
  40. (r'^(\s*/\s+)([^:]+)(:)', bygroups(Punctuation, Name.Variable, Punctuation)), # definitions
  41. (r'<[a-zA-Z_][a-zA-Z0-9_-]*>', Name.Label), # label
  42. (r'@[a-zA-Z_][a-zA-Z0-9_-]*', Name.Label), # reference
  43. (r'\\#', Text), # escaped
  44. (words(('#let', '#set', '#show'), suffix=r'\b'), Keyword.Declaration, 'inline_code'),
  45. (r'(#[a-zA-Z_][a-zA-Z0-9_]*)(\[)', bygroups(Name.Function, Punctuation), 'markup'),
  46. (r'(#[a-zA-Z_][a-zA-Z0-9_]*)(\()', bygroups(Name.Function, Punctuation), 'inline_code'),
  47. (r'#[a-zA-Z_][a-zA-Z0-9_]*', Name.Variable),
  48. (r'```(?:.|\n)*?```', String.Backtick), # code block
  49. (r'https?://[0-9a-zA-Z~/%#&=\',;.+?]*', Generic.Emph), # links
  50. (words((r'---', r'\\', r'~', r'--', r'...'), suffix=r'\b'), Punctuation), # special chars shorthand
  51. (r'\\\[', Punctuation), # escaped
  52. (r'\\\]', Punctuation), # escaped
  53. (r'\[', Punctuation, '#push'),
  54. (r'\]', Punctuation, '#pop'),
  55. include('common'),
  56. ],
  57. 'maths': [
  58. include('comment'),
  59. (words(('_', '^', '+', '-', '/', '*', '->', '<-', '!=', '=='),
  60. suffix=r'\b'), Operator),
  61. (words((r'\\', '$='), suffix=r'\b'), Operator), # maths markup operators
  62. (r'\\\$', Punctuation), # escaped
  63. (r'\$', Punctuation, '#pop'), # end of math mode
  64. include('code'),
  65. ],
  66. 'comment': [
  67. (r'//.*$', Comment.Single),
  68. (r'/[*](.|\n)*?[*]/', Comment.Multiline),
  69. ],
  70. 'code': [
  71. include('comment'),
  72. (r'\[', Punctuation, 'markup'),
  73. (r'\(|\{', Punctuation, 'code'),
  74. (r'\)|\}', Punctuation, '#pop'),
  75. (r'"[^"]*"', String.Double),
  76. (r'[=,]', Operator),
  77. (words(('and', 'or', 'not'), suffix=r'\b'), Operator.Word),
  78. (r'=>|<=|==|!=|>|<|-=|\+=|\*=|/=|\+|-|\\|\*', Operator), # comparisons
  79. (r'([a-zA-Z_][a-zA-Z0-9_]*)(:)', bygroups(Name.Variable, Punctuation), '#push'),
  80. (r'([a-zA-Z_][a-zA-Z0-9_]*)(\()', bygroups(Name.Function, Punctuation), '#push'),
  81. (words(('as', 'break', 'export', 'continue', 'else', 'for', 'if',
  82. 'import', 'in', 'include', 'return', 'while'), suffix=r'\b'),
  83. Keyword.Reserved),
  84. (words(('auto', 'none', 'true', 'false'), suffix=r'\b'), Keyword.Constant),
  85. (r'([0-9.]+)(mm|pt|cm|in|em|fr|%)', bygroups(Number, Keyword.Reserved)),
  86. (words(('let', 'set', 'show'), suffix=r'\b'), Keyword.Declaration),
  87. # FIXME: make this work
  88. ## (r'(import|include)( *)(")([^"])(")',
  89. ## bygroups(Keyword.Reserved, Text, Punctuation, String.Double, Punctuation)),
  90. include('common'),
  91. ],
  92. 'inline_code': [
  93. (r';$', Punctuation, '#pop'),
  94. include('code'),
  95. ],
  96. }