Skip to content

Commit c651182

Browse files
committed
Support comments in custom DRC rules
1 parent 8ea4302 commit c651182

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

kikit/sexpr.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,18 @@ def readWhitespace(stream):
141141
c = stream.peek()
142142
return "".join(w)
143143

144+
def readWhitespaceWithComments(stream):
145+
w = []
146+
c = stream.peek()
147+
while c.isspace() or c == "#":
148+
w.append(stream.read())
149+
if c == "#":
150+
while stream.peek() != "\n":
151+
w.append(stream.read())
152+
w.append(stream.read())
153+
c = stream.peek()
154+
return "".join(w)
155+
144156
def readSexpr(stream, limit=None):
145157
"""
146158
Reads SExpression from the stream. You can optionally try to parse only the
@@ -193,10 +205,10 @@ def parseSexprListF(sourceStream, limit=None):
193205
sexprs = []
194206
stream = Stream(sourceStream)
195207
while stream.peek() != "":
196-
lw = readWhitespace(stream)
208+
lw = readWhitespaceWithComments(stream)
197209
s = readSexpr(stream, limit=limit)
198210
s.leadingWhitespace = lw
199-
s.trailingOuterWhitespace = readWhitespace(stream)
211+
s.trailingOuterWhitespace = readWhitespaceWithComments(stream)
200212
sexprs.append(s)
201213
return sexprs
202214

test/units/test_sexpr.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ def test_readQuotedString():
2828
res = str(a)
2929
assert res == SOURCE
3030

31-
3231
def test_identiy():
3332
SOURCE = "../resources/conn.kicad_pcb"
3433
with open(SOURCE, encoding="utf-8") as f:
@@ -41,3 +40,10 @@ def test_identiy():
4140
with open(SOURCE, encoding="utf-8") as f:
4241
ast2 = parseSexprF(f, limit=3)
4342
assert str(ast2) == truth
43+
44+
def test_readwhitespace_with_comments():
45+
stream = Stream(StringIO(" \n\n# Aloha\n("))
46+
assert readWhitespaceWithComments(stream) == " \n\n# Aloha\n"
47+
48+
stream = Stream(StringIO(" \n\n \t# Aloha\n("))
49+
assert readWhitespaceWithComments(stream) == " \n\n \t# Aloha\n"

0 commit comments

Comments
 (0)