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

LTSV scanner #127

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions lib/coderay/helpers/file_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ def shebang filename
'java' => :java,
'js' => :java_script,
'json' => :json,
'ltsv' => :ltsv,
'mab' => :ruby,
'pas' => :delphi,
'patch' => :diff,
Expand Down
49 changes: 49 additions & 0 deletions lib/coderay/scanners/ltsv.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
module CodeRay
module Scanners

# A scanner for LTSV.
# http://ltsv.org
class Ltsv < Scanner

register_for :ltsv
file_extension 'ltsv'

protected

def scan_tokens encoder, options

state = :initial

until eos?

if match = scan(/(?:\r?\n)+/)
encoder.text_token match, :space
state = :initial if match.index(/\r?\n/)

elsif (state == :initial || state == :tab) && match = scan(/[^:]+/)
encoder.text_token match, :key
state = :label

elsif state == :label && match = scan(/:/)
encoder.text_token match, :delimiter
state = :colon

elsif state == :colon && match = scan(/[^\t\r\n]*/)
encoder.text_token match, :value
state = :value

elsif state == :value && match = scan(/\t/)
encoder.text_token match, :space
state = :tab

else
raise
end
end

encoder
end
end

end
end