@@ -1103,6 +1103,7 @@ def on_command_call(receiver, operator, message, arguments)
11031103 # :call-seq:
11041104 # on_comment: (String value) -> Comment
11051105 def on_comment ( value )
1106+ # char is the index of the # character in the source.
11061107 char = char_pos
11071108 location =
11081109 Location . token (
@@ -1112,8 +1113,24 @@ def on_comment(value)
11121113 size : value . size - 1
11131114 )
11141115
1115- index = source . rindex ( /[^\t ]/ , char - 1 ) if char != 0
1116- inline = index && ( source [ index ] != "\n " )
1116+ # Loop backward in the source string, starting from the beginning of the
1117+ # comment, and find the first character that is not a space or a tab. If
1118+ # index is -1, this indicates that we've checked all of the characters
1119+ # back to the start of the source, so this comment must be at the
1120+ # beginning of the file.
1121+ #
1122+ # We are purposefully not using rindex or regular expressions here because
1123+ # they check if there are invalid characters, which is actually possible
1124+ # with the use of __END__ syntax.
1125+ index = char - 1
1126+ while index > -1 && ( source [ index ] == "\t " || source [ index ] == " " )
1127+ index -= 1
1128+ end
1129+
1130+ # If we found a character that was not a space or a tab before the comment
1131+ # and it's a newline, then this comment is inline. Otherwise, it stands on
1132+ # its own and can be attached as its own node in the tree.
1133+ inline = index != -1 && source [ index ] != "\n "
11171134 comment =
11181135 Comment . new ( value : value . chomp , inline : inline , location : location )
11191136
0 commit comments