-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathmakeschemedata.py
More file actions
152 lines (127 loc) · 5 KB
/
makeschemedata.py
File metadata and controls
152 lines (127 loc) · 5 KB
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
#!/usr/bin/env python
# This file is part of python-ly, https://pypi.python.org/pypi/python-ly
#
# Copyright (c) 2008 - 2015 by Wilbert Berendsen
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 3
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
# See http://www.gnu.org/licenses/ for more information.
"""
generate all scheme words in the _scheme_data.py file
"""
from __future__ import unicode_literals
import os
import re
try:
from urllib.request import urlopen
except:
from urllib import urlopen
VERSION = "2.24"
GUILE_URL = "https://www.gnu.org/software/guile/docs/docs-2.2/guile-ref/Procedure-Index.html#Procedure-Index"
LY_FUNCTION_URL = "http://lilypond.org/doc/v2.24/Documentation/internals/scheme-functions"
SCM_PATH = os.path.join(os.environ['HOME'], 'lilies/2.24.0/share/lilypond/2.24.0/scm/lily')
GUILE_IGNONED_TYPE = (
'Apply Traps',
'Common Trap Options',
'Continuations',
'Debug on Error',
'Display Backtrace',
'Encryption',
'Entry Traps',
'Exit Traps',
'Garbage Collection Functions',
'getopt-long Reference',
'Internationalization',
'Location Traps',
'Network Databases',
'Network Socket Address',
'Network Sockets and Communication',
'Procedure Traps',
'Signals',
'Threads',
'Trap Utilities',
'Source Traps',
'Step Traps',
'SRFI-37',
'Stepping and Continuing',
'System asyncs',
'System Identification',
'User asyncs',
'User Information',
'User level options interfaces',
)
GUILE_OTHER_WORDS = ['else', 'set!']
def writeList(file_, name, lst):
file_.write("{} = [\n".format(name))
for word in sorted(set(lst)):
file_.write(" '{0}',\n".format(word))
file_.write("]\n\n")
def replace(word):
word = word.replace('<', '<')
word = word.replace('>', '>')
word = word.replace('&', '&')
return word
guilePage = urlopen(GUILE_URL).read()
guilePat = re.compile(r"<code>([a-z\d\+\?\!\*&;/=:-]+)</code></a>:</td><td> </td><td valign=\"top\"><a href=\".+\">(.+)</a>")
lyFunctionPage = urlopen(LY_FUNCTION_URL).read()
lyFunctionPat = re.compile(r'<u>Function:</u> <b><code>(ly:.+)</code></b>')
defineFunc = re.compile(r'\((?:define\*?|define-safe)-public\s+\(([-><a-zA-Z:\?]+)[\s\)]')
defineMacro = re.compile(r'(?m)^\((?:defmacro\*?|define-syntax(?:-rule)?)-public\s+\(?([-><a-zA-Z:\?]+)[\s\)]')
defineVar = re.compile(r'\((?:define\*?|define-safe)-public\s+([-><a-zA-Z:\?]+)[\s\)]')
startWithLy = re.compile(r"(ly:[-><a-zA-Z:\?]+)[\s\)]")
schemeDataPath = os.path.join(os.path.split(__file__)[0], '_scheme_data.py')
def main():
with open(schemeDataPath, "w") as f:
f.write("#generated by makeschemedata.py\n\nversion=\"{}\"\n\n".format(VERSION))
guileWords = []
for m in guilePat.finditer(guilePage.decode('utf-8')):
if m.group(2) not in GUILE_IGNONED_TYPE:
proc = m.group(1)
if not proc.startswith('gds-'):
guileWords.append(replace(proc))
guileWords += GUILE_OTHER_WORDS
lyFunctions = []
lyVars = []
lyCons = []
for word in lyFunctionPat.finditer(lyFunctionPage.decode('utf-8')):
lyFunctions.append(replace(word.group(1)))
for filename in os.listdir(SCM_PATH):
path = os.path.join(SCM_PATH, filename)
with open(path) as inp:
text = inp.read()
for m in defineFunc.finditer(text):
lyFunctions.append(m.group(1))
if m.group(1) == "name":
print("defineFunc")
for m in defineMacro.finditer(text):
lyFunctions.append(m.group(1))
if m.group(1) == "name":
print("defineMacro")
for m in startWithLy.finditer(text):
lyFunctions.append(m.group(1))
for m in defineVar.finditer(text):
word = m.group(1)
if word.isupper():
lyCons.append(word)
else:
lyVars.append(word)
for word in lyFunctions:
if word in lyVars:
lyVars.remove(word)
writeList(f, 'scheme_keywords', guileWords)
writeList(f, 'scheme_functions', lyFunctions)
writeList(f, 'scheme_variables', lyVars)
writeList(f, 'scheme_constants', lyCons)
if __name__ == "__main__":
main()