Skip to content
Snippets Groups Projects
Commit 886ff32c authored by Manish R Jain's avatar Manish R Jain
Browse files

also lex and parse labels

parent 00e88fea
No related branches found
No related tags found
No related merge requests found
...@@ -76,6 +76,9 @@ func Parse(line string) (rnq NQuad, rerr error) { ...@@ -76,6 +76,9 @@ func Parse(line string) (rnq NQuad, rerr error) {
if item.Typ == itemValidEnd { if item.Typ == itemValidEnd {
vend = true vend = true
} }
if item.Typ == itemLabel {
rnq.Label = stripBracketsIfPresent(item.Val)
}
} }
if !vend { if !vend {
return rnq, fmt.Errorf("Invalid end of input") return rnq, fmt.Errorf("Invalid end of input")
......
...@@ -150,6 +150,32 @@ var testNQuads = []struct { ...@@ -150,6 +150,32 @@ var testNQuads = []struct {
}, },
hasErr: false, hasErr: false,
}, },
{
input: `_:alice <knows> "stuff"^^<xs:string> <label> .`,
nq: NQuad{
Subject: "_:alice",
Predicate: "knows",
ObjectId: "",
ObjectValue: "stuff@@xs:string",
Label: "label",
},
hasErr: false,
},
{
input: `_:alice <knows> "stuff"^^<xs:string> _:label .`,
nq: NQuad{
Subject: "_:alice",
Predicate: "knows",
ObjectId: "",
ObjectValue: "stuff@@xs:string",
Label: "_:label",
},
hasErr: false,
},
{
input: `_:alice <knows> "stuff"^^<xs:string> "label" .`,
hasErr: true,
},
} }
func TestLex(t *testing.T) { func TestLex(t *testing.T) {
......
...@@ -71,6 +71,11 @@ Loop: ...@@ -71,6 +71,11 @@ Loop:
l.Emit(itemText) l.Emit(itemText)
return lexObject return lexObject
} else if l.Depth == AT_LABEL {
l.Backup()
l.Emit(itemText)
return lexLabel
} else { } else {
return l.Errorf("Invalid input: %v at lexText", r) return l.Errorf("Invalid input: %v at lexText", r)
} }
...@@ -133,7 +138,6 @@ func lexBlankNode(l *lex.Lexer, styp lex.ItemType, ...@@ -133,7 +138,6 @@ func lexBlankNode(l *lex.Lexer, styp lex.ItemType,
} }
if isSpace(r) { if isSpace(r) {
l.Emit(styp) l.Emit(styp)
l.Depth += 1
return sfn return sfn
} }
return l.Errorf("Invalid character %v found for itemType: %v", r, styp) return l.Errorf("Invalid character %v found for itemType: %v", r, styp)
...@@ -147,6 +151,7 @@ func lexSubject(l *lex.Lexer) lex.StateFn { ...@@ -147,6 +151,7 @@ func lexSubject(l *lex.Lexer) lex.StateFn {
} }
if r == '_' { if r == '_' {
l.Depth += 1
return lexBlankNode(l, itemSubject, lexText) return lexBlankNode(l, itemSubject, lexText)
} }
...@@ -221,6 +226,7 @@ func lexObject(l *lex.Lexer) lex.StateFn { ...@@ -221,6 +226,7 @@ func lexObject(l *lex.Lexer) lex.StateFn {
return lexUntilClosing(l, itemObject, lexText) return lexUntilClosing(l, itemObject, lexText)
} }
if r == '_' { if r == '_' {
l.Depth += 1
return lexBlankNode(l, itemObject, lexText) return lexBlankNode(l, itemObject, lexText)
} }
if r == '"' { if r == '"' {
...@@ -231,6 +237,19 @@ func lexObject(l *lex.Lexer) lex.StateFn { ...@@ -231,6 +237,19 @@ func lexObject(l *lex.Lexer) lex.StateFn {
return l.Errorf("Invalid char: %v at lexObject", r) return l.Errorf("Invalid char: %v at lexObject", r)
} }
func lexLabel(l *lex.Lexer) lex.StateFn {
r := l.Next()
if r == '<' {
l.Depth += 1
return lexUntilClosing(l, itemLabel, lexText)
}
if r == '_' {
l.Depth += 1
return lexBlankNode(l, itemLabel, lexText)
}
return l.Errorf("Invalid char: %v at lexLabel", r)
}
func isClosingBracket(r rune) bool { func isClosingBracket(r rune) bool {
return r == '>' return r == '>'
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment