Skip to content

Commit 2b6e661

Browse files
committed
Start better documenting C API
1 parent dfdcc98 commit 2b6e661

File tree

8 files changed

+339
-106
lines changed

8 files changed

+339
-106
lines changed

Doxyfile

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Doxyfile 1.9.5
2+
3+
# This file describes the settings to be used by the documentation system
4+
# doxygen (www.doxygen.org) for a project.
5+
#
6+
# All text after a double hash (##) is considered a comment and is placed in
7+
# front of the TAG it is preceding.
8+
#
9+
# All text after a single hash (#) is considered a comment and will be ignored.
10+
# The format is:
11+
# TAG = value [value, ...]
12+
# For lists, items can also be appended using:
13+
# TAG += value [value, ...]
14+
# Values that contain spaces should be placed between quotes (\" \").
15+
#
16+
# Note:
17+
#
18+
# Use doxygen to compare the used configuration file with the template
19+
# configuration file:
20+
# doxygen -x [configFile]
21+
22+
# Non-default options
23+
PROJECT_NAME = "Prism"
24+
OUTPUT_DIRECTORY = doc
25+
JAVADOC_AUTOBRIEF = YES
26+
OPTIMIZE_OUTPUT_FOR_C = YES
27+
EXTRACT_ALL = YES
28+
INPUT = src src/enc src/util include include/prism include/prism/enc include/prism/util
29+
HTML_OUTPUT = c
30+
31+
# Default options we might want to edit in the future
32+
HTML_STYLESHEET =
33+
HTML_COLORSTYLE = AUTO_LIGHT
34+
GENERATE_LATEX = NO
35+
36+
# Default options we definitely want to edit in the future
37+
WARN_IF_UNDOCUMENTED = NO
38+
WARN_NO_PARAMDOC = NO
39+
WARN_AS_ERROR = NO

include/prism/diagnostic.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,11 @@
88
#include <stdlib.h>
99
#include <assert.h>
1010

