3 # utility to check meta errors for simple format spec mistakes.
5 # Copyright (C) 2016-2024 Free Software Foundation, Inc.
7 # This file is part of GNU Modula-2.
9 # GNU Modula-2 is free software; you can redistribute it and/or modify
10 # it under the terms of the GNU General Public License as published by
11 # the Free Software Foundation; either version 3, or (at your option)
14 # GNU Modula-2 is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU General Public License for more details.
19 # You should have received a copy of the GNU General Public License
20 # along with GNU Modula-2; see the file COPYING. If not, write to the
21 # Free Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
33 def visit_dir(directory
, ext
, func
):
34 # visit_dir - call func for each file below, dir, matching extension, ext.
35 list_of_files
= os
.listdir(directory
)
37 for filename
in list_of_files
:
38 path
= pathlib
.Path(filename
)
39 full
= os
.path
.join(directory
, filename
)
40 if path
.suffix
== ext
:
44 def check_format_spec(filename
, line
, no
):
47 percent
= line
.find('%')
50 for ch
in line
[percent
:]:
53 elif ch
in ['1', '2', '3', '4']:
55 sys
.stderr
.write('%s:%d: format specifier error, the symbol position digit must be before the specifier: %s\n' % (filename
, no
, line
))
61 def search_format(filename
, line
, no
):
64 colon
= line
.find(':', cbra
)
65 end
= line
.find('}', cbra
)
67 if (colon
>= 0) and (colon
< end
):
69 check_format_spec(filename
, line
[cbra
:end
], no
)
70 cbra
= line
.find('{', end
)
75 def check_string_quote (filename
, line
, no
, quote
):
76 end
= line
.find(quote
, 1)
78 search_format(filename
, line
[1:end
], no
)
81 def check_string (filename
, line
, no
):
82 quote
= line
.find("'")
84 check_string_quote(filename
, line
[quote
:], no
, "'")
85 quote
= line
.find('"')
87 check_string_quote(filename
, line
[quote
:], no
, '"')
90 def check_meta_spec (filename
):
91 lines
= open(filename
).readlines()
93 for no
, line
in enumerate(lines
):
96 check_string(filename
, line
, no
+1)
98 meta
= line
.find("Meta")
100 bra
= line
.find("(", meta
)
102 check_string(filename
, line
[bra
:], no
+1)
106 def handle_arguments():
107 # handle_arguments create and return the args object.
108 parser
= argparse
.ArgumentParser()
109 parser
.add_argument('-s', '--srcdir',
110 help='set source directory.',
111 default
='.', action
='store')
112 args
= parser
.parse_args()
117 args
= handle_arguments()
118 visit_dir(args
.srcdir
, '.mod', check_meta_spec
)
119 visit_dir(args
.srcdir
, '.bnf', check_meta_spec
)