Skip to content

Commit

Permalink
EOF token is zero length
Browse files Browse the repository at this point in the history
  • Loading branch information
pocke committed Sep 6, 2024
1 parent 86c34f9 commit 16006c2
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 5 deletions.
2 changes: 1 addition & 1 deletion ext/rbs_extension/lexer.c
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ token rbsparser_next_token(lexstate *state) {
yy1:
rbs_skip(state);
#line 144 "ext/rbs_extension/lexer.re"
{ return next_token(state, pEOF); }
{ return next_eof_token(state); }
#line 121 "ext/rbs_extension/lexer.c"
yy2:
rbs_skip(state);
Expand Down
5 changes: 5 additions & 0 deletions ext/rbs_extension/lexer.h
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,11 @@ void skipn(lexstate *state, size_t size);
* */
token next_token(lexstate *state, enum TokenType type);

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

token rbsparser_next_token(lexstate *state);

void print_token(token tok);
Expand Down
2 changes: 1 addition & 1 deletion ext/rbs_extension/lexer.re
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ token rbsparser_next_token(lexstate *state) {
skip = ([ \t]+|[\r\n]);

skip { return next_token(state, tTRIVIA); }
"\x00" { return next_token(state, pEOF); }
"\x00" { return next_eof_token(state); }
* { return next_token(state, ErrorToken); }
*/
}
16 changes: 16 additions & 0 deletions ext/rbs_extension/lexstate.c
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,22 @@ token next_token(lexstate *state, enum TokenType type) {
return t;
}

token next_eof_token(lexstate *state) {
if (state->current.byte_pos == RSTRING_LEN(state->string)+1) {
// End of String
token t;
t.type = pEOF;
t.range.start = state->start;
t.range.end = state->start;
state->start = state->current;

return t;
} else {
// NULL byte in the middle of the string
return next_token(state, pEOF);
}
}

void rbs_skip(lexstate *state) {
if (!state->last_char) {
peek(state);
Expand Down
2 changes: 1 addition & 1 deletion lib/rbs/errors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def detailed_message(highlight: false, **)
return msg unless location.start_line == location.end_line

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

io = StringIO.new
io.puts msg
Expand Down
2 changes: 1 addition & 1 deletion test/rbs/cli_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -912,7 +912,7 @@ def test_parse_method_type

assert_raises(SystemExit) { cli.run(['parse', '--method-type', '-e', '()']) }
assert_equal [
"-e:1:2...1:3: Syntax error: expected a token `pARROW`, token=`` (pEOF) (RBS::ParsingError)",
"-e:1:2...1:2: Syntax error: expected a token `pARROW`, token=`` (pEOF) (RBS::ParsingError)",
"",
" ()",
" ^"
Expand Down
2 changes: 1 addition & 1 deletion test/rbs/parser_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -834,6 +834,6 @@ class Foo[T < Integer] < Bar # Comment
assert_equal [:tTRIVIA, "\n", 52...53], tokens.shift.then { |t| [t[0], t[1].source, t[1].range] }
assert_equal [:kEND, 'end', 53...56], tokens.shift.then { |t| [t[0], t[1].source, t[1].range] }
assert_equal [:tTRIVIA, "\n", 56...57], tokens.shift.then { |t| [t[0], t[1].source, t[1].range] }
assert_equal [:pEOF, '', 57...58], tokens.shift.then { |t| [t[0], t[1].source, t[1].range] }
assert_equal [:pEOF, '', 57...57], tokens.shift.then { |t| [t[0], t[1].source, t[1].range] }
end
end

0 comments on commit 16006c2

Please sign in to comment.