Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parser::Builder::Default.check_lvar_name patch #2195

Closed
rubys opened this issue Apr 13, 2021 · 2 comments · Fixed by #2243
Closed

Parser::Builder::Default.check_lvar_name patch #2195

rubys opened this issue Apr 13, 2021 · 2 comments · Fixed by #2243

Comments

@rubys
Copy link
Contributor

rubys commented Apr 13, 2021

Test case:

require 'parser/ruby30'

if RUBY_ENGINE == 'opal'
  require 'corelib/string/unpack'

  # https://github.com/opal/opal/blob/master/lib/opal/parser/patch.rb
  class Parser::Source::Buffer
    def source_lines
      @lines ||= begin
        lines = @source.lines.to_a
        lines << '' if @source.end_with?("\n")
        lines.map { |line| line.chomp("\n") }
      end
    end
  end

  # https://github.com/whitequark/parser/issues/784
  module Parser
    class Diagnostic
      undef :render_line

      def render_line(range, ellipsis=false, range_end=false)
        source_line    = range.source_line
        highlight_line = [' '] * source_line.length

        @highlights.each do |highlight|
          line_range = range.source_buffer.line_range(range.line)
          if highlight = highlight.intersect(line_range)
            highlight_line[highlight.column_range] = ['~'] * highlight.size
          end
        end

        if range.is?("\n")
          highlight_line << "^"
        else
          if !range_end && range.size >= 1
            highlight_line[range.column_range] = ['^'] + ['~'] * (range.size - 1)
          else
            highlight_line[range.column_range] = ['~'] * range.size
          end
        end

        highlight_line += %w(. . .) if ellipsis
        highlight_line = highlight_line.join

        [source_line, highlight_line].
          map { |line| "#{range.source_buffer.name}:#{range.line}: #{line}" }
      end
    end
  end
end

p Parser::Ruby30.parse('1 => a')

The issue is a regular expression using POSIX bracket expressions: https://github.com/whitequark/parser/blob/21684f12eb73ac416fa88ecee7198739a1847596/lib/parser/builders/default.rb#L1726-L1732

Patch:

  module Parser
    class Builders::Default
      def check_lvar_name(name, loc)
        if name =~ /^[_a-z][_\w]*$/
          # OK
        else
          diagnostic :error, :lvar_name, { name: name }, loc
        end
      end
    end
  end
@rubys
Copy link
Contributor Author

rubys commented Apr 14, 2021

An alternative regex:

/^\p{Ll}(\p{L}|\p{N}|_)*$/u

Seems to be nearly universally supported these days

@hmdne hmdne linked a pull request Jul 5, 2021 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants