Skip to content

Rename position, token, and rangetypes #30

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 10 additions & 12 deletions ext/rbs_extension/location.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,15 @@
rbs_loc_range RBS_LOC_NULL_RANGE = { -1, -1 };
VALUE RBS_Location;

position rbs_loc_position(int char_pos) {
position pos = { 0, char_pos, -1, -1 };
return pos;
rbs_position_t rbs_loc_position(int char_pos) {
return (rbs_position_t) { 0, char_pos, -1, -1 };
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could be calling rbs_loc_position3 while at it.

}

position rbs_loc_position3(int char_pos, int line, int column) {
position pos = { 0, char_pos, line, column };
return pos;
rbs_position_t rbs_loc_position3(int char_pos, int line, int column) {
return (rbs_position_t) { 0, char_pos, line, column };
}

static rbs_loc_range rbs_new_loc_range(range rg) {
static rbs_loc_range rbs_new_loc_range(rbs_range_t rg) {
rbs_loc_range r = { rg.start.char_pos, rg.end.char_pos };
return r;
}
Expand Down Expand Up @@ -57,7 +55,7 @@ static void check_children_cap(rbs_loc *loc) {
}
}

void rbs_loc_legacy_add_optional_child(rbs_loc *loc, rbs_constant_id_t name, range r) {
void rbs_loc_legacy_add_optional_child(rbs_loc *loc, rbs_constant_id_t name, rbs_range_t r) {
check_children_cap(loc);

unsigned short i = loc->children->len++;
Expand All @@ -67,7 +65,7 @@ void rbs_loc_legacy_add_optional_child(rbs_loc *loc, rbs_constant_id_t name, ran
};
}

void rbs_loc_legacy_add_required_child(rbs_loc *loc, rbs_constant_id_t name, range r) {
void rbs_loc_legacy_add_required_child(rbs_loc *loc, rbs_constant_id_t name, rbs_range_t r) {
rbs_loc_legacy_add_optional_child(loc, name, r);

unsigned short last_index = loc->children->len - 1;
Expand Down Expand Up @@ -181,7 +179,7 @@ static rbs_constant_id_t rbs_constant_pool_insert_ruby_symbol(VALUE symbol) {
static VALUE location_add_required_child(VALUE self, VALUE name, VALUE start, VALUE end) {
rbs_loc *loc = rbs_check_location(self);

range rg;
rbs_range_t rg;
rg.start = rbs_loc_position(FIX2INT(start));
rg.end = rbs_loc_position(FIX2INT(end));

Expand All @@ -193,7 +191,7 @@ static VALUE location_add_required_child(VALUE self, VALUE name, VALUE start, VA
static VALUE location_add_optional_child(VALUE self, VALUE name, VALUE start, VALUE end) {
rbs_loc *loc = rbs_check_location(self);

range rg;
rbs_range_t rg;
rg.start = rbs_loc_position(FIX2INT(start));
rg.end = rbs_loc_position(FIX2INT(end));

Expand All @@ -210,7 +208,7 @@ static VALUE location_add_optional_no_child(VALUE self, VALUE name) {
return Qnil;
}

VALUE rbs_new_location(VALUE buffer, range rg) {
VALUE rbs_new_location(VALUE buffer, rbs_range_t rg) {
rbs_loc *loc;
VALUE obj = TypedData_Make_Struct(RBS_Location, rbs_loc, &location_type, loc);

Expand Down
2 changes: 1 addition & 1 deletion ext/rbs_extension/location.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ typedef struct {
/**
* Returns new RBS::Location object, with given buffer and range.
* */
VALUE rbs_new_location(VALUE buffer, range rg);
VALUE rbs_new_location(VALUE buffer, rbs_range_t rg);

/**
* Return rbs_loc associated with the RBS::Location object.
Expand Down
10 changes: 5 additions & 5 deletions ext/rbs_extension/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ static NORETURN(void) raise_error(rbs_error_t *error, VALUE buffer) {
}

VALUE location = rbs_new_location(buffer, error->token.range);
VALUE type = rb_str_new_cstr(token_type_str(error->token.type));
VALUE type = rb_str_new_cstr(rbs_token_type_str(error->token.type));

VALUE rb_error = rb_funcall(
RBS_ParsingError,
Expand Down Expand Up @@ -112,7 +112,7 @@ static VALUE parse_type_try(VALUE a) {
if (RB_TEST(arg->require_eof)) {
parser_advance(parser);
if (parser->current_token.type != pEOF) {
set_error(parser, parser->current_token, true, "expected a token `%s`", token_type_str(pEOF));
set_error(parser, parser->current_token, true, "expected a token `%s`", rbs_token_type_str(pEOF));
raise_error(parser->error, arg->buffer);
}
}
Expand Down Expand Up @@ -202,7 +202,7 @@ static VALUE parse_method_type_try(VALUE a) {
if (RB_TEST(arg->require_eof)) {
parser_advance(parser);
if (parser->current_token.type != pEOF) {
set_error(parser, parser->current_token, true, "expected a token `%s`", token_type_str(pEOF));
set_error(parser, parser->current_token, true, "expected a token `%s`", rbs_token_type_str(pEOF));
raise_error(parser->error, arg->buffer);
}
}
Expand Down Expand Up @@ -289,10 +289,10 @@ static VALUE rbsparser_lex(VALUE self, VALUE buffer, VALUE end_pos) {
lexstate *lexer = alloc_lexer_from_buffer(&allocator, string, encoding, 0, FIX2INT(end_pos));

VALUE results = rb_ary_new();
token token = NullToken;
rbs_token_t token = NullToken;
while (token.type != pEOF) {
token = rbsparser_next_token(lexer);
VALUE type = ID2SYM(rb_intern(token_type_str(token.type)));
VALUE type = ID2SYM(rb_intern(rbs_token_type_str(token.type)));
VALUE location = rbs_new_location(buffer, token.range);
VALUE pair = rb_ary_new3(2, type, location);
rb_ary_push(results, pair);
Expand Down
42 changes: 21 additions & 21 deletions include/rbs/lexer.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,17 +103,17 @@ typedef struct {
int char_pos;
int line;
int column;
} position;
} rbs_position_t;

typedef struct {
position start;
position end;
} range;
rbs_position_t start;
rbs_position_t end;
} rbs_range_t;

typedef struct {
enum RBSTokenType type;
range range;
} token;
rbs_range_t range;
} rbs_token_t;

/**
* The lexer state is the curren token.
Expand All @@ -129,27 +129,27 @@ typedef struct {
rbs_string_t string;
int start_pos; /* The character position that defines the start of the input */
int end_pos; /* The character position that defines the end of the input */
position current; /* The current position */
position start; /* The start position of the current token */
rbs_position_t current; /* The current position */
rbs_position_t start; /* The start position of the current token */
bool first_token_of_line; /* This flag is used for tLINECOMMENT */
unsigned int last_char; /* Last peeked character */
const rbs_encoding_t *encoding;
} lexstate;

extern token NullToken;
extern position NullPosition;
extern range NULL_RANGE;
extern rbs_token_t NullToken;
extern rbs_position_t NullPosition;
extern rbs_range_t NULL_RANGE;

char *rbs_peek_token(lexstate *state, token tok);
int rbs_token_chars(token tok);
int rbs_token_bytes(token tok);
char *rbs_peek_token(lexstate *state, rbs_token_t tok);
int rbs_token_chars(rbs_token_t tok);
int rbs_token_bytes(rbs_token_t tok);

#define rbs_null_position_p(pos) (pos.byte_pos == -1)
#define rbs_null_range_p(range) (range.start.byte_pos == -1)
#define rbs_nonnull_pos_or(pos1, pos2) (rbs_null_position_p(pos1) ? pos2 : pos1)
#define RBS_RANGE_BYTES(range) (range.end.byte_pos - range.start.byte_pos)

const char *token_type_str(enum RBSTokenType type);
const char *rbs_token_type_str(enum RBSTokenType type);

/**
* Read next character.
Expand All @@ -167,17 +167,17 @@ void rbs_skip(lexstate *state);
void rbs_skipn(lexstate *state, size_t size);

/**
* Return new token with given type.
* Return new rbs_token_t with given type.
* */
token rbs_next_token(lexstate *state, enum RBSTokenType type);
rbs_token_t rbs_next_token(lexstate *state, enum RBSTokenType type);

/**
* Return new token with EOF type.
* Return new rbs_token_t with EOF type.
* */
token rbs_next_eof_token(lexstate *state);
rbs_token_t rbs_next_eof_token(lexstate *state);

token rbsparser_next_token(lexstate *state);
rbs_token_t rbsparser_next_token(lexstate *state);

void rbs_print_token(token tok);
void rbs_print_token(rbs_token_t tok);

#endif
18 changes: 9 additions & 9 deletions include/rbs/parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,19 @@
* A comment object represents the six lines of comments.
* */
typedef struct rbs_comment_t {
position start;
position end;
rbs_position_t start;
rbs_position_t end;

size_t line_size;
size_t line_count;
token *tokens;
rbs_token_t *tokens;

struct rbs_comment_t *next_comment;
} rbs_comment_t;

typedef struct rbs_error_t {
char *message;
token token;
rbs_token_t token;
bool syntax_error;
} rbs_error_t;

Expand All @@ -46,10 +46,10 @@ typedef struct rbs_error_t {
typedef struct {
lexstate *lexstate;

token current_token;
token next_token; /* The first lookahead token */
token next_token2; /* The second lookahead token */
token next_token3; /* The third lookahead token */
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 */
Expand Down Expand Up @@ -124,7 +124,7 @@ void print_parser(rbs_parser_t *parser);
* */
rbs_ast_comment_t *get_comment(rbs_parser_t *parser, int subject_line);

void set_error(rbs_parser_t *parser, token tok, bool syntax_error, const char *fmt, ...) RBS_ATTRIBUTE_FORMAT(4, 5);
void set_error(rbs_parser_t *parser, rbs_token_t tok, bool syntax_error, const char *fmt, ...) RBS_ATTRIBUTE_FORMAT(4, 5);

bool parse_type(rbs_parser_t *parser, rbs_node_t **type);
bool parse_method_type(rbs_parser_t *parser, rbs_methodtype_t **method_type);
Expand Down
8 changes: 4 additions & 4 deletions include/rbs/rbs_location.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,17 @@
#include "rbs/rbs_location_internals.h"

typedef struct rbs_location {
range rg;
rbs_range_t rg;
rbs_loc_children *children;
} rbs_location_t;

void rbs_loc_alloc_children(rbs_allocator_t *, rbs_location_t *loc, size_t capacity);
void rbs_loc_add_required_child(rbs_location_t *loc, rbs_constant_id_t name, range r);
void rbs_loc_add_optional_child(rbs_location_t *loc, rbs_constant_id_t name, range r);
void rbs_loc_add_required_child(rbs_location_t *loc, rbs_constant_id_t name, rbs_range_t r);
void rbs_loc_add_optional_child(rbs_location_t *loc, rbs_constant_id_t name, rbs_range_t r);

/**
* Allocate new rbs_location_t object through the given allocator.
* */
rbs_location_t *rbs_location_new(rbs_allocator_t *, range rg);
rbs_location_t *rbs_location_new(rbs_allocator_t *, rbs_range_t rg);

#endif
2 changes: 1 addition & 1 deletion src/lexer.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#line 1 "src/lexer.re"
#include "rbs/lexer.h"

token rbsparser_next_token(lexstate *state) {
rbs_token_t rbsparser_next_token(lexstate *state) {
lexstate backup;

backup = *state;
Expand Down
2 changes: 1 addition & 1 deletion src/lexer.re
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "rbs/lexer.h"

token rbsparser_next_token(lexstate *state) {
rbs_token_t rbsparser_next_token(lexstate *state) {
lexstate backup;

backup = *state;
Expand Down
22 changes: 11 additions & 11 deletions src/lexstate.c
Original file line number Diff line number Diff line change
Expand Up @@ -89,19 +89,19 @@ static const char *RBS_TOKENTYPE_NAMES[] = {
"tANNOTATION", /* Annotation */
};

token NullToken = { .type = NullType, .range = {} };
position NullPosition = { -1, -1, -1, -1 };
range NULL_RANGE = { { -1, -1, -1, -1 }, { -1, -1, -1, -1 } };
rbs_token_t NullToken = { .type = NullType, .range = {} };
rbs_position_t NullPosition = { -1, -1, -1, -1 };
rbs_range_t NULL_RANGE = { { -1, -1, -1, -1 }, { -1, -1, -1, -1 } };

const char *token_type_str(enum RBSTokenType type) {
const char *rbs_token_type_str(enum RBSTokenType type) {
return RBS_TOKENTYPE_NAMES[type];
}

int rbs_token_chars(token tok) {
int rbs_token_chars(rbs_token_t tok) {
return tok.range.end.char_pos - tok.range.start.char_pos;
}

int rbs_token_bytes(token tok) {
int rbs_token_bytes(rbs_token_t tok) {
return RBS_RANGE_BYTES(tok.range);
}

Expand All @@ -120,8 +120,8 @@ unsigned int rbs_peek(lexstate *state) {
}
}

token rbs_next_token(lexstate *state, enum RBSTokenType type) {
token t;
rbs_token_t rbs_next_token(lexstate *state, enum RBSTokenType type) {
rbs_token_t t;

t.type = type;
t.range.start = state->start;
Expand All @@ -134,10 +134,10 @@ token rbs_next_token(lexstate *state, enum RBSTokenType type) {
return t;
}

token rbs_next_eof_token(lexstate *state) {
rbs_token_t rbs_next_eof_token(lexstate *state) {
if ((size_t) state->current.byte_pos == rbs_string_len(state->string) + 1) {
// End of String
token t;
rbs_token_t t;
t.type = pEOF;
t.range.start = state->start;
t.range.end = state->start;
Expand Down Expand Up @@ -183,6 +183,6 @@ void rbs_skipn(lexstate *state, size_t size) {
}
}

char *rbs_peek_token(lexstate *state, token tok) {
char *rbs_peek_token(lexstate *state, rbs_token_t tok) {
return (char *) state->string.start + tok.range.start.byte_pos;
}
Loading
Loading