Skip to content

Commit 00b76e6

Browse files
committed
Refactor source code converter from "*.ino" to "*.cpp" // Resolve #610
1 parent b708582 commit 00b76e6

File tree

2 files changed

+23
-33
lines changed

2 files changed

+23
-33
lines changed

HISTORY.rst

+2
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ PlatformIO 2.0
3434
(`issue #631 <https://github.com/platformio/platformio/issues/631>`_)
3535
* Automatically reboot Teensy board after upload when Teensy Loader GUI is used
3636
(`issue #609 <https://github.com/platformio/platformio/issues/609>`_)
37+
* Refactored source code converter from ``*.ino`` to ``*.cpp``
38+
(`issue #610 <https://github.com/platformio/platformio/issues/610>`_)
3739
* Forced ``-std=gnu++11`` for Atmel SAM development platform
3840
(`issue #601 <https://github.com/platformio/platformio/issues/601>`_)
3941
* Don't check OS type for ARM mbed-enabled boards and ST STM32 development

platformio/builder/tools/piomisc.py

+21-33
Original file line numberDiff line numberDiff line change
@@ -37,47 +37,34 @@ class InoToCPPConverter(object):
3737

3838
DETECTMAIN_RE = re.compile(r"void\s+(setup|loop)\s*\(", re.M | re.I)
3939

40-
STRIPCOMMENTS_RE = re.compile(r"(/\*.*?\*/|^\s*//[^\r\n]*$)",
41-
re.M | re.S)
42-
4340
def __init__(self, nodes):
4441
self.nodes = nodes
4542

4643
def is_main_node(self, contents):
4744
return self.DETECTMAIN_RE.search(contents)
4845

49-
@staticmethod
50-
def _replace_comments_callback(match):
51-
if "\n" in match.group(1):
52-
return "\n" * match.group(1).count("\n")
53-
else:
54-
return " "
55-
56-
def _parse_prototypes(self, contents):
46+
def _parse_prototypes(self, file_path, contents):
5747
prototypes = []
5848
reserved_keywords = set(["if", "else", "while"])
59-
for item in self.PROTOTYPE_RE.findall(contents):
60-
if set([item[1].strip(), item[2].strip()]) & reserved_keywords:
49+
for match in self.PROTOTYPE_RE.finditer(contents):
50+
if (set([match.group(2).strip(), match.group(3).strip()]) &
51+
reserved_keywords):
6152
continue
62-
prototypes.append(item[0])
53+
prototypes.append((file_path, match.start(), match.group(1)))
6354
return prototypes
6455

65-
def append_prototypes(self, fname, contents, prototypes):
66-
contents = self.STRIPCOMMENTS_RE.sub(self._replace_comments_callback,
67-
contents)
56+
def append_prototypes(self, file_path, contents, prototypes):
6857
result = []
69-
is_appended = False
70-
linenum = 0
71-
for line in contents.splitlines():
72-
linenum += 1
73-
line = line.strip()
74-
75-
if not is_appended and line and not line.startswith("#"):
76-
is_appended = True
77-
result.append("%s;" % ";\n".join(prototypes))
78-
result.append('#line %d "%s"' % (linenum, fname))
58+
if not prototypes:
59+
return result
7960

80-
result.append(line)
61+
first_pos = prototypes[0][1]
62+
result.append(contents[:first_pos].strip())
63+
result.append("%s;" % ";\n".join([p[2] for p in prototypes]))
64+
result.append('#line %d "%s"' % (
65+
contents.count("\n", 0, first_pos + len(prototypes[0][2])) + 1,
66+
file_path))
67+
result.append(contents[first_pos:].strip())
8168

8269
return result
8370

@@ -86,9 +73,9 @@ def convert(self):
8673
data = []
8774
for node in self.nodes:
8875
ino_contents = node.get_text_contents()
89-
prototypes += self._parse_prototypes(ino_contents)
76+
prototypes += self._parse_prototypes(node.get_path(), ino_contents)
9077

91-
item = (basename(node.get_path()), ino_contents)
78+
item = (node.get_path(), ino_contents)
9279
if self.is_main_node(ino_contents):
9380
data = [item] + data
9481
else:
@@ -99,12 +86,13 @@ def convert(self):
9986

10087
result = ["#include <Arduino.h>"]
10188
is_first = True
89+
for file_path, contents in data:
90+
result.append('#line 1 "%s"' % file_path)
10291

103-
for name, contents in data:
10492
if is_first and prototypes:
105-
result += self.append_prototypes(name, contents, prototypes)
93+
result += self.append_prototypes(
94+
file_path, contents, prototypes)
10695
else:
107-
result.append('#line 1 "%s"' % name)
10896
result.append(contents)
10997
is_first = False
11098

0 commit comments

Comments
 (0)