11-
// This struct represents a diagnostic found during parsing.
11+
/**
12+
* This struct represents a diagnostic found during parsing.
13+
*
14+
* @extends pm_list_node_t
15+
*/
1216
typedef struct {
1317
pm_list_node_t node;
1418
const uint8_t *start;

include/prism/parser.h

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -243,16 +243,24 @@ typedef enum {
243243
PM_COMMENT___END__
244244
} pm_comment_type_t;
245245

246-
// This is a node in the linked list of comments that we've found while parsing.
246+
/**
247+
* This is a node in the linked list of comments that we've found while parsing.
248+
*
249+
* @extends pm_list_node_t
250+
*/
247251
typedef struct pm_comment {
248252
pm_list_node_t node;
249253
const uint8_t *start;
250254
const uint8_t *end;
251255
pm_comment_type_t type;
252256
} pm_comment_t;
253257

254-
// This is a node in the linked list of magic comments that we've found while
255-
// parsing.
258+
/**
259+
* This is a node in the linked list of magic comments that we've found while
260+
* parsing.
261+
*
262+
* @extends pm_list_node_t
263+
*/
256264
typedef struct {
257265
pm_list_node_t node;
258266
const uint8_t *key_start;

include/prism/util/pm_string.h

Lines changed: 58 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -19,44 +19,82 @@ typedef struct {
1919

2020
#define PM_EMPTY_STRING ((pm_string_t) { .type = PM_STRING_CONSTANT, .source = NULL, .length = 0 })
2121

22-
// Initialize a shared string that is based on initial input.
22+
/**
23+
* Initialize a shared string that is based on initial input.
24+
*
25+
* @memberof pm_string_t
26+
*/
2327
void pm_string_shared_init(pm_string_t *string, const uint8_t *start, const uint8_t *end);
2428

25-
// Initialize an owned string that is responsible for freeing allocated memory.
29+
/**
30+
* Initialize an owned string that is responsible for freeing allocated memory.
31+
*
32+
* @memberof pm_string_t
33+
*/
2634
void pm_string_owned_init(pm_string_t *string, uint8_t *source, size_t length);
2735

28-
// Initialize a constant string that doesn't own its memory source.
36+
/**
37+
* Initialize a constant string that doesn't own its memory source.
38+
*
39+
* @memberof pm_string_t
40+
*/
2941
void pm_string_constant_init(pm_string_t *string, const char *source, size_t length);
3042

31-
// Read the file indicated by the filepath parameter into source and load its
32-
// contents and size into the given pm_string_t.
33-
// The given pm_string_t should be freed using pm_string_free() when it is no longer used.
34-
//
35-
// We want to use demand paging as much as possible in order to avoid having to
36-
// read the entire file into memory (which could be detrimental to performance
37-
// for large files). This means that if we're on windows we'll use
38-
// `MapViewOfFile`, on POSIX systems that have access to `mmap` we'll use
39-
// `mmap`, and on other POSIX systems we'll use `read`.
43+
/**
44+
* Read the file indicated by the filepath parameter into source and load its
45+
* contents and size into the given `pm_string_t`. The given `pm_string_t`
46+
* should be freed using `pm_string_free` when it is no longer used.
47+
*
48+
* We want to use demand paging as much as possible in order to avoid having to
49+
* read the entire file into memory (which could be detrimental to performance
50+
* for large files). This means that if we're on windows we'll use
51+
* `MapViewOfFile`, on POSIX systems that have access to `mmap` we'll use
52+
* `mmap`, and on other POSIX systems we'll use `read`.
53+
*
54+
* @memberof pm_string_t
55+
*/
4056
PRISM_EXPORTED_FUNCTION bool pm_string_mapped_init(pm_string_t *string, const char *filepath);
4157

42-
// Returns the memory size associated with the string.
58+
/**
59+
* Returns the memory size associated with the string.
60+
*
61+
* @memberof pm_string_t
62+
*/
4363
size_t pm_string_memsize(const pm_string_t *string);
4464

45-
// Ensure the string is owned. If it is not, then reinitialize it as owned and
46-
// copy over the previous source.
65+
/**
66+
* Ensure the string is owned. If it is not, then reinitialize it as owned and
67+
* copy over the previous source.
68+
*
69+
* @memberof pm_string_t
70+
*/
4771
void pm_string_ensure_owned(pm_string_t *string);
4872

49-
// Returns the length associated with the string.
73+
/**
74+
* Returns the length associated with the string.
75+
*
76+
* @memberof pm_string_t
77+
*/
5078
PRISM_EXPORTED_FUNCTION size_t pm_string_length(const pm_string_t *string);
5179

52-
// Returns the start pointer associated with the string.
80+
/**
81+
* Returns the start pointer associated with the string.
82+
*
83+
* @memberof pm_string_t
84+
*/
5385
PRISM_EXPORTED_FUNCTION const uint8_t * pm_string_source(const pm_string_t *string);
5486

55-
// Free the associated memory of the given string.
87+
/**
88+
* Free the associated memory of the given string.
89+
*
90+
* @memberof pm_string_t
91+
*/
5692
PRISM_EXPORTED_FUNCTION void pm_string_free(pm_string_t *string);
5793

58-
// Returns the size of the pm_string_t struct. This is necessary to allocate the
59-
// correct amount of memory in the FFI backend.
94+
/**
95+
* Returns the size of the pm_string_t struct. This is necessary to allocate the
96+
* correct amount of memory in the FFI backend.
97+
*/
6098
PRISM_EXPORTED_FUNCTION size_t pm_string_sizeof(void);
6199

62100
#endif // PRISM_STRING_H

rakelib/rdoc.rake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ require "rdoc/task"
55
RDoc::Task.new do |rdoc|
66
rdoc.main = "README.md"
77
rdoc.markup = "markdown"
8-
rdoc.rdoc_dir = "doc"
8+
rdoc.rdoc_dir = "doc/ruby"
99

1010
rdoc.options << "--all"
1111
rdoc.options << "--coverage-report"

src/prism.c

Lines changed: 102 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,104 @@
11
#include "prism.h"
22

3+
/**
4+
* @mainpage
5+
*
6+
* Prism is a parser for the Ruby programming language. It is designed to be
7+
* portable, error tolerant, and maintainable. It is written in C99 and has no
8+
* dependencies. It is currently being integrated into
9+
* [CRuby](https://github.com/ruby/ruby),
10+
* [JRuby](https://github.com/jruby/jruby),
11+
* [TruffleRuby](https://github.com/oracle/truffleruby),
12+
* [Sorbet](https://github.com/sorbet/sorbet), and
13+
* [Syntax Tree](https://github.com/ruby-syntax-tree/syntax_tree).
14+
*
15+
* @section getting-started Getting started
16+
*
17+
* If you're vendoring this project and compiling it statically then as long as
18+
* you have a C99 compiler you will be fine. If you're linking against it as
19+
* shared library, then you should compile with `-fvisibility=hidden` and
20+
* `-DPRISM_EXPORT_SYMBOLS` to tell prism to make only its public interface
21+
* visible.
22+
*
23+
* @section parsing Parsing
24+
*
25+
* In order to parse Ruby code, the structures and functions that you're going
26+
* to want to use and be aware of are:
27+
*
28+
* * @ref pm_parser_t - the main parser structure
29+
* * @ref pm_parser_init - initialize a parser
30+
* * @ref pm_parse - parse and return the root node
31+
* * @ref pm_node_destroy - deallocate the root node returned by `pm_parse`
32+
* * @ref pm_parser_free - free the internal memory of the parser
33+
*
34+
* Putting all of this together would look something like:
35+
*
36+
* ```c
37+
* void parse(const uint8_t *source, size_t length) {
38+
* pm_parser_t parser;
39+
* pm_parser_init(&parser, source, length, NULL);
40+
*
41+
* pm_node_t *root = pm_parse(&parser);
42+
* printf("PARSED!\n");
43+
*
44+
* pm_node_destroy(root);
45+
* pm_parser_free(&parser);
46+
* }
47+
* ```
48+
*
49+
* All of the nodes "inherit" from `pm_node_t` by embedding those structures as
50+
* their first member. This means you can downcast and upcast any node in the
51+
* tree to a `pm_node_t`.
52+
*
53+
* @section serializing Serializing
54+
*
55+
* Prism provides the ability to serialize the AST and its related metadata into
56+
* a binary format. This format is designed to be portable to different
57+
* languages and runtimes so that you only need to make one FFI call in order to
58+
* parse Ruby code. The structures and functions that you're going to want to
59+
* use and be aware of are:
60+
*
61+
* * @ref pm_buffer_t - a small buffer object that will hold the serialized AST
62+
* * @ref pm_buffer_free - free the memory associated with the buffer
63+
* * @ref pm_serialize - serialize the AST into a buffer
64+
* * @ref pm_parse_serialize - parse and serialize the AST into a buffer
65+
*
66+
* Putting all of this together would look something like:
67+
*
68+
* ```c
69+
* void serialize(const uint8_t *source, size_t length) {
70+
* pm_buffer_t buffer = { 0 };
71+
*
72+
* pm_parse_serialize(source, length, &buffer, NULL);
73+
* printf("SERIALIZED!\n");
74+
*
75+
* pm_buffer_free(&buffer);
76+
* }
77+
* ```
78+
*
79+
* @section inspecting Inspecting
80+
*
81+
* Prism provides the ability to inspect the AST by pretty-printing nodes. You
82+
* can do this with the `pm_prettyprint` function, which you would use like:
83+
*
84+
* ```c
85+
* void prettyprint(const uint8_t *source, size_t length) {
86+
* pm_parser_t parser;
87+
* pm_parser_init(&parser, source, length, NULL);
88+
*
89+
* pm_node_t *root = pm_parse(&parser);
90+
* pm_buffer_t buffer = { 0 };
91+
*
92+
* pm_prettyprint(&buffer, &parser, root);
93+
* printf("*.s%\n", (int) buffer.length, buffer.value);
94+
*
95+
* pm_buffer_free(&buffer);
96+
* pm_node_destroy(root);
97+
* pm_parser_free(&parser);
98+
* }
99+
* ```
100+
*/
101+
3102
// The prism version and the serialization format.
4103
const char *
5104
pm_version(void) {
@@ -15716,7 +15815,9 @@ pm_parser_free(pm_parser_t *parser) {
1571615815
}
1571715816
}
1571815817

15719-
// Parse the Ruby source associated with the given parser and return the tree.
15818+
/**
15819+
* Parse the Ruby source associated with the given parser and return the tree.
15820+
*/
1572015821
PRISM_EXPORTED_FUNCTION pm_node_t *
1572115822
pm_parse(pm_parser_t *parser) {
1572215823
return parse_program(parser);

0 commit comments

Comments
 (0)