Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[rosidl_adapter] Fix how comments in action interfaces are processed. #632

Merged
merged 10 commits into from
Jan 13, 2022
41 changes: 24 additions & 17 deletions rosidl_adapter/rosidl_adapter/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -448,36 +448,40 @@ def parse_message_file(pkg_name, interface_filename):
pkg_name, msg_name, h.read())


def extract_file_level_comments(message_string):
lines = message_string.splitlines()
index = next(
(i for i, v in enumerate(lines) if not v.startswith(COMMENT_DELIMITER)), -1)
if index != -1:
file_level_comments = lines[0:index]
ivanpauno marked this conversation as resolved.
Show resolved Hide resolved
other = lines[index:]
ivanpauno marked this conversation as resolved.
Show resolved Hide resolved
else:
file_level_comments = []
other = lines[:]
file_level_comments = [line.lstrip(COMMENT_DELIMITER) for line in file_level_comments]
return file_level_comments, other


def parse_message_string(pkg_name, msg_name, message_string):
file_level_ended = False
message_comments = []
ivanpauno marked this conversation as resolved.
Show resolved Hide resolved
fields = []
constants = []
last_element = None # either a field or a constant
# replace tabs with spaces
message_string.replace('\t', ' ')
ivanpauno marked this conversation as resolved.
Show resolved Hide resolved

current_comments = []
lines = message_string.splitlines()
message_comments, lines = extract_file_level_comments(message_string)
for line in lines:
line = line.rstrip()

# replace tabs with spaces
line = line.replace('\t', ' ')

# ignore empty lines
if not line:
# file-level comments stop at the first empty line
file_level_ended = True
continue

index = line.find(COMMENT_DELIMITER)

# file-level comment line
if index == 0 and not file_level_ended:
message_comments.append(line.lstrip(COMMENT_DELIMITER))
continue

file_level_ended = True

# comment
comment = None
if index >= 0:
Expand Down Expand Up @@ -884,14 +888,17 @@ def parse_action_file(pkg_name, interface_filename):


def parse_action_string(pkg_name, action_name, action_string):
action_blocks = re.split(
'^' + ACTION_REQUEST_RESPONSE_SEPARATOR + '$', action_string, flags=re.MULTILINE)
if len(action_blocks) != 3:
lines = action_string.splitlines()
separator_indices = [
index for index, line in enumerate(lines) if line == ACTION_REQUEST_RESPONSE_SEPARATOR]
if len(separator_indices) != 2:
raise InvalidActionSpecification(
"Number of '%s' separators nonconformant with action definition" %
ACTION_REQUEST_RESPONSE_SEPARATOR)

goal_string, result_string, feedback_string = action_blocks
goal_string = '\n'.join(lines[:separator_indices[0]])
result_string = '\n'.join(lines[separator_indices[0] + 1:separator_indices[1]])
feedback_string = '\n'.join(lines[separator_indices[1] + 1:])

goal_message = parse_message_string(
pkg_name, action_name + ACTION_GOAL_SUFFIX, goal_string)
Expand Down
11 changes: 10 additions & 1 deletion rosidl_adapter/test/data/action/Test.action
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
# goal definition
# foo

# bar
bool bool_value
# baz
byte byte_value
char char_value
# asd
char char_value # bsd
float32 float32_value
float64 float64_value
int8 int8_value
Expand All @@ -15,7 +20,11 @@ uint64 uint64_value
string string_value
---
# result definition

# ok docs
bool ok
---
# feedback definition

ivanpauno marked this conversation as resolved.
Show resolved Hide resolved
# sequence docs
int32[] sequence
18 changes: 15 additions & 3 deletions rosidl_adapter/test/data/action/Test.expected.idl
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,20 @@
module test_msgs {
module action {
@verbatim (language="comment", text=
"goal definition")
"goal definition" "\n"
"foo")
struct Test_Goal {
@verbatim (language="comment", text=
"bar")
boolean bool_value;

@verbatim (language="comment", text=
"baz")
octet byte_value;

@verbatim (language="comment", text=
"asd" "\n"
"bsd")
uint8 char_value;

float float32_value;
Expand All @@ -36,14 +44,18 @@ module test_msgs {

string string_value;
};
@verbatim (language="comment", text=
"result definition")
struct Test_Result {
@verbatim (language="comment", text=
"result definition")
"ok docs")
boolean ok;
};
@verbatim (language="comment", text=
"feedback definition")
struct Test_Feedback {
@verbatim (language="comment", text=
"feedback definition")
"sequence docs")
sequence<int32> sequence;
};
};
Expand Down
9 changes: 9 additions & 0 deletions rosidl_adapter/test/data/msg/Test.expected.idl
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,20 @@

module test_msgs {
module msg {
@verbatim (language="comment", text=
"msg level doc")
struct Test {
@verbatim (language="comment", text=
"field level doc")
boolean bool_value;

@verbatim (language="comment", text=
"field level doc, style 2")
octet byte_value;

@verbatim (language="comment", text=
"combined styles" "\n"
"combined styles, part 2")
uint8 char_value;

float float32_value;
Expand Down
8 changes: 6 additions & 2 deletions rosidl_adapter/test/data/msg/Test.msg
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# msg level doc

# field level doc
bool bool_value
byte byte_value
char char_value
byte byte_value # field level doc, style 2
# combined styles
char char_value # combined styles, part 2
float32 float32_value
float64 float64_value
int8 int8_value
Expand Down
14 changes: 14 additions & 0 deletions rosidl_adapter/test/data/srv/Test.expected.idl
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,21 @@

module test_msgs {
module srv {
@verbatim (language="comment", text=
"1" "\n"
"2")
struct Test_Request {
@verbatim (language="comment", text=
"3")
boolean bool_value;

@verbatim (language="comment", text=
"4")
octet byte_value;

@verbatim (language="comment", text=
"5" "\n"
"6")
uint8 char_value;

float float32_value;
Expand All @@ -34,7 +44,11 @@ module test_msgs {

string string_value;
};
@verbatim (language="comment", text=
"7")
struct Test_Response {
@verbatim (language="comment", text=
"8")
boolean ok;
};
};
Expand Down
13 changes: 11 additions & 2 deletions rosidl_adapter/test/data/srv/Test.srv
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
# 1
# 2

# 3
bool bool_value
byte byte_value
char char_value
byte byte_value # 4
# 5
char char_value # 6
float32 float32_value
float64 float64_value
int8 int8_value
Expand All @@ -13,4 +18,8 @@ int64 int64_value
uint64 uint64_value
string string_value
---
# 7

# 8

bool ok