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()))
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()))
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()))
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.assertEquals(data, list(ast.items()))
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)
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))
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)
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)
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 [])
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)
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
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
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 = []
def __init__(self, ast=None, **kwargs): super(Choice, self).__init__(ast=AST(options=ast)) assert isinstance(self.options, list), urepr(self.options)
def test_empty(self): ast = AST() self.assertIsNone(ast.name)
def test_ast(self): ast = AST() self.assertEquals([], list(ast.items())) self.assertTrue(hasattr(ast, '__json__'))
def test_ast(self): ast = AST() self.assertEqual([], list(ast.items())) self.assertTrue(hasattr(ast, '__json__'))
def _push_ast(self): self._push_cst() self._ast_stack.append(AST())
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 = []
def __init__(self, ast=None, **kwargs): super(OverrideList, self).__init__(ast=AST(name='@', exp=ast))
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)
def __init__(self, ast=None, **kwargs): super(Comment, self).__init__(ast=AST(comment=ast))
def negative(self, ast): neg = model.NegativeLookahead(ast) any = model.Pattern('.') return model.Sequence(AST(sequence=[neg, any]))
def paragraph_only_br(self, ast): el = etree.Element("br") el.tail = "\n" return AST({"content": [el]})