codekingpro/portable-devtools
114k
1"""2 pygments.lexers.typst3 ~~~~~~~~~~~~~~~~~~~~~4 5 Lexers for Typst language.6 7 :copyright: Copyright 2006-2024 by the Pygments team, see AUTHORS.8 :license: BSD, see LICENSE for details.9"""10 11from pygments.lexer import RegexLexer, words, bygroups, include12from pygments.token import Comment, Keyword, Name, String, Punctuation, \13 Whitespace, Generic, Operator, Number, Text14 15__all__ = ['TypstLexer']16 17 18class TypstLexer(RegexLexer):19 """20 For Typst code.21 """22 23 name = 'Typst'24 aliases = ['typst']25 filenames = ['*.typ']26 mimetypes = ['text/x-typst']27 url = 'https://typst.app'28 version_added = '2.18'29 30 tokens = {31 'root': [32 include('markup'),33 ],34 'common': [35 (r'[ \t]+', Whitespace),36 (r'((?!=[*_$`\-+0-9/<@\\#\[]|https?://).)+', Text),37 ],38 'markup': [39 include('comment'),40 (r'^\s*=+.*$', Generic.Heading),41 (r'[*][^*]*[*]', Generic.Strong),42 (r'_[^_]*_', Generic.Emph),43 (r'\$', Punctuation, 'maths'),44 (r'`[^`]*`', String.Backtick), # inline code45 (r'^\s*-', Punctuation), # unnumbered list46 (r'^\s*\+', Punctuation), # numbered list47 (r'^\s*[0-9.]+', Punctuation), # numbered list variant48 (r'^(\s*/\s+)([^:]+)(:)', bygroups(Punctuation, Name.Variable, Punctuation)), # definitions49 (r'<[a-zA-Z_][a-zA-Z0-9_-]*>', Name.Label), # label50 (r'@[a-zA-Z_][a-zA-Z0-9_-]*', Name.Label), # reference51 (r'\\#', Text), # escaped52 (words(('#let', '#set', '#show'), suffix=r'\b'), Keyword.Declaration, 'inline_code'),53 (r'(#[a-zA-Z_][a-zA-Z0-9_]*)(\[)', bygroups(Name.Function, Punctuation), 'markup'),54 (r'(#[a-zA-Z_][a-zA-Z0-9_]*)(\()', bygroups(Name.Function, Punctuation), 'inline_code'),55 (r'#[a-zA-Z_][a-zA-Z0-9_]*', Name.Variable),56 (r'```(?:.|\n)*?```', String.Backtick), # code block57 (r'https?://[0-9a-zA-Z~/%#&=\',;.+?]*', Generic.Emph), # links58 (words((r'---', r'\\', r'~', r'--', r'...'), suffix=r'\b'), Punctuation), # special chars shorthand59 (r'\\\[', Punctuation), # escaped60 (r'\\\]', Punctuation), # escaped61 (r'\[', Punctuation, '#push'),62 (r'\]', Punctuation, '#pop'),63 include('common'),64 ],65 'maths': [66 include('comment'),67 (words(('_', '^', '+', '-', '/', '*', '->', '<-', '!=', '=='),68 suffix=r'\b'), Operator),69 (words((r'\\', '$='), suffix=r'\b'), Operator), # maths markup operators70 (r'\\\$', Punctuation), # escaped71 (r'\$', Punctuation, '#pop'), # end of math mode72 include('code'),73 ],74 'comment': [75 (r'//.*$', Comment.Single),76 (r'/[*](.|\n)*?[*]/', Comment.Multiline),77 ],78 'code': [79 include('comment'),80 (r'\[', Punctuation, 'markup'),81 (r'\(|\{', Punctuation, 'code'),82 (r'\)|\}', Punctuation, '#pop'),83 (r'"[^"]*"', String.Double),84 (r'[=,]', Operator),85 (words(('and', 'or', 'not'), suffix=r'\b'), Operator.Word),86 (r'=>|<=|==|!=|>|<|-=|\+=|\*=|/=|\+|-|\\|\*', Operator), # comparisons87 (r'([a-zA-Z_][a-zA-Z0-9_]*)(:)', bygroups(Name.Variable, Punctuation), '#push'),88 (r'([a-zA-Z_][a-zA-Z0-9_]*)(\()', bygroups(Name.Function, Punctuation), '#push'),89 (words(('as', 'break', 'export', 'continue', 'else', 'for', 'if',90 'import', 'in', 'include', 'return', 'while'), suffix=r'\b'),91 Keyword.Reserved),92 (words(('auto', 'none', 'true', 'false'), suffix=r'\b'), Keyword.Constant),93 (r'([0-9.]+)(mm|pt|cm|in|em|fr|%)', bygroups(Number, Keyword.Reserved)),94 (words(('let', 'set', 'show'), suffix=r'\b'), Keyword.Declaration),95 # FIXME: make this work96 ## (r'(import|include)( *)(")([^"])(")',97 ## bygroups(Keyword.Reserved, Text, Punctuation, String.Double, Punctuation)),98 include('common'),99 ],100 'inline_code': [101 (r';$', Punctuation, '#pop'),102 include('code'),103 ],104 }105 