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 #84 from dennwc/pass_ctx
Browse files Browse the repository at this point in the history
Pass context to node interface
  • Loading branch information
juanjux authored Aug 8, 2018
2 parents c4d4ca0 + 507eabf commit 6c06178
Show file tree
Hide file tree
Showing 8 changed files with 135 additions and 150 deletions.
4 changes: 1 addition & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,7 @@ set(libsrc
set(libinclude
${CMAKE_SOURCE_DIR}/src/export.h
${CMAKE_SOURCE_DIR}/src/uast.h
${CMAKE_SOURCE_DIR}/src/roles.h
${CMAKE_SOURCE_DIR}/src/node_iface.h
${CMAKE_SOURCE_DIR}/src/nodes.h)
${CMAKE_SOURCE_DIR}/src/roles.h)

# Dependencies
set(libdep ${LIBXML2_LIBRARIES})
Expand Down
42 changes: 21 additions & 21 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(NodeHandle node) {
static const char *InternalType(const Uast* ctx, NodeHandle node) {
return ((Node *)node)->internal_type.data();
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Expand Down
45 changes: 0 additions & 45 deletions src/node_iface.h

This file was deleted.

18 changes: 0 additions & 18 deletions src/nodes.h

This file was deleted.

62 changes: 31 additions & 31 deletions src/uast.cc
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@ static xmlNodePtr CreateXmlNode(const Uast *ctx, NodeHandle node,

char buf[BUF_SIZE];

const char *internal_type = ctx->iface.InternalType(node);
const char *internal_type = ctx->iface.InternalType(ctx, node);
xmlNodePtr xmlNode = static_cast<xmlNodePtr>(xmlNewNode(nullptr, BAD_CAST(internal_type)));
int children_size = 0;
int roles_size = 0;
Expand All @@ -419,17 +419,17 @@ static xmlNodePtr CreateXmlNode(const Uast *ctx, NodeHandle node,
}

// Token
token = ctx->iface.Token(node);
token = ctx->iface.Token(ctx, node);
if (token) {
if (!xmlNewProp(xmlNode, BAD_CAST("token"), BAD_CAST(token))) {
throw CreateXMLNodeException();
}
}

// Roles
roles_size = ctx->iface.RolesSize(node);
roles_size = ctx->iface.RolesSize(ctx, node);
for (int i = 0; i < roles_size; i++) {
uint16_t role = ctx->iface.RoleAt(node, i);
uint16_t role = ctx->iface.RoleAt(ctx, node, i);
const char *role_name = RoleNameForId(role);
if (role_name != nullptr) {
if (!xmlNewProp(xmlNode, BAD_CAST(role_name), nullptr)) {
Expand All @@ -439,53 +439,53 @@ static xmlNodePtr CreateXmlNode(const Uast *ctx, NodeHandle node,
}

// Properties
for (size_t i = 0; i < ctx->iface.PropertiesSize(node); i++) {
const char *key = ctx->iface.PropertyKeyAt(node, i);
const char *value = ctx->iface.PropertyValueAt(node, i);
for (size_t i = 0; i < ctx->iface.PropertiesSize(ctx, node); i++) {
const char *key = ctx->iface.PropertyKeyAt(ctx, node, i);
const char *value = ctx->iface.PropertyValueAt(ctx, node, i);
if (!xmlNewProp(xmlNode, BAD_CAST(key), BAD_CAST(value))) {
throw CreateXMLNodeException();
}
}

// Position
if (ctx->iface.HasStartOffset(node)) {
int ret = snprintf(buf, BUF_SIZE, "%" PRIu32, ctx->iface.StartOffset(node));
if (ctx->iface.HasStartOffset(ctx, node)) {
int ret = snprintf(buf, BUF_SIZE, "%" PRIu32, ctx->iface.StartOffset(ctx, node));
if (ret < 0 || ret >= BUF_SIZE) {
throw CreateXMLNodeException("Unable to set start offset\n");
}
if (!xmlNewProp(xmlNode, BAD_CAST "startOffset", BAD_CAST buf)) {
throw CreateXMLNodeException();
}
}
if (ctx->iface.HasStartLine(node)) {
int ret = snprintf(buf, BUF_SIZE, "%" PRIu32, ctx->iface.StartLine(node));
if (ctx->iface.HasStartLine(ctx, node)) {
int ret = snprintf(buf, BUF_SIZE, "%" PRIu32, ctx->iface.StartLine(ctx, node));
if (ret < 0 || ret >= BUF_SIZE) {
throw CreateXMLNodeException("Unable to start line\n");
}
if (!xmlNewProp(xmlNode, BAD_CAST "startLine", BAD_CAST buf)) {
throw CreateXMLNodeException();
}
}
if (ctx->iface.HasStartCol(node)) {
int ret = snprintf(buf, BUF_SIZE, "%" PRIu32, ctx->iface.StartCol(node));
if (ctx->iface.HasStartCol(ctx, node)) {
int ret = snprintf(buf, BUF_SIZE, "%" PRIu32, ctx->iface.StartCol(ctx, node));
if (ret < 0 || ret >= BUF_SIZE) {
throw CreateXMLNodeException("Unable to start column\n");
}
if (!xmlNewProp(xmlNode, BAD_CAST "startCol", BAD_CAST buf)) {
throw CreateXMLNodeException();
}
}
if (ctx->iface.HasEndOffset(node)) {
int ret = snprintf(buf, BUF_SIZE, "%" PRIu32, ctx->iface.EndOffset(node));
if (ctx->iface.HasEndOffset(ctx, node)) {
int ret = snprintf(buf, BUF_SIZE, "%" PRIu32, ctx->iface.EndOffset(ctx, node));
if (ret < 0 || ret >= BUF_SIZE) {
throw CreateXMLNodeException("Unable to set end offset\n");
}
if (!xmlNewProp(xmlNode, BAD_CAST "endOffset", BAD_CAST buf)) {
throw CreateXMLNodeException();
}
}
if (ctx->iface.HasEndLine(node)) {
int ret = snprintf(buf, BUF_SIZE, "%" PRIu32, ctx->iface.EndLine(node));
if (ctx->iface.HasEndLine(ctx, node)) {
int ret = snprintf(buf, BUF_SIZE, "%" PRIu32, ctx->iface.EndLine(ctx, node));
if (ret < 0 || ret >= BUF_SIZE) {
Error(nullptr, "Unable to set end line\n");
throw CreateXMLNodeException();
Expand All @@ -494,8 +494,8 @@ static xmlNodePtr CreateXmlNode(const Uast *ctx, NodeHandle node,
throw CreateXMLNodeException();
}
}
if (ctx->iface.HasEndCol(node)) {
int ret = snprintf(buf, BUF_SIZE, "%" PRIu32, ctx->iface.EndCol(node));
if (ctx->iface.HasEndCol(ctx, node)) {
int ret = snprintf(buf, BUF_SIZE, "%" PRIu32, ctx->iface.EndCol(ctx, node));
if (ret < 0 || ret >= BUF_SIZE) {
throw CreateXMLNodeException("Unable to set end column\n");
}
Expand All @@ -505,9 +505,9 @@ static xmlNodePtr CreateXmlNode(const Uast *ctx, NodeHandle node,
}

// Recursivelly visit all children
children_size = ctx->iface.ChildrenSize(node);
children_size = ctx->iface.ChildrenSize(ctx, node);
for (int i = 0; i < children_size; i++) {
NodeHandle child = ctx->iface.ChildAt(node, i);
NodeHandle child = ctx->iface.ChildAt(ctx, node, i);
if (!CreateXmlNode(ctx, child, xmlNode)) {
throw CreateXMLNodeException();
}
Expand Down Expand Up @@ -549,7 +549,7 @@ static NodeHandle transformChildAt(UastIterator *iter, NodeHandle parent, size_t
assert(iter);
assert(parent);

auto child = iter->ctx->iface.ChildAt(parent, pos);
auto child = iter->ctx->iface.ChildAt(iter->ctx, parent, pos);
return iter->nodeTransform ? iter->nodeTransform(child): child;
}

Expand All @@ -560,7 +560,7 @@ static bool Visited(UastIterator *iter, NodeHandle node) {
const bool visited = iter->visited.find(node) != iter->visited.end();

if(!visited) {
int children_size = iter->ctx->iface.ChildrenSize(node);
int children_size = iter->ctx->iface.ChildrenSize(iter->ctx, node);
for (int i = children_size - 1; i >= 0; i--) {
iter->pending.push_front(transformChildAt(iter, node, i));
}
Expand All @@ -580,7 +580,7 @@ static NodeHandle PreOrderNext(UastIterator *iter) {
return 0;
}

int children_size = iter->ctx->iface.ChildrenSize(retNode);
int children_size = iter->ctx->iface.ChildrenSize(iter->ctx, retNode);
for (int i = children_size - 1; i >= 0; i--) {
iter->pending.push_front(transformChildAt(iter, retNode, i));
}
Expand All @@ -597,7 +597,7 @@ static NodeHandle LevelOrderNext(UastIterator *iter) {
return 0;
}

int children_size = iter->ctx->iface.ChildrenSize(retNode);
int children_size = iter->ctx->iface.ChildrenSize(iter->ctx, retNode);
for (int i = 0; i < children_size; i++) {
iter->pending.push_back(transformChildAt(iter, retNode, i));
}
Expand Down Expand Up @@ -636,15 +636,15 @@ static void sortPendingByPosition(UastIterator *iter) {

std::sort(iter->pending.begin(), iter->pending.end(), [&iter](NodeHandle i, NodeHandle j) {
auto ic = iter->ctx->iface;
if (ic.HasStartOffset(i) && ic.HasStartOffset(j)) {
return ic.StartOffset(i) < ic.StartOffset(j);
if (ic.HasStartOffset(iter->ctx, i) && ic.HasStartOffset(iter->ctx, j)) {
return ic.StartOffset(iter->ctx, i) < ic.StartOffset(iter->ctx, j);
}

// Continue: some didn't have offset, check by line/col
auto firstLine = ic.HasStartLine(i) ? ic.StartLine(i) : 0;
auto firstCol = ic.HasStartCol(i) ? ic.StartCol(i) : 0;
auto secondLine = ic.HasStartLine(j) ? ic.StartLine(j) : 0;
auto secondCol = ic.HasStartCol(j) ? ic.StartCol(j) : 0;
auto firstLine = ic.HasStartLine(iter->ctx, i) ? ic.StartLine(iter->ctx, i) : 0;
auto firstCol = ic.HasStartCol(iter->ctx, i) ? ic.StartCol(iter->ctx, i) : 0;
auto secondLine = ic.HasStartLine(iter->ctx, j) ? ic.StartLine(iter->ctx, j) : 0;
auto secondCol = ic.HasStartCol(iter->ctx, j) ? ic.StartCol(iter->ctx, j) : 0;

if (firstLine == secondLine) {
return firstCol < secondCol;
Expand Down
Loading

0 comments on commit 6c06178

Please sign in to comment.