Loading...
Searching...
No Matches
parse.h
/* SPDX-License-Identifier: BSD-3-Clause
* Copyright 2016, Olivier MATZ <zer0@droids-corp.org>
*/
#pragma once
#include <limits.h>
#include <stdbool.h>
#include <stdio.h>
#include <sys/queue.h>
#include <sys/types.h>
struct ec_node;
struct ec_pnode;
struct ec_pnode *ec_pnode(const struct ec_node *node);
void ec_pnode_free(struct ec_pnode *pnode);
void ec_pnode_free_children(struct ec_pnode *pnode);
struct ec_pnode *ec_pnode_dup(const struct ec_pnode *pnode);
const struct ec_strvec *ec_pnode_get_strvec(const struct ec_pnode *pnode);
struct ec_pnode *ec_parse(const struct ec_node *node, const char *str);
struct ec_pnode *ec_parse_strvec(const struct ec_node *node, const struct ec_strvec *strvec);
#define EC_PARSE_NOMATCH INT_MAX
int ec_parse_child(
const struct ec_node *node,
struct ec_pnode *pstate,
const struct ec_strvec *strvec
);
void ec_pnode_link_child(struct ec_pnode *pnode, struct ec_pnode *child);
void ec_pnode_unlink_child(struct ec_pnode *child);
#define EC_PNODE_GET_ROOT(parse) \
({ \
const struct ec_pnode *p_ = parse; /* check type */ \
struct ec_pnode *pnode_ = (struct ec_pnode *)parse; \
__typeof__(parse) res_; \
(void)p_; \
res_ = ec_pnode_get_root(pnode_); \
res_; \
})
struct ec_pnode *ec_pnode_get_root(struct ec_pnode *pnode);
struct ec_pnode *ec_pnode_get_parent(const struct ec_pnode *pnode);
struct ec_pnode *ec_pnode_get_first_child(const struct ec_pnode *pnode);
struct ec_pnode *ec_pnode_get_last_child(const struct ec_pnode *pnode);
struct ec_pnode *ec_pnode_next(const struct ec_pnode *pnode);
#define EC_PNODE_FOREACH_CHILD(child, pnode) \
for (child = ec_pnode_get_first_child(pnode); child != NULL; child = ec_pnode_next(child))
const struct ec_node *ec_pnode_get_node(const struct ec_pnode *pnode);
void ec_pnode_del_last_child(struct ec_pnode *pnode);
struct ec_dict *ec_pnode_get_attrs(const struct ec_pnode *pnode);
void ec_pnode_dump(FILE *out, const struct ec_pnode *pnode);
const struct ec_pnode *ec_pnode_find(const struct ec_pnode *root, const char *id);
const struct ec_pnode *ec_pnode_find_next(
const struct ec_pnode *root,
const struct ec_pnode *prev,
const char *id,
bool iter_children
);
unsigned int ec_pnode_count(const struct ec_pnode *root, const char *id);
struct ec_pnode *
__ec_pnode_iter_next(const struct ec_pnode *root, struct ec_pnode *pnode, bool iter_children);
#define EC_PNODE_ITER_NEXT(root, parse, iter_children) \
({ \
const struct ec_pnode *p_ = parse; /* check type */ \
struct ec_pnode *pnode_ = (struct ec_pnode *)parse; \
__typeof__(parse) res_; \
(void)p_; \
res_ = __ec_pnode_iter_next(root, pnode_, iter_children); \
res_; \
})
#define EC_PNODE_FOREACH(iter, root) \
for ((iter) = (root); (iter) != NULL; (iter) = EC_PNODE_ITER_NEXT((root), (iter), true))
size_t ec_pnode_len(const struct ec_pnode *pnode);
bool ec_pnode_matches(const struct ec_pnode *pnode);
ec_dict
struct ec_dict * ec_dict(void)
Create a hash table.
ec_node
struct ec_node * ec_node(const char *typename, const char *id)
Create a new node from its type name.
ec_parse_child
int ec_parse_child(const struct ec_node *node, struct ec_pnode *pstate, const struct ec_strvec *strvec)
Parse a string vector using a grammar tree, from a parent node.
ec_pnode_matches
bool ec_pnode_matches(const struct ec_pnode *pnode)
Check if the parsing tree matches the input.
ec_pnode_next
struct ec_pnode * ec_pnode_next(const struct ec_pnode *pnode)
Get the next sibling node.
ec_pnode_get_parent
struct ec_pnode * ec_pnode_get_parent(const struct ec_pnode *pnode)
Get the parent node in the parsing tree.
ec_pnode_get_attrs
struct ec_dict * ec_pnode_get_attrs(const struct ec_pnode *pnode)
Get attributes associated with a node in a parsing tree.
ec_parse
struct ec_pnode * ec_parse(const struct ec_node *node, const char *str)
Parse a string using a grammar tree.
ec_pnode_del_last_child
void ec_pnode_del_last_child(struct ec_pnode *pnode)
Unlink and free the last child.
ec_pnode_link_child
void ec_pnode_link_child(struct ec_pnode *pnode, struct ec_pnode *child)
Link a parsing node to a parsing tree.
ec_pnode_get_last_child
struct ec_pnode * ec_pnode_get_last_child(const struct ec_pnode *pnode)
Get the last child of a node in the parsing tree.
ec_parse_strvec
struct ec_pnode * ec_parse_strvec(const struct ec_node *node, const struct ec_strvec *strvec)
Parse a string vector using a grammar tree.
ec_pnode_dump
void ec_pnode_dump(FILE *out, const struct ec_pnode *pnode)
Dump a parsing tree.
ec_pnode_free
void ec_pnode_free(struct ec_pnode *pnode)
Free a parsing tree.
ec_pnode_get_strvec
const struct ec_strvec * ec_pnode_get_strvec(const struct ec_pnode *pnode)
Get the string vector associated with a parsing node.
ec_pnode_find
const struct ec_pnode * ec_pnode_find(const struct ec_pnode *root, const char *id)
Find a node from its identifier.
ec_pnode_get_root
struct ec_pnode * ec_pnode_get_root(struct ec_pnode *pnode)
Get the root of the parsing tree.
ec_pnode_free_children
void ec_pnode_free_children(struct ec_pnode *pnode)
Remove and free all the children of a parsing tree node.
ec_pnode_unlink_child
void ec_pnode_unlink_child(struct ec_pnode *child)
Remove a child node from parsing tree.
ec_pnode_get_first_child
struct ec_pnode * ec_pnode_get_first_child(const struct ec_pnode *pnode)
Get the first child of a node in the parsing tree.
ec_pnode_find_next
const struct ec_pnode * ec_pnode_find_next(const struct ec_pnode *root, const struct ec_pnode *prev, const char *id, bool iter_children)
Find the next node matching an identifier.
ec_pnode_len
size_t ec_pnode_len(const struct ec_pnode *pnode)
Get the number of strings in the parsed string vector.
ec_pnode_dup
struct ec_pnode * ec_pnode_dup(const struct ec_pnode *pnode)
Duplicate a parsing tree.
ec_pnode_get_node
const struct ec_node * ec_pnode_get_node(const struct ec_pnode *pnode)
Get the grammar node corresponding to the parsing node.
ec_pnode
struct ec_pnode * ec_pnode(const struct ec_node *node)
Create an empty parsing tree.
ec_pnode_count
unsigned int ec_pnode_count(const struct ec_pnode *root, const char *id)
Count node occurrences in a parse subtree.
ec_strvec
struct ec_strvec * ec_strvec(void)
Allocate a new empty string vector.