Skip to content

Commit f2dde17

Browse files
committed
Accept // line comments in MINT so LFR-generated headers parse.
compile_lfr writes PORT COUNT CORRECTION blocks with //, which the lexer previously rejected.
1 parent 7406870 commit f2dde17

3 files changed

Lines changed: 33 additions & 5 deletions

File tree

‎mint.g4‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ WS: [ \t]+;
222222

223223
NL: [\r\n]+ -> skip; // Define whitespace rule, toss it out
224224

225-
COMMENT: '#' ~[\r\n]* -> skip;
225+
COMMENT: ('//' | '#') ~[\r\n]* -> skip;
226226

227227
Real_number:
228228
Unsigned_number '.' Unsigned_number

‎pymint/mintdevice.py‎

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from __future__ import annotations
22

3+
from pathlib import Path
34
from typing import Dict, List, Optional
45

56
from parchmint import Component, Connection, Layer, Params, Target
@@ -19,6 +20,24 @@
1920
)
2021

2122

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+
2241
class MINTDevice(MINTProtocol):
2342
"""Device class abstracting parchmint Device object adding additional
2443
methods for generating MINT
@@ -308,15 +327,17 @@ def from_mint_file(filepath: str, skip_constraints: bool = False) -> MINTDevice:
308327
"""
309328
import io
310329

311-
from antlr4 import CommonTokenStream, FileStream, ParseTreeWalker
330+
from antlr4 import CommonTokenStream, InputStream, ParseTreeWalker
312331

313332
from pymint.antlrgen.mintLexer import mintLexer
314333
from pymint.antlrgen.mintParser import mintParser
315334
from pymint.constraints.constraintlistener import ConstraintListener
316335
from pymint.mintcompiler import MINTCompiler
317336
from pymint.mintErrorListener import MINTErrorListener
318337

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
320341

321342
lexer = mintLexer(finput)
322343

‎pymint/mintwriter.py‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ def to_params_MINT(params: Params) -> str:
1313
Returns:
1414
str: MINT string fragment
1515
"""
16-
skip_list = ["paths", "wayPoints", "position"]
16+
skip_list = ["paths", "wayPoints", "position", "start", "end", "segments"]
1717
ret = ""
1818
for key in params.data:
1919
if key in skip_list:
@@ -61,8 +61,15 @@ def to_connection_MINT(connection: Connection) -> str:
6161
Returns:
6262
str: This is the MINT string for the serialization
6363
"""
64+
# mint.g4 channelStat is `(entity | 'CHANNEL')`. The CHANNEL keyword is a
65+
# distinct lexer token, so `ROUNDED CHANNEL name from ...` does not parse.
66+
# Emit the CHANNEL keyword and carry the rounded 3DuF profile as crossSection.
67+
entity = connection.entity or "CHANNEL"
68+
mint_entity = "CHANNEL" if "ROUND" in str(entity).upper() else entity
69+
if "ROUND" in str(entity).upper() and not connection.params.exists("crossSection"):
70+
connection.params.set_param("crossSection", 1)
6471
ret = "{} {} from {} to {} {} ;".format(
65-
connection.entity,
72+
mint_entity,
6673
connection.ID,
6774
to_target_MINT(connection.source) if connection.source is not None else "",
6875
", ".join([to_target_MINT(item) for item in connection.sinks]),

0 commit comments

Comments
 (0)