File size: 7,167 Bytes
9c6594c |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
"""
pygments.lexers.typst
~~~~~~~~~~~~~~~~~~~~~
Lexers for Typst language.
:copyright: Copyright 2006-2025 by the Pygments team, see AUTHORS.
:license: BSD, see LICENSE for details.
"""
from pygments.lexer import RegexLexer, words, bygroups, include
from pygments.token import Comment, Keyword, Name, String, Punctuation, \
Whitespace, Generic, Operator, Number, Text
from pygments.util import get_choice_opt
__all__ = ['TypstLexer']
class TypstLexer(RegexLexer):
"""
For Typst code.
Additional options accepted:
`start`
Specifies the starting state of the lexer (one of 'markup', 'math',
'code'). The default is 'markup'.
"""
name = 'Typst'
aliases = ['typst']
filenames = ['*.typ']
mimetypes = ['text/x-typst']
url = 'https://typst.app'
version_added = '2.18'
MATH_SHORTHANDS = (
'[|', '|]', '||', '*', ':=', '::=', '...', '\'', '-', '=:', '!=', '>>',
'>=', '>>>', '<<', '<=', '<<<', '->', '|->', '=>', '|=>', '==>',
'-->', '~~>', '~>', '>->', '->>', '<-', '<==', '<--', '<~~', '<~',
'<-<','<<-','<->','<=>','<==>','<-->', '>', '<', '~', ':', '|'
)
tokens = {
'root': [
include('markup'),
],
# common cases going from math/markup into code mode
'into_code': [
(words(('#let', '#set', '#show'), suffix=r'\b'), Keyword.Declaration, 'inline_code'),
(words(('#import', '#include'), suffix=r'\b'), Keyword.Namespace, 'inline_code'),
(words(('#if', '#for', '#while', '#export'), suffix=r'\b'), Keyword.Reserved, 'inline_code'),
(r'#\{', Punctuation, 'code'),
(r'#\(', Punctuation, 'code'),
(r'(#[a-zA-Z_][a-zA-Z0-9_-]*)(\[)', bygroups(Name.Function, Punctuation), 'markup'),
(r'(#[a-zA-Z_][a-zA-Z0-9_-]*)(\()', bygroups(Name.Function, Punctuation), 'code'),
(words(('#true', '#false', '#none', '#auto'), suffix=r'\b'), Keyword.Constant),
(r'#[a-zA-Z_][a-zA-Z0-9_]*', Name.Variable),
(r'#0x[0-9a-fA-F]+', Number.Hex),
(r'#0b[01]+', Number.Bin),
(r'#0o[0-7]+', Number.Oct),
(r'#[0-9]+[\.e][0-9]+', Number.Float),
(r'#[0-9]+', Number.Integer),
],
'markup': [
include('comment'),
(r'^\s*=+.*$', Generic.Heading),
(r'[*][^*]*[*]', Generic.Strong),
(r'_[^_]*_', Generic.Emph),
(r'\$', Punctuation, 'math'),
(r'`[^`]*`', String.Backtick), # inline code
(r'^(\s*)(-)(\s+)', bygroups(Whitespace, Punctuation, Whitespace)), # unnumbered list
(r'^(\s*)(\+)(\s+)', bygroups(Whitespace, Punctuation, Whitespace)), # numbered list
(r'^(\s*)([0-9]+\.)', bygroups(Whitespace, Punctuation)), # numbered list variant
(r'^(\s*)(/)(\s+)([^:]+)(:)', bygroups(Whitespace, Punctuation, Whitespace, Name.Variable, Punctuation)), # definitions
(r'<[a-zA-Z_][a-zA-Z0-9_-]*>', Name.Label), # label
(r'@[a-zA-Z_][a-zA-Z0-9_-]*', Name.Label), # reference
(r'\\#', Text), # escaped
include('into_code'),
(r'```(?:.|\n)*?```', String.Backtick), # code block
(r'https?://[0-9a-zA-Z~/%#&=\',;.+?]*', Generic.Emph), # links
(words(('---', '\\', '~', '--', '...'), suffix=r'\B'), Punctuation), # special chars shorthand
(r'\\\[', Punctuation), # escaped
(r'\\\]', Punctuation), # escaped
(r'\[', Punctuation, '#push'),
(r'\]', Punctuation, '#pop'),
(r'[ \t]+\n?|\n', Whitespace),
(r'((?![*_$`<@\\#\] ]|https?://).)+', Text),
],
'math': [
include('comment'),
(words(('\\_', '\\^', '\\&')), Text), # escapes
(words(('_', '^', '&', ';')), Punctuation),
(words(('+', '/', '=') + MATH_SHORTHANDS), Operator),
(r'\\', Punctuation), # line break
(r'\\\$', Punctuation), # escaped
(r'\$', Punctuation, '#pop'), # end of math mode
include('into_code'),
(r'([a-zA-Z][a-zA-Z0-9-]*)(\s*)(\()', bygroups(Name.Function, Whitespace, Punctuation)),
(r'([a-zA-Z][a-zA-Z0-9-]*)(:)', bygroups(Name.Variable, Punctuation)), # named arguments in math functions
(r'([a-zA-Z][a-zA-Z0-9-]*)', Name.Variable), # both variables and symbols (_ isn't supported for variables)
(r'[0-9]+(\.[0-9]+)?', Number),
(r'\.{1,3}|\(|\)|,|\{|\}', Punctuation),
(r'"[^"]*"', String.Double),
(r'[ \t\n]+', Whitespace),
],
'comment': [
(r'//.*$', Comment.Single),
(r'/[*](.|\n)*?[*]/', Comment.Multiline),
],
'code': [
include('comment'),
(r'\[', Punctuation, 'markup'),
(r'\(|\{', Punctuation, 'code'),
(r'\)|\}', Punctuation, '#pop'),
(r'"[^"]*"', String.Double),
(r',|\.{1,2}', Punctuation),
(r'=', Operator),
(words(('and', 'or', 'not'), suffix=r'\b'), Operator.Word),
(r'=>|<=|==|!=|>|<|-=|\+=|\*=|/=|\+|-|\\|\*', Operator), # comparisons
(r'([a-zA-Z_][a-zA-Z0-9_-]*)(:)', bygroups(Name.Variable, Punctuation)),
(r'([a-zA-Z_][a-zA-Z0-9_-]*)(\()', bygroups(Name.Function, Punctuation), 'code'),
(words(('as', 'break', 'export', 'continue', 'else', 'for', 'if',
'in', 'return', 'while'), suffix=r'\b'),
Keyword.Reserved),
(words(('import', 'include'), suffix=r'\b'), Keyword.Namespace),
(words(('auto', 'none', 'true', 'false'), suffix=r'\b'), Keyword.Constant),
(r'([0-9.]+)(mm|pt|cm|in|em|fr|%)', bygroups(Number, Keyword.Reserved)),
(r'0x[0-9a-fA-F]+', Number.Hex),
(r'0b[01]+', Number.Bin),
(r'0o[0-7]+', Number.Oct),
(r'[0-9]+[\.e][0-9]+', Number.Float),
(r'[0-9]+', Number.Integer),
(words(('let', 'set', 'show'), suffix=r'\b'), Keyword.Declaration),
# FIXME: make this work
## (r'(import|include)( *)(")([^"])(")',
## bygroups(Keyword.Reserved, Text, Punctuation, String.Double, Punctuation)),
(r'([a-zA-Z_][a-zA-Z0-9_-]*)', Name.Variable),
(r'[ \t\n]+', Whitespace),
(r':', Punctuation), # from imports like "import a: b" or "show: text.with(..)"
],
'inline_code': [
(r';\b', Punctuation, '#pop'),
(r'\n', Whitespace, '#pop'),
include('code'),
],
}
def __init__(self, **options):
self.start_state = get_choice_opt(
options, 'start', ['markup', 'code', 'math'], 'markup', True)
RegexLexer.__init__(self, **options)
def get_tokens_unprocessed(self, text):
stack = ['root']
if self.start_state != 'markup': # markup is equivalent to root
stack.append(self.start_state)
yield from RegexLexer.get_tokens_unprocessed(self, text, stack)
|