/
baseTokens.go
113 lines (96 loc) · 3.58 KB
/
baseTokens.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
// 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"
)
// tokenURI represent a RDF URI
type tokenURI struct {
value string
}
// newTokenURI creates a new tokenURI
func newTokenURI(value string) *tokenURI {
return &tokenURI{value}
}
// Interpret evaluate the token & produce an action.
// In the case of a tokenURI, it push a URI on top of the stack
func (t tokenURI) Interpret(nodeStack *stack, prefixes *map[string]string, out chan rdf.Triple) error {
nodeStack.Push(rdf.NewURI(t.value))
return nil
}
// tokenLiteral represent a RDF Literal
type tokenLiteral struct {
value string
}
// newTokenLiteral creates a new tokenLiteral
func newTokenLiteral(value string) *tokenLiteral {
return &tokenLiteral{value}
}
// Interpret evaluate the token & produce an action.
// In the case of a tokenLiteral, it push a Literal on top of the stack
func (t tokenLiteral) Interpret(nodeStack *stack, prefixes *map[string]string, out chan rdf.Triple) error {
nodeStack.Push(rdf.NewLiteral(t.value))
return nil
}
// tokenType represent a type for a RDF Literal
type tokenType struct {
value string
*tokenPosition
}
// newTokenType creates a new tokenType.
// Since this token can produce an error, its position is needed for a better error handling
func newTokenType(value string, line int, row int) *tokenType {
return &tokenType{value, newTokenPosition(line, row)}
}
// Interpret evaluate the token & produce an action.
// In the case of a tokenType, it push a typed Literal on top of the stack
func (t tokenType) Interpret(nodeStack *stack, prefixes *map[string]string, out chan rdf.Triple) error {
if nodeStack.Len() < 1 {
return errors.New("encountered a malformed literal at " + t.position())
}
literal, isLiteral := nodeStack.Pop().(rdf.Literal)
if !isLiteral {
return errors.New("A XML type can only be associated with a RDF Literal, at " + t.position())
}
nodeStack.Push(rdf.NewTypedLiteral(literal.Value, t.value))
return nil
}
// tokenLang represent a localization information about a RDF Literal
type tokenLang struct {
value string
*tokenPosition
}
// newTokenLang creates a new tokenLang.
// Since this token can produce an error, its position is needed for a better error handling
func newTokenLang(value string, line int, row int) *tokenLang {
return &tokenLang{value, newTokenPosition(line, row)}
}
// Interpret evaluate the token & produce an action.
// In the case of a tokenLang, it push a Literal with its associated language on top of the stack
func (t tokenLang) Interpret(nodeStack *stack, prefixes *map[string]string, out chan rdf.Triple) error {
if nodeStack.Len() < 1 {
return errors.New("encountered a malformed literal at " + t.position())
}
literal, isLiteral := nodeStack.Pop().(rdf.Literal)
if !isLiteral {
return errors.New("A localization information can only be associated with a RDF Literal, at " + t.position())
}
nodeStack.Push(rdf.NewLangLiteral(literal.Value, t.value))
return nil
}
// tokenBlankNode represent a RDF Blank Node
type tokenBlankNode struct {
value string
}
// newTokenBlankNode creates a new tokenBlankNode
func newTokenBlankNode(value string) *tokenBlankNode {
return &tokenBlankNode{value}
}
// Interpret evaluate the token & produce an action.
// In the case of a tokenBlankNode, it push a Blank Node on top of the stack
func (t tokenBlankNode) Interpret(nodeStack *stack, prefixes *map[string]string, out chan rdf.Triple) error {
nodeStack.Push(rdf.NewBlankNode(t.value))
return nil
}