Parent directory

parser.go

610 bytes
 1package rpn
 2
 3type Input struct {
 4	characters []rune
 5	nextChar   rune
 6}
 7
 8func NewInput(input string) *Input {
 9	characters := []rune(input)
10	if len(characters) == 0 {
11		return nil
12	}
13
14	i := &Input{
15		characters: characters,
16		nextChar:   characters[0],
17	}
18	return i
19}
20
21// Eat consumes a character from the input and returns the next one.
22func (i *Input) Eat() rune {
23	if len(i.characters) == 0 {
24		return 0
25	}
26
27	i.characters = i.characters[1:]
28	if len(i.characters) == 0 {
29		i.nextChar = 0
30	} else {
31		i.nextChar = i.characters[0]
32	}
33
34	return i.nextChar
35}
36
37func (i *Input) NextChar() rune {
38	return i.nextChar
39}