forked from ZebraDevs/rosgo
/
parser.go
198 lines (177 loc) · 6.09 KB
/
parser.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
package main
import (
"fmt"
"strconv"
"strings"
"unicode"
)
const (
// Sep represents a package seperator in ROS message
Sep = "/"
// MsgDir defines directory name inside package that contains ROS message files
MsgDir = "msg"
// SrvDir defines directory name inside package that contains ROS service files
SrvDir = "srv"
// ExtMsg defines ROS message definition file extension
ExtMsg = ".msg"
// ExtSrv defines ROS service definition file extension
ExtSrv = ".srv"
// ConstChar defines character that is used in constant fields of ROS message definitions
ConstChar = "="
// CommentChar defines character that is used in comments in ROS message definitions
CommentChar = "#"
// IoDelim defines delimiter used to seperate Goal, Result & Feedback sections in ROS action definitons
IoDelim = "---"
)
// SyntaxError is an error type that is returned when parser encounter
// invalid ROS message syntax in the ROS message definition
type SyntaxError struct {
FullName string
Line int
Message string
}
// NewSyntaxError returns a new instance of SyntaxError
func NewSyntaxError(fullName string, line int, message string) *SyntaxError {
return &SyntaxError{fullName, line, message}
}
// Error implements the error interface and returns the underlying error message string
func (e *SyntaxError) Error() string {
return fmt.Sprintf("[%s@%d] %s", e.FullName, e.Line, e.Message)
}
/// Convert constant literal to a Go object
/// Original implementation (genmsg) depends on Python's literal syntax.
func convertConstantValue(fieldType string, valueLiteral string) (interface{}, error) {
switch fieldType {
case "float32":
result, e := strconv.ParseFloat(valueLiteral, 32)
return float32(result), e
case "float64":
return strconv.ParseFloat(valueLiteral, 64)
case "string":
return strings.TrimSpace(valueLiteral), nil
case "byte":
result, e := strconv.ParseInt(valueLiteral, 0, 8)
return int8(result), e
case "int8":
result, e := strconv.ParseInt(valueLiteral, 0, 8)
return int8(result), e
case "int16":
result, e := strconv.ParseInt(valueLiteral, 0, 16)
return int16(result), e
case "int32":
result, e := strconv.ParseInt(valueLiteral, 0, 32)
return int32(result), e
case "int64":
return strconv.ParseInt(valueLiteral, 0, 64)
case "char":
result, e := strconv.ParseUint(valueLiteral, 0, 8)
return uint8(result), e
case "uint8":
result, e := strconv.ParseUint(valueLiteral, 0, 8)
return uint8(result), e
case "uint16":
result, e := strconv.ParseUint(valueLiteral, 0, 16)
return uint16(result), e
case "uint32":
result, e := strconv.ParseUint(valueLiteral, 0, 32)
return uint32(result), e
case "uint64":
return strconv.ParseUint(valueLiteral, 0, 64)
case "bool":
// The spec of ROS message doesn't specify boolean literal exactly.
// genmsg implementation determines true/false based Python's eval() and accepts any valid Python expression.
if valueLiteral == "None" || valueLiteral == "False" {
return false, nil
} else if valueLiteral == "True" {
return true, nil
} else if val, e := strconv.ParseUint(valueLiteral, 10, 0); e == nil {
return val != 0, nil
} else {
return nil, fmt.Errorf("Inavalid constant literal for bool: [%s]", valueLiteral)
}
default:
return nil, fmt.Errorf("Invalid constant type: [%s]", fieldType)
}
}
func packageResourceName(name string) (string, string, error) {
const Separator = "/"
if strings.Contains(name, Separator) {
components := strings.Split(name, Separator)
if len(components) == 2 {
return components[0], components[1], nil
}
return "", "", fmt.Errorf("Invalid name %s", name)
}
return "", name, nil
}
func stripComment(line string) string {
return strings.TrimSpace(strings.Split(line, CommentChar)[0])
}
func loadConstantLine(line string) (*Constant, error) {
cleanLine := stripComment(line)
sepIndex := strings.IndexFunc(cleanLine, unicode.IsSpace)
if sepIndex < 0 {
return nil, fmt.Errorf("Could not find a constant name after the type name")
}
fieldType := cleanLine[:sepIndex]
if !isValidConsantType(fieldType) {
return nil, fmt.Errorf("[%s] is not a legal constant type", fieldType)
}
var name, valueText string
if fieldType == "string" {
// Strings contain anything to the right of the equal sign, no comments allowd.
sepIndex := strings.IndexFunc(line, unicode.IsSpace)
if sepIndex < 0 {
return nil, fmt.Errorf("Could not find a constant name after the type name")
}
keyValue := line[sepIndex:]
kvSplits := strings.SplitN(keyValue, "=", 2)
if len(kvSplits) != 2 {
return nil, fmt.Errorf("A constant definition requires its value")
}
name = strings.TrimSpace(kvSplits[0])
valueText = strings.TrimLeftFunc(kvSplits[1], unicode.IsSpace)
} else {
keyValue := strings.TrimSpace(cleanLine[sepIndex:])
kvSplits := strings.SplitN(keyValue, "=", 2)
if len(kvSplits) != 2 {
return nil, fmt.Errorf("A constant definition requires its value")
}
name = strings.TrimSpace(kvSplits[0])
valueText = strings.TrimSpace(kvSplits[1])
}
value, e := convertConstantValue(fieldType, valueText)
if e != nil {
return nil, e
}
return NewConstant(fieldType, name, value, valueText), nil
}
func loadFieldLine(line string, packageName string) (*Field, error) {
cleanLine := stripComment(line)
lineSplits := strings.SplitN(cleanLine, " ", 2)
if len(lineSplits) != 2 {
return nil, fmt.Errorf("Invalid declaration: %s", line)
}
fieldType := strings.TrimSpace(lineSplits[0])
name := strings.TrimSpace(lineSplits[1])
if !isValidMsgFieldName(name) {
return nil, fmt.Errorf("%s is not a legal message field name", name)
}
if !isValidMsgType(fieldType) {
return nil, fmt.Errorf("%s is not a legal message field type", fieldType)
}
if len(packageName) > 0 && !strings.Contains(fieldType, Sep) {
if fieldType == HeaderType {
fieldType = HeaderFullName
} else if !isBuiltinType(baseMsgType(fieldType)) {
fieldType = fmt.Sprintf("%s/%s", packageName, fieldType)
}
} else if fieldType == HeaderType {
fieldType = HeaderFullName
}
pkg, baseType, isArray, arrayLen, err := parseType(fieldType)
if err != nil {
return nil, err
}
return NewField(pkg, baseType, name, isArray, arrayLen), nil
}