Spaces:
Sleeping
Sleeping
File size: 10,254 Bytes
395651c | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 | import re
import logging
from typing import List, Tuple, Dict, Any
from .models import Point, Constraint
logger = logging.getLogger(__name__)
class DSLParser:
def parse(self, text: str) -> Tuple[List[Point], List[Constraint], bool]:
"""Parse DSL text into points and constraints. Stateless per call."""
points: Dict[str, Point] = {}
explicit_point_ids: List[str] = []
constraints: List[Constraint] = []
polygon_order: List[str] = []
circles: List[Dict[str, Any]] = []
segments: List[List[str]] = []
lines_ext: List[List[str]] = []
rays: List[List[str]] = []
is_3d = False
logger.info("==[DSLParser] Parsing DSL input==")
logger.debug(f"[DSLParser] Raw DSL:\n{text}")
lines = text.strip().split('\n')
for line in lines:
line = line.strip()
if not line or line.startswith('//') or line.startswith('#'):
continue
# POINT(A) or POINT(A, 0, 0, 5)
m = re.match(r'POINT\((\w+)(?:,\s*([\d\.-]+),\s*([\d\.-]+)(?:,\s*([\d\.-]+))?)?\)', line)
if m:
name = m.group(1)
x = float(m.group(2)) if m.group(2) else None
y = float(m.group(3)) if m.group(3) else None
z = float(m.group(4)) if m.group(4) else None
# z=0 with x,y is still the xy-plane; only treat as 3D when z is meaningfully non-zero.
# Otherwise POINT(A,0,0,0) incorrectly forced is_3d and broke 2D engine paths.
if z is not None and abs(z) > 1e-9:
is_3d = True
points[name] = Point(id=name, x=x, y=y, z=z)
if name not in explicit_point_ids:
explicit_point_ids.append(name)
logger.debug(f"[DSLParser] + POINT: {name} ({x}, {y}, {z})")
continue
# LENGTH(AB, 5)
m = re.match(r'LENGTH\((\w+),\s*([\d\.]+)\)', line)
if m:
target, value = m.group(1), float(m.group(2))
pts = [target[i:i+1] for i in range(len(target))]
constraints.append(Constraint(type='length', targets=pts, value=value))
logger.debug(f"[DSLParser] + LENGTH: {pts} = {value}")
continue
# ANGLE(A, 90) or ANGLE(A, 90deg)
m = re.match(r'ANGLE\((\w+),\s*([\d\.]+)(?:deg)?\)', line)
if m:
target, value = m.group(1), float(m.group(2))
constraints.append(Constraint(type='angle', targets=[target], value=value))
logger.debug(f"[DSLParser] + ANGLE: vertex={target}, degrees={value}")
continue
# PARALLEL(AB, CD)
m = re.match(r'PARALLEL\((\w+),\s*(\w+)\)', line)
if m:
seg1, seg2 = m.group(1), m.group(2)
constraints.append(Constraint(type='parallel', targets=list(seg1) + list(seg2), value=0))
logger.debug(f"[DSLParser] + PARALLEL: {seg1} || {seg2}")
continue
# PERPENDICULAR(AB, CD)
m = re.match(r'PERPENDICULAR\((\w+),\s*(\w+)\)', line)
if m:
seg1, seg2 = m.group(1), m.group(2)
constraints.append(Constraint(type='perpendicular', targets=list(seg1) + list(seg2), value=0))
logger.debug(f"[DSLParser] + PERPENDICULAR: {seg1} _|_ {seg2}")
continue
# MIDPOINT(M, AB) — M is midpoint of AB
m = re.match(r'MIDPOINT\((\w+),\s*(\w+)\)', line)
if m:
mid, seg = m.group(1), m.group(2)
if mid not in points:
points[mid] = Point(id=mid)
pts = [mid] + [seg[i:i+1] for i in range(len(seg))]
constraints.append(Constraint(type='midpoint', targets=pts, value=0))
logger.debug(f"[DSLParser] + MIDPOINT: {mid} = mid({seg})")
continue
# SECTION(E, A, C, 0.66) — E lies on AC s.t. AE = 0.66 * AC
m = re.match(r'SECTION\((\w+),\s*(\w+),\s*(\w+),\s*([\d\.-]+)\)', line)
if m:
target, p1, p2, k = m.group(1), m.group(2), m.group(3), float(m.group(4))
if target not in points:
points[target] = Point(id=target)
constraints.append(Constraint(type='section', targets=[target, p1, p2], value=k))
logger.debug(f"[DSLParser] + SECTION: {target} = {p1} + {k}({p2}-{p1})")
continue
# CIRCLE(O, r)
m = re.match(r'CIRCLE\((\w+),\s*([\d\.]+)\)', line)
if m:
center, radius = m.group(1), float(m.group(2))
if center not in points:
points[center] = Point(id=center)
constraints.append(Constraint(type='circle', targets=[center], value=radius))
circles.append({"center": center, "radius": radius})
logger.debug(f"[DSLParser] + CIRCLE: center={center}, r={radius}")
continue
# POLYGON_ORDER(A, B, C, D) — thứ tự nối điểm để vẽ đa giác
m = re.match(r'POLYGON_ORDER\(([^)]+)\)', line)
if m:
polygon_order = [p.strip() for p in m.group(1).split(',')]
logger.debug(f"[DSLParser] + POLYGON_ORDER: {polygon_order}")
continue
# SEGMENT(M, N) — đoạn thẳng phụ cần vẽ
m = re.match(r'SEGMENT\((\w+),\s*(\w+)\)', line)
if m:
p1, p2 = m.group(1), m.group(2)
segments.append([p1, p2])
constraints.append(Constraint(type='segment', targets=[p1, p2], value=0))
logger.debug(f"[DSLParser] + SEGMENT: {p1}—{p2}")
continue
# LINE(A, B) — infinite line
m = re.match(r'LINE\((\w+),\s*(\w+)\)', line)
if m:
p1, p2 = m.group(1), m.group(2)
lines_ext.append([p1, p2])
constraints.append(Constraint(type='line', targets=[p1, p2], value=0))
logger.debug(f"[DSLParser] + LINE: {p1}-{p2}")
continue
# RAY(A, B) — ray AB starting at A
m = re.match(r'RAY\((\w+),\s*(\w+)\)', line)
if m:
p1, p2 = m.group(1), m.group(2)
rays.append([p1, p2])
constraints.append(Constraint(type='ray', targets=[p1, p2], value=0))
logger.debug(f"[DSLParser] + RAY: {p1}->{p2}")
continue
# TRIANGLE(ABC) / PYRAMID(S_ABCD) / PRISM(ABC_DEF)
m = re.match(r'(TRIANGLE|PYRAMID|PRISM)\(([^)]+)\)', line)
if m:
pt_type = m.group(1)
targets = m.group(2)
if pt_type in ["PYRAMID", "PRISM"]:
is_3d = True
if pt_type == "TRIANGLE":
if not polygon_order: polygon_order = list(targets)
elif pt_type == "PYRAMID":
# S_ABCD -> S is apex, ABCD is base
if "_" in targets:
apex, base = targets.split("_")
# Add segments from apex to all base points
for p in base:
segments.append([apex, p])
constraints.append(Constraint(type='segment', targets=[apex, p], value=0))
if not polygon_order: polygon_order = list(base)
elif pt_type == "PRISM":
# ABC_DEF -> two bases
if "_" in targets:
b1, b2 = targets.split("_")
for p1, p2 in zip(b1, b2):
segments.append([p1, p2])
constraints.append(Constraint(type='segment', targets=[p1, p2], value=0))
logger.debug(f"[DSLParser] + {pt_type}: {targets}")
continue
# SPHERE(O, r)
m = re.match(r'SPHERE\((\w+),\s*([\d\.]+)\)', line)
if m:
is_3d = True
center, radius = m.group(1), float(m.group(2))
if center not in points:
points[center] = Point(id=center)
constraints.append(Constraint(type='sphere', targets=[center], value=radius))
logger.debug(f"[DSLParser] + SPHERE: center={center}, r={radius}")
continue
logger.warning(f"[DSLParser] ? Unrecognized DSL line: '{line}'")
logger.info(
"[DSLParser] Parsed %d points, %d constraints, is_3d=%s.",
len(points),
len(constraints),
is_3d,
)
# Safety sweep: Ensure all points referenced in constraints actually exist in the points dictionary
for c in constraints:
for pid in c.targets:
# Some targets might be values or comma-separated strings (handled elsewhere),
# but most are single-character point IDs.
if isinstance(pid, str) and len(pid) == 1 and pid not in points:
points[pid] = Point(id=pid)
logger.debug(f"[DSLParser] ! Auto-declared missing point from constraint: {pid}")
# Attach metadata to a synthetic constraint for downstream use
if polygon_order:
constraints.append(Constraint(type='polygon_order', targets=polygon_order, value=0))
elif explicit_point_ids:
# Re-use polygon_order as a carrier for explicit points IF no real order was specified
constraints.append(Constraint(type='explicit_points', targets=explicit_point_ids, value=0))
# Add auxiliary metadata for lines and rays
if lines_ext:
constraints.append(Constraint(type='lines_metadata', targets=[",".join(l) for l in lines_ext], value=0))
if rays:
constraints.append(Constraint(type='rays_metadata', targets=[",".join(l) for l in rays], value=0))
return list(points.values()), constraints, is_3d
|