Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
dgraph
Manage
Activity
Members
Labels
Plan
Issues
0
Issue boards
Milestones
Wiki
Code
Merge requests
0
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Mirror
dgraph
Commits
1d92022e
Commit
1d92022e
authored
9 years ago
by
Manish R Jain
Browse files
Options
Downloads
Patches
Plain Diff
working parser
parent
c1032e50
No related branches found
No related tags found
No related merge requests found
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
lex/lexer.go
+1
-1
1 addition, 1 deletion
lex/lexer.go
rdf/parse.go
+82
-0
82 additions, 0 deletions
rdf/parse.go
rdf/parse_test.go
+117
-0
117 additions, 0 deletions
rdf/parse_test.go
rdf/state.go
+6
-1
6 additions, 1 deletion
rdf/state.go
rdf/state_test.go
+0
-105
0 additions, 105 deletions
rdf/state_test.go
with
206 additions
and
107 deletions
lex/lexer.go
+
1
−
1
View file @
1d92022e
...
...
@@ -91,7 +91,7 @@ func (l *Lexer) Emit(t ItemType) {
"start"
:
l
.
Start
,
"pos"
:
l
.
Pos
,
"typ"
:
t
,
})
.
Info
(
"Invalid emit"
)
})
.
Debug
(
"Invalid emit"
)
return
}
l
.
Items
<-
item
{
...
...
This diff is collapsed.
Click to expand it.
rdf/parse.go
0 → 100644
+
82
−
0
View file @
1d92022e
/*
* Copyright 2015 Manish R Jain <manishrjain@gmail.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package
rdf
import
(
"fmt"
"github.com/dgraph-io/dgraph/lex"
)
type
NQuad
struct
{
Subject
string
Predicate
string
ObjectId
string
ObjectValue
interface
{}
Label
string
Language
string
}
func
stripBracketsIfPresent
(
val
string
)
string
{
if
val
[
0
]
!=
'<'
{
return
val
}
if
val
[
len
(
val
)
-
1
]
!=
'>'
{
return
val
}
return
val
[
1
:
len
(
val
)
-
1
]
}
func
Parse
(
line
string
)
(
rnq
NQuad
,
rerr
error
)
{
l
:=
lex
.
NewLexer
(
line
)
go
run
(
l
)
var
oval
string
for
item
:=
range
l
.
Items
{
if
item
.
Typ
==
itemSubject
{
rnq
.
Subject
=
stripBracketsIfPresent
(
item
.
Val
)
}
if
item
.
Typ
==
itemPredicate
{
rnq
.
Predicate
=
stripBracketsIfPresent
(
item
.
Val
)
}
if
item
.
Typ
==
itemObject
{
rnq
.
ObjectId
=
stripBracketsIfPresent
(
item
.
Val
)
}
if
item
.
Typ
==
itemLiteral
{
oval
=
item
.
Val
}
if
item
.
Typ
==
itemLanguage
{
rnq
.
Language
=
item
.
Val
}
if
item
.
Typ
==
itemObjectType
{
if
len
(
oval
)
==
0
{
glog
.
Fatalf
(
"itemObject should be emitted before itemObjectType. Input: %q"
,
line
)
}
oval
+=
"@@"
+
stripBracketsIfPresent
(
item
.
Val
)
}
}
if
len
(
oval
)
>
0
{
rnq
.
ObjectValue
=
oval
}
if
len
(
rnq
.
Subject
)
==
0
||
len
(
rnq
.
Predicate
)
==
0
{
return
rnq
,
fmt
.
Errorf
(
"Empty required fields in NQuad"
)
}
if
len
(
rnq
.
ObjectId
)
==
0
&&
rnq
.
ObjectValue
==
nil
{
return
rnq
,
fmt
.
Errorf
(
"No Object in NQuad"
)
}
return
rnq
,
nil
}
This diff is collapsed.
Click to expand it.
rdf/parse_test.go
0 → 100644
+
117
−
0
View file @
1d92022e
/*
* Copyright 2015 Manish R Jain <manishrjain@gmail.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package
rdf
import
(
"reflect"
"testing"
)
var
testNQuads
=
[]
struct
{
input
string
nq
NQuad
hasErr
bool
}{
{
input
:
`<some_subject_id> <predicate> <object_id> .`
,
nq
:
NQuad
{
Subject
:
"some_subject_id"
,
Predicate
:
"predicate"
,
ObjectId
:
"object_id"
,
ObjectValue
:
nil
,
},
},
{
input
:
`_:alice <predicate> <object_id> .`
,
nq
:
NQuad
{
Subject
:
"_:alice"
,
Predicate
:
"predicate"
,
ObjectId
:
"object_id"
,
ObjectValue
:
nil
,
},
},
{
input
:
`_:alice <follows> _:bob0 .`
,
nq
:
NQuad
{
Subject
:
"_:alice"
,
Predicate
:
"follows"
,
ObjectId
:
"_:bob0"
,
ObjectValue
:
nil
,
},
},
{
input
:
`_:alice <name> "Alice In Wonderland" .`
,
nq
:
NQuad
{
Subject
:
"_:alice"
,
Predicate
:
"name"
,
ObjectId
:
""
,
ObjectValue
:
"Alice In Wonderland"
,
},
},
{
input
:
`_:alice <name> "Alice In Wonderland"@en-0 .`
,
nq
:
NQuad
{
Subject
:
"_:alice"
,
Predicate
:
"name"
,
ObjectId
:
""
,
ObjectValue
:
"Alice In Wonderland"
,
Language
:
"en-0"
,
},
},
{
input
:
`_:alice <age> "013"^^<integer> .`
,
nq
:
NQuad
{
Subject
:
"_:alice"
,
Predicate
:
"age"
,
ObjectId
:
""
,
ObjectValue
:
"013@@integer"
,
},
},
{
input
:
`<http://www.w3.org/2001/sw/RDFCore/ntriples/> <http://purl.org/dc/terms/title> "N-Triples"@en-US .`
,
nq
:
NQuad
{
Subject
:
"http://www.w3.org/2001/sw/RDFCore/ntriples/"
,
Predicate
:
"http://purl.org/dc/terms/title"
,
ObjectId
:
""
,
ObjectValue
:
"N-Triples"
,
Language
:
"en-US"
,
},
},
{
input
:
`_:art <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://xmlns.com/foaf/0.1/Person> .`
,
nq
:
NQuad
{
Subject
:
"_:art"
,
Predicate
:
"http://www.w3.org/1999/02/22-rdf-syntax-ns#type"
,
ObjectId
:
"http://xmlns.com/foaf/0.1/Person"
,
ObjectValue
:
nil
,
},
},
}
func
TestLex
(
t
*
testing
.
T
)
{
for
_
,
test
:=
range
testNQuads
{
rnq
,
err
:=
Parse
(
test
.
input
)
if
test
.
hasErr
{
if
err
==
nil
{
t
.
Errorf
(
"Expected error for input: %q"
,
test
.
input
)
}
}
if
!
reflect
.
DeepEqual
(
rnq
,
test
.
nq
)
{
t
.
Errorf
(
"Expected %v. Got: %v"
,
test
.
nq
,
rnq
)
}
}
}
This diff is collapsed.
Click to expand it.
rdf/state.go
+
6
−
1
View file @
1d92022e
...
...
@@ -18,7 +18,10 @@
// http://www.w3.org/TR/n-quads/
package
rdf
import
"github.com/dgraph-io/dgraph/lex"
import
(
"github.com/dgraph-io/dgraph/lex"
"github.com/dgraph-io/dgraph/x"
)
const
(
itemText
lex
.
ItemType
=
5
+
iota
// plain text
...
...
@@ -38,6 +41,8 @@ const (
AT_LABEL
)
var
glog
=
x
.
Log
(
"rdf"
)
func
run
(
l
*
lex
.
Lexer
)
{
for
state
:=
lexText
;
state
!=
nil
;
{
state
=
state
(
l
)
...
...
This diff is collapsed.
Click to expand it.
rdf/state_test.go
deleted
100644 → 0
+
0
−
105
View file @
c1032e50
/*
* Copyright 2015 Manish R Jain <manishrjain@gmail.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package
rdf
import
(
"testing"
"github.com/dgraph-io/dgraph/lex"
)
var
testNQuads
=
[]
struct
{
input
string
entity
string
attr
string
valueid
string
value
interface
{}
}{
{
input
:
`<some_subject_id> <predicate> <Object_id> .`
,
entity
:
"<some_subject_id>"
,
attr
:
"<predicate>"
,
valueid
:
"<Object_id>"
,
},
{
input
:
`_:alice <predicate> <Object_id> .`
,
entity
:
"_:alice"
,
attr
:
"<predicate>"
,
valueid
:
"<Object_id>"
,
},
{
input
:
`_:alice <follows> _:bob0 .`
,
entity
:
"_:alice"
,
attr
:
"<follows>"
,
valueid
:
"_:bob0"
,
},
{
input
:
`_:alice <name> "Alice In Wonderland" .`
,
entity
:
"_:alice"
,
attr
:
"<name>"
,
valueid
:
"Alice In Wonderland"
,
},
{
input
:
`_:alice <name> "Alice In Wonderland"@en-0 .`
,
entity
:
"_:alice"
,
attr
:
"<name>"
,
valueid
:
"Alice In Wonderland"
,
},
{
input
:
`_:alice <age> "013"^^<integer> .`
,
entity
:
"_:alice"
,
attr
:
"<age>"
,
valueid
:
"Alice In Wonderland"
,
},
{
input
:
`<http://www.w3.org/2001/sw/RDFCore/ntriples/> <http://purl.org/dc/terms/title> "N-Triples"@en-US .`
,
entity
:
"<http://www.w3.org/2001/sw/RDFCore/ntriples/>"
,
attr
:
"<http://purl.org/dc/terms/title>"
,
valueid
:
"Alice In Wonderland"
,
},
}
func
TestLex
(
t
*
testing
.
T
)
{
for
_
,
test
:=
range
testNQuads
{
l
:=
lex
.
NewLexer
(
test
.
input
)
go
run
(
l
)
for
item
:=
range
l
.
Items
{
t
.
Logf
(
"Item: %v"
,
item
)
if
item
.
Typ
==
itemSubject
{
if
item
.
Val
!=
test
.
entity
{
t
.
Errorf
(
"Expected: %v. Got: %v"
,
test
.
entity
,
item
.
Val
)
}
else
{
t
.
Logf
(
"Subject matches"
)
}
}
if
item
.
Typ
==
itemPredicate
{
if
item
.
Val
!=
test
.
attr
{
t
.
Errorf
(
"Expected: %v. Got: %v"
,
test
.
attr
,
item
.
Val
)
}
else
{
t
.
Logf
(
"Predicate matches"
)
}
}
if
item
.
Typ
==
itemObject
{
if
item
.
Val
!=
test
.
valueid
{
t
.
Errorf
(
"Expected: %v. Got: %v"
,
test
.
valueid
,
item
.
Val
)
}
else
{
t
.
Logf
(
"Object matches"
)
}
}
}
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment