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

Skip instead of Scan #192

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 18 additions & 17 deletions lib/coderay/scanners/ruby.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,30 +62,31 @@ def scan_tokens encoder, options
if match = scan(/[ \t\f\v]+/)
encoder.text_token match, :space

elsif match = scan(/\n/)
elsif skip(/\n/)
if heredocs
unscan # heredoc scanning needs \n at start
state = heredocs.shift
encoder.begin_group state.type
heredocs = nil if heredocs.empty?
else
state = :initial if state == :undef_comma_expected
encoder.text_token match, :space
encoder.text_token "\n", :space
value_expected = true
end

elsif match = scan(bol? ? / \#(!)?.* | #{patterns::RUBYDOC_OR_DATA} /ox : /\#.*/)
encoder.text_token match, self[1] ? :doctype : :comment

elsif match = scan(/\\\n/)
elsif skip(/\\\n/)
if heredocs
unscan # heredoc scanning needs \n at start
encoder.text_token scan(/\\/), :space
skip(/\\/)
encoder.text_token '\\', :space
state = heredocs.shift
encoder.begin_group state.type
heredocs = nil if heredocs.empty?
else
encoder.text_token match, :space
encoder.text_token "\\\n", :space
end

elsif state == :initial
Expand All @@ -96,7 +97,7 @@ def scan_tokens encoder, options
/#{patterns::METHOD_NAME}/o)

kind = patterns::IDENT_KIND[match]
if value_expected != :colon_expected && scan(/:(?!:)/)
if value_expected != :colon_expected && skip(/:(?!:)/)
value_expected = true
encoder.text_token match, :key
encoder.text_token ':', :operator
Expand Down Expand Up @@ -185,9 +186,9 @@ def scan_tokens encoder, options
value_expected = false
encoder.text_token match, :instance_variable

elsif value_expected && match = scan(/\//)
elsif value_expected && skip(/\//)
encoder.begin_group :regexp
encoder.text_token match, :delimiter
encoder.text_token '/', :delimiter
state = self.class::StringState.new :regexp, true, '/'

elsif match = scan(value_expected ? /[-+]?#{patterns::NUMERIC}/o : /#{patterns::NUMERIC}/o)
Expand Down Expand Up @@ -232,10 +233,10 @@ def scan_tokens encoder, options
value_expected = match == '?' ? :colon_expected : true
encoder.text_token match, :operator

elsif match = scan(/`/)
elsif skip(/`/)
encoder.begin_group :shell
encoder.text_token match, :delimiter
state = self.class::StringState.new :shell, true, match
encoder.text_token '`', :delimiter
state = self.class::StringState.new :shell, true, '`'

elsif match = scan(unicode ? /#{patterns::GLOBAL_VARIABLE}/uo :
/#{patterns::GLOBAL_VARIABLE}/o)
Expand All @@ -247,8 +248,8 @@ def scan_tokens encoder, options
encoder.text_token match, :class_variable
value_expected = false

elsif match = scan(/\\\z/)
encoder.text_token match, :space
elsif skip(/\\\z/)
encoder.text_token '\\', :space

else
if method_call_expected
Expand Down Expand Up @@ -300,8 +301,8 @@ def scan_tokens encoder, options
end

elsif state == :module_expected
if match = scan(/<</)
encoder.text_token match, :operator
if skip(/<</)
encoder.text_token '<<', :operator
else
state = :initial
if match = scan(unicode ? / (?:#{patterns::IDENT}::)* #{patterns::IDENT} /oux :
Expand Down Expand Up @@ -332,8 +333,8 @@ def scan_tokens encoder, options
end

elsif state == :undef_comma_expected
if match = scan(/,/)
encoder.text_token match, :operator
if skip(/,/)
encoder.text_token ',', :operator
state = :undef_expected
else
state = :initial
Expand Down