From df2ad667d852c0856908b4c144effa2e399dc7e7 Mon Sep 17 00:00:00 2001 From: Alec Thomas Date: Sat, 9 Sep 2023 20:09:32 +1000 Subject: [PATCH] feat: add Position.Add() Useful for sub-parsers. --- bin/.jq-1.7.pkg | 1 - bin/jq | 1 - lexer/api.go | 14 ++++++++++++++ 3 files changed, 14 insertions(+), 2 deletions(-) delete mode 120000 bin/.jq-1.7.pkg delete mode 120000 bin/jq diff --git a/bin/.jq-1.7.pkg b/bin/.jq-1.7.pkg deleted file mode 120000 index 383f4511..00000000 --- a/bin/.jq-1.7.pkg +++ /dev/null @@ -1 +0,0 @@ -hermit \ No newline at end of file diff --git a/bin/jq b/bin/jq deleted file mode 120000 index f3d25875..00000000 --- a/bin/jq +++ /dev/null @@ -1 +0,0 @@ -.jq-1.7.pkg \ No newline at end of file diff --git a/lexer/api.go b/lexer/api.go index 5e732f5c..2784dd87 100644 --- a/lexer/api.go +++ b/lexer/api.go @@ -114,6 +114,20 @@ func (p *Position) Advance(span string) { } } +// Add returns a new Position that is the sum of this position and "pos". +// +// This is useful when parsing values from a parent grammar. +func (p Position) Add(pos Position) Position { + p.Line += pos.Line - 1 + if pos.Line > 1 { + p.Column = pos.Column + } else { + p.Column += pos.Column - 1 + } + p.Offset += pos.Offset + return p +} + func (p Position) GoString() string { return fmt.Sprintf("Position{Filename: %q, Offset: %d, Line: %d, Column: %d}", p.Filename, p.Offset, p.Line, p.Column)