This is an automated email from the ASF dual-hosted git repository.

mlibbey pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/trafficserver.git


The following commit(s) were added to refs/heads/master by this push:
     new 57d73b7397 hrw4u -- accept text from stdin (#12309)
57d73b7397 is described below

commit 57d73b739706a3fcf2ee93c8bcf43b81fbd10baf
Author: mlibbey <[email protected]>
AuthorDate: Tue Jul 8 13:13:50 2025 -0700

    hrw4u -- accept text from stdin (#12309)
    
    Sometimes it it nice to have hrw4u accept input from stdin instead
    of a file
---
 tools/hrw4u/scripts/hrw4u | 14 ++++++++------
 tools/hrw4u/src/errors.py |  4 ++--
 2 files changed, 10 insertions(+), 8 deletions(-)

diff --git a/tools/hrw4u/scripts/hrw4u b/tools/hrw4u/scripts/hrw4u
index 871bb07468..a2b5e42ad7 100755
--- a/tools/hrw4u/scripts/hrw4u
+++ b/tools/hrw4u/scripts/hrw4u
@@ -19,7 +19,7 @@
 import sys
 import argparse
 
-from antlr4 import FileStream, CommonTokenStream
+from antlr4 import InputStream, CommonTokenStream
 from antlr4.error.ErrorStrategy import BailErrorStrategy
 
 from hrw4u.hrw4uLexer import hrw4uLexer
@@ -38,7 +38,8 @@ def fatal(message: str):
 
 def main():
     parser_arg = argparse.ArgumentParser(description="Process HRW4U input and 
produce output (AST or HRW).")
-    parser_arg.add_argument("input_file", help="The input file to parse.")
+    parser_arg.add_argument(
+        "input_file", help="The input file to parse.", nargs='?', 
type=argparse.FileType('r', encoding='UTF-8'), default=sys.stdin)
 
     group = parser_arg.add_mutually_exclusive_group()
     group.add_argument("--ast", action="store_true", help="Produce the AST 
tree only from ANTLR.")
@@ -46,14 +47,15 @@ def main():
     parser_arg.add_argument("--debug", action="store_true", help="Enable debug 
output")
 
     args = parser_arg.parse_args()
+    input_content = args.input_file.read()
+
+    if args.input_file is not sys.stdin:
+        args.input_file.close()
 
     if not (args.ast or args.hrw):
         args.hrw = True
 
-    try:
-        input_stream = FileStream(args.input_file, encoding="utf-8")
-    except Exception as e:
-        fatal(f"{args.input_file}:0:0 - hrw4u error: cannot open file: {e}")
+    input_stream = InputStream(input_content)
 
     lexer = hrw4uLexer(input_stream)
     lexer.removeErrorListeners()
diff --git a/tools/hrw4u/src/errors.py b/tools/hrw4u/src/errors.py
index 63b32c124a..e9e8b7e84c 100644
--- a/tools/hrw4u/src/errors.py
+++ b/tools/hrw4u/src/errors.py
@@ -48,7 +48,7 @@ class Hrw4uSyntaxError(Exception):
         super().__init__(self._format_error(filename, line, column, message, 
source_line))
 
     def _format_error(self, filename, line, col, message, source_line):
-        error = f"{filename}:{line}:{col}: error: {message}"
+        error = f"{filename.name}:{line}:{col}: error: {message}"
 
         lineno = f"{line:4d}"
         code_line = f"{lineno} | {source_line}"
@@ -63,7 +63,7 @@ class SymbolResolutionError(Exception):
         super().__init__(message or f"Unrecognized symbol: '{name}'")
 
 
-# Main errror handling function, use this in the visitor and symbols
+# Main error handling function, use this in the visitor and symbols
 def hrw4u_error(filename, ctx, exc):
     if isinstance(exc, Hrw4uSyntaxError):
         return exc

Reply via email to