Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to get the value of current ? #55

Closed
david542542 opened this issue Oct 27, 2022 · 2 comments
Closed

How to get the value of current ? #55

david542542 opened this issue Oct 27, 2022 · 2 comments

Comments

@david542542
Copy link

I've been having a bit of trouble getting the current string-value of a lex token. Here is what I'm currently doing:

from ExprGenerator import ExprGenerator
from grammarinator.runtime import *
import random

class MyExprGenerator(ExprGenerator):

    def __init__(self, **kwargs):
        super().__init__(**kwargs)

    def Int(self, parent=None):
        with RuleContext(self, UnlexerRule(name='Int', parent=parent)) as current:
            UnlexerRule(src=str(random.random()), parent=current)

            # such a hackish way to get the current value?
            _current_val = "{}".format(current)
            if float(_current_val) < 0.5: return self.Int(parent=parent)

            return current

Is there a better way to get current ? I tried a few things, but I was basically getting a parent/child node or None, for example:

>>> current.__dict__
# {'name': 'Int', 'parent': <grammarinator.runtime.tree.UnparserRule object at 0x107f0deb0>, 'children': [<grammarinator.runtime.tree.UnlexerRule object at 0x107f1ca90>], 'level': None, 'depth': None, 'src': None}

What's the suggested way to get the current value? What I'm trying to do is validation, for example, make sure a number is > 0.5, which is pretty difficult to do directly from the antlr grammar, so I'm doing some additional validation in the listeners/generators. Thanks so much for your time and help!

David

@renatahodovan
Copy link
Owner

If your only goal is to ensure that Int generates numbers bigger than 0.5, then there is a simpler way:

def Int(self, parent=None):
    with RuleContext(self, UnlexerRule(name='Int', parent=parent)) as current:
        UnlexerRule(src=str(random.random() + 0.5), parent=current)
        return current

If you like/need your approach then:

def Int(self, parent=None):
    with RuleContext(self, UnlexerRule(name='Int', parent=parent)) as current:
        value = UnlexerRule(src=str(random.random()), parent=current)
        if float(str(current)) < 0.5: return self.Int(parent=parent)
            # Or ...
            # if float(value.src) < 0.5: return self.Int(parent=parent)
            return current

@david542542
Copy link
Author

@renatahodovan thanks so much that works. By the way, I sent you an email to your contact on Github about a few questions if that's ok -- thanks again for your help!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants