regen/core/hook.rs
1//! Core logic for parser hooks
2use crate::core::ExtractFromLiteral;
3use crate::grammar::{pt, Ctx};
4
5/// Definition of a Parser Hook
6///
7/// After generating the Abstract Syntax Tree (AST), we still need to convert it to the internal objects.
8/// This is done by the Parse Tree (PT) and Parser Hooks.
9/// The PT uses the body of the rules to prelimilarily convert the AST to a simpler form
10/// and the hooks can be used to inject custom validation logic and types to the PT.
11/// The goal of the hooks is that the PT will produce the final object ready to be used by the application.
12/// Therefore there is no need to do another round of traversing.
13///
14/// The hooks are extern and need to be imported, therefore 3 things need to be defined
15/// - The module name as a string literal
16/// - The function name
17/// - The return type of the function (used to generate appropriate structs higher up in the PT)
18///
19/// The generated import statement depends on the target language:
20/// - Rust: `use {module}::{name};`
21/// - Python: `from {module} import {name}`
22/// - TypeScript: `import { {name} } from "{module}";`
23///
24/// The hooks are implemented as extern functions. The exact implementation depends on the target language.
25/// The inputs are:
26/// - The PT node that is being processed
27/// - The context object, which contains semantic information, errors, and optionally a custom context
28///
29/// The hook will wrap the PT node with a custom object returned by the hook
30#[derive(Debug, Clone)]
31pub struct Hook {
32 /// Function name
33 pub name: String,
34 /// Return type
35 pub return_type: String,
36}
37
38pub fn parse_hook(pt: &pt::HookAttribute, _: &mut Ctx) -> Option<Hook> {
39 Some(Hook {
40 name: pt.m_hook_name.strip_quotes(),
41 return_type: pt.m_hook_type.strip_quotes(),
42 })
43}