blob: d50eb36868a8df6c17e639fb54375146bd0b5d12 [file] [log] [blame]
Sketch🕴️d04f3622026-02-28 19:09:06 +04001package token
2
3import "fmt"
4
5// Type represents the type of a lexical token.
6type Type int
7
8const (
9 Number Type = iota // numeric literal
10 Plus // +
11 Minus // -
12 Star // *
13 Slash // /
14 LParen // (
15 RParen // )
16 EOF // end of input
17)
18
19// String returns a human-readable name for the token type.
20func (t Type) String() string {
21 switch t {
22 case Number:
23 return "Number"
24 case Plus:
25 return "+"
26 case Minus:
27 return "-"
28 case Star:
29 return "*"
30 case Slash:
31 return "/"
32 case LParen:
33 return "("
34 case RParen:
35 return ")"
36 case EOF:
37 return "EOF"
38 default:
39 return fmt.Sprintf("Unknown(%d)", int(t))
40 }
41}
42
43// Token represents a single lexical token.
44type Token struct {
45 Type Type // the kind of token
46 Literal string // raw text (e.g. "3.14", "+")
47 Pos int // byte offset in input string
48}