Skip to content
This repository has been archived by the owner on Mar 8, 2020. It is now read-only.

Commit

Permalink
Merge pull request #83 from dennwc/node_handle
Browse files Browse the repository at this point in the history
Explicitly define node handle type
  • Loading branch information
juanjux authored Aug 8, 2018
2 parents b9fb8e3 + b68f3bf commit c4d4ca0
Show file tree
Hide file tree
Showing 8 changed files with 201 additions and 198 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,8 @@ Once the `Uast` context is created, it can be easily used to run xpath queries a
// creating a Uast as explained above
Uast *ctx = CreateContextFromIface();

// get a c pointer to the native node
void *node = (void*)pointerToNativeNode;
// get a handle or c pointer for the native node
NodeHandle node = (NodeHandle)pointerToNativeNode;

// consume the Uast API (xpath in this case)
Nodes *nodes = UastFilter(ctx, node, "//NumLiteral")
Expand All @@ -160,7 +160,7 @@ Nodes *nodes = UastFilter(ctx, node, query);
if (nodes) {
// iterate over the results and print the nodes
for (int i = 0; i < NodesSize(nodes); i++) {
void *node = NodeAt(nodes, i);
NodeHandle node = NodeAt(nodes, i);
print_node(node);
}
}
Expand All @@ -180,8 +180,8 @@ Example:
```c
UastIterator *iter = UastIteratorNew(ctx, node, PRE_ORDER);
void *curNode = NULL;
while((curNode = UastIteratorNext(iter)) != NULL) {
NodeHandle curNode = 0;
while((curNode = UastIteratorNext(iter)) != 0) {
// ... do something with the node
}
Expand Down
46 changes: 23 additions & 23 deletions examples/example.cc
Original file line number Diff line number Diff line change
Expand Up @@ -41,87 +41,87 @@ class Node {
void SetEndPosition(position p) { end_position = p; }
};

static const char *InternalType(const void *node) {
static const char *InternalType(NodeHandle node) {
return ((Node *)node)->internal_type.data();
}

static const char *Token(const void *node) {
static const char *Token(NodeHandle node) {
return ((Node *)node)->token.data();
}

static size_t ChildrenSize(const void *node) {
static size_t ChildrenSize(NodeHandle node) {
return ((Node *)node)->children.size();
}

static void *ChildAt(const void *node, int index) {
return ((Node *)node)->children.at(index);
static NodeHandle ChildAt(NodeHandle node, int index) {
return (NodeHandle)(((Node *)node)->children.at(index));
}

static size_t RolesSize(const void *node) {
static size_t RolesSize(NodeHandle node) {
return ((Node *)node)->roles.size();
}

static uint16_t RoleAt(const void *node, int index) {
static uint16_t RoleAt(NodeHandle node, int index) {
return ((Node *)node)->roles.at(index);
}

static size_t PropertiesSize(const void *node) {
static size_t PropertiesSize(NodeHandle node) {
return ((Node *)node)->properties.size();
}

static const char *PropertyKeyAt(const void *node, int index) {
static const char *PropertyKeyAt(NodeHandle node, int index) {
return std::get<0>(((Node *)node)->properties.at(index)).data();
}

static const char *PropertyValueAt(const void *node, int index) {
static const char *PropertyValueAt(NodeHandle node, int index) {
return std::get<1>(((Node *)node)->properties.at(index)).data();
}

static bool HasStartOffset(const void *node) {
static bool HasStartOffset(NodeHandle node) {
return ((Node *)node)->start_position != NO_POSITION;
}

static uint32_t StartOffset(const void *node) {
static uint32_t StartOffset(NodeHandle node) {
return ((Node *)node)->start_position.offset;
}

static bool HasStartLine(const void *node) {
static bool HasStartLine(NodeHandle node) {
return ((Node *)node)->start_position != NO_POSITION;
}

static uint32_t StartLine(const void *node) {
static uint32_t StartLine(NodeHandle node) {
return ((Node *)node)->start_position.line;
}

static bool HasStartCol(const void *node) {
static bool HasStartCol(NodeHandle node) {
return ((Node *)node)->start_position != NO_POSITION;
}

static uint32_t StartCol(const void *node) {
static uint32_t StartCol(NodeHandle node) {
return ((Node *)node)->start_position.col;
}

static bool HasEndOffset(const void *node) {
static bool HasEndOffset(NodeHandle node) {
return ((Node *)node)->end_position != NO_POSITION;
}

static uint32_t EndOffset(const void *node) {
static uint32_t EndOffset(NodeHandle node) {
return ((Node *)node)->end_position.offset;
}

static bool HasEndLine(const void *node) {
static bool HasEndLine(NodeHandle node) {
return ((Node *)node)->end_position != NO_POSITION;
}

static uint32_t EndLine(const void *node) {
static uint32_t EndLine(NodeHandle node) {
return ((Node *)node)->end_position.line;
}

static bool HasEndCol(const void *node) {
static bool HasEndCol(NodeHandle node) {
return ((Node *)node)->end_position != NO_POSITION;
}

static uint32_t EndCol(const void *node) {
static uint32_t EndCol(NodeHandle node) {
return ((Node *)node)->end_position.col;
}

Expand Down Expand Up @@ -179,7 +179,7 @@ int main() {
.EndCol = EndCol,
});

Nodes *nodes = UastFilter(ctx, &root, "/compilation_unit//identifier");
Nodes *nodes = UastFilter(ctx, NodeHandle(&root), "/compilation_unit//identifier");
if (!nodes) {
char *error = LastError();
std::cerr << "libuast.find() failed: " << error;
Expand Down
46 changes: 24 additions & 22 deletions src/node_iface.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,38 +5,40 @@
#include <stdint.h>
#include <stddef.h>

typedef uintptr_t NodeHandle;

// This interface must be implemented to create a Uast context.
typedef struct NodeIface {
const char *(*InternalType)(const void *);
const char *(*Token)(const void *);
const char *(*InternalType)(NodeHandle);
const char *(*Token)(NodeHandle);

// Children
size_t (*ChildrenSize)(const void *);
void *(*ChildAt)(const void *, int);
size_t (*ChildrenSize)(NodeHandle);
NodeHandle (*ChildAt)(NodeHandle, int);

// Roles
size_t (*RolesSize)(const void *);
uint16_t (*RoleAt)(const void *, int);
size_t (*RolesSize)(NodeHandle);
uint16_t (*RoleAt)(NodeHandle, int);

// Properties
size_t (*PropertiesSize)(const void *);
const char *(*PropertyKeyAt)(const void *, int);
const char *(*PropertyValueAt)(const void *, int);
size_t (*PropertiesSize)(NodeHandle);
const char *(*PropertyKeyAt)(NodeHandle, int);
const char *(*PropertyValueAt)(NodeHandle, int);

// Postion
bool (*HasStartOffset)(const void *);
uint32_t (*StartOffset)(const void *);
bool (*HasStartLine)(const void *);
uint32_t (*StartLine)(const void *);
bool (*HasStartCol)(const void *);
uint32_t (*StartCol)(const void *);

bool (*HasEndOffset)(const void *);
uint32_t (*EndOffset)(const void *);
bool (*HasEndLine)(const void *);
uint32_t (*EndLine)(const void *);
bool (*HasEndCol)(const void *);
uint32_t (*EndCol)(const void *);
bool (*HasStartOffset)(NodeHandle);
uint32_t (*StartOffset)(NodeHandle);
bool (*HasStartLine)(NodeHandle);
uint32_t (*StartLine)(NodeHandle);
bool (*HasStartCol)(NodeHandle);
uint32_t (*StartCol)(NodeHandle);

bool (*HasEndOffset)(NodeHandle);
uint32_t (*EndOffset)(NodeHandle);
bool (*HasEndLine)(NodeHandle);
uint32_t (*EndLine)(NodeHandle);
bool (*HasEndCol)(NodeHandle);
uint32_t (*EndCol)(NodeHandle);

} NodeIface;

Expand Down
3 changes: 2 additions & 1 deletion src/nodes.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@
#define LIBUAST_NODES_H_

#include "export.h"
#include "node_iface.h"

typedef struct Nodes Nodes;

// Returns the amount of nodes
EXPORT int NodesSize(const Nodes *nodes);

// Returns the node at the given index.
EXPORT void *NodeAt(const Nodes *nodes, int index);
EXPORT NodeHandle NodeAt(const Nodes *nodes, int index);

// Releases the resources associated with nodes
EXPORT void NodesFree(Nodes *nodes);
Expand Down
Loading

0 comments on commit c4d4ca0

Please sign in to comment.