Skip to content
This repository was archived by the owner on Nov 5, 2024. It is now read-only.

Commit f7b267c

Browse files
committed
added docs; constified what's possible
1 parent f7df893 commit f7b267c

39 files changed

+303
-89
lines changed

.depend

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ target/debug/byte_ptr.o: src/byte_ptr.cpp src/byte_ptr.h
22

33
target/debug/bytes.o: src/bytes.cpp src/bytes.h src/byte_ptr.h
44

5-
target/debug/comment.o: src/comment.cpp src/comment.h src/comment_type.h
5+
target/debug/comment.o: src/comment.cpp src/comment.h src/comment_type.h \
6+
src/loc.h src/bytes.h src/byte_ptr.h src/input.h
67

78
target/debug/custom_decoder.o: src/custom_decoder.cpp \
89
src/custom_decoder.h src/bytes.h src/byte_ptr.h src/helpers.h
@@ -35,7 +36,17 @@ target/debug/low_level.o: src/low_level.cpp src/low_level.h \
3536
src/node.h
3637

3738
target/debug/magic_comment.o: src/magic_comment.cpp src/magic_comment.h \
38-
src/magic_comment_kind.h
39+
src/magic_comment_kind.h src/loc.h src/bytes.h src/byte_ptr.h \
40+
src/input.h
41+
42+
target/debug/message.o: src/message.cpp src/message.h \
43+
src/gen/messages/variant.h src/gen/messages/classes.h \
44+
src/gen/messages/../../bytes.h src/byte_ptr.h
45+
46+
target/debug/node.o: src/node.cpp src/node.h src/gen/nodes/variant.h \
47+
src/gen/nodes/classes.h src/gen/nodes/../../loc.h src/bytes.h \
48+
src/byte_ptr.h src/input.h src/gen/nodes/../../bytes.h \
49+
src/gen/nodes/../../node.h
3950

4051
target/debug/parser_options.o: src/parser_options.cpp \
4152
src/parser_options.h src/custom_decoder.h src/bytes.h src/byte_ptr.h \

README.md

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
# C++ bindings for `lib-ruby-parser`
22

3-
tldr; You can find examples in `test.c`. Valgrind and ASAN give no errors.
3+
tldr; You can find examples in `test/test.cpp`. Valgrind and ASAN give no errors.
44

55
## API
66

77
All classes/methods are defined in the `lib_ruby_parser` namespace.
88

9-
1. `ParserResult::from_source(std::string source)`
9+
1. `ParserResult::from_source(Bytes source, ParserOptions options)`
1010

1111
Parses given input into `ParserResult`, has the following fields:
1212
```cpp
@@ -26,7 +26,7 @@ All classes/methods are defined in the `lib_ruby_parser` namespace.
2626
std::vector<MagicComment> magic_comments;
2727

2828
// Decoded input
29-
std::string input;
29+
Input input;
3030
```
3131

