@@ -37,47 +37,34 @@ class InoToCPPConverter(object):
37
37
38
38
DETECTMAIN_RE = re .compile (r"void\s+(setup|loop)\s*\(" , re .M | re .I )
39
39
40
- STRIPCOMMENTS_RE = re .compile (r"(/\*.*?\*/|^\s*//[^\r\n]*$)" ,
41
- re .M | re .S )
42
-
43
40
def __init__ (self , nodes ):
44
41
self .nodes = nodes
45
42
46
43
def is_main_node (self , contents ):
47
44
return self .DETECTMAIN_RE .search (contents )
48
45
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 ):
57
47
prototypes = []
58
48
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 ):
61
52
continue
62
- prototypes .append (item [ 0 ] )
53
+ prototypes .append (( file_path , match . start (), match . group ( 1 )) )
63
54
return prototypes
64
55
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 ):
68
57
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
79
60
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 ())
81
68
82
69
return result
83
70
@@ -86,9 +73,9 @@ def convert(self):
86
73
data = []
87
74
for node in self .nodes :
88
75
ino_contents = node .get_text_contents ()
89
- prototypes += self ._parse_prototypes (ino_contents )
76
+ prototypes += self ._parse_prototypes (node . get_path (), ino_contents )
90
77
91
- item = (basename ( node .get_path () ), ino_contents )
78
+ item = (node .get_path (), ino_contents )
92
79
if self .is_main_node (ino_contents ):
93
80
data = [item ] + data
94
81
else :
@@ -99,12 +86,13 @@ def convert(self):
99
86
100
87
result = ["#include <Arduino.h>" ]
101
88
is_first = True
89
+ for file_path , contents in data :
90
+ result .append ('#line 1 "%s"' % file_path )
102
91
103
- for name , contents in data :
104
92
if is_first and prototypes :
105
- result += self .append_prototypes (name , contents , prototypes )
93
+ result += self .append_prototypes (
94
+ file_path , contents , prototypes )
106
95
else :
107
- result .append ('#line 1 "%s"' % name )
108
96
result .append (contents )
109
97
is_first = False
110
98
0 commit comments