add float support to scanner/parser for arguments

This commit is contained in:
Matt Jaffee 2017-03-06 16:28:59 -06:00
parent 3d8be89775
commit dbfc6052b7
5 changed files with 34 additions and 2 deletions

View file

@ -174,6 +174,12 @@ func (p *Parser) parseArgs() (map[string]interface{}, error) {
return nil, err
}
value = v
case FLOAT:
v, err := strconv.ParseFloat(lit, 64)
if err != nil {
return nil, err
}
value = v
case LBRACK:
v, err := p.parseList()
if err != nil {

View file

@ -80,6 +80,24 @@ func TestParser_Parse(t *testing.T) {
}
})
// Parse with float arguments.
t.Run("WithFloatArgs", func(t *testing.T) {
q, err := pql.ParseString(`MyCall( key=12.25, foo= 13.167)`)
if err != nil {
t.Fatal(err)
} else if !reflect.DeepEqual(q.Calls[0],
&pql.Call{
Name: "MyCall",
Args: map[string]interface{}{
"key": 12.25,
"foo": 13.167,
},
},
) {
t.Fatalf("unexpected call: %#v", q.Calls[0])
}
})
// Parse with both child calls and arguments.
t.Run("ChildrenAndArguments", func(t *testing.T) {
q, err := pql.ParseString(`TopN(Bitmap(id=100, frame=other), frame=f, n=3)`)

View file

@ -143,17 +143,23 @@ func (s *Scanner) scanIdent() (tok Token, pos Pos, lit string) {
// scanNumber consumes consecutive integer digits.
func (s *Scanner) scanNumber() (tok Token, pos Pos, lit string) {
pos = s.pos
tok = NUMBER
var buf bytes.Buffer
var seenDot bool
for {
ch := s.read()
if !isDigit(ch) {
if !isDigit(ch) && (seenDot || ch != '.') {
s.unread()
break
}
if ch == '.' {
seenDot = true
tok = FLOAT
}
buf.WriteRune(ch)
}
return NUMBER, pos, buf.String()
return tok, pos, buf.String()
}
// scanString consumes a single-quoted or double-quoted string.

View file

@ -30,6 +30,7 @@ func TestScanner_Scan(t *testing.T) {
{s: `foo`, tok: pql.IDENT, lit: `foo`},
{s: `100`, tok: pql.NUMBER, lit: `100`},
{s: `100.3`, tok: pql.FLOAT, lit: `100.3`},
{s: `all`, tok: pql.ALL, lit: `all`},
{s: `ALL`, tok: pql.ALL, lit: `ALL`}, // case insensitive

View file

@ -16,6 +16,7 @@ const (
STRING // "foo"
BADSTRING // bad escape or unclosed string
NUMBER // 12345
FLOAT // 100.2
literal_end
keyword_beg