1 # SPDX-License-Identifier: MIT
3 # Copyright The SCons Foundation
6 Phony lex for testing SCons.
8 Writes the contents of input file to stdout,
9 after "substituting" $LEXFLAGS and $I_ARGS
11 Needs to understand all the lex/flex options the testcases might use.
16 from pathlib
import Path
19 def make_side_effect(path
, text
):
21 if str(p
.parent
) != '.':
22 p
.parent
.mkdir(parents
=True, exist_ok
=True)
23 with p
.open(mode
="wb") as f
:
31 if sys
.platform
== 'win32':
32 longopts
= ['nounistd']
35 longopts
.extend(['header-file=', 'tables-file='])
36 cmd_opts
, args
= getopt
.getopt(sys
.argv
[1:], 'I:tx', longopts
)
40 for opt
, arg
in cmd_opts
:
42 i_arguments
= f
'{i_arguments} {arg}'
43 elif opt
== '--header-file':
45 opt_string
= f
'{opt_string} {opt}={arg}'
46 elif opt
== '--tables-file':
48 opt_string
= f
'{opt_string} {opt}={arg}'
50 opt_string
= f
'{opt_string} {opt}'
53 with
open(arg
, 'rb') as ifp
:
54 contents
= ifp
.read().decode(encoding
='utf-8')
55 contents
= contents
.replace('LEXFLAGS', opt_string
)
56 contents
= contents
.replace('LEX', 'mylex.py')
57 contents
= contents
.replace('I_ARGS', i_arguments
)
58 sys
.stdout
.write(contents
)
62 make_side_effect(make_header
, b
"lex header\n")
64 make_side_effect(make_table
, b
"lex table\n")
67 if __name__
== '__main__':