Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit f933abc

Browse files
committedAug 21, 2024·
first working cut of tree-parser
1 parent b689ab7 commit f933abc

File tree

9 files changed

+69
-53
lines changed

9 files changed

+69
-53
lines changed
 

‎DESCRIPTION

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Package: pkgsimil
22
Title: Similarity Metrics between R Packages
3-
Version: 0.0.1.008
3+
Version: 0.0.1.009
44
Authors@R: c(
55
person("Mark", "Padgham", , "mark.padgham@email.com", role = c("aut", "cre"),
66
comment = c(ORCID = "0000-0003-2172-5265")),
@@ -14,8 +14,6 @@ License: MIT + file LICENSE
1414
URL: https://docs.ropensci.org/pkgsimil/,
1515
https://github.com/ropensci-review-tools/pkgsimil
1616
BugReports: https://github.com/ropensci-review-tools/pkgsimil/issues
17-
LinkingTo:
18-
cpp11
1917
NeedsCompilation: yes
2018
Encoding: UTF-8
2119
Language: en-GB

‎R/cpp11.R

Lines changed: 0 additions & 5 deletions
This file was deleted.

‎R/treesim.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#' treesim function'
22
#' @export
33
treesim <- function () {
4-
cpp_parse_one_file ()
4+
.Call ("c_parse_one_file")
55
}

‎codemeta.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"codeRepository": "https://github.com/ropensci-review-tools/pkgstats",
99
"issueTracker": "https://github.com/ropensci-review-tools/pkgstats/issues",
1010
"license": "https://spdx.org/licenses/GPL-3.0",
11-
"version": "0.0.1.008",
11+
"version": "0.0.1.009",
1212
"programmingLanguage": {
1313
"@type": "ComputerLanguage",
1414
"name": "R",

‎src/Makevars

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ tree-sitter-files = tree-sitter/lib/src/lib.o
99
tree-sitter-r-files = tree-sitter-r/parser.o tree-sitter-r/scanner.o
1010

1111
lib-files = \
12-
cpp11.o \
1312
tree-parser-utils.o \
1413
tree-parser.o
1514

‎src/cpp11.cpp

Lines changed: 0 additions & 27 deletions
This file was deleted.

‎src/tree-parser.c

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
#include "tree-parser.h"
2+
3+
#include <R.h>
4+
#include <Rinternals.h>
5+
6+
SEXP c_parse_one_file() {
7+
const bool colour = false;
8+
9+
TSParser *parser = ts_parser_new();
10+
ts_parser_set_language(parser, tree_sitter_r());
11+
12+
char *source_code = loadfile("junk.R");
13+
14+
TSTree *tree = ts_parser_parse_string(
15+
parser,
16+
NULL,
17+
source_code,
18+
strlen(source_code));
19+
20+
TSNode root_node = ts_tree_root_node(tree);
21+
22+
// https://github.com/tree-sitter/py-tree-sitter/issues/33
23+
TSTreeCursor cursor = ts_tree_cursor_new(root_node);
24+
25+
bool reached_foot = false;
26+
int brackets[2] = {0, 0};
27+
printf("(");
28+
while (!reached_foot) {
29+
print_cursor(&cursor, source_code, colour, brackets);
30+
if (ts_tree_cursor_goto_first_child(&cursor)) continue;
31+
if (ts_tree_cursor_goto_next_sibling(&cursor)) continue;
32+
33+
bool retracing = true;
34+
while (retracing) {
35+
if (!ts_tree_cursor_goto_parent(&cursor)) {
36+
retracing = false;
37+
reached_foot = true;
38+
}
39+
if (ts_tree_cursor_goto_next_sibling(&cursor)) {
40+
// const char *field_name = ts_tree_cursor_current_field_name(&cursor);
41+
retracing = false;
42+
}
43+
}
44+
}
45+
printf(")\n\n");
46+
printf("Bracket counts: [%i, %i]\n", brackets[0], brackets[1]);
47+
48+
// char *string = ts_node_string(root_node);
49+
// printf("Syntax tree: %s\n", string);
50+
// free(string);
51+
52+
free(source_code);
53+
ts_tree_delete(tree);
54+
ts_parser_delete(parser);
55+
ts_tree_cursor_delete(&cursor);
56+
57+
SEXP result;
58+
PROTECT(result = allocVector(STRSXP, 1)); // Allocate space for one string
59+
SET_STRING_ELT(result, 0, mkChar("Hello from C!")); // Set the value of the string
60+
UNPROTECT(1); // Unprotect the allocated memory
61+
return result; // Return the SEXP (Symbolic EXPression) object
62+
}

‎src/tree-parser.cpp

Lines changed: 0 additions & 12 deletions
This file was deleted.

‎src/tree-parser.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
#pragma once
22

3-
#include <cpp11.hpp>
4-
53
#include "tree-parser-utils.h"
64

7-
std::string parse_one_file();
5+
#include <R.h>
6+
#include <Rinternals.h>
7+
8+
SEXP c_parse_one_file();

0 commit comments

Comments
 (0)
Please sign in to comment.