Ejemplo n.º 1
0
 def test_init(self):
     ast = AST()
     data = list(reversed([(0, 0), (1, 2), (2, 4), (3, 6), (4, 8),
                           (5, 10)]))
     for k, v in data:
         ast[k] = v
     self.assertEqual(data, list(ast.items()))
Ejemplo n.º 2
0
 def test_iter(self):
     ast = AST()
     ast['name'] = 'hello'
     ast['name'] = 'world'
     ast['value'] = 1
     self.assertEqual(['name', 'value'], list(ast))
     self.assertEqual([['hello', 'world'], 1], list(ast.values()))
Ejemplo n.º 3
0
 def elements(self, ast):
     elements = [e for e in ast if e is not None]
     if not elements:
         return model.Void()
     elif len(elements) == 1:
         return elements[0]
     else:
         return model.Sequence(AST(sequence=elements))
Ejemplo n.º 4
0
 def __init__(self, ast=None, exp=None, **kwargs):
     if exp is not None:
         self.exp = exp
     elif not isinstance(ast, AST):
         # Patch to avoid bad interactions with attribute setting in Model.
         # Also a shortcut for subexpressions that are not ASTs.
         ast = AST(exp=ast)
     super(Decorator, self).__init__(ast)
     assert isinstance(self.exp, Model)
Ejemplo n.º 5
0
    def __init__(self,
                 semantics=None,
                 parseinfo=False,
                 trace=False,
                 encoding='utf-8',
                 comments_re=None,
                 eol_comments_re=None,
                 whitespace=None,
                 ignorecase=False,
                 nameguard=None,
                 memoize_lookaheads=True,
                 left_recursion=True,
                 trace_length=72,
                 trace_separator=':',
                 trace_filename=False,
                 colorize=False,
                 keywords=None,
                 namechars='',
                 **kwargs):
        super(ParseContext, self).__init__()

        self._buffer = None
        self.semantics = semantics
        self.encoding = encoding
        self.parseinfo = parseinfo
        self.trace = trace
        self.trace_length = trace_length
        self.trace_separator = trace_separator
        self.trace_filename = trace_filename

        self.comments_re = comments_re
        self.eol_comments_re = eol_comments_re
        self.whitespace = whitespace
        self.ignorecase = ignorecase
        self.nameguard = nameguard
        self.memoize_lookaheads = memoize_lookaheads
        self.left_recursion = left_recursion
        self.namechars = namechars

        self._ast_stack = [AST()]
        self._concrete_stack = [None]
        self._rule_stack = []
        self._cut_stack = [False]
        self._memoization_cache = dict()

        self._last_node = None
        self._state = None
        self._lookahead = 0

        self._recursive_results = dict()
        self._recursive_eval = []
        self._recursive_head = []

        self.colorize = colorize
        self.keywords = set(keywords or [])
Ejemplo n.º 6
0
    def test_add(self):
        ast = AST()
        ast['name'] = 'hello'
        self.assertIsNotNone(ast.name)
        self.assertEqual('hello', ast.name)

        ast['name'] = 'world'
        self.assertEqual(['hello', 'world'], ast.name)

        ast['value'] = 1
        self.assertEqual(1, ast.value)
Ejemplo n.º 7
0
 def effect(self, intercepted_ast):
     """ When encountering an effect rule, verify whether the body is a probabilistic effect.
     If not, convert it into one with a probability of 1 for the outcome. """
     # when the effects of an action does not have probabilistic components ...
     if 'prob_effects' not in intercepted_ast.args:
         # ... retrieve what the effect is ...
         conjunction = intercepted_ast.args
         # ... and convert it into a probabilistic effect with probability 1.
         intercepted_ast = AST({
             'args':
             AST({
                 'prob_effects':
                 [AST({
                     'conjunction': conjunction,
                     'value': 1
                 })]
             })
         })
     # push this change into the parse tree
     return intercepted_ast
Ejemplo n.º 8
0
 def head(self, ast):
     """
     Flatten lists in head and do not allow normal disjunctions.
     """
     if ast['disjunction']:
         raise SemanticError('Normal disjunctions are not allowed.')
     new_ast = AST()
     for item in ast:
         if ast[item]:
             new_ast[item] = list(flatten(ast[item]))
         else:
             new_ast[item] = None
     return new_ast
