Skip to content

Commit

Permalink
no longer format indents for code snippets, still remove offset
Browse files Browse the repository at this point in the history
  • Loading branch information
reedeveris committed Feb 14, 2024
1 parent 4ae7501 commit 1b58282
Showing 1 changed file with 11 additions and 28 deletions.
39 changes: 11 additions & 28 deletions src/plcc.py
Original file line number Diff line number Diff line change
Expand Up @@ -1106,7 +1106,6 @@ def sem(nxt):

def getCode(nxt):
code = []
ws = 0
offset = None
for line in nxt:
line = line.rstrip()
Expand All @@ -1125,11 +1124,9 @@ def getCode(nxt):
for line in nxt:
if re.match(stopMatch, line):
break
if ws == 0:
ws = getWS(line)
if offset == None:
offset = getOffset(line,ws)
line = forceIndent(line, ws, offset)
offset = getOffset(line)
line = removeOffset(line, offset)
code.append(line)
else:
deathLNO('premature end of file')
Expand Down Expand Up @@ -1472,14 +1469,15 @@ def nt2cls(nt):
# return the class name of the nonterminal nt
return nt[0].upper() + nt[1:]

def forceIndent(ln, ws, offset):
def removeOffset(ln, offset):
check = ln.strip()
if len(check) == 0:
return ln
if ws == 0:
if offset == 0:
return ln
wscount = 0
numtab = 0
indent = ''
for c in ln:
if c == ' ':
wscount += 1
Expand All @@ -1489,13 +1487,15 @@ def forceIndent(ln, ws, offset):
break
line = ln.lstrip()
if wscount != 0:
tabs = '\t' * ((wscount // ws) - offset)
wscount = wscount - offset
indent += ' ' * wscount
else:
tabs = '\t' * (numtab - offset)
line = tabs + line
numtab = numtab - offset
indent += '\t' * numtab
line = indent + line
return line

def getWS(line):
def getOffset(line):
check = line.lstrip()
if len(check) == 0 or check[0] == '#':
return 0
Expand All @@ -1509,22 +1509,5 @@ def getWS(line):
break
return ws

def getOffset(line, ws):
check = line.lstrip()
if len(check) == 0 or check[0] == '#':
return None
wscount = 0
tabs = 0
for c in line:
if c == ' ':
wscount += 1
elif c == '\t':
tabs += 1
else:
break
if ws != 0:
tabs = wscount // ws
return tabs

if __name__ == '__main__':
main()

0 comments on commit 1b58282

Please sign in to comment.