When creating new DtsLine object, pass expected indentation depth as constructor, instead of assigning it immediately after, so the code will be easier to read and explicit (depth is not supposed to change during DtsLine lifetime).
Showing the changes to scripts (1 of 1 file). Show all
Unified · Split
| @@ -82,11 +82,12 @@ class DtsLine: · 81 unmodified lines | |||
| 82 | 'node_name', 'node_addr', 'label', 'ref_name', 'depth', | 82 | 'node_name', 'node_addr', 'label', 'ref_name', 'depth', |
| 83 | 'closures') | 83 | 'closures') |
| 84 | 84 | ||
| 85 | def __init__(self, lineno, raw, linetype, indent_str, stripped): | 85 | def __init__(self, lineno, raw, linetype, depth, indent_str, stripped): |
| 86 | self.lineno = lineno # 1-based within the block | 86 | self.lineno = lineno # 1-based within the block |
| 87 | self.raw = raw | 87 | self.raw = raw |
| 88 | self.linetype = linetype | 88 | self.linetype = linetype |
| 89 | self.indent_str = indent_str # leading whitespace as-is | 89 | self.indent_str = indent_str # leading whitespace as-is |
| 90 | self.depth = depth | ||
| 90 | self.stripped = stripped | 91 | self.stripped = stripped |
| 91 | self.prop_name = None | 92 | self.prop_name = None |
| 92 | self.continuations = [] | 93 | self.continuations = [] |
| @@ -94,7 +95,6 @@ class DtsLine: · 1 unmodified line | |||
| 94 | self.node_addr = None | 95 | self.node_addr = None |
| 95 | self.label = None | 96 | self.label = None |
| 96 | self.ref_name = None | 97 | self.ref_name = None |
| 97 | self.depth = 0 # filled in by classify_lines | ||
| 98 | self.closures = 1 # count of '}' on a NODE_CLOSE line | 98 | self.closures = 1 # count of '}' on a NODE_CLOSE line |
| 99 | 99 | ||
| 100 | 100 | ||
| @@ -162,16 +162,14 @@ def classify_lines(text): · 61 unmodified lines | |||
| 162 | # or a blank line). | 162 | # or a blank line). |
| 163 | if in_cpp_macro: | 163 | if in_cpp_macro: |
| 164 | dl = DtsLine(i, raw, LineType.PREPROCESSOR, | 164 | dl = DtsLine(i, raw, LineType.PREPROCESSOR, |
| 165 | indent_str, stripped) | 165 | depth, indent_str, stripped) |
| 166 | dl.depth = depth | ||
| 167 | out.append(dl) | 166 | out.append(dl) |
| 168 | in_cpp_macro = (bool(stripped) and | 167 | in_cpp_macro = (bool(stripped) and |
| 169 | stripped.rstrip().endswith('\\')) | 168 | stripped.rstrip().endswith('\\')) |
| 170 | continue | 169 | continue |
| 171 | 170 | ||
| 172 | if not stripped: | 171 | if not stripped: |
| 173 | dl = DtsLine(i, raw, LineType.BLANK, '', '') | 172 | dl = DtsLine(i, raw, LineType.BLANK, depth, '', '') |
| 174 | dl.depth = depth | ||
| 175 | out.append(dl) | 173 | out.append(dl) |
| 176 | continue | 174 | continue |
| 177 | 175 | ||
| @@ -180,15 +178,13 @@ def classify_lines(text): · 2 unmodified lines | |||
| 180 | else LineType.COMMENT_BODY) | 178 | else LineType.COMMENT_BODY) |
| 181 | if ltype == LineType.COMMENT_END: | 179 | if ltype == LineType.COMMENT_END: |
| 182 | in_block_comment = False | 180 | in_block_comment = False |
| 183 | dl = DtsLine(i, raw, ltype, indent_str, stripped) | 181 | dl = DtsLine(i, raw, ltype, depth, indent_str, stripped) |
| 184 | dl.depth = depth | ||
| 185 | out.append(dl) | 182 | out.append(dl) |
| 186 | continue | 183 | continue |
| 187 | 184 | ||
| 188 | if (stripped.startswith('#') or stripped.startswith('/')) and is_preprocessor(stripped): | 185 | if (stripped.startswith('#') or stripped.startswith('/')) and is_preprocessor(stripped): |
| 189 | dl = DtsLine(i, raw, LineType.PREPROCESSOR, | 186 | dl = DtsLine(i, raw, LineType.PREPROCESSOR, depth, |
| 190 | indent_str, stripped) | 187 | indent_str, stripped) |
| 191 | dl.depth = depth | ||
| 192 | out.append(dl) | 188 | out.append(dl) |
| 193 | prev_complete = True | 189 | prev_complete = True |
| 194 | in_cpp_macro = stripped.rstrip().endswith('\\') | 190 | in_cpp_macro = stripped.rstrip().endswith('\\') |
| @@ -205,14 +201,12 @@ def classify_lines(text): · 10 unmodified lines | |||
| 205 | # structural classification entirely. | 201 | # structural classification entirely. |
| 206 | if not code: | 202 | if not code: |
| 207 | ltype = LineType.COMMENT_START if opens_block else LineType.COMMENT | 203 | ltype = LineType.COMMENT_START if opens_block else LineType.COMMENT |
| 208 | dl = DtsLine(i, raw, ltype, indent_str, stripped) | 204 | dl = DtsLine(i, raw, ltype, depth, indent_str, stripped) |
| 209 | dl.depth = depth | ||
| 210 | out.append(dl) | 205 | out.append(dl) |
| 211 | continue | 206 | continue |
| 212 | 207 | ||
| 213 | if not prev_complete: | 208 | if not prev_complete: |
| 214 | dl = DtsLine(i, raw, LineType.CONTINUATION, indent_str, code) | 209 | dl = DtsLine(i, raw, LineType.CONTINUATION, depth, indent_str, code) |
| 215 | dl.depth = depth | ||
| 216 | out.append(dl) | 210 | out.append(dl) |
| 217 | prev_complete = (code.endswith(';') or | 211 | prev_complete = (code.endswith(';') or |
| 218 | code.endswith('{') or | 212 | code.endswith('{') or |
| @@ -227,26 +221,23 @@ def classify_lines(text): · 8 unmodified lines | |||
| 227 | if re_only_closures.match(code): | 221 | if re_only_closures.match(code): |
| 228 | closures = code.count('}') | 222 | closures = code.count('}') |
| 229 | depth = max(depth - closures, 0) | 223 | depth = max(depth - closures, 0) |
| 230 | dl = DtsLine(i, raw, LineType.NODE_CLOSE, indent_str, code) | 224 | dl = DtsLine(i, raw, LineType.NODE_CLOSE, depth, indent_str, code) |
| 231 | dl.depth = depth | ||
| 232 | dl.closures = closures | 225 | dl.closures = closures |
| 233 | out.append(dl) | 226 | out.append(dl) |
| 234 | prev_complete = True | 227 | prev_complete = True |
| 235 | continue | 228 | continue |
| 236 | 229 | ||
| 237 | if code.endswith('{'): | 230 | if code.endswith('{'): |
| 238 | dl = DtsLine(i, raw, LineType.NODE_OPEN, indent_str, code) | 231 | dl = DtsLine(i, raw, LineType.NODE_OPEN, depth, indent_str, code) |
| 239 | parse_node_header(dl) | 232 | parse_node_header(dl) |
| 240 | dl.depth = depth | ||
| 241 | out.append(dl) | 233 | out.append(dl) |
| 242 | depth += 1 | 234 | depth += 1 |
| 243 | prev_complete = True | 235 | prev_complete = True |
| 244 | continue | 236 | continue |
| 245 | 237 | ||
| 246 | # Property (or first line of a multi-line property). | 238 | # Property (or first line of a multi-line property). |
| 247 | dl = DtsLine(i, raw, LineType.PROPERTY, indent_str, code) | 239 | dl = DtsLine(i, raw, LineType.PROPERTY, depth, indent_str, code) |
| 248 | parse_property_name(dl) | 240 | parse_property_name(dl) |
| 249 | dl.depth = depth | ||
| 250 | out.append(dl) | 241 | out.append(dl) |
| 251 | prev_complete = code.endswith(';') | 242 | prev_complete = code.endswith(';') |
| 252 | 243 | ||