File: pygments2chroma_xml.py

package info (click to toggle)
golang-github-alecthomas-chroma-v2 2.14.0-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental, sid, trixie
  • size: 9,472 kB
  • sloc: xml: 39,589; python: 422; javascript: 357; sh: 37; makefile: 36
file content (192 lines) | stat: -rwxr-xr-x 6,167 bytes parent folder | download | duplicates (2)
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#!/usr/bin/env python3
import functools
import importlib
import json
import os
import re
import sys
import types
import html

import pystache
from pygments import lexer as pygments_lexer
from pygments.token import _TokenType

TEMPLATE = r'''
<lexer>
  <config>
    <name>{{name}}</name>
    {{#aliases}}
    <alias>{{alias}}</alias>
    {{/aliases}}
    {{#filenames}}
    <filename>{{filename}}</filename>
    {{/filenames}}
    {{#mimetypes}}
    <mime_type>{{mimetype}}</mime_type>
    {{/mimetypes}}
    {{#re_ignorecase}}
    <case_insensitive>true</case_insensitive>
    {{/re_ignorecase}}
    {{#re_dotall}}
    <dot_all>true</dot_all>
    {{/re_dotall}}
    {{#re_not_multiline}}
    <not_multiline>true</not_multiline>
    {{/re_not_multiline}}
  </config>
  <rules>
    {{#tokens}}
    <state name="{{state}}">
      {{#rules}}
      {{{.}}}
      {{/rules}}
    </state>
    {{/tokens}}
  </rules>
</lexer>
'''


def xml_regex(s):
    return xml_string(s)

def xml_string(s):
    s = html.escape(s)
    return '"' + s + '"'


def to_camel_case(snake_str):
    components = snake_str.split('_')
    return ''.join(x.title() for x in components)


def warning(message):
    print('warning: ' + message, file=sys.stderr)


def resolve_emitter(emitter):
    if isinstance(emitter, types.FunctionType):
        if repr(emitter).startswith('<function bygroups.'):
            args = emitter.__closure__[0].cell_contents
            emitter = '<bygroups>%s</bygroups>' % ''.join(resolve_emitter(e) for e in args)
        elif repr(emitter).startswith('<function using.'):
            args = emitter.__closure__[0].cell_contents
            if isinstance(args, dict):
                state = 'root'
                if 'stack' in args:
                    state = args['stack'][1]
                    args.pop('stack')
                assert args == {}, args
                emitter = '<usingself state="%s"/>' % state
            elif issubclass(args, pygments_lexer.Lexer):
                name = args.__name__
                if name.endswith('Lexer'):
                    name = name[:-5]
                emitter = '<using lexer="%s"/>' % state
            else:
                raise ValueError('only support "using" with lexer classes, not %r' % args)
        else:
            warning('unsupported emitter function %r' % emitter)
            emitter = '?? %r ??' % emitter
    elif isinstance(emitter, _TokenType):
        emitter = '<token type="%s"/>' % str(emitter).replace('.', '')[5:]
    elif emitter is None:
        return 'None'
    else:
        raise ValueError('unsupported emitter type %r' % emitter)
    assert isinstance(emitter, str)
    return emitter


def process_state_action(action):
    if isinstance(action, tuple):
        return functools.reduce(lambda a, b: a + b, (process_state_action(a) for a in action))
    if action.startswith('#'):
        action = action[1:]
        if action== 'pop':
            action = '<pop depth="1"/>'
        elif action.startswith('pop:'):
            action = '<pop depth="%s"/>' % action[4:]
        elif action == 'push':
            action = '<push/>'
        elif action.startswith('push:'):
            action = '<push state="%s"/>' % action[5:]
        else:
            raise ValueError('unsupported action %r' % (action,))
    else:
        action = '<push state="%s"/>' % action
    return (action,)


def translate_rules(rules):
    out = []
    for rule in rules:
        if isinstance(rule, tuple):
            regex = rule[0]
            if isinstance(regex, str):
                regex = xml_regex(regex)
            elif isinstance(regex, pygments_lexer.words):
                regex = xml_string('%s(%s)%s' % (regex.prefix,
                                      '|'.join(w for w in regex.words),
                                      regex.suffix))
            else:
                raise ValueError('expected regex string but got %r' % regex)
            emitter = resolve_emitter(rule[1])
            if len(rule) == 2:
                modifier = ''
            elif type(rule[2]) is str:
                modifier = process_state_action(rule[2])[0]
            elif isinstance(rule[2], pygments_lexer.combined):
                modifier = '<combined state="%s"/>' % '" state="'.join(rule[2])
            elif type(rule[2]) is tuple:
                modifier = '<push state="%s"/>' % '" state="'.join(rule[2])
            else:
                raise ValueError('unsupported modifier %r' % (rule[2],))
            out.append('<rule pattern={}>{}{}</rule>'.format(regex, emitter, modifier))
        elif isinstance(rule, pygments_lexer.include):
            out.append('<rule><include state="{}"/></rule>'.format(rule))
        elif isinstance(rule, pygments_lexer.default):
            process_state_action(rule.state)
            out.append('<rule>{}</rule>'.format(''.join(process_state_action(rule.state))))
        else:
            raise ValueError('unsupported rule %r' % (rule,))
    return out


class TemplateView(object):
    def __init__(self, **kwargs):
        for key, value in kwargs.items():
            setattr(self, key, value)

    def re_not_multiline(self):
        return not (self.regex_flags & re.MULTILINE)

    def re_dotall(self):
        return self.regex_flags & re.DOTALL

    def re_ignorecase(self):
        return self.regex_flags & re.IGNORECASE


def main():
    package_name, symbol_name = sys.argv[1].rsplit(sep=".", maxsplit=1)

    package = importlib.import_module(package_name)

    lexer_cls = getattr(package, symbol_name)

    assert issubclass(lexer_cls, pygments_lexer.RegexLexer), 'can only translate from RegexLexer'

    print(pystache.render(TEMPLATE, TemplateView(
        name=lexer_cls.name,
        regex_flags=lexer_cls.flags,
        aliases=[{'alias': alias} for alias in lexer_cls.aliases],
        filenames=[{'filename': filename} for filename in lexer_cls.filenames],
        mimetypes=[{'mimetype': mimetype} for mimetype in lexer_cls.mimetypes],
        tokens=[{'state': state, 'rules': translate_rules(rules)} for (state, rules) in lexer_cls.get_tokendefs().items()],
    )))


if __name__ == '__main__':
    main()