Skip to content

Commit

Permalink
improving error messages for grammars
Browse files Browse the repository at this point in the history
  • Loading branch information
jonrkarr committed Jan 13, 2020
1 parent fc4069d commit a8d0f92
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions wc_lang/core.py
Expand Up @@ -246,6 +246,8 @@ def gbl(self, *args):
for arg in args:
if isinstance(arg, lark.lexer.Token) and \
arg.type == 'SPECIES_COEFFICIENT__SPECIES__COMPARTMENT__ID':
if arg.value not in self.objects.get(Compartment, {}):
raise ValueError('Model must contain compartment with id "{}"'.format(arg.value))
comp = self.get_or_create_model_obj(
Compartment, _serialized_val=arg.value)

Expand All @@ -258,9 +260,12 @@ def gbl(self, *args):
for part in arg.children[0]:
st = part['st']
coeff = sign * part['coeff']
s_id = Species._gen_id(st.id, comp.id)
if s_id not in self.objects.get(Species, {}):
raise ValueError('Model must contain species with id "{}"'.format(s_id))
s = self.get_or_create_model_obj(
Species,
_serialized_val=Species._gen_id(st.id, comp.id))
_serialized_val=s_id)
spec_coeff = self.get_or_create_model_obj(
SpeciesCoefficient,
_serialized_val=SpeciesCoefficient._serialize(s, coeff),
Expand All @@ -281,6 +286,8 @@ def gbl_part(self, *args):
coeff = 1.
for arg in args:
if arg.type == 'SPECIES_COEFFICIENT__SPECIES__SPECIES_TYPE__ID':
if arg.value not in self.objects.get(SpeciesType, {}):
raise ValueError('Model must contain species type with id "{}"'.format(arg.value))
st = self.get_or_create_model_obj(
SpeciesType, _serialized_val=arg.value)
elif arg.type == 'SPECIES_COEFFICIENT__COEFFIFICIENT':
Expand Down Expand Up @@ -319,19 +326,26 @@ def lcl_part(self, *args):
coeff = 1.
for arg in args:
if arg.type == 'SPECIES_COEFFICIENT__SPECIES__SPECIES_TYPE__ID':
if arg.value not in self.objects.get(SpeciesType, {}):
raise ValueError('Model must contain species type with id "{}"'.format(arg.value))
st = self.get_or_create_model_obj(
SpeciesType,
_serialized_val=arg.value)
elif arg.type == 'SPECIES_COEFFICIENT__SPECIES__COMPARTMENT__ID':
if arg.value not in self.objects.get(Compartment, {}):
raise ValueError('Model must contain compartment with id "{}"'.format(arg.value))
comp = self.get_or_create_model_obj(
Compartment,
_serialized_val=arg.value)
elif arg.type == 'SPECIES_COEFFICIENT__COEFFIFICIENT':
coeff = float(arg.value)

s_id = Species._gen_id(st.id, comp.id)
if s_id not in self.objects.get(Species, {}):
raise ValueError('Model must contain species with id "{}"'.format(s_id))
s = self.get_or_create_model_obj(
Species,
_serialized_val=Species._gen_id(st.id, comp.id))
_serialized_val=s_id)
return {'s': s, 'coeff': coeff}

def validate(self, obj, value, tolerance=1E-10):
Expand Down

0 comments on commit a8d0f92

Please sign in to comment.