summaryrefslogtreecommitdiff
path: root/src/script/api/generate_widget.vbs
blob: 0bd38bb299ef3635dfc3484abfd6406da03f40ef (plain)
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
Option Explicit

' $Id$
'
' This file is part of OpenTTD.
' OpenTTD 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, version 2.
' OpenTTD 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 OpenTTD. If not, see <http://www.gnu.org/licenses/>.

Dim FSO, filename, skiptillend, eof
Set FSO = CreateObject("Scripting.FileSystemObject")

filename = "script_window.hpp"
skiptillend = False
eof = vbCrLf

If Not FSO.FileExists(filename) Then
	WScript.Echo filename & " not found"
	WScript.Quit 1
End If

Function GetFiles(pattern)
	Dim parent, re, files, f
	Set files = CreateObject("Scripting.Dictionary")
	Set re = New RegExp

	parent = FSO.GetParentFolderName(pattern)
	pattern = FSO.GetFileName(pattern)

	' Convert pattern to a valid regex
	re.Global = True
	re.Pattern = "\."
	pattern = re.Replace(pattern, "\.")
	re.Pattern = "\*"
	pattern = re.Replace(pattern, ".*")
	re.Pattern = pattern

	' Get the file list
	For Each f In FSO.GetFolder(parent).Files
		If re.Test(f.Path) Then
			f = parent & "/" & f.Name
			files.Add f, f
		End If
	Next

	' Sort the file list
	Set GetFiles = CreateObject("Scripting.Dictionary")
	While files.Count <> 0
		Dim first
		first = ""
		For Each f in files
			If first = "" Or StrComp(first, f) = 1 Then first = f
		Next
		GetFiles.Add first, first
		files.Remove(First)
	Wend
End Function

Sub Generate(line, file)
	Dim re, add_indent, enum_pattern, file_pattern, f, active, active_comment, comment, rm_indent
	Set re = New RegExp

	re.Global = True
	re.Pattern = "[^	]*"
	add_indent = re.Replace(line, "")
	re.Global = False
	re.Pattern = ".*@enum *"
	line = Split(re.Replace(line, ""))
	enum_pattern = line(0)
	file_pattern = line(1)
	For Each f In GetFiles(file_pattern).Items
		active = 0
		active_comment = 0
		comment = ""
		file.Write add_indent & "/* automatically generated from " & f & " */" & eof
		Set f = FSO.OpenTextFile(f, 1)
		While Not f.AtEndOfStream
			re.Pattern = rm_indent
			line = re.Replace(f.ReadLine, "")

			' Remember possible doxygen comment before enum declaration
			re.Pattern = "/\*\*"
			If active = 0 And re.Test(line) Then
				comment = add_indent & line
				active_comment = 1
			ElseIf active_comment = 1 Then
				comment = comment & vbCrLf & add_indent & line
			End If

			' Check for enum match
			re.Pattern = "^	*enum *" & enum_pattern & " *\{"
			If re.Test(line) Then
				re.Global = True
				re.Pattern = "[^	]*"
				rm_indent = re.Replace(line, "")
				re.Global = False
				active = 1
				If active_comment > 0 Then file.Write comment & eof
				active_comment = 0
				comment = ""
			End If

			' Forget doxygen comment, if no enum follows
			If active_comment = 2 And line <> "" Then
				active_comment = 0
				comment = ""
			End If
			re.Pattern = "\*/"
			If active_comment = 1 And re.Test(line) Then active_comment = 2

			If active <> 0 Then
				re.Pattern = "^	*[A-Za-z0-9_]* *[,=]"
				If re.Test(line) Then
					Dim parts
					' Transform enum values
					re.Pattern = " *=[^,]*"
					line = re.Replace(line, "")
					re.Pattern = " *//"
					line = re.Replace(line, " //")

					re.Pattern = "^(	*)([A-Za-z0-9_]+),(.*)"
					Set parts = re.Execute(line)

					With parts.Item(0).SubMatches
						If .Item(2) = "" Then
							file.Write add_indent & .Item(0) & .Item(1) & String(45 - Len(.Item(1)), " ") & "= ::" & .Item(1) & "," & eof
						Else
							file.Write add_indent & .Item(0) & .Item(1) & String(45 - Len(.Item(1)), " ") & "= ::" & .Item(1) & "," & String(44 - Len(.Item(1)), " ") & .Item(2) & eof
						End If
					End With
				ElseIf line = "" Then
					file.Write eof
				Else
					file.Write add_indent & line & eof
				End If
			End If

			re.Pattern = "^	*\};"
			If re.Test(line) Then
				If active <> 0 Then file.Write eof
				active = 0
			End If
			Wend
		f.Close
	Next
End Sub

Sub Parse(line, file)
	Dim re
	Set re = New RegExp

	re.pattern = "@enum"
	If re.Test(line) Then
		file.Write line & eof
		Generate line, file
		skiptillend = True
		Exit Sub
	End If

	re.pattern = "@endenum"
	If re.Test(line) Then
		file.Write line & eof
		skiptillend = False
		Exit Sub
	End If

	If Not skiptillend Then
		file.Write line & eof
	End If
End Sub

Dim file, source, lines, i

WScript.Echo "Starting to parse " & filename
Set file = FSO.OpenTextFile(filename, 1)
If Not file.AtEndOfStream Then
	source = file.ReadAll
End IF
file.Close

lines = Split(source, eof)
If UBound(lines) = 0 Then
	eof = vbLf
	lines = Split(source, eof)
End If

Set file = FSO.OpenTextFile(filename, 2)
For i = LBound(lines) To UBound(lines) - 1 ' Split adds an extra line, we must ignore it
	Parse lines(i), file
Next
file.Close
WScript.Echo "Done"