|
1 | 1 | from __future__ import annotations |
2 | 2 |
|
| 3 | +from pathlib import Path |
3 | 4 | from typing import Dict, List, Optional |
4 | 5 |
|
5 | 6 | from parchmint import Component, Connection, Layer, Params, Target |
|
19 | 20 | ) |
20 | 21 |
|
21 | 22 |
|
| 23 | +def strip_mint_line_comments(text: str) -> str: |
| 24 | + """Remove MINT line comments so the lexer never sees them. |
| 25 | +
|
| 26 | + ``mint.g4`` skips both ``#`` (original) and ``//`` (LFR-style headers that |
| 27 | + ``compile_lfr`` writes onto generated MINT, e.g. PORT COUNT CORRECTION). |
| 28 | + Strip here as well so files parse even before ANTLR is regenerated. |
| 29 | + """ |
| 30 | + out: List[str] = [] |
| 31 | + for line in text.splitlines(): |
| 32 | + if "//" in line: |
| 33 | + line = line.split("//", 1)[0] |
| 34 | + hash_at = line.find("#") |
| 35 | + if hash_at != -1: |
| 36 | + line = line[:hash_at] |
| 37 | + out.append(line) |
| 38 | + return "\n".join(out) |
| 39 | + |
| 40 | + |
22 | 41 | class MINTDevice(MINTProtocol): |
23 | 42 | """Device class abstracting parchmint Device object adding additional |
24 | 43 | methods for generating MINT |
@@ -308,15 +327,17 @@ def from_mint_file(filepath: str, skip_constraints: bool = False) -> MINTDevice: |
308 | 327 | """ |
309 | 328 | import io |
310 | 329 |
|
311 | | - from antlr4 import CommonTokenStream, FileStream, ParseTreeWalker |
| 330 | + from antlr4 import CommonTokenStream, InputStream, ParseTreeWalker |
312 | 331 |
|
313 | 332 | from pymint.antlrgen.mintLexer import mintLexer |
314 | 333 | from pymint.antlrgen.mintParser import mintParser |
315 | 334 | from pymint.constraints.constraintlistener import ConstraintListener |
316 | 335 | from pymint.mintcompiler import MINTCompiler |
317 | 336 | from pymint.mintErrorListener import MINTErrorListener |
318 | 337 |
|
319 | | - finput = FileStream(filepath, encoding="utf-8") |
| 338 | + raw = Path(filepath).read_text(encoding="utf-8") |
| 339 | + finput = InputStream(strip_mint_line_comments(raw)) |
| 340 | + finput.name = filepath |
320 | 341 |
|
321 | 342 | lexer = mintLexer(finput) |
322 | 343 |
|
|
0 commit comments