Skip to content

Commit d9f71c6

Browse files
committed
Generate parser files with same newlines as come in
1 parent bc94b45 commit d9f71c6

File tree

4 files changed

+49
-7
lines changed

4 files changed

+49
-7
lines changed

graalpython/com.oracle.graal.python.frozen/freeze_modules.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -607,6 +607,7 @@ def write_frozen_module_file(file, modules):
607607
stat_result = os.stat(file)
608608
atime, mtime = stat_result.st_atime, stat_result.st_mtime
609609
else:
610+
linesep = os.linesep
610611
content = None
611612
os.makedirs(os.path.dirname(file), exist_ok=True)
612613
with open(file, "w", encoding="utf-8", newline=linesep) as out_file:

graalpython/com.oracle.graal.python.pegparser.generator/CMakeLists.txt

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,7 @@ add_custom_target(grammar ALL
3737
DEPENDS "${PARSER_OUTPUT}")
3838
add_custom_command(
3939
OUTPUT "${PARSER_OUTPUT}"
40-
COMMAND ${PYTHON_EXE} "${CMAKE_CURRENT_LIST_DIR}/main_parser_gen.py" "${GRAMMAR}" "${TOKENS}" "${PARSER_OUTPUT}"
41-
COMMAND ${CMAKE_COMMAND} -E copy_if_different
42-
${PARSER_OUTPUT}
43-
${PARSER_TARGET}
40+
COMMAND ${PYTHON_EXE} "${CMAKE_CURRENT_LIST_DIR}/main_parser_gen.py" "${GRAMMAR}" "${TOKENS}" "${PARSER_TARGET}"
4441
DEPENDS "${CMAKE_CURRENT_LIST_DIR}/main_parser_gen.py" "${GRAMMAR}" "${TOKENS}" "${PARSER_TARGET}" ${PEGEN_FILES} ${PEGJAVA_FILES})
4542

4643
add_custom_target(asdl ALL

graalpython/com.oracle.graal.python.pegparser.generator/asdl/java_file.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,24 @@ def define(self, line: str, *annotations: str):
131131
def create(out_dir_base: str, java_package: str, java_class_name: str):
132132
sst_dir = os.path.join(out_dir_base, *java_package.split('.'))
133133
os.makedirs(sst_dir, exist_ok=True)
134-
with open(os.path.join(sst_dir, java_class_name + '.java'), 'w') as f:
134+
filename = os.path.join(sst_dir, java_class_name + '.java')
135+
136+
# Determine which line separator to use
137+
if os.path.exists(filename):
138+
with open(filename, "r", encoding="utf-8", newline=os.linesep) as f:
139+
content = f.read()
140+
if os.linesep != "\n":
141+
if content.replace(os.linesep, "\n") == content:
142+
# Windows file has Unix line endings
143+
linesep = "\n"
144+
else:
145+
linesep = os.linesep
146+
else:
147+
linesep = "\n"
148+
else:
149+
linesep = os.linesep
150+
151+
with open(filename, 'w', encoding="utf-8", newline=linesep) as f:
135152
emitter = Emitter(f)
136153
f.write(HEADER)
137154
f.write(f'package {java_package};\n')

graalpython/com.oracle.graal.python.pegparser.generator/main_parser_gen.py

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#!/usr/bin/env python3
2-
# Copyright (c) 2021, 2022, Oracle and/or its affiliates. All rights reserved.
2+
# Copyright (c) 2021, 2025, Oracle and/or its affiliates. All rights reserved.
33
# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
#
55
# The Universal Permissive License (UPL), Version 1.0
@@ -69,10 +69,37 @@ def main():
6969
with open(args.tokens_file, "r") as tok_file:
7070
all_tokens, exact_tokens, non_exact_tokens = generate_token_definitions(tok_file)
7171

72-
with open(args.output_file, "w") as file:
72+
# Determine which line separator to use
73+
if os.path.exists(args.output_file):
74+
stat_result = os.stat(args.output_file)
75+
atime, mtime = stat_result.st_atime, stat_result.st_mtime
76+
with open(args.output_file, "r", encoding="utf-8", newline=os.linesep) as f:
77+
content = f.read()
78+
if os.linesep != "\n":
79+
unix_content = content.replace(os.linesep, "\n")
80+
if unix_content == content:
81+
# Windows file has Unix line endings
82+
linesep = "\n"
83+
content = unix_content
84+
else:
85+
linesep = os.linesep
86+
else:
87+
linesep = "\n"
88+
else:
89+
content = None
90+
linesep = os.linesep
91+
92+
with open(args.output_file, "w", encoding="utf-8", newline=linesep) as file:
7393
gen = JavaParserGenerator(grammar, all_tokens, exact_tokens, non_exact_tokens, file, debug=args.debug)
7494
gen.generate(os.path.basename(args.grammar_file))
7595

96+
with open(args.output_file, "r", encoding="utf-8", newline=linesep) as file:
97+
new_content = file.read()
98+
99+
if content == new_content:
100+
print(f"{args.output_file} not modified")
101+
os.utime(args.output_file, (atime, mtime))
102+
76103

77104
if __name__ == '__main__':
78105
main()

0 commit comments

Comments
 (0)