-
Notifications
You must be signed in to change notification settings - Fork 240
Expand file tree
/
Copy pathparser.h
More file actions
153 lines (130 loc) · 4.19 KB
/
parser.h
File metadata and controls
153 lines (130 loc) · 4.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#ifndef RBS__PARSER_H
#define RBS__PARSER_H
#include "rbs/defines.h"
#include "rbs/util/rbs_allocator.h"
#include "rbs/util/rbs_constant_pool.h"
#include "rbs/util/rbs_buffer.h"
#include "rbs/lexer.h"
#include "rbs/ast.h"
#include <stdbool.h>
#include <stddef.h>
/**
* comment represents a sequence of comment lines.
*
* # Comment for the method.
* #
* # ```rb
* # object.foo() # Do something
* # ```
* #
* def foo: () -> void
*
* A comment object represents the six lines of comments.
* */
typedef struct rbs_comment_t {
rbs_position_t start;
rbs_position_t end;
size_t line_tokens_count;
rbs_buffer_t /* of rbs_token_t */ line_tokens;
struct rbs_comment_t *next_comment;
} rbs_comment_t;
typedef struct rbs_error_t {
char *message;
rbs_token_t token;
bool syntax_error;
} rbs_error_t;
/**
* An RBS parser is a LL(3) parser.
* */
typedef struct {
rbs_lexer_t *rbs_lexer_t;
rbs_token_t current_token;
rbs_token_t next_token; /* The first lookahead token */
rbs_token_t next_token2; /* The second lookahead token */
rbs_token_t next_token3; /* The third lookahead token */
struct id_table *vars; /* Known type variables */
rbs_comment_t *last_comment; /* Last read comment */
rbs_constant_pool_t constant_pool;
rbs_allocator_t *allocator;
rbs_error_t *error;
} rbs_parser_t;
/**
* Insert new table entry.
* Setting `reset` inserts a _reset_ entry, which stops searching.
*
* ```
* class Foo[A]
* ^^^ <= push new table with reset
* def foo: [B] () -> [A, B]
* ^^^ <= push new table without reset
*
* class Baz[C]
* ^^^ <= push new table with reset
* end
* end
* ```
* */
void rbs_parser_push_typevar_table(rbs_parser_t *parser, bool reset);
/**
* Insert new type variable into the latest table.
* */
NODISCARD bool rbs_parser_insert_typevar(rbs_parser_t *parser, rbs_constant_id_t id);
/**
* Allocate new rbs_lexer_t object.
*
* ```
* VALUE string = rb_funcall(buffer, rb_intern("content"), 0);
* rbs_lexer_new(string, 0, 31) // New rbs_lexer_t with buffer content
* ```
* */
rbs_lexer_t *rbs_lexer_new(rbs_allocator_t *, rbs_string_t string, const rbs_encoding_t *encoding, int start_pos, int end_pos);
/**
* Allocate new rbs_parser_t object.
*
* ```
* rbs_parser_new(buffer, string, encoding, 0, 1);
* ```
* */
rbs_parser_t *rbs_parser_new(rbs_string_t string, const rbs_encoding_t *encoding, int start_pos, int end_pos);
void rbs_parser_free(rbs_parser_t *parser);
/**
* Advance one token.
* */
void rbs_parser_advance(rbs_parser_t *parser);
void rbs_parser_print(rbs_parser_t *parser);
/**
* Returns a RBS::Comment object associated with an subject at `subject_line`.
*
* ```rbs
* # Comment1
* class Foo # This is the subject line for Comment1
*
* # Comment2
* %a{annotation} # This is the subject line for Comment2
* def foo: () -> void
* end
* ```
* */
rbs_ast_comment_t *rbs_parser_get_comment(rbs_parser_t *parser, int subject_line);
void rbs_parser_set_error(rbs_parser_t *parser, rbs_token_t tok, bool syntax_error, const char *fmt, ...) RBS_ATTRIBUTE_FORMAT(4, 5);
bool rbs_parse_type(rbs_parser_t *parser, rbs_node_t **type, bool void_allowed, bool self_allowed);
bool rbs_parse_method_type(rbs_parser_t *parser, rbs_method_type_t **method_type);
bool rbs_parse_signature(rbs_parser_t *parser, rbs_signature_t **signature);
bool rbs_parse_type_params(rbs_parser_t *parser, bool module_type_params, rbs_node_list_t **params);
/**
* Parse an inline leading annotation from a string.
*
* @param parser The parser to use
* @param annotation Pointer to store the resulting annotation
* @return true if parsing succeeded, false otherwise
*/
bool rbs_parse_inline_leading_annotation(rbs_parser_t *parser, rbs_ast_ruby_annotations_t **annotation);
/**
* Parse an inline trailing annotation from a string.
*
* @param parser The parser to use
* @param annotation Pointer to store the resulting annotation
* @return true if parsing succeeded, false otherwise
*/
bool rbs_parse_inline_trailing_annotation(rbs_parser_t *parser, rbs_ast_ruby_annotations_t **annotation);
#endif