Skip to content

abstract class CSV::Lexer
inherits Reference

A CSV lexer lets you consume a CSV token by token. You can use this to efficiently parse a CSV without the need to allocate intermediate arrays.

require "csv"

lexer = CSV::Lexer.new "one,two\nthree"
lexer.next_token # => CSV::Token(@kind=Cell, @value="one")
lexer.next_token # => CSV::Token(@kind=Cell, @value="two")
lexer.next_token # => CSV::Token(@kind=Newline, @value="two")
lexer.next_token # => CSV::Token(@kind=Cell, @value="three")
lexer.next_token # => CSV::Token(@kind=Eof, @value="three")

Class methods

.new(string : String, separator = DEFAULT_SEPARATOR, quote_char = DEFAULT_QUOTE_CHAR)

Creates a CSV lexer from a String.

View source

.new(io : IO, separator = DEFAULT_SEPARATOR, quote_char = DEFAULT_QUOTE_CHAR)

Creates a CSV lexer from an IO.

View source

Methods

#next_token

Returns the next Token in this CSV.

View source

#quote_char : Char

View source

#rewind

Rewinds this lexer to the beginning

View source

#separator : Char

View source

#token : Token

Returns the current Token.

View source