Ejemplo n.º 9
0
    def _initialize_caches(self):
        self._ast_stack = [AST()]
        self._concrete_stack = [None]
        self._rule_stack = []
        self._cut_stack = [False]
        self._memoization_cache = dict()

        self._last_node = None
        self._state = None
        self._lookahead = 0

        self._recursive_results = dict()
        self._recursive_eval = []
        self._recursive_head = []
Ejemplo n.º 10
0
 def __init__(self,
              ast,
              name,
              exp,
              base,
              params,
              kwparams,
              decorators=None):
     super(BasedRule, self).__init__(ast,
                                     name,
                                     exp,
                                     params or base.params,
                                     kwparams or base.kwparams,
                                     decorators=decorators)
     self.base = base
     ast = AST(sequence=[self.base.exp, self.exp])
     ast._parseinfo = self.base.parseinfo
     self.rhs = Sequence(ast)
Ejemplo n.º 11
0
 def paragraph_only_br(self, ast):
     el = etree.Element("br")
     el.tail = "\n"
     return AST({"content": [el]})
Ejemplo n.º 12
0
 def test_empty(self):
     ast = AST()
     self.assertIsNone(ast.name)
Ejemplo n.º 13
0
 def negative(self, ast):
     neg = model.NegativeLookahead(ast)
     any = model.Pattern('.')
     return model.Sequence(AST(sequence=[neg, any]))
Ejemplo n.º 14
0
 def test_ast(self):
     ast = AST()
     self.assertEqual([], list(ast.items()))
     self.assertTrue(hasattr(ast, '__json__'))
Ejemplo n.º 15
0
 def __init__(self, ast=None, **kwargs):
     super(Comment, self).__init__(ast=AST(comment=ast))
Ejemplo n.º 16
0
 def _push_ast(self):
     self._push_cst()
     self._ast_stack.append(AST())
Ejemplo n.º 17
0
    def _reset(self,
               text=None,
               filename=None,
               semantics=None,
               trace=None,
               comments_re=None,
               eol_comments_re=None,
               whitespace=None,
               ignorecase=None,
               nameguard=None,
               memoize_lookaheads=None,
               left_recursion=None,
               colorize=False,
               namechars='',
               **kwargs):
        if ignorecase is None:
            ignorecase = self.ignorecase
        if nameguard is None:
            nameguard = self.nameguard
        if memoize_lookaheads is not None:
            self.memoize_lookaheads = memoize_lookaheads
        if left_recursion is not None:
            self.left_recursion = left_recursion

        if trace is not None:
            self.trace = trace
        if semantics is not None:
            self.semantics = semantics

        if colorize is not None:
            self.colorize = colorize

        if self.colorize:
            color.init()

        if isinstance(text, buffering.Buffer):
            buffer = text
        else:
            buffer = buffering.Buffer(
                text,
                filename=filename,
                comments_re=comments_re or self.comments_re,
                eol_comments_re=eol_comments_re or self.eol_comments_re,
                whitespace=notnone(whitespace, default=self.whitespace),
                ignorecase=ignorecase,
                nameguard=nameguard,
                namechars=namechars or self.namechars,
                **kwargs)
        self._buffer = buffer
        self._ast_stack = [AST()]
        self._concrete_stack = [None]
        self._rule_stack = []
        self._cut_stack = [False]
        self._memoization_cache = dict()

        self._last_node = None
        self._state = None
        self._lookahead = 0

        self._recursive_results = dict()
        self._recursive_eval = []
        self._recursive_head = []
Ejemplo n.º 18
0
 def __init__(self, ast=None, **kwargs):
     if not isinstance(ast, AST):
         ast = AST(exp=ast)
     super(_Decorator, self).__init__(ast)
     assert isinstance(self.exp, Model)
Ejemplo n.º 19
0
 def __init__(self, ast=None, **kwargs):
     super(OverrideList, self).__init__(ast=AST(name='@', exp=ast))
Ejemplo n.º 20
0
 def __init__(self, ast=None, **kwargs):
     super(Choice, self).__init__(ast=AST(options=ast))
     assert isinstance(self.options, list), urepr(self.options)