From 43b2cff5b7dc2d6ba90e7314ce7fd04f803c2860 Mon Sep 17 00:00:00 2001 From: Alejandro Mery Date: Mon, 28 Aug 2023 22:56:11 +0000 Subject: [PATCH] lexer: introduce Position.Next()/Position.NextLine() factories Signed-off-by: Alejandro Mery --- lexer/position.go | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/lexer/position.go b/lexer/position.go index d350eb7..8431306 100644 --- a/lexer/position.go +++ b/lexer/position.go @@ -84,3 +84,29 @@ func (p *Position) Add(rel Position) { p.Column += rel.Column - 1 } } + +// Next returns a new Position one rune forward +// on the line +func (p Position) Next() Position { + if p.Line == 0 { + p.Reset() + } + + return Position{ + Line: p.Line, + Column: p.Column + 1, + } +} + +// NextLine returns a new Position at the beginning of the next +// line. +func (p Position) NextLine() Position { + if p.Line == 0 { + p.Reset() + } + + return Position{ + Line: p.Line + 1, + Column: 1, + } +}