blob: 2c9f5d33f72af7de50f5d609181b9202f84bc708 [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 // )
Sketch🕴️c3ec07d2026-02-28 20:35:06 +040016 Ident // identifier (e.g. "f", "x")
17 Comma // ,
18 Equals // =
Sketch🕴️d04f3622026-02-28 19:09:06 +040019 EOF // end of input
20)
21
22// String returns a human-readable name for the token type.
23func (t Type) String() string {
24 switch t {
25 case Number:
26 return "Number"
27 case Plus:
28 return "+"
29 case Minus:
30 return "-"
31 case Star:
32 return "*"
33 case Slash:
34 return "/"
35 case LParen:
36 return "("
37 case RParen:
38 return ")"
Sketch🕴️c3ec07d2026-02-28 20:35:06 +040039 case Ident:
40 return "Ident"
41 case Comma:
42 return ","
43 case Equals:
44 return "="
Sketch🕴️d04f3622026-02-28 19:09:06 +040045 case EOF:
46 return "EOF"
47 default:
48 return fmt.Sprintf("Unknown(%d)", int(t))
49 }
50}
51
52// Token represents a single lexical token.
53type Token struct {
54 Type Type // the kind of token
55 Literal string // raw text (e.g. "3.14", "+")
56 Pos int // byte offset in input string
57}