|
| 1 | +require 'prism' |
| 2 | +require 'set' |
| 3 | + |
| 4 | +# Tokenize Ruby code as RDoc::Parser::RipperStateLex style types and token squashing. |
| 5 | +# Token squashing is required by RDoc::TokenStream's syntax highlighting. |
| 6 | +module RDoc::Parser::Tokenizer |
| 7 | + # This constants and token type map are for compatibility with RDoc::Parser::RipperStateLex. |
| 8 | + OTHER = :other |
| 9 | + SPACE = :on_sp |
| 10 | + NEWLINE = :on_nl |
| 11 | + KEYWORD = :on_kw |
| 12 | + OP = :on_op |
| 13 | + HEREDOC_BEG = :on_heredoc_beg |
| 14 | + HEREDOC_CONTENT = :on_heredoc |
| 15 | + HEREDOC_END = :on_heredoc_end |
| 16 | + COMMENT = :on_comment |
| 17 | + INTEGER = :on_int |
| 18 | + FLOAT = :on_float |
| 19 | + RATIONAL = :on_rational |
| 20 | + IMAGINARY = :on_imaginary |
| 21 | + SYMBOL = :on_symbol |
| 22 | + REGEXP = :on_regexp |
| 23 | + STRING = :on_tstring |
| 24 | + WORDS = :on_dstring |
| 25 | + DEF_METHOD_NAME = :on_ident |
| 26 | + DSTRING = :on_dstring |
| 27 | + |
| 28 | + OP_TOKENS = %i[ |
| 29 | + AMPERSAND AMPERSAND_AMPERSAND |
| 30 | + BANG BANG_EQUAL BANG_TILDE CARET COLON COLON_COLON |
| 31 | + EQUAL EQUAL_EQUAL EQUAL_GREATER EQUAL_TILDE |
| 32 | + GREATER GREATER_GREATER |
| 33 | + LESS LESS_EQUAL LESS_EQUAL_GREATER LESS_LESS |
| 34 | + MINUS MINUS_GREATER PERCENT PIPE PIPE_PIPE PLUS |
| 35 | + QUESTION_MARK SLASH STAR STAR_STAR TILDE |
| 36 | + UAMPERSAND UMINUS UPLUS USTAR USTAR_STAR |
| 37 | + ].to_set |
| 38 | + |
| 39 | + TOKEN_TYPE_MAP = { |
| 40 | + IDENTIFIER: :on_ident, |
| 41 | + METHOD_NAME: :on_ident, |
| 42 | + INSTANCE_VARIABLE: :on_ivar, |
| 43 | + CLASS_VARIABLE: :on_cvar, |
| 44 | + GLOBAL_VARIABLE: :on_gvar, |
| 45 | + BACK_REFERENCE: :on_backref, |
| 46 | + NUMBERED_REFERENCE: :on_backref, |
| 47 | + CONSTANT: :on_const, |
| 48 | + LABEL: :on_label, |
| 49 | + INTEGER: :on_int, |
| 50 | + FLOAT: :on_float, |
| 51 | + RATIONAL: :on_rational, |
| 52 | + IMAGINARY: :on_imaginary, |
| 53 | + } |
| 54 | + |
| 55 | + class << self |
| 56 | + def tokenize(code) |
| 57 | + result = Prism.parse_lex(code) |
| 58 | + program_node, unordered_tokens = result.value |
| 59 | + prism_tokens = unordered_tokens.map(&:first).sort_by! { |token| token.location.start_offset } |
| 60 | + partial_tokenize(code, program_node, prism_tokens, result.comments, 0, code.bytesize) |
| 61 | + end |
| 62 | + |
| 63 | + def partial_tokenize(whole_code, node, prism_tokens, prism_comments, start_offset = nil, end_offset = nil) |
| 64 | + start_offset ||= node.location.start_offset |
| 65 | + end_offset ||= node.location.end_offset |
| 66 | + visitor = SquashTokenVisitor.new |
| 67 | + node.accept(visitor) |
| 68 | + squashed_tokens = visitor.tokens |
| 69 | + comment_tokens = comment_tokens(slice_by_location(prism_comments, start_offset, end_offset)) |
| 70 | + normal_tokens = normal_tokens(slice_by_location(prism_tokens, start_offset, end_offset)) |
| 71 | + prior_tokens = (squashed_tokens + comment_tokens).sort_by {|_, start_offset, _| start_offset } |
| 72 | + unify_tokens(whole_code, prior_tokens, normal_tokens, start_offset, end_offset) |
| 73 | + end |
| 74 | + |
| 75 | + private |
| 76 | + |
| 77 | + def slice_by_location(items, start_offset, end_offset) |
| 78 | + start_index = items.bsearch_index { |item| item.location.end_offset > start_offset } || items.size |
| 79 | + end_index = items.bsearch_index { |item| item.location.start_offset >= end_offset } || items.size |
| 80 | + items[start_index...end_index] |
| 81 | + end |
| 82 | + |
| 83 | + # Unify prior tokens and normal tokens into a token stream. |
| 84 | + # Prior tokens have higher priority than normal tokens. |
| 85 | + # Also adds missing text (spaces, newlines, etc.) as separate tokens |
| 86 | + # so that the entire code is covered. |
| 87 | + def unify_tokens(code, prior_tokens, normal_tokens, start_offset, end_offset) |
| 88 | + tokens = [] |
| 89 | + offset = start_offset |
| 90 | + |
| 91 | + # Add missing text such as spaces and newlines as a separate token |
| 92 | + flush = -> next_offset { |
| 93 | + return if offset == next_offset |
| 94 | + |
| 95 | + code[offset...next_offset].scan(/\n|\s+|[^\s]+/) do |text| |
| 96 | + type = |
| 97 | + if text == "\n" |
| 98 | + NEWLINE |
| 99 | + elsif /\A\s+\z/.match?(text) |
| 100 | + SPACE |
| 101 | + else |
| 102 | + OTHER |
| 103 | + end |
| 104 | + tokens << [type, text] |
| 105 | + end |
| 106 | + } |
| 107 | + |
| 108 | + until prior_tokens.empty? && normal_tokens.empty? |
| 109 | + ptok = prior_tokens.first |
| 110 | + ntok = normal_tokens.first |
| 111 | + if ntok && (!ptok || ntok[2] <= ptok[1]) |
| 112 | + token = normal_tokens.shift |
| 113 | + else |
| 114 | + token = prior_tokens.shift |
| 115 | + end |
| 116 | + type, start_pos, end_pos = token |
| 117 | + next if start_pos < offset |
| 118 | + |
| 119 | + flush.call(start_pos) |
| 120 | + tokens << [type, code.byteslice(start_pos...end_pos)] |
| 121 | + offset = end_pos |
| 122 | + end |
| 123 | + flush.call(end_offset) |
| 124 | + tokens |
| 125 | + end |
| 126 | + |
| 127 | + # Extract normal comment and embdoc comment (consists of multiple tokens) as a single token |
| 128 | + def comment_tokens(comments) |
| 129 | + comments.map do |comment| |
| 130 | + [COMMENT, comment.location.start_offset, comment.location.end_offset] |
| 131 | + end |
| 132 | + end |
| 133 | + |
| 134 | + # Convert normal Prism tokens to [type, start_offset, end_offset] |
| 135 | + def normal_tokens(tokens) |
| 136 | + tokens.map do |token,| |
| 137 | + type = |
| 138 | + if token.type.start_with?('KEYWORD_') |
| 139 | + KEYWORD |
| 140 | + elsif OP_TOKENS.include?(token.type.to_sym) |
| 141 | + OP |
| 142 | + else |
| 143 | + TOKEN_TYPE_MAP[token.type] || OTHER |
| 144 | + end |
| 145 | + [type, token.location.start_offset, token.location.end_offset] |
| 146 | + end |
| 147 | + end |
| 148 | + end |
| 149 | + |
| 150 | + # Visitor to squash several tokens that consist a single node into a single token |
| 151 | + class SquashTokenVisitor < Prism::Visitor |
| 152 | + attr_reader :tokens |
| 153 | + def initialize |
| 154 | + @tokens = [] |
| 155 | + end |
| 156 | + |
| 157 | + def heredoc?(node) |
| 158 | + /\A<</.match?(node.opening) |
| 159 | + end |
| 160 | + |
| 161 | + # Squash UMINUS and its operand(integer, float, rational, imaginary) token into a single token |
| 162 | + def visit_integer_node(node) |
| 163 | + push_location(node.location, INTEGER) |
| 164 | + end |
| 165 | + |
| 166 | + def visit_float_node(node) |
| 167 | + push_location(node.location, FLOAT) |
| 168 | + end |
| 169 | + |
| 170 | + def visit_rational_node(node) |
| 171 | + push_location(node.location, RATIONAL) |
| 172 | + end |
| 173 | + |
| 174 | + def visit_imaginary_node(node) |
| 175 | + push_location(node.location, IMAGINARY) |
| 176 | + end |
| 177 | + |
| 178 | + def visit_symbol_node(node) |
| 179 | + push_location(node.location, SYMBOL) |
| 180 | + end |
| 181 | + alias visit_interpolated_symbol_node visit_symbol_node |
| 182 | + |
| 183 | + def visit_regular_expression_node(node) |
| 184 | + push_location(node.location, REGEXP) |
| 185 | + end |
| 186 | + alias visit_match_last_line_node visit_regular_expression_node |
| 187 | + alias visit_interpolated_regular_expression_node visit_regular_expression_node |
| 188 | + alias visit_interpolated_match_last_line_node visit_regular_expression_node |
| 189 | + |
| 190 | + def visit_string_node(node) |
| 191 | + if heredoc?(node) |
| 192 | + push_location(node.opening_loc, HEREDOC_BEG) |
| 193 | + push_location(node.content_loc, HEREDOC_CONTENT) |
| 194 | + push_location(node.closing_loc, HEREDOC_END) |
| 195 | + else |
| 196 | + push_location(node.location, STRING) |
| 197 | + end |
| 198 | + end |
| 199 | + alias visit_x_string_node visit_string_node |
| 200 | + |
| 201 | + def visit_array_node(node) |
| 202 | + if /\A%/.match?(node.opening) |
| 203 | + # Percent array: squash entire node into a single token. |
| 204 | + # We don't handle embedded expressions inside yet. |
| 205 | + push_location(node.location, WORDS) |
| 206 | + else |
| 207 | + super |
| 208 | + end |
| 209 | + end |
| 210 | + |
| 211 | + def push_location(location, type) |
| 212 | + @tokens << [type, location.start_offset, location.end_offset] |
| 213 | + end |
| 214 | + |
| 215 | + def visit_def_node(node) |
| 216 | + # For special colorizing of method name in def node |
| 217 | + push_location(node.name_loc, DEF_METHOD_NAME) |
| 218 | + super |
| 219 | + end |
| 220 | + |
| 221 | + def visit_interpolated_string_node(node) |
| 222 | + if /\A<</.match?(node.opening) |
| 223 | + # Heredocs. Squash content into a single token. |
| 224 | + # We don't tokenize embedded expressions inside, and don't handle nested heredocs yet. |
| 225 | + push_location(node.opening_loc, HEREDOC_BEG) |
| 226 | + unless node.parts.empty? |
| 227 | + # Squash heredoc content into a single token |
| 228 | + part_locations = node.parts.map(&:location) |
| 229 | + @tokens << [ |
| 230 | + HEREDOC_CONTENT, |
| 231 | + part_locations.map(&:start_offset).min, |
| 232 | + part_locations.map(&:end_offset).max |
| 233 | + ] |
| 234 | + end |
| 235 | + # incomplete heredoc might not have closing_loc |
| 236 | + push_location(node.closing_loc, HEREDOC_END) if node.closing_loc |
| 237 | + else |
| 238 | + # Squash entire node into a single token |
| 239 | + push_location(node.location, DSTRING) |
| 240 | + end |
| 241 | + end |
| 242 | + alias visit_interpolated_x_string_node visit_interpolated_string_node |
| 243 | + end |
| 244 | +end |
0 commit comments