pythontextx

TextX retreiving object parent types


Assume that we have two rules like this in our grammar:

Value: AttributeReference | PrimitiveValue;
PrimitiveValue: BoolValue | StringValue | IntValue;

I want to implement a code generator for this language. Basically, for each rule, I defined a function that does that. For example, code_gen_value function gets an object and if the type of the object is AttributeReference then it will call code_gen_attribute_reference (passing the object), or if the type is PrimitiveValue, then it calls code_gen_primitive_value (passing the object). The issue is that I am using the following to retrieve the type of the object:

value_type = cname(value) where:

def cname(o):
     return o.__class__.__name__

However, when I run it on an example, the value_type is StringValue in code_gen_value function for the input object. My question is, how can I check for the object to be a subtype of the PrimitiveValue?

In XTexnd, one could use instanceof keyword and actually if obj instanceof PrimitiveValue would work in the code_gen_value. I am wondering how we can express that in TextX.

Is there a way to refer to the types in the metamodel generated for our grammar?

Thanks!


Solution

  • To check if object is an instance of some type according to the inheritance specified by the textX grammar you can use textx_isinstance. In the current release of textX (2.2.0) it can be imported from textx.scoping.tools. Starting from 2.3.0 it will be available in the textx module.