v2 / vlib / toml / token / token.v
53 lines · 50 sloc · 1.38 KB · c51d30bf5309653c6b573ec815268e69a78ea8cc
Raw
1// Copyright (c) 2021 Lars Pontoppidan. 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
6// Token holds information about the current scan of bytes.
7pub struct Token {
8pub:
9 kind Kind // the token number/enum; for quick comparisons
10 lit string // literal representation of the token
11 col int // the column in the source where the token occurred
12 line_nr int // the line number in the source where the token occurred
13 pos int // the position of the token in scanner text
14 len int // length of the literal
15}
16
17// Kind represents a logical type of entity found in any given TOML document.
18pub enum Kind {
19 unknown
20 eof
21 bare // user
22 boolean // true or false
23 number // 123
24 quoted // 'foo', "foo", """foo""" or '''foo'''
25 plus // +
26 minus // -
27 underscore // _
28 comma // ,
29 colon // :
30 hash // # comment
31 assign // =
32 lcbr // {
33 rcbr // }
34 lsbr // [
35 rsbr // ]
36 nl // \n linefeed / newline character
37 cr // \r carriage return
38 tab // \t character
39 whitespace // ` `
40 period // .
41 _end_
42}
43
44// pos returns the exact position of a token in the input.
45@[inline]
46pub fn (tok &Token) pos() Pos {
47 return Pos{
48 len: tok.len
49 line_nr: tok.line_nr - 1
50 pos: tok.pos
51 col: tok.col - 1
52 }
53}
54