summaryrefslogtreecommitdiff
path: root/tools/net/sunrpc/xdrgen/subcmds/source.py
diff options
context:
space:
mode:
authorChuck Lever <chuck.lever@oracle.com>2025-12-22 09:44:59 -0500
committerChuck Lever <chuck.lever@oracle.com>2026-01-26 10:10:58 -0500
commit9abb3549227e4fb70f0d8ba515bf7ddd249ad710 (patch)
treee7824270654e61cba1ff112df072a7bc50500edb /tools/net/sunrpc/xdrgen/subcmds/source.py
parenteb1f3b55ac6202a013daf14ed508066947cdafa8 (diff)
downloadlwn-9abb3549227e4fb70f0d8ba515bf7ddd249ad710.tar.gz
lwn-9abb3549227e4fb70f0d8ba515bf7ddd249ad710.zip
xdrgen: Improve parse error reporting
The current verbose Lark exception output makes it difficult to quickly identify and fix syntax errors in XDR specifications. Users must wade through hundreds of lines of cascading errors to find the root cause. Replace this with concise, compiler-style error messages showing file, line, column, the unexpected token, and the source line with a caret pointing to the error location. Before: Unexpected token Token('__ANON_1', '+1') at line 14, column 35. Expected one of: * SEMICOLON Previous tokens: [Token('__ANON_0', 'LM_MAXSTRLEN')] [hundreds more cascading errors...] After: file.x:14:35: parse error Unexpected number '+1' const LM_MAXNAMELEN = LM_MAXSTRLEN+1; ^ The error handler now raises XdrParseError on the first error, preventing cascading messages that obscure the root cause. Signed-off-by: Chuck Lever <chuck.lever@oracle.com>
Diffstat (limited to 'tools/net/sunrpc/xdrgen/subcmds/source.py')
-rw-r--r--tools/net/sunrpc/xdrgen/subcmds/source.py16
1 files changed, 8 insertions, 8 deletions
diff --git a/tools/net/sunrpc/xdrgen/subcmds/source.py b/tools/net/sunrpc/xdrgen/subcmds/source.py
index 2024954748f0..bc7d38802df3 100644
--- a/tools/net/sunrpc/xdrgen/subcmds/source.py
+++ b/tools/net/sunrpc/xdrgen/subcmds/source.py
@@ -8,7 +8,6 @@ import logging
from argparse import Namespace
from lark import logger
-from lark.exceptions import UnexpectedInput
from generators.source_top import XdrSourceTopGenerator
from generators.enum import XdrEnumGenerator
@@ -23,6 +22,7 @@ from xdr_ast import _XdrAst, _XdrEnum, _XdrPointer
from xdr_ast import _XdrStruct, _XdrTypedef, _XdrUnion
from xdr_parse import xdr_parser, set_xdr_annotate
+from xdr_parse import make_error_handler, XdrParseError
logger.setLevel(logging.INFO)
@@ -92,19 +92,19 @@ def generate_client_source(filename: str, root: Specification, language: str) ->
# cel: todo: client needs PROC macros
-def handle_parse_error(e: UnexpectedInput) -> bool:
- """Simple parse error reporting, no recovery attempted"""
- print(e)
- return True
-
-
def subcmd(args: Namespace) -> int:
"""Generate encoder and decoder functions"""
set_xdr_annotate(args.annotate)
parser = xdr_parser()
with open(args.filename, encoding="utf-8") as f:
- parse_tree = parser.parse(f.read(), on_error=handle_parse_error)
+ source = f.read()
+ try:
+ parse_tree = parser.parse(
+ source, on_error=make_error_handler(source, args.filename)
+ )
+ except XdrParseError:
+ return 1
ast = transform_parse_tree(parse_tree)
match args.peer:
case "server":