xref: /linux/tools/net/sunrpc/xdrgen/subcmds/definitions.py (revision 566ab427f827b0256d3e8ce0235d088e6a9c28bd)
1#!/usr/bin/env python3
2# ex: set filetype=python:
3
4"""Translate an XDR specification into executable code that
5can be compiled for the Linux kernel."""
6
7import logging
8
9from argparse import Namespace
10from lark import logger
11from lark.exceptions import UnexpectedInput
12
13from generators.constant import XdrConstantGenerator
14from generators.enum import XdrEnumGenerator
15from generators.header_bottom import XdrHeaderBottomGenerator
16from generators.header_top import XdrHeaderTopGenerator
17from generators.pointer import XdrPointerGenerator
18from generators.program import XdrProgramGenerator
19from generators.typedef import XdrTypedefGenerator
20from generators.struct import XdrStructGenerator
21from generators.union import XdrUnionGenerator
22
23from xdr_ast import transform_parse_tree, Specification
24from xdr_ast import _RpcProgram, _XdrConstant, _XdrEnum, _XdrPointer
25from xdr_ast import _XdrTypedef, _XdrStruct, _XdrUnion
26from xdr_parse import xdr_parser, set_xdr_annotate
27
28logger.setLevel(logging.INFO)
29
30
31def emit_header_definitions(
32    root: Specification, language: str, peer: str
33) -> None:
34    """Emit header definitions"""
35    for definition in root.definitions:
36        if isinstance(definition.value, _XdrConstant):
37            gen = XdrConstantGenerator(language, peer)
38        elif isinstance(definition.value, _XdrEnum):
39            gen = XdrEnumGenerator(language, peer)
40        elif isinstance(definition.value, _XdrPointer):
41            gen = XdrPointerGenerator(language, peer)
42        elif isinstance(definition.value, _RpcProgram):
43            gen = XdrProgramGenerator(language, peer)
44        elif isinstance(definition.value, _XdrTypedef):
45            gen = XdrTypedefGenerator(language, peer)
46        elif isinstance(definition.value, _XdrStruct):
47            gen = XdrStructGenerator(language, peer)
48        elif isinstance(definition.value, _XdrUnion):
49            gen = XdrUnionGenerator(language, peer)
50        else:
51            continue
52        gen.emit_definition(definition.value)
53
54
55def handle_parse_error(e: UnexpectedInput) -> bool:
56    """Simple parse error reporting, no recovery attempted"""
57    print(e)
58    return True
59
60
61def subcmd(args: Namespace) -> int:
62    """Generate definitions"""
63
64    set_xdr_annotate(args.annotate)
65    parser = xdr_parser()
66    with open(args.filename, encoding="utf-8") as f:
67        parse_tree = parser.parse(f.read(), on_error=handle_parse_error)
68        ast = transform_parse_tree(parse_tree)
69
70        gen = XdrHeaderTopGenerator(args.language, args.peer)
71        gen.emit_definition(args.filename, ast)
72
73        emit_header_definitions(ast, args.language, args.peer)
74
75        gen = XdrHeaderBottomGenerator(args.language, args.peer)
76        gen.emit_definition(args.filename, ast)
77
78    return 0
79