From f76d4d4a7582a35e6c5bd6e96070cf45b1391aab Mon Sep 17 00:00:00 2001 From: Masataka Pocke Kuwabara Date: Fri, 10 Jul 2020 21:21:32 +0900 Subject: [PATCH] Make parser faster --- lib/rbs/parser.y | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/lib/rbs/parser.y b/lib/rbs/parser.y index 14c56c00f..bb962f8f3 100644 --- a/lib/rbs/parser.y +++ b/lib/rbs/parser.y @@ -1030,6 +1030,7 @@ def initialize(type, buffer:, eof_re:) @eof = false @bound_variables_stack = [] @comments = {} + @ascii_only = buffer.content.ascii_only? end def start_merged_variables_scope @@ -1123,8 +1124,9 @@ def push_comment(string, location) end def new_token(type, value = input.matched) - start_index = input.charpos - input.matched.size - end_index = input.charpos + charpos = charpos(input) + start_index = charpos - input.matched.size + end_index = charpos location = RBS::Location.new(buffer: buffer, start_pos: start_index, @@ -1133,6 +1135,14 @@ def new_token(type, value = input.matched) [type, LocatedValue.new(location: location, value: value)] end +def charpos(scanner) + if @ascii_only + scanner.pos + else + scanner.charpos + end +end + def empty_params_result [ [], @@ -1250,8 +1260,9 @@ def next_token when input.scan(/\s+/) # skip when input.scan(/#(( *)|( ?(?.*)))\n/) - start_index = input.charpos - input.matched.size - end_index = input.charpos-1 + charpos = charpos(input) + start_index = charpos - input.matched.size + end_index = charpos-1 location = RBS::Location.new(buffer: buffer, start_pos: start_index,