/
turtleTokens.go
53 lines (45 loc) · 1.55 KB
/
turtleTokens.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
// Copyright (c) 2016 Thomas Minier. All rights reserved.
// Use of this source code is governed by a MIT License
// license that can be found in the LICENSE file.
package parser
import (
"errors"
"github.com/Callidon/joseki/rdf"
"strings"
)
// tokenPrefixedURI represent a prefixed RDF URI
type tokenPrefixedURI struct {
value string
*tokenPosition
}
// newTokenPrefixedURI creates a new tokenPrefixedURI
func newTokenPrefixedURI(value string, line int, row int) *tokenPrefixedURI {
return &tokenPrefixedURI{value, newTokenPosition(line, row)}
}
// Interpret evaluate the token & produce an action.
// In the case of a tokenPrefixedURI, it push a URI to the stack
func (t tokenPrefixedURI) Interpret(nodeStack *stack, prefixes *map[string]string, out chan rdf.Triple) error {
sepIndex := strings.Index(t.value, ":")
prefixValue := string(t.value[0:sepIndex])
prefixURI, inPrefixes := (*prefixes)[prefixValue]
if !inPrefixes {
return errors.New("unkown prefix " + prefixValue + " at " + t.position())
}
nodeStack.Push(rdf.NewURI(prefixURI + t.value[sepIndex+1:]))
return nil
}
// tokenPrefix represent a prefix
type tokenPrefix struct {
name string
value string
}
// newTokenPrefix creates a new tokenPrefix
func newTokenPrefix(name, value string) *tokenPrefix {
return &tokenPrefix{name, value}
}
// Interpret evaluate the token & produce an action.
// In the case of a tokenPrefix, it register a new prefix
func (t tokenPrefix) Interpret(nodeStack *stack, prefixes *map[string]string, out chan rdf.Triple) error {
(*prefixes)[t.name] = t.value
return nil
}