You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I am playing around with the Java grammar and models/listeners, where the lexer grammar contains the following rules:
IntegerLiteral:
DecimalIntegerLiteral
| HexIntegerLiteral
| OctalIntegerLiteral
| BinaryIntegerLiteral
;
fragment DecimalIntegerLiteral: DecimalNumeral IntegerTypeSuffix?;
fragment HexIntegerLiteral: HexNumeral IntegerTypeSuffix?;
fragment OctalIntegerLiteral: OctalNumeral IntegerTypeSuffix?;
fragment BinaryIntegerLiteral: BinaryNumeral IntegerTypeSuffix?;
Suppose I want the quantifier in DecimalIntegerLiteral to be treated differently from the other quantifiers, how is this possible?
The model's quantify method only receives the parent node (the node representing IntegerLiteral) and idx is 0 for all quantifiers as they're the first in the fragment.
The text was updated successfully, but these errors were encountered:
First of all, you need to checkout the latest master of Grammarinator, since it contains a fix which is needed to handle sub-lexer rules properly.
From this point, you simply need to define a custom DispatchingModel that overrides the quantifier handling of the chosen rule. In you case, you need something like this:
java_model.py:
importrandomfromgrammarinator.runtimeimportDispatchingModelclassJavaModel(DispatchingModel):
defquantify_DecimalIntegerLiteral(self, node, idx, cnt, start, stop):
print('Custom decision for the quantifiers of DecimalIntegerLiteral')
ifidx==0: # There is only one quantifier in this rule, hence this condition would not be necessarily.ifcnt<stop: # Do not exceed the maximum allowed quantified items.# Implement your decision mechanism here and return True or False accordingly.returnrandom.choice([True, False])
returnFalse
Finally, register this modul as a model of grammarinator-generate (and don't forget to add the directory of the model to PYTHONPATH with the --sys-path argument):
I am playing around with the Java grammar and models/listeners, where the lexer grammar contains the following rules:
Suppose I want the quantifier in
DecimalIntegerLiteral
to be treated differently from the other quantifiers, how is this possible?The model's
quantify
method only receives the parent node (the node representingIntegerLiteral
) andidx
is 0 for all quantifiers as they're the first in the fragment.The text was updated successfully, but these errors were encountered: