add memory limit
This commit is contained in:
38
dsl/lexer.go
38
dsl/lexer.go
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user