Add ActionCode primitive type. Fix constraint checking.

This commit is contained in:
Joeri Exelmans 2024-10-07 16:08:23 +02:00
parent 0785b9218e
commit 59de61d0a3
11 changed files with 256 additions and 82 deletions

View file

@ -24,11 +24,13 @@ _NL: /(\r?\n[\t ]*)+/
literal: INT
| STR
| BOOL
| CODE
INT: /[0-9]+/
STR: /"[^"]*"/
| /'[^']*'/
BOOL: "True" | "False"
CODE: /`[^`]*`/
object: [IDENTIFIER] ":" IDENTIFIER [link_spec] _NL [_INDENT slot+ _DEDENT]
link_spec: "(" IDENTIFIER "->" IDENTIFIER ")"
@ -46,6 +48,12 @@ class TreeIndenter(Indenter):
parser = Lark(grammar, parser='lalr', postlex=TreeIndenter())
# internal use only
# just a dumb wrapper to distinguish between code and string
class _Code:
def __init__(self, code):
self.code = code
# given a concrete syntax text string, and a meta-model, parses the CS
def parse_od(state, cs_text, mm):
tree = parser.parse(cs_text)
@ -70,7 +78,10 @@ def parse_od(state, cs_text, mm):
return token == "True"
def STR(self, token):
return str(token[1:-1]) # strip the ""
return str(token[1:-1]) # strip the "" or ''
def CODE(self, token):
return _Code(str(token[1:-1])) # strip the ``
def literal(self, el):
return el[0]
@ -111,9 +122,12 @@ def parse_od(state, cs_text, mm):
tgt = od.create_integer_value(value_name, value)
elif isinstance(value, str):
tgt = od.create_string_value(value_name, value)
elif isinstance(value, _Code):
tgt = od.create_actioncode_value(value_name, value.code)
else:
raise Exception("Unimplemented type "+value)
od.create_slot(attr_name, obj_name, tgt)
return obj_name
t = T(visit_tokens=True).transform(tree)