Skip to content

Commit e114561

Browse files
committed
Fix spurious rebuilds of pegparser
1 parent 7f8f8bd commit e114561

File tree

3 files changed

+24
-27
lines changed

3 files changed

+24
-27
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ def main():
6565

6666
old_contents = stamp(args.sst_path, args.ast_path)
6767
generate(args.input_file, args.sst_path, args.ast_path)
68-
if old_contents != stamp(args.sst_path, args.ast_path):
68+
if old_contents != stamp(args.sst_path, args.ast_path) or not args.stamp.exists():
6969
args.stamp.touch()
7070
else:
7171
print(f"{args.sst_path} and {args.ast_path} not modified")

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

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
import os
4242
import sys
4343
import tokenize
44+
import io
4445

4546
from pegen.build import generate_token_definitions
4647
from pegen.grammar_parser import GeneratedParser as GrammarParser
@@ -71,34 +72,29 @@ def main():
7172

7273
# Determine which line separator to use
7374
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
75+
with open(args.output_file, "rb") as f:
76+
raw_old_content = f.read()
77+
if b'\r\n' in raw_old_content:
78+
linesep = '\r\n'
8479
else:
85-
linesep = os.linesep
86-
else:
87-
linesep = "\n"
80+
linesep = '\n'
81+
with open(args.output_file, "r", encoding="utf-8") as f:
82+
old_content = f.read()
8883
else:
89-
content = None
84+
old_content = None
9085
linesep = os.linesep
9186

92-
with open(args.output_file, "w", encoding="utf-8", newline=linesep) as file:
93-
gen = JavaParserGenerator(grammar, all_tokens, exact_tokens, non_exact_tokens, file, debug=args.debug)
94-
gen.generate(os.path.basename(args.grammar_file))
87+
out = io.StringIO()
88+
class_name = os.path.splitext(os.path.basename(args.output_file))[0]
89+
gen = JavaParserGenerator(grammar, all_tokens, exact_tokens, non_exact_tokens, out, class_name, debug=args.debug)
90+
gen.generate(os.path.basename(args.grammar_file))
9591

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:
92+
if out.getvalue() != old_content:
93+
print(f"Writing new {args.output_file}")
94+
with open(args.output_file, "w", encoding="utf-8", newline=linesep) as f:
95+
f.write(out.getvalue())
96+
else:
10097
print(f"{args.output_file} not modified")
101-
os.utime(args.output_file, (atime, mtime))
10298

10399

104100
if __name__ == '__main__':

graalpython/com.oracle.graal.python.pegparser.generator/pegjava/java_generator.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -874,10 +874,12 @@ def __init__(
874874
exact_tokens: Dict[str, int],
875875
non_exact_tokens: Set[str],
876876
file: Optional[IO[Text]],
877+
class_name: str,
877878
debug: bool = True,
878879
skip_actions: bool = False,
879880
):
880881
super().__init__(grammar, set(tokens.values()), file)
882+
self.class_name = class_name
881883
self.typingvisitor = TypingVisitor(self) # Java type hack
882884
self.callmakervisitor = JavaCallMakerVisitor(self, exact_tokens, non_exact_tokens, self.print)
883885
self.lookahead_functions: Dict[str, FunctionCall] = {}
@@ -930,10 +932,9 @@ def generate(self, filename: str) -> None:
930932
self.print(f"// Generated from {filename} by pegen")
931933
self.print("package com.oracle.graal.python.pegparser;")
932934
self.print(IMPORTS)
933-
className = os.path.splitext(os.path.basename(self.file.name))[0]
934935
self.print('@SuppressWarnings({"all", "cast"})')
935936
self.print('@SuppressFBWarnings')
936-
self.print("public final class %s extends AbstractParser {" % className)
937+
self.print("public final class %s extends AbstractParser {" % self.class_name)
937938
# Java needs a few fields declarations. Also, we're now in a class
938939
self.level += 1
939940
self.print()
@@ -945,12 +946,12 @@ def generate(self, filename: str) -> None:
945946
self.print(f"private static final int {rulename.upper()}_ID = {i};{comment}")
946947
self.print()
947948
# Java needs a constructor
948-
self.print("public %s(String source, SourceRange sourceRange, ParserCallbacks parserCb, InputType startRule, EnumSet<Flags> flags, int featureVersion) {" % className)
949+
self.print("public %s(String source, SourceRange sourceRange, ParserCallbacks parserCb, InputType startRule, EnumSet<Flags> flags, int featureVersion) {" % self.class_name)
949950
with self.indent():
950951
self.print("super(source, sourceRange, parserCb, startRule, flags, featureVersion);")
951952
self.print("}")
952953
self.print()
953-
self.print("public %s(String source, ParserCallbacks parserCb, InputType startRule, EnumSet<Flags> flags, int featureVersion) {" % className)
954+
self.print("public %s(String source, ParserCallbacks parserCb, InputType startRule, EnumSet<Flags> flags, int featureVersion) {" % self.class_name)
954955
with self.indent():
955956
self.print("super(source, null, parserCb, startRule, flags, featureVersion);")
956957
self.print("}")

0 commit comments

Comments
 (0)