3232
2. `Node::is<T>` where `T` is one of the ~100 node types.
@@ -61,10 +61,12 @@ All classes/methods are defined in the `lib_ruby_parser` namespace.
6161
6262
```cpp
6363
ErrorLevel level; // enum with WARNING and ERROR values
64-
std::string message;
64+
std::unique_ptr<DiagnosticMessage> message;
6565
std::unique_ptr<Loc> loc;
6666
```
6767
68+
can be rendered either using `render_message()` or `render(const Bytes &)`
69+
6870
7. `Comment` has the following fields:
6971
7072
```cpp
@@ -77,7 +79,7 @@ All classes/methods are defined in the `lib_ruby_parser` namespace.
7779
```cpp
7880
uint32_t begin;
7981
uint32_t end;
80-
std::string source(const std::string &input);
82+
std::string source(Input &input);
8183
```
8284
8385
`input` is what you get from `ParserResult::from`. It can be different from your original source if it has magic encoding comment.

lib-ruby-parser-cpp-bindings/build.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ fn build_bindings() {
6060
.whitelist_type("lib_ruby_parser::Node")
6161
.opaque_type("lib_ruby_parser::Node")
6262
.blacklist_function("lib_ruby_parser::Node::.*")
63+
.blacklist_function("lib_ruby_parser::Node_Node")
6364
// C++ class Loc
6465
.whitelist_type("lib_ruby_parser::Loc")
6566
.opaque_type("lib_ruby_parser::Loc")

lib-ruby-parser-cpp-bindings/gen/cpp/messages/render_cpp.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ namespace lib_ruby_parser {{
2929
3030
extern \"C\" {{
3131
32-
BytePtr render_message_diagnostic(Diagnostic *diagnostic)
32+
BytePtr render_message_diagnostic(const Diagnostic *diagnostic)
3333
{{
3434
return std::visit([diagnostic](auto &&message) {{
3535
using MessageT = std::decay_t<decltype(message)>;
@@ -38,7 +38,7 @@ BytePtr render_message_diagnostic(Diagnostic *diagnostic)
3838
}}, diagnostic->message->variant);
3939
}}
4040
41-
BytePtr render_diagnostic(Diagnostic *diagnostic, BytePtr input)
41+
BytePtr render_diagnostic(const Diagnostic *diagnostic, BytePtr input)
4242
{{
4343
return std::visit([diagnostic, input](auto &&message) {{
4444
using MessageT = std::decay_t<decltype(message)>;

lib-ruby-parser-cpp-bindings/gen/cpp/messages/render_h.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ extern \"C\" {{
3636
3737
{render_fns}
3838
39-
BytePtr render_message_diagnostic(Diagnostic *diagnostic);
40-
BytePtr render_diagnostic(Diagnostic *diagnostic, BytePtr input);
39+
BytePtr render_message_diagnostic(const Diagnostic *diagnostic);
40+
BytePtr render_diagnostic(const Diagnostic *diagnostic, BytePtr input);
4141
4242
}}
4343

src/byte_ptr.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,20 +32,20 @@ namespace lib_ruby_parser
3232
}
3333
}
3434

35-
std::string byte_ptr_to_owned_string(BytePtr byte_ptr)
35+
std::string byte_ptr_to_owned_string(const BytePtr &byte_ptr)
3636
{
3737
auto result = std::string(byte_ptr.ptr, byte_ptr.size);
3838
free_byte_ptr(byte_ptr);
3939
return result;
4040
}
4141

42-
std::string byte_ptr_to_utf8_lossy_string(BytePtr byte_ptr)
42+
std::string byte_ptr_to_utf8_lossy_string(const BytePtr &byte_ptr)
4343
{
4444
auto utf8_byte_ptr = byte_ptr_to_utf8_lossy_byte_ptr(byte_ptr);
4545
return byte_ptr_to_owned_string(utf8_byte_ptr);
4646
}
4747

48-
std::vector<char> byte_ptr_to_vec(BytePtr byte_ptr)
48+
std::vector<char> byte_ptr_to_vec(const BytePtr &byte_ptr)
4949
{
5050
auto vec = std::vector<char>();
5151
vec.reserve(byte_ptr.size);

src/byte_ptr.h

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,46 @@ namespace lib_ruby_parser
99
{
1010
extern "C"
1111
{
12+
// Low-level data structure that is used in Rust <-> C++ communications.
13+
// Represents a sequence of bytes.
1214
struct BytePtr
1315
{
1416
public:
17+
// Pointer to beginning of the byte sequence
1518
char *ptr;
19+
20+
// Size of the byte sequence
1621
uint32_t size;
1722
};
1823

24+
// Constructor of the BytePtr
1925
BytePtr make_byte_ptr(const char *ptr, uint32_t size);
26+
27+
// Destructor of the BytePtr
2028
void free_byte_ptr(BytePtr byte_ptr);
2129

22-
// Defined in Rust
30+
// Converts given `byte_ptr` into a UTF-8-valid BytePtr.
31+
// It doesn't modify or drop given `byte_ptr`.
2332
BytePtr byte_ptr_to_utf8_lossy_byte_ptr(BytePtr byte_ptr);
2433
}
2534

26-
std::string byte_ptr_to_owned_string(BytePtr byte_ptr);
27-
std::string byte_ptr_to_owned_string_lossy(BytePtr byte_ptr);
28-
std::vector<char> byte_ptr_to_vec(BytePtr byte_ptr);
35+
// Converts given BytePtr to a string.
36+
// This function doesn't care about UTF-8-incompatible byte sequences,
37+
// and so resulting string can be invalid in UTF-8.
38+
// It doesn't modify or drop given `byte_ptr`.
39+
std::string byte_ptr_to_owned_string(const BytePtr &byte_ptr);
40+
41+
// Converts given BytePtr to a string.
42+
// All UTF-8-incompatible byte sub-sequences in the given BytePtr
43+
// are substituted with a special REPLACEMENT char.
44+
// It doesn't modify or drop given `byte_ptr`.
45+
std::string byte_ptr_to_owned_string_lossy(const BytePtr &byte_ptr);
46+
47+
// Converts given BytePtr to a vector of bytes.
48+
// This function doesn't care about UTF-8-incompatible byte sequences,
49+
// and so resulting vector can be invalid in UTF-8.
50+
// It doesn't modify or drop given `byte_ptr`.
51+
std::vector<char> byte_ptr_to_vec(const BytePtr &byte_ptr);
2952
} // namespace lib_ruby_parser
3053

3154
#endif // LIB_RUBY_PARSER_BYTE_PTR_H

src/bytes.h

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,31 +8,65 @@
88

99
namespace lib_ruby_parser
1010
{
11+
// A sequence of Bytes, potentially invalid in UTF-8
1112
class Bytes
1213
{
14+
// Pointer to beginning of the byte sequence
1315
char *bytes_;
16+
17+
// Size of the byte sequence
1418
uint32_t size_;
19+
20+
// Indicates that Bytes are "borrowed",
21+
// when set to true disables cleanup in destructor.
22+
// TODO: remove this hack by making a cleanup on all call sites.
1523
bool borrowed = false;
1624

1725
public:
26+
// Destructor
1827
~Bytes();
1928

29+
// Constructs Bytes from a string (by copying)
2030
explicit Bytes(std::string s);
31+
32+
// Constructs Bytes from a given ptr and size (by taking ownership)
2133
explicit Bytes(char *ptr, uint32_t size);
34+
35+
// Constructs Bytes from a BytePtr (by taking ownership)
2236
explicit Bytes(BytePtr byte_ptr);
2337

2438
Bytes(Bytes &&);
2539
Bytes(const Bytes &) = delete;
2640
Bytes &operator=(Bytes &&other);
2741

42+
// Returns a BytePtr that shares data with initial Bytes
43+
// Deleting returned BytePtr will cause a segfault.
2844
BytePtr borrow_ptr() const;
45+
46+
// Consumes `this` and returns a BytePtr with own data.
2947
BytePtr into_ptr();
48+
49+
// Returns size of the byte sequence
3050
uint32_t size() const;
51+
52+
// Returns a copy of `this`
3153
Bytes clone() const;
54+
55+
// Returns a byte at the given index
3256
char at(uint32_t idx) const;
57+
58+
// Returns a sub-sequence at the given range
3359
Bytes range(uint32_t begin, uint32_t end) const;
60+
61+
// Returns a string that contains the same byte sequence (by copying)
3462
std::string to_string() const;
63+
64+
// Returns a UTF-8 valid string that contains the same byte sequence.
65+
// Invalid UTF-8 bytes are replaced with a special REPLACEMENT char.
3566
std::string to_string_lossy() const;
67+
68+
// Marks `this` as borrowed, so that destructor will not free the inner pointer.
69+
// TODO: remove this hack.
3670
void mark_borrowed();
3771

3872
bool operator==(const Bytes &other);

src/comment.cpp

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,8 @@
22

33
namespace lib_ruby_parser
44
{
5-
class Loc
6-
{
7-
public:
8-
bool operator==(const Loc &other);
9-
};
10-
5+
Comment::Comment(CommentType kind, std::unique_ptr<Loc> location) : kind(kind),
6+
location(std::move(location)) {}
117
bool Comment::operator==(const Comment &other)
128
{
139
return (kind == other.kind) && (*location == *(other.location));

src/comment.h

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,25 @@
33

44
#include <memory>
55
#include "comment_type.h"
6+
#include "loc.h"
67

78
namespace lib_ruby_parser
89
{
9-
class Loc;
10-
10+
// Representation of the comment in source code
1111
class Comment
1212
{
1313
public:
14+
// Kind of the comment
1415
CommentType kind;
16+
17+
// Location of the comment
1518
std::unique_ptr<Loc> location;
1619

1720
Comment() = delete;
1821
Comment(Comment &&) = default;
1922
Comment(const Comment &) = delete;
20-
explicit Comment(CommentType kind, std::unique_ptr<Loc> location) : kind(kind),
21-
location(std::move(location)){};
23+
24+
explicit Comment(CommentType kind, std::unique_ptr<Loc> location);
2225

2326
bool operator==(const Comment &other);
2427
bool operator!=(const Comment &other);

0 commit comments

Comments
 (0)