⚠
The web version only has simple instructions since chapter 04, while the full book has detailed explanations and background info.
0302: Parse Values
Currently only 2 data types are implemented: int64 and string. They are distinguished by the first charactor:
func (p *Parser) parseValue(out *Cell) error {
p.skipSpaces()
if p.pos >= len(p.buf) {
return errors.New("expect value")
}
ch := p.buf[p.pos]
if ch == '"' || ch == '\'' {
return p.parseString(out)
} else if isDigit(ch) || ch == '-' || ch == '+' {
return p.parseInt(out)
} else {
return errors.New("expect value")
}
}When you implement syntax parsing later, you will see that many languages can decide how to parse next based on the first few tokens.
Parse int64
The rules for int64 are simple: it may start with - or +, followed by digits.
func (p *Parser) parseInt(out *Cell) errorParse Strings
func (p *Parser) parseString(out *Cell) errorThe rules are more complex:
- Enclosed by single or double quotes.
- Quotes inside the string must be escaped with a slash.
- A slash must also be escaped with a slash.
For example, "is\'nt" or 'is\'nt' both mean isn't.
To make this practical, more escapes are needed, like \n, \xFF, \u3412. But we have more important topics to learn, so we stop here.
ⓘ
CodeCrafters.io has similar courses in many programming languages, including build your own Redis, SQLite, Docker, etc. It’s worth checking out.