-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathother_test.go
80 lines (70 loc) · 2.31 KB
/
other_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
// SPDX-FileCopyrightText: 2021 Iwan Aucamp
//
// SPDX-License-Identifier: CC0-1.0 OR Apache-2.0
package rdf4go
import (
"fmt"
"os"
"testing"
"github.com/antlr/antlr4/runtime/Go/antlr"
"github.com/stretchr/testify/assert"
ntp "gobn.github.io/rdf4go/internal/nt_parser"
"gobn.github.io/rdf4go/internal/utils"
)
var (
svf = SimpleValueFactory{}
)
type TreeShapeListener struct {
*ntp.BaseNTListener
}
func NewTreeShapeListener() *TreeShapeListener {
return new(TreeShapeListener)
}
func (t *TreeShapeListener) EnterEveryRule(ctx antlr.ParserRuleContext) {
fmt.Fprintf(os.Stderr, "%s\n", ctx.GetText())
}
func (t *TreeShapeListener) EnterSubject(ctx *ntp.SubjectContext) {
fmt.Fprintf(os.Stderr, "subject = %s\n", ctx.GetText())
if iriT := ctx.GetToken(ntp.NTLexerIRIREF, 0); iriT != nil {
t := iriT.GetText()
iri, err := svf.NewIRI(t[1 : len(t)-1])
if err != nil {
utils.Flog().Errorf("err = %v", err)
}
fmt.Fprintf(os.Stderr, "subject.iri = %v -> %v\n", iriT, iri)
}
}
func (t *TreeShapeListener) EnterPredicate(ctx *ntp.SubjectContext) {
fmt.Fprintf(os.Stderr, "subject = %s\n", ctx.GetText())
if iriT := ctx.GetToken(ntp.NTLexerIRIREF, 0); iriT != nil {
t := iriT.GetText()
iri, err := svf.NewIRI(t[1 : len(t)-1])
if err != nil {
utils.Flog().Errorf("err = %v", err)
}
fmt.Fprintf(os.Stderr, "subject.iri = %v -> %v\n", iriT, iri)
}
if bnodeT := ctx.GetToken(ntp.NTLexerBLANK_NODE_LABEL, 0); bnodeT != nil {
bnode, err := svf.NewBNode(bnodeT.GetText()[2:])
if err != nil {
utils.Flog().Errorf("err = %v", err)
}
fmt.Fprintf(os.Stderr, "subject.bnode = %v -> %v\n", bnodeT, bnode)
}
}
func TestNTParser(t *testing.T) {
// https://github.com/antlr/antlr4/blob/master/doc/go-target.md
input := antlr.NewInputStream(
`<http://one.example/subject1> <http://one.example/predicate1> <http://one.example/object1> . # comments here
# or on a line by themselves
_:subject1 <http://an.example/predicate1> "object1" .
_:subject2 <http://an.example/predicate2> "object2" .`)
lexer := ntp.NewNTLexer(input)
stream := antlr.NewCommonTokenStream(lexer, 0)
parser := ntp.NewNTParser(stream)
parser.AddErrorListener(antlr.NewDiagnosticErrorListener(true))
parser.BuildParseTrees = true
tree := parser.NtriplesDoc()
antlr.ParseTreeWalkerDefault.Walk(NewTreeShapeListener(), tree)
assert.Equal(t, 1, 1)
}