-
Notifications
You must be signed in to change notification settings - Fork 2
/
tag.go
238 lines (213 loc) · 5.07 KB
/
tag.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
package plc
import (
"bytes"
"fmt"
"strconv"
"strings"
"unicode"
)
// TagWithIndex provides the fully qualified tag for the given index of an array.
func TagWithIndex(name string, index int) string {
// Array tags can be read by adding the index to the string, e.g. "EXAMPLE[0]"
// Perhaps this should have error checking on index<0.
return fmt.Sprintf("%s[%d]", name, index)
}
type Tag struct {
Name string
TagType uint16
ElementSize uint16
Dimensions []int
}
func (tag Tag) String() string {
name := fmt.Sprintf("%s{%04X}", tag.Name, int(tag.TagType))
if len(tag.Dimensions) == 0 {
return name
}
strs := make([]string, len(tag.Dimensions))
for i, v := range tag.Dimensions {
strs[i] = strconv.Itoa(v)
}
return name + "[" + strings.Join(strs, ",") + "]"
}
func (tag Tag) ElemCount() int {
count := 1
for _, dim := range tag.Dimensions {
if dim != 0 {
count *= dim
}
}
return count
}
// ParseQualifiedTagName consumes a tag name containing
// zero or more qualifications (ie. a field name or an
// array index) and splits them into their respresentative
// parts.
//
// From libplctag (we are ignoring bit_seg)
/*
* The EBNF is:
*
* tag ::= SYMBOLIC_SEG ( tag_seg )* ( bit_seg )?
*
* tag_seg ::= '.' SYMBOLIC_SEG
* '[' array_seg ']'
*
* bit_seg ::= '.' [0-9]+
*
* array_seg ::= NUMERIC_SEQ ( ',' NUMERIC_SEQ )*
*
* SYMBOLIC_SEG ::= [a-zA-Z]([a-zA-Z0-9_]*)
*
* NUMERIC_SEG ::= [0-9]+
*
*/
func ParseQualifiedTagName(qtn string) ([]string, error) {
var ret []string
i := 0
alpha := "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
num := "0123456789"
alphanum := alpha + num + "_"
/* SYMBOLIC_SEG := [a-zA-Z][a-zA-Z0-9_]* */
parseSymbolicSegment := func() error {
begin := i
/* [a-zA-Z] */
if i >= len(qtn) {
return fmt.Errorf("Expected alphabetic character")
}
if unicode.IsSpace(rune(qtn[i])) {
return fmt.Errorf("Expected alphabetic character, got whitespace")
}
if !bytes.ContainsAny([]byte{qtn[i]}, alpha) {
return fmt.Errorf("symbolic sequence begins with a non-alphabetic character '%c'", qtn[i])
}
/* [a-zA-Z0-9_]* */
for ; i < len(qtn); i++ {
if unicode.IsSpace(rune(qtn[i])) {
return fmt.Errorf("Expected alphanumeric character, got whitespace")
}
if !bytes.ContainsAny([]byte{qtn[i]}, alphanum) {
break
}
}
ret = append(ret, qtn[begin:i])
return nil
}
/* NUMERIC_SEG := [:space:]* [0-9]+ [:space:]* */
parseNumericSegment := func() error {
var begin, end int
/* [:space:]* */
if i >= len(qtn) {
return fmt.Errorf("expected number")
}
for ; i < len(qtn); i++ {
if !unicode.IsSpace(rune(qtn[i])) {
break
}
}
/* [0-9]+ */
begin = i
/* [0-9] */
if i >= len(qtn) {
return fmt.Errorf("expected number")
}
if !bytes.ContainsAny([]byte{qtn[i]}, num) {
return fmt.Errorf("Expected digit, got '%c'", qtn[i])
}
i++
/* [0-9]* */
for ; i < len(qtn); i++ {
if unicode.IsSpace(rune(qtn[i])) {
break
}
if !bytes.ContainsAny([]byte{qtn[i]}, num) {
break
}
}
end = i
/* [:space:]* */
for ; i < len(qtn); i++ {
if !unicode.IsSpace(rune(qtn[i])) {
break
}
}
asUint64, err := strconv.ParseUint(qtn[begin:end], 10, 32)
if err != nil {
return fmt.Errorf("Invalid array index '%s'", qtn[begin:end])
}
ret = append(ret, fmt.Sprintf("%d", asUint64))
return nil
}
/* array_seg ::= numeric_seg ( ',' numeric_seg )* */
parseArraySegment := func() error {
if err := parseNumericSegment(); err != nil {
return err
}
for i < len(qtn) {
if qtn[i] != ',' {
return nil
}
i++
if err := parseNumericSegment(); err != nil {
return err
}
}
return nil
}
/* tag_seg ::= '.' SYMBOLIC_SEG | '[' array_seg ']' */
parseTagSegment := func() error {
if i >= len(qtn) {
return fmt.Errorf("expected '.' or '['")
}
switch qtn[i] {
case '.':
i++
return parseSymbolicSegment()
case '[':
i++
if err := parseArraySegment(); err != nil {
return err
}
if i >= len(qtn) {
return fmt.Errorf("expected ']'")
}
if qtn[i] != ']' {
return fmt.Errorf("expected ']'; got '%c'", qtn[i])
}
i++
default:
return fmt.Errorf("expected '.' or '['; got '%c'", qtn[i])
}
return nil
}
/* If the tag begins with "Program:", drop that prefix; we will append it
* to the starting symbolic segment before returning. */
var hadProgramPrefix bool
if strings.HasPrefix(qtn, "Program:") {
hadProgramPrefix = true
qtn = strings.TrimPrefix(qtn, "Program:")
}
/* Check position-independent invariants: the tagname must be nonempty and
* must only contain alphanumeric characters.
*/
if qtn == "" {
return nil, fmt.Errorf("Empty tagname")
}
for i, c := range qtn {
if c > unicode.MaxASCII {
return nil, fmt.Errorf("Non-ASCII character (codepoint %d) at index %d", int(c), i)
}
}
/* tag ::= SYMBOLIC_SEG ( tag_seg )* */
if err := parseSymbolicSegment(); err != nil {
return nil, err
}
for i < len(qtn) {
if err := parseTagSegment(); err != nil {
return nil, err
}
}
if hadProgramPrefix {
ret[0] = "Program:" + ret[0]
}
return ret, nil
}