File: makelang.py

package info (click to toggle)
gwyddion 2.62-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 51,952 kB
  • sloc: ansic: 398,486; python: 7,877; sh: 5,492; makefile: 4,723; xml: 3,883; cpp: 1,969; pascal: 418; perl: 154; ruby: 130
file content (73 lines) | stat: -rwxr-xr-x 2,098 bytes parent folder | download | duplicates (4)
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
#!/usr/bin/python2
import sys, re

header = '''\
<?xml version="1.0" encoding="UTF-8"?>
<!--

 This is a ''' + 'GENERATED' + ''' file.

-->
<language id="pygwy" _name="Python + gwy" version="2.0" _section="Sources">
  <metadata>
    <property name="mimetypes">text/x-python;application/x-python</property>
    <property name="globs">*.py</property>
    <property name="line-comment-start">#</property>
  </metadata>

  <styles>
    <style id="class" _name="Gwy Class" map-to="python:builtin-object"/>
    <style id="function" _name="Gwy Function" map-to="python:builtin-function"/>
    <style id="enum-value" _name="Gwy Enum Value" map-to="python:builtin-constant"/>
  </styles>

  <definitions>
    <context id="pygwy-keywords">
      <include>
'''

footer = '''\
      </include>
    </context>

    <context id="pygwy" class="no-spell-check">
      <include>
        <context ref="pygwy-keywords"/>
        <context ref="python:python"/>
      </include>
    </context>
  </definitions>
</language>
'''

def wrap(symbols, cid, styleref):
    symbols = '\n'.join('          <keyword>' + x + '</keyword>'
                        for x in symbols)
    intro = '<context id="%s" style-ref="%s">' % (cid, styleref)
    return ('        ' + intro + '\n'
            + symbols + '\n'
            '        </context>\n')

text = sys.stdin.read()

sys.stdout.write(header)

classes = []
for m in re.finditer(r'(?ms)^\(define-(?:object|enum|flags) (?P<name>\w+)$',
                     text):
    classes.append(m.group('name'))
sys.stdout.write(wrap(classes, 'classes', 'class'))

functions = []
for m in re.finditer(r'(?ms)^\(define-function (?P<name>\w+)$', text):
    functions.append(m.group('name'))
sys.stdout.write(wrap(functions, 'functions', 'function'))

constants = []
for m in re.finditer(r'(?ms)^\s+\(values(?P<values>.*?)^\s+\)$', text):
    for mm in re.finditer(r'(?ms)^\s+\'\("[^"]+" "GWY_(?P<name>\w+)"\)$',
                          m.group('values')):
        constants.append(mm.group('name'))
sys.stdout.write(wrap(constants, 'enum-values', 'enum-value'))

sys.stdout.write(footer)