~tristan957/monkey

843b339ba6639ba081f106982e9660f6efafb793 — Tristan Partin 6 months ago f67a4eb
Add ability to create lexer from io.Reader
1 files changed, 18 insertions(+), 0 deletions(-)

M lexer/lexer.go
M lexer/lexer.go => lexer/lexer.go +18 -0
@@ 19,6 19,24 @@ type Lexer struct {
	ch           rune
}

// NewFromReader creates a new Lexer from an io.Reader implementation.
func NewFromReader(input io.Reader) *Lexer {
	reader := bufio.NewReader(input)
	l := &Lexer{
		input: reader,
		currPosition: &token.Position{
			Line:   1,
			Column: 1,
		},
		nextPosition: &token.Position{
			Line:   1,
			Column: 1,
		},
	}

	return l
}

// NewFromString creates a new Lexer when given a string input.
func NewFromString(input string) *Lexer {
	reader := bufio.NewReader(strings.NewReader(input))