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) {
if item.Typ == itemValidEnd {
vend = true
}
if item.Typ == itemLabel {
rnq.Label = stripBracketsIfPresent(item.Val)
}
}
if !vend {
return rnq, fmt.Errorf("Invalid end of input")
......
......@@ -150,6 +150,32 @@ var testNQuads = []struct {
},
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) {
......
......@@ -71,6 +71,11 @@ Loop:
l.Emit(itemText)
return lexObject
} else if l.Depth == AT_LABEL {
l.Backup()
l.Emit(itemText)
return lexLabel
} else {
return l.Errorf("Invalid input: %v at lexText", r)
}
......@@ -133,7 +138,6 @@ func lexBlankNode(l *lex.Lexer, styp lex.ItemType,
}
if isSpace(r) {
l.Emit(styp)
l.Depth += 1
return sfn
}
return l.Errorf("Invalid character %v found for itemType: %v", r, styp)
......@@ -147,6 +151,7 @@ func lexSubject(l *lex.Lexer) lex.StateFn {
}
if r == '_' {
l.Depth += 1
return lexBlankNode(l, itemSubject, lexText)
}
......@@ -221,6 +226,7 @@ func lexObject(l *lex.Lexer) lex.StateFn {
return lexUntilClosing(l, itemObject, lexText)
}
if r == '_' {
l.Depth += 1
return lexBlankNode(l, itemObject, lexText)
}
if r == '"' {
......@@ -231,6 +237,19 @@ func lexObject(l *lex.Lexer) lex.StateFn {
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 {
return r == '>'
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment