add memory limit

This commit is contained in:
2026-04-10 18:45:40 +03:00
parent 86b8d83643
commit a977d4d9f5
13 changed files with 662 additions and 37 deletions

View File

@@ -14,6 +14,7 @@ const (
TOKEN_FLOAT
TOKEN_INT
TOKEN_DURATION
TOKEN_SIZE
TOKEN_LBRACE
TOKEN_RBRACE
@@ -37,6 +38,8 @@ func (t TokenType) String() string {
return "INT"
case TOKEN_DURATION:
return "DURATION"
case TOKEN_SIZE:
return "SIZE"
case TOKEN_LBRACE:
return "{"
case TOKEN_RBRACE:
@@ -353,6 +356,10 @@ func (l *Lexer) readNumberOrDuration(line, col int) (Token, error) {
}
}
if sizeSuffix := l.tryReadSizeSuffix(); sizeSuffix != "" {
return Token{TOKEN_SIZE, buf.String() + sizeSuffix, line, col}, nil
}
suffix := l.tryReadDurationSuffix()
if suffix != "" {
return Token{TOKEN_DURATION, buf.String() + suffix, line, col}, nil
@@ -364,6 +371,37 @@ func (l *Lexer) readNumberOrDuration(line, col int) (Token, error) {
return Token{TOKEN_INT, buf.String(), line, col}, nil
}
// tryReadSizeSuffix reads memory size suffixes: B, K, KB, KiB, M, MB, MiB, G, GB, GiB.
// Units are case-sensitive uppercase to avoid collision with duration "m" (minutes).
func (l *Lexer) tryReadSizeSuffix() string {
ch, ok := l.peek()
if !ok {
return ""
}
var unit rune
switch ch {
case 'B':
l.advance()
return "B"
case 'K', 'M', 'G':
unit = ch
default:
return ""
}
l.advance()
var buf strings.Builder
buf.WriteRune(unit)
if next, ok := l.peek(); ok && next == 'i' {
l.advance()
buf.WriteRune('i')
}
if next, ok := l.peek(); ok && next == 'B' {
l.advance()
buf.WriteRune('B')
}
return buf.String()
}
func (l *Lexer) tryReadDurationSuffix() string {
ch, ok := l.peek()
if !ok {