Skip to content

Commit 9777d42

Browse files
authored
Merge pull request #1788 from pocke/Omit_unnecessary_field_from_location_range
Omit unnecessary field from location range
2 parents 65f6d48 + 553543d commit 9777d42

File tree

13 files changed

+97
-55
lines changed

13 files changed

+97
-55
lines changed

ext/rbs_extension/lexer.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ token rbsparser_next_token(lexstate *state) {
116116
yy1:
117117
rbs_skip(state);
118118
#line 144 "ext/rbs_extension/lexer.re"
119-
{ return next_token(state, pEOF); }
119+
{ return next_eof_token(state); }
120120
#line 121 "ext/rbs_extension/lexer.c"
121121
yy2:
122122
rbs_skip(state);

ext/rbs_extension/lexer.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,11 @@ void skipn(lexstate *state, size_t size);
167167
* */
168168
token next_token(lexstate *state, enum TokenType type);
169169

170+
/**
171+
* Return new token with EOF type.
172+
* */
173+
token next_eof_token(lexstate *state);
174+
170175
token rbsparser_next_token(lexstate *state);
171176

172177
void print_token(token tok);

ext/rbs_extension/lexer.re

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ token rbsparser_next_token(lexstate *state) {
141141
skip = ([ \t]+|[\r\n]);
142142

143143
skip { return next_token(state, tTRIVIA); }
144-
"\x00" { return next_token(state, pEOF); }
144+
"\x00" { return next_eof_token(state); }
145145
* { return next_token(state, ErrorToken); }
146146
*/
147147
}

ext/rbs_extension/lexstate.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,22 @@ token next_token(lexstate *state, enum TokenType type) {
129129
return t;
130130
}
131131

132+
token next_eof_token(lexstate *state) {
133+
if (state->current.byte_pos == RSTRING_LEN(state->string)+1) {
134+
// End of String
135+
token t;
136+
t.type = pEOF;
137+
t.range.start = state->start;
138+
t.range.end = state->start;
139+
state->start = state->current;
140+
141+
return t;
142+
} else {
143+
// NULL byte in the middle of the string
144+
return next_token(state, pEOF);
145+
}
146+
}
147+
132148
void rbs_skip(lexstate *state) {
133149
if (!state->last_char) {
134150
peek(state);

ext/rbs_extension/location.c

Lines changed: 27 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
#define RBS_LOC_REQUIRED_P(loc, i) ((loc)->children->required_p & (1 << (i)))
44
#define RBS_LOC_OPTIONAL_P(loc, i) (!RBS_LOC_REQUIRED_P((loc), (i)))
55
#define RBS_LOC_CHILDREN_SIZE(cap) (sizeof(rbs_loc_children) + sizeof(rbs_loc_entry) * ((cap) - 1))
6+
#define NULL_LOC_RANGE_P(rg) ((rg).start == -1)
67

8+
rbs_loc_range RBS_LOC_NULL_RANGE = { -1, -1 };
79
VALUE RBS_Location;
810

911
position rbs_loc_position(int char_pos) {
@@ -16,6 +18,11 @@ position rbs_loc_position3(int char_pos, int line, int column) {
1618
return pos;
1719
}
1820

21+
rbs_loc_range rbs_new_loc_range(range rg) {
22+
rbs_loc_range r = { rg.start.char_pos, rg.end.char_pos };
23+
return r;
24+
}
25+
1926
static void check_children_max(unsigned short n) {
2027
size_t max = sizeof(rbs_loc_entry_bitmap) * 8;
2128
if (n > max) {
@@ -51,7 +58,7 @@ void rbs_loc_add_required_child(rbs_loc *loc, ID name, range r) {
5158

5259
unsigned short i = loc->children->len++;
5360
loc->children->entries[i].name = name;
54-
loc->children->entries[i].rg = r;
61+
loc->children->entries[i].rg = rbs_new_loc_range(r);
5562

5663
loc->children->required_p |= 1 << i;
5764
}
@@ -61,10 +68,10 @@ void rbs_loc_add_optional_child(rbs_loc *loc, ID name, range r) {
6168

6269
unsigned short i = loc->children->len++;
6370
loc->children->entries[i].name = name;
64-
loc->children->entries[i].rg = r;
71+
loc->children->entries[i].rg = rbs_new_loc_range(r);
6572
}
6673

67-
void rbs_loc_init(rbs_loc *loc, VALUE buffer, range rg) {
74+
void rbs_loc_init(rbs_loc *loc, VALUE buffer, rbs_loc_range rg) {
6875
loc->buffer = buffer;
6976
loc->rg = rg;
7077
loc->children = NULL;
@@ -100,7 +107,7 @@ static VALUE location_s_allocate(VALUE klass) {
100107
rbs_loc *loc;
101108
VALUE obj = TypedData_Make_Struct(klass, rbs_loc, &location_type, loc);
102109

103-
rbs_loc_init(loc, Qnil, NULL_RANGE);
110+
rbs_loc_init(loc, Qnil, RBS_LOC_NULL_RANGE);
104111

105112
return obj;
106113
}
@@ -112,8 +119,8 @@ rbs_loc *rbs_check_location(VALUE obj) {
112119
static VALUE location_initialize(VALUE self, VALUE buffer, VALUE start_pos, VALUE end_pos) {
113120
rbs_loc *loc = rbs_check_location(self);
114121

115-
position start = rbs_loc_position(FIX2INT(start_pos));
116-
position end = rbs_loc_position(FIX2INT(end_pos));
122+
int start = FIX2INT(start_pos);
123+
int end = FIX2INT(end_pos);
117124

118125
loc->buffer = buffer;
119126
loc->rg.start = start;
@@ -143,38 +150,12 @@ static VALUE location_buffer(VALUE self) {
143150

144151
static VALUE location_start_pos(VALUE self) {
145152
rbs_loc *loc = rbs_check_location(self);
146-
return INT2FIX(loc->rg.start.char_pos);
153+
return INT2FIX(loc->rg.start);
147154
}
148155

149156
static VALUE location_end_pos(VALUE self) {
150157
rbs_loc *loc = rbs_check_location(self);
151-
return INT2FIX(loc->rg.end.char_pos);
152-
}
153-
154-
static VALUE location_start_loc(VALUE self) {
155-
rbs_loc *loc = rbs_check_location(self);
156-
157-
if (loc->rg.start.line >= 0) {
158-
VALUE pair = rb_ary_new_capa(2);
159-
rb_ary_push(pair, INT2FIX(loc->rg.start.line));
160-
rb_ary_push(pair, INT2FIX(loc->rg.start.column));
161-
return pair;
162-
} else {
163-
return Qnil;
164-
}
165-
}
166-
167-
static VALUE location_end_loc(VALUE self) {
168-
rbs_loc *loc = rbs_check_location(self);
169-
170-
if (loc->rg.end.line >= 0) {
171-
VALUE pair = rb_ary_new_capa(2);
172-
rb_ary_push(pair, INT2FIX(loc->rg.end.line));
173-
rb_ary_push(pair, INT2FIX(loc->rg.end.column));
174-
return pair;
175-
} else {
176-
return Qnil;
177-
}
158+
return INT2FIX(loc->rg.end);
178159
}
179160

180161
static VALUE location_add_required_child(VALUE self, VALUE name, VALUE start, VALUE end) {
@@ -213,6 +194,15 @@ VALUE rbs_new_location(VALUE buffer, range rg) {
213194
rbs_loc *loc;
214195
VALUE obj = TypedData_Make_Struct(RBS_Location, rbs_loc, &location_type, loc);
215196

197+
rbs_loc_init(loc, buffer, rbs_new_loc_range(rg));
198+
199+
return obj;
200+
}
201+
202+
static VALUE rbs_new_location_from_loc_range(VALUE buffer, rbs_loc_range rg) {
203+
rbs_loc *loc;
204+
VALUE obj = TypedData_Make_Struct(RBS_Location, rbs_loc, &location_type, loc);
205+
216206
rbs_loc_init(loc, buffer, rg);
217207

218208
return obj;
@@ -226,12 +216,12 @@ static VALUE location_aref(VALUE self, VALUE name) {
226216
if (loc->children != NULL) {
227217
for (unsigned short i = 0; i < loc->children->len; i++) {
228218
if (loc->children->entries[i].name == id) {
229-
range result = loc->children->entries[i].rg;
219+
rbs_loc_range result = loc->children->entries[i].rg;
230220

231-
if (RBS_LOC_OPTIONAL_P(loc, i) && null_range_p(result)) {
221+
if (RBS_LOC_OPTIONAL_P(loc, i) && NULL_LOC_RANGE_P(result)) {
232222
return Qnil;
233223
} else {
234-
return rbs_new_location(loc->buffer, result);
224+
return rbs_new_location_from_loc_range(loc->buffer, result);
235225
}
236226
}
237227
}
@@ -294,8 +284,6 @@ void rbs__init_location(void) {
294284
rb_define_method(RBS_Location, "buffer", location_buffer, 0);
295285
rb_define_method(RBS_Location, "start_pos", location_start_pos, 0);
296286
rb_define_method(RBS_Location, "end_pos", location_end_pos, 0);
297-
rb_define_private_method(RBS_Location, "_start_loc", location_start_loc, 0);
298-
rb_define_private_method(RBS_Location, "_end_loc", location_end_loc, 0);
299287
rb_define_method(RBS_Location, "_add_required_child", location_add_required_child, 3);
300288
rb_define_method(RBS_Location, "_add_optional_child", location_add_optional_child, 3);
301289
rb_define_method(RBS_Location, "_add_optional_no_child", location_add_optional_no_child, 1);

ext/rbs_extension/location.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,14 @@
99
* */
1010
extern VALUE RBS_Location;
1111

12+
typedef struct {
13+
int start;
14+
int end;
15+
} rbs_loc_range;
16+
1217
typedef struct {
1318
ID name;
14-
range rg;
19+
rbs_loc_range rg;
1520
} rbs_loc_entry;
1621

1722
typedef unsigned int rbs_loc_entry_bitmap;
@@ -27,7 +32,7 @@ typedef struct {
2732

2833
typedef struct {
2934
VALUE buffer;
30-
range rg;
35+
rbs_loc_range rg;
3136
rbs_loc_children *children; // NULL when no children is allocated
3237
} rbs_loc;
3338

lib/rbs/buffer.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@ def ranges
2525
@ranges << range
2626
offset += size
2727
end
28+
29+
if !content.end_with?("\n") && content.size > 0
30+
@ranges[-1] = @ranges[-1].begin...(@ranges[-1].end+1)
31+
end
32+
2833
@ranges
2934
end
3035
end

lib/rbs/errors.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ def detailed_message(highlight: false, **)
3535
return msg unless location.start_line == location.end_line
3636

3737
indent = " " * location.start_column
38-
marker = "^" * (location.end_column - location.start_column)
38+
marker = "^" * ([location.end_column - location.start_column, 1].max or raise)
3939

4040
io = StringIO.new
4141
io.puts msg

lib/rbs/location_aux.rb

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,15 +49,11 @@ def end_column
4949
end
5050

5151
def start_loc
52-
@start_loc ||= begin
53-
_start_loc || buffer.pos_to_loc(start_pos)
54-
end
52+
@start_loc ||= buffer.pos_to_loc(start_pos)
5553
end
5654

5755
def end_loc
58-
@end_loc ||= begin
59-
_end_loc || buffer.pos_to_loc(end_pos)
60-
end
56+
@end_loc ||= buffer.pos_to_loc(end_pos)
6157
end
6258

6359
def range

sig/location.rbs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,6 @@ module RBS
9999

100100
private
101101

102-
def _start_loc: () -> Buffer::loc?
103-
def _end_loc: () -> Buffer::loc?
104-
105102
def _add_required_child: (RequiredChildKeys name, Integer start_pos, Integer end_pos) -> void
106103
def _add_optional_child: (OptionalChildKeys name, Integer start_pos, Integer end_pos) -> void
107104
def _add_optional_no_child: (OptionalChildKeys name) -> void

0 commit comments

Comments
 (0)