Add concrete syntax for class diagrams + example (woods2.py)

This commit is contained in:
Joeri Exelmans 2024-10-09 15:09:16 +02:00
parent c248fc9090
commit 52ded8af77
6 changed files with 311 additions and 58 deletions

View file

@ -1,3 +1,5 @@
from lark import Transformer
def indent(multiline_string, how_much):
lines = multiline_string.split('\n')
return '\n'.join([' '*how_much+l for l in lines])
@ -14,3 +16,42 @@ def display_value(val: any, type_name: str, indentation=0):
return str(val)
else:
raise Exception("don't know how to display value" + type_name)
# internal use only
# just a dumb wrapper to distinguish between code and string
class _Code:
def __init__(self, code):
self.code = code
class TBase(Transformer):
def IDENTIFIER(self, token):
return str(token)
def INT(self, token):
return int(token)
def BOOL(self, token):
return token == "True"
def STR(self, token):
return str(token[1:-1]) # strip the "" or ''
def CODE(self, token):
return _Code(str(token[1:-1])) # strip the ``
def INDENTED_CODE(self, token):
skip = 4 # strip the ``` and the following newline character
space_count = 0
while token[skip+space_count] == " ":
space_count += 1
lines = token.split('\n')[1:-1]
for line in lines:
if len(line) >= space_count and line[0:space_count] != ' '*space_count:
raise Exception("wrong indentation of INDENTED_CODE")
unindented_lines = [l[space_count:] for l in lines]
return _Code('\n'.join(unindented_lines))
def literal(self, el):
return el[0]