/
id_node.go
88 lines (71 loc) · 1.91 KB
/
id_node.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
// Copyright (c) 2017 Opsidian Ltd.
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
package conflow
import (
"fmt"
"github.com/conflowio/parsley/parsley"
"github.com/conflowio/conflow/src/schema"
)
const (
ClassifierNone = rune(0)
ClassifierAnnotation = '@'
)
// IDNode contains an identifier
type IDNode struct {
id ID
classifier rune
pos parsley.Pos
readerPos parsley.Pos
}
// NewIDNode creates a new id node
func NewIDNode(id ID, classifier rune, pos parsley.Pos, readerPos parsley.Pos) *IDNode {
if id == "" {
panic("identifier can not be empty")
}
return &IDNode{
id: id,
classifier: classifier,
pos: pos,
readerPos: readerPos,
}
}
// ID returns with the identifier string
func (i *IDNode) ID() ID {
return i.id
}
// Classifier returns with classifier of the id
// A classifier is a single rune, like '@', '$', '&'.
func (i *IDNode) Classifier() rune {
return i.classifier
}
// Token returns with the node token
func (i *IDNode) Token() string {
return "ID"
}
// Schema returns the schema for the node's value
func (i *IDNode) Schema() interface{} {
return schema.StringValue()
}
// ValueOf returns with the id of the node
func (i *IDNode) Value() interface{} {
return i.id
}
// Pos returns the position
func (i *IDNode) Pos() parsley.Pos {
return i.pos
}
// ReaderPos returns the position of the first character immediately after this node
func (i *IDNode) ReaderPos() parsley.Pos {
return i.readerPos
}
// SetReaderPos changes the reader position
func (i *IDNode) SetReaderPos(f func(parsley.Pos) parsley.Pos) {
i.readerPos = f(i.readerPos)
}
// String returns with a string representation of the node
func (i *IDNode) String() string {
return fmt.Sprintf("ID{%v, %d..%d}", i.id, i.pos, i.readerPos)
}