@@ -26,23 +26,24 @@ import (
26
26
27
27
// The constants represent different types of lexed Items possible for an rdf N-Quad.
28
28
const (
29
- itemText lex.ItemType = 5 + iota // plain text
30
- itemSubject // subject, 6
31
- itemPredicate // predicate, 7
32
- itemObject // object, 8
33
- itemLabel // label, 9
34
- itemLiteral // literal, 10
35
- itemLanguage // language, 11
36
- itemObjectType // object type, 12
37
- itemValidEnd // end with dot, 13
38
- itemComment // comment, 14
39
- itemComma // comma, 15
40
- itemEqual // equal, 16
41
- itemLeftRound // '(', 17
42
- itemRightRound // ')', 18
43
- itemStar // *, 19
44
- itemVarKeyword // var, 20
45
- itemVarName // 21
29
+ itemText lex.ItemType = 5 + iota // plain text
30
+ itemSubject // subject, 6
31
+ itemPredicate // predicate, 7
32
+ itemObject // object, 8
33
+ itemLabel // label, 9
34
+ itemLiteral // literal, 10
35
+ itemLanguage // language, 11
36
+ itemObjectType // object type, 12
37
+ itemValidEnd // end with dot, 13
38
+ itemComment // comment, 14
39
+ itemComma // comma, 15
40
+ itemEqual // equal, 16
41
+ itemLeftRound // '(', 17
42
+ itemRightRound // ')', 18
43
+ itemStar // *, 19
44
+ itemSubjectFunc // uid, 20
45
+ itemObjectFunc // uid, 21
46
+ itemVarName // 22
46
47
)
47
48
48
49
// These constants keep a track of the depth while parsing an rdf N-Quad.
@@ -137,6 +138,7 @@ func lexText(l *lex.Lexer) lex.StateFn {
137
138
l .Depth = atSubject
138
139
}
139
140
141
+ // TODO(Aman): add support for more functions here.
140
142
case r == 'u' :
141
143
if l .Depth != atSubject && l .Depth != atObject {
142
144
return l .Errorf ("Unexpected char 'u'" )
@@ -422,40 +424,39 @@ func lexComment(l *lex.Lexer) lex.StateFn {
422
424
func lexVariable (l * lex.Lexer ) lex.StateFn {
423
425
var r rune
424
426
427
+ // TODO(Aman): add support for more functions here.
425
428
for _ , c := range "uid" {
426
429
if r = l .Next (); r != c {
427
- return l .Errorf ("Unexpected char '%c' when parsing var keyword" , r )
430
+ return l .Errorf ("Unexpected char '%c' when parsing uid keyword" , r )
428
431
}
429
432
}
430
- l .Emit (itemVarKeyword )
433
+ if l .Depth == atObject {
434
+ l .Emit (itemObjectFunc )
435
+ } else if l .Depth == atSubject {
436
+ l .Emit (itemSubjectFunc )
437
+ }
431
438
l .IgnoreRun (isSpace )
432
439
433
440
if r = l .Next (); r != '(' {
434
- return l .Errorf ("Expected '(' after var keyword, found: '%c'" , r )
441
+ return l .Errorf ("Expected '(' after uid keyword, found: '%c'" , r )
435
442
}
436
443
l .Emit (itemLeftRound )
437
-
438
444
l .IgnoreRun (isSpace )
439
445
440
- for {
441
- r := l .Next ()
442
- if r == lex .EOF {
443
- return l .Errorf ("Unexpected end of input while reading variable name." )
444
- }
445
- if r == ')' {
446
- l .Backup ()
447
- break
448
- }
449
- if isSpace (r ) {
450
- break
451
- }
446
+ // TODO(Aman): we support all characters in variable names except space and
447
+ // right bracket. we should support only limited characters in variable names.
448
+ // For now, this is fine because variables names must be used once in query
449
+ // block before they can be used here. And, we throw an error if number of
450
+ // used variables are different than number of defined variables.
451
+ acceptVar := func (r rune ) bool { return ! (isSpace (r ) || r == ')' ) }
452
+ if _ , valid := l .AcceptRun (acceptVar ); ! valid {
453
+ return l .Errorf ("Unexpected end of input while reading variable name" )
452
454
}
453
455
l .Emit (itemVarName )
454
-
455
456
l .IgnoreRun (isSpace )
456
457
457
458
if r = l .Next (); r != ')' {
458
- return l .Errorf ("Expected ')' while reading var , found: '%c'" , r )
459
+ return l .Errorf ("Expected ')' while reading uid func , found: '%c'" , r )
459
460
}
460
461
l .Emit (itemRightRound )
461
462
l .Depth ++
0 commit comments