mirror of
https://github.com/infiniflow/ragflow.git
synced 2026-07-13 08:28:22 +08:00
CLI: Add float parsing (#13955)
### What problem does this PR solve? Add float parsing ### Type of change - [x] New Feature (non-breaking change which adds functionality) Signed-off-by: Jin Hai <haijin.chn@gmail.com>
This commit is contained in:
@@ -126,7 +126,7 @@ func (p *Parser) parseContextSearchCommand() (*Command, error) {
|
||||
if strings.ToLower(p.curToken.Value) == "n" {
|
||||
p.nextToken()
|
||||
var err error
|
||||
if p.curToken.Type != TokenNumber {
|
||||
if p.curToken.Type != TokenInteger {
|
||||
return nil, fmt.Errorf("expect number")
|
||||
}
|
||||
cmd.Params["number"], err = p.parseNumber()
|
||||
@@ -140,7 +140,7 @@ func (p *Parser) parseContextSearchCommand() (*Command, error) {
|
||||
if strings.ToLower(p.curToken.Value) == "t" {
|
||||
p.nextToken()
|
||||
var err error
|
||||
if p.curToken.Type != TokenNumber {
|
||||
if p.curToken.Type != TokenInteger {
|
||||
return nil, fmt.Errorf("expect number")
|
||||
}
|
||||
cmd.Params["threshold"], err = p.parseFloat()
|
||||
|
||||
@@ -96,8 +96,7 @@ func (l *Lexer) NextToken() Token {
|
||||
ident := l.readIdentifier()
|
||||
return l.lookupIdent(ident)
|
||||
} else if isDigit(l.ch) {
|
||||
tok.Type = TokenNumber
|
||||
tok.Value = l.readNumber()
|
||||
tok.Value, tok.Type = l.readNumber()
|
||||
return tok
|
||||
}
|
||||
|
||||
@@ -129,12 +128,25 @@ func (l *Lexer) readIdentifier() string {
|
||||
return l.input[start:l.pos]
|
||||
}
|
||||
|
||||
func (l *Lexer) readNumber() string {
|
||||
func (l *Lexer) readNumber() (string, int) {
|
||||
start := l.pos
|
||||
tokenType := TokenInteger
|
||||
|
||||
// Read integer part
|
||||
for isDigit(l.ch) {
|
||||
l.readChar()
|
||||
}
|
||||
return l.input[start:l.pos]
|
||||
|
||||
// If encountering a decimal point followed by a digit, read as float
|
||||
if l.ch == '.' && isDigit(l.peekChar()) {
|
||||
tokenType = TokenFloat
|
||||
l.readChar() // Consume '.'
|
||||
for isDigit(l.ch) {
|
||||
l.readChar()
|
||||
}
|
||||
}
|
||||
|
||||
return l.input[start:l.pos], tokenType
|
||||
}
|
||||
|
||||
func (l *Lexer) readQuotedString(quote byte) string {
|
||||
|
||||
@@ -270,14 +270,14 @@ func (p *Parser) parseIdentifier() (string, error) {
|
||||
}
|
||||
|
||||
func (p *Parser) parseNumber() (int, error) {
|
||||
if p.curToken.Type != TokenNumber {
|
||||
if p.curToken.Type != TokenInteger {
|
||||
return 0, fmt.Errorf("expected number, got %s", p.curToken.Value)
|
||||
}
|
||||
return strconv.Atoi(p.curToken.Value)
|
||||
}
|
||||
|
||||
func (p *Parser) parseFloat() (float64, error) {
|
||||
if p.curToken.Type != TokenNumber {
|
||||
if p.curToken.Type != TokenInteger {
|
||||
return math.NaN(), fmt.Errorf("expected number, got %s", p.curToken.Value)
|
||||
}
|
||||
result, err := strconv.ParseFloat(p.curToken.Value, 64)
|
||||
|
||||
@@ -126,7 +126,8 @@ const (
|
||||
// Literals
|
||||
TokenIdentifier
|
||||
TokenQuotedString
|
||||
TokenNumber
|
||||
TokenInteger
|
||||
TokenFloat
|
||||
|
||||
// Special
|
||||
TokenSemicolon
|
||||
|
||||
@@ -605,7 +605,7 @@ func (p *Parser) parseCreateIndex() (*Command, error) {
|
||||
}
|
||||
p.nextToken()
|
||||
|
||||
if p.curToken.Type != TokenNumber {
|
||||
if p.curToken.Type != TokenInteger {
|
||||
return nil, fmt.Errorf("expected vector size number, got %s", p.curToken.Value)
|
||||
}
|
||||
vectorSize, err := strconv.Atoi(p.curToken.Value)
|
||||
@@ -1922,7 +1922,7 @@ func (p *Parser) parseSearchCommand() (*Command, error) {
|
||||
if strings.ToLower(p.curToken.Value) == "n" {
|
||||
p.nextToken()
|
||||
var err error
|
||||
if p.curToken.Type != TokenNumber {
|
||||
if p.curToken.Type != TokenInteger {
|
||||
return nil, fmt.Errorf("expect number")
|
||||
}
|
||||
cmd.Params["number"], err = p.parseNumber()
|
||||
@@ -1936,7 +1936,7 @@ func (p *Parser) parseSearchCommand() (*Command, error) {
|
||||
//if strings.ToLower(p.curToken.Value) == "t" {
|
||||
// p.nextToken()
|
||||
// var err error
|
||||
// if p.curToken.Type != TokenNumber {
|
||||
// if p.curToken.Type != TokenInteger {
|
||||
// return nil, fmt.Errorf("expect number")
|
||||
// }
|
||||
// cmd.Params["threshold"], err = p.parseFloat()
|
||||
|
||||
Reference in New Issue
Block a user