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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
|
# Program : concheck (tex & context syntax checker)
# Copyright : PRAGMA ADE / Hasselt NL / www.pragma-ade.com
# Author : Hans Hagen
# Version : 1.1 / 2003.08.18
# remarks:
#
# - the error messages are formatted like tex's messages so that scite can see them
# - begin and end tags are only tested on a per line basis because we assume clean sources
# - maybe i'll add begin{something} ... end{something} checking
# # example validation file
#
# begin interface en
#
# 1 text
# 4 Question
# 0 endinput
# 0 setupsomething
# 0 chapter
#
# end interface en
# nicer
# class Interface
# def initialize (language = 'unknown')
# @valid = Array.new
# @language = language
# end
# def register (left, right)
# @valid.push([left,right])
# end
# end
# $interfaces = Hash.new
# $interfaces['en'] = Interface.new('english')
# $interfaces['nl'] = Interface.new('dutch')
# $interfaces['en'].add('\\\\start','\\\\stop')
# $interfaces['en'].add('\\\\begin','\\\\end')
# $interfaces['en'].add('\\\\Start','\\\\Stop')
# $interfaces['en'].add('\\\\Begin','\\\\End')
# $interfaces['nl'].add('\\\\start','\\\\stop')
# $interfaces['nl'].add('\\\\beginvan','\\\\eindvan')
# $interfaces['nl'].add('\\\\Start','\\\\Stop')
# $interfaces['nl'].add('\\\\BeginVan','\\\\Eindvan')
# rest todo
$valid = Hash.new
$valid['en'] = Array.new
$valid['nl'] = Array.new
#$valid['en'].push(['',''])
$valid['en'].push(['\\\\start','\\\\stop'])
$valid['en'].push(['\\\\begin','\\\\end'])
$valid['en'].push(['\\\\Start','\\\\Stop'])
$valid['en'].push(['\\\\Begin','\\\\End'])
#$valid['nl'].push(['',''])
$valid['nl'].push(['\\\\start','\\\\stop'])
$valid['nl'].push(['\\\\beginvan','\\\\eindvan'])
$valid['nl'].push(['\\\\Start','\\\\Stop'])
$valid['nl'].push(['\\\\BeginVan','\\\\Eindvan'])
$valid_tex = "\\\\end\(input|insert|csname|linechar|graf|buffer|strut\)"
$valid_mp = "(enddef||end||endinput)"
$start_verbatim = Hash.new
$stop_verbatim = Hash.new
$start_verbatim['en'] = '\\\\starttyping'
$start_verbatim['nl'] = '\\\\starttypen'
$stop_verbatim['en'] = '\\\\stoptyping'
$stop_verbatim['nl'] = '\\\\stoptypen'
def message(str, filename=nil, line=nil, column=nil)
if filename then
if line then
if column then
puts("error in file #{filename} at line #{line} in column #{column}: #{str}\n")
else
puts("error in file #{filename} at line #{line}: #{str}\n")
end
else
puts("file #{filename}: #{str}\n")
end
else
puts(str+"\n")
end
end
def load_file (filename='')
begin
data = IO.readlines(filename)
data.collect! do |d|
if d =~ /^\s*%/o then
''
elsif d =~ /(.*?[^\\])%.*$/o then
$1
else
d
end
end
rescue
message("provide proper filename")
return nil
end
# print data.to_s + "\n"
return data
end
def guess_interface(data)
if data.first =~ /^%.*interface\=(.*)\s*/ then
return $1
else
data.each do |line|
case line
when /\\(starttekst|stoptekst|startonderdeel|startdocument|startoverzicht)/o then return 'nl'
when /\\(stelle|verwende|umgebung|benutze)/o then return 'de'
when /\\(stel|gebruik|omgeving)/ then return 'nl'
when /\\(use|setup|environment)/ then return 'en'
when /\\(usa|imposta|ambiente)/ then return 'it'
when /(height|width|style)=/ then return 'en'
when /(hoehe|breite|schrift)=/ then return 'de'
when /(hoogte|breedte|letter)=/ then return 'nl'
when /(altezza|ampiezza|stile)=/ then return 'it'
when /externfiguur/ then return 'nl'
when /externalfigure/ then return 'en'
when /externeabbildung/ then return 'de'
when /figuraesterna/ then return 'it'
end
end
return 'en'
end
end
def cleanup_data(data, interface='en')
verbatim = 0
re_start = /^\s*#{$start_verbatim[interface]}/
re_stop = /^\s*#{$stop_verbatim[interface]}/
data.collect! do |d|
if d =~ re_start then
verbatim += 1
if verbatim>1 then
''
else
d
end
elsif d =~ re_stop then
verbatim -= 1
if verbatim>0 then
''
else
d
end
elsif verbatim > 0 then
''
else
d
end
end
return data
end
def load_valid(data, interface=nil)
if data && (data.first =~ /^%.*valid\=(.*)\s*/)
filename = $1
filename = '../' + filename unless test(?f,filename)
filename = '../' + filename unless test(?f,filename)
if test(?f,filename) then
interface = guess_interface(data) unless interface
if $valid.has_key?(interface) then
interface = $valid[interface]
else
interface = $valid['en']
end
begin
message("loading validation file",filename)
validkeys = Hash.new
line = 1
IO.readlines(filename).each do |l|
if l =~ /\s+[\#\%]/io then
# ignore line
elsif l =~ /^\s*(begin|end)\s+interface\s+([a-z][a-z])/o then
# not yet supported
elsif l =~ /^\s*(\d+)\s+([a-zA-Z]*)$/o then
type, key = $1.to_i, $2.strip
if interface[type] then
validkeys[interface[type].first+key] = true
validkeys[interface[type].last+key] = true
else
error_message(filename,line,nil,'wrong definition')
end
end
line += 1
end
if validkeys then
message("#{validkeys.length} validation keys loaded",filename)
end
return validkeys
rescue
message("invalid validation file",filename)
end
else
message("unknown validation file", filename)
end
else
message("no extra validation file specified")
end
return nil
end
def some_chr_error(data, filename, left, right)
levels = Array.new
for line in 0..data.length-1 do
str = data[line]
# str = data[line].gsub(/\\[\#{left}\#{right}]/,'')
column = 0
while column<str.length do
case str[column].chr
when "\%" then
break
when "\\" then
column += 2
when left then
levels.push([line,column])
column += 1
when right then
if levels.pop
column += 1
else
message("missing #{left} for #{right}",filename,line+1,column+1)
return true
end
else
column += 1
end
end
end
if levels && levels.length>0 then
levels.each do |l|
column = l.pop
line = l.pop
message("missing #{right} for #{left}",filename,line+1,column+1)
end
return true
else
return false
end
end
def some_wrd_error(data, filename, start, stop, ignore)
levels = Array.new
len = 0
re_start = /[^\%]*(#{start})([a-zA-Z]*)/
re_stop = /[^\%]*(#{stop})([a-zA-Z]*)/
re_ignore = /#{ignore}.*/
str_start = start.gsub(/\\+/,'\\')
str_stop = stop.gsub(/\\+/,'\\')
line = 0
while line<data.length do
dataline = data[line].split(/[^\\A-Za-z]/)
if dataline.length>0 then
# todo: more on one line
dataline.each do |dataword|
case dataword
when re_ignore then
# just go on
when re_start then
levels.push([line,$2])
# print ' '*levels.length + '>' + $2 + "\n"
when re_stop then
# print ' '*levels.length + '<' + $2 + "\n"
if levels && levels.last && (levels.last[1] == $2) then
levels.pop
elsif levels && levels.last then
message("#{str_stop}#{levels.last[1]} expected instead of #{str_stop}#{$2}",filename,line+1)
return true
else
message("missing #{str_start}#{$2} for #{str_stop}#{$2}",filename,line+1)
return true
end
else
# just go on
end
end
end
line += 1
end
if levels && levels.length>0 then
levels.each do |l|
text = l.pop
line = l.pop
message("missing #{str_stop}#{text} for #{str_start}#{text}",filename,line+1)
end
return true
else
return false
end
end
def some_sym_error (data, filename, symbol,template=false)
saved = Array.new
inside = false
level = 0
for line in 0..data.length-1 do
str = data[line]
column = 0
while column<str.length do
case str[column].chr
when "[" then
level += 1 if template
when "]" then
level -= 1 if template && level > 0
when "\%" then
break
when "\\" then
column += 1
when symbol then
if level == 0 then
inside = ! inside
saved = [line,column]
else
# we're in some kind of template or so
end
else
# go on
end
column += 1
end
end
if inside && saved && level == 0 then
column = saved.pop
line = saved.pop
message("missing #{symbol} for #{symbol}",filename,line+1)
return true
else
return false
end
end
def some_key_error(data, filename, valid)
return if (! valid) || (valid.length == 0)
error = false
# data.foreach do |line| ... end
for line in 0..data.length-1 do
data[line].scan(/\\([a-zA-Z]+)/io) do
unless valid.has_key?($1) then
message("unknown command \\#{$1}",filename,line+1)
error = true
end
end
end
return error
end
# todo : language dependent
def check_file_tex (filename)
error = false
if data = load_file(filename) then
message("checking tex file", filename)
interface = guess_interface(data)
valid = load_valid(data,interface)
data = cleanup_data(data,interface)
# data.each do |d| print d end
$valid[interface].each do |v|
if some_wrd_error(data, filename, v[0], v[1] ,$valid_tex) then
error = true
break
end
end
# return false if some_wrd_error(data, filename, '\\\\start' , '\\\\stop' , $valid_tex)
# return false if some_wrd_error(data, filename, '\\\\Start' , '\\\\Stop' , $valid_tex)
# return false if some_wrd_error(data, filename, '\\\\beginvan', '\\\\eindvan', $valid_tex)
# return false if some_wrd_error(data, filename, '\\\\begin' , '\\\\end|\\\\eind', $valid_tex)
error = true if some_sym_error(data, filename, '$', false)
error = true if some_sym_error(data, filename, '|', true)
error = true if some_chr_error(data, filename, '{', '}')
error = true if some_chr_error(data, filename, '[', ']')
error = true if some_chr_error(data, filename, '(', ')')
error = true if some_key_error(data, filename, valid)
message("no errors in tex code", filename) unless error
return error
else
return false
end
end
def check_file_mp (filename)
error = false
if data = load_file(filename) then
message("checking metapost file", filename)
interface = guess_interface(data)
valid = load_valid(data,interface)
$valid[interface].each do |v|
if some_wrd_error(data, filename, v[0], v[1] ,$valid_tex) then
error = true
break
end
end
# return false if some_wrd_error(data, filename, '', 'begin', 'end', $valid_mp)
error = true if some_chr_error(data, filename, '{', '}')
error = true if some_chr_error(data, filename, '[', ']')
error = true if some_chr_error(data, filename, '(', ')')
error = true if some_key_error(data, filename, valid)
message("no errors in metapost code", filename) unless error
return error
else
return false
end
end
def check_file_text(filename='')
if data = load_file(filename) then
for line in 0..data.length-1 do
# case data[line]
# when /\s([\:\;\,\.\?\!])/ then
# message("space before #{$1}",filename,line+1)
# when /\D([\:\;\,\.\?\!])\S/ then
# message("no space after #{$1}",filename,line+1)
# end
if data[line] =~ /\s([\:\;\,\.\?\!])/ then
message("space before #{$1}",filename,line+1)
else
data[line].gsub!(/\[.*?\]/o, '')
data[line].gsub!(/\(.*?\)/o, '')
data[line].gsub!(/\[.*?$/o, '')
data[line].gsub!(/^.*?\]/o, '')
if data[line] =~ /\D([\:\;\,\.\?\!])\S/ then
message("no space after #{$1}",filename,line+1)
end
end
end
end
end
def check_file(filename='')
case filename
when '' then
message("provide filename")
return false
when /\.(tex|mk.+)$/i then
return check_file_tex(filename) # && check_file_text(filename)
when /\.mp$/i then
return check_file_mp(filename)
else
message("only tex and metapost files are checked")
return false
end
end
if ARGV.size > 0 then
someerror = false
ARGV.each do |filename|
somerror = true if check_file(filename)
end
exit (if someerror then 1 else 0 end)
else
exit 1
end
|