v2 / vlib / v / token / pos.v
55 lines · 48 sloc · 1.34 KB · 144b57c4b79e69409e15dbcdb5d7e4161c954e0d
Raw
1// Copyright (c) 2019-2024 Alexander Medvednikov. All rights reserved.
2// Use of this source code is governed by an MIT license
3// that can be found in the LICENSE file.
4module token
5
6pub struct Pos {
7pub:
8 len int // length of the literal in the source
9 line_nr int // the line number in the source where the token occurred
10 pos int // the position of the token in scanner text
11 col u16 // the column in the source where the token occurred
12 file_idx i16 = -1 // file idx in the global table `filelist`
13pub mut:
14 last_line int // the line number where the ast object ends (used by vfmt)
15}
16
17@[unsafe]
18pub fn (mut p Pos) free() {
19}
20
21pub fn (p Pos) line_str() string {
22 return '{l: ${p.line_nr + 1:5}, c: ${p.col:3}, p: ${p.pos:5}, ll: ${p.last_line + 1:5}}'
23}
24
25pub fn (pos Pos) extend(end Pos) Pos {
26 return Pos{
27 ...pos
28 len: end.pos - pos.pos + end.len
29 last_line: end.last_line
30 }
31}
32
33pub fn (pos Pos) extend_with_last_line(end Pos, last_line int) Pos {
34 return Pos{
35 ...pos
36 len: end.pos - pos.pos + end.len
37 last_line: last_line - 1
38 }
39}
40
41pub fn (mut pos Pos) update_last_line(last_line int) {
42 pos.last_line = last_line - 1
43}
44
45@[inline]
46pub fn (tok &Token) pos() Pos {
47 return Pos{
48 file_idx: tok.file_idx
49 len: tok.len
50 line_nr: tok.line_nr - 1
51 pos: tok.pos
52 last_line: tok.line_nr - 1
53 col: tok.col - 1
54 }
55}
56