Esempio n. 1
0
    def eval(self, bundle, leader):
        # Stack args in symtable
        bundle.symtree.push_scope(self.name, self.name)
        i = 0
        argref = []
        if not isinstance(self.arguments, EmptyCollection):
            symset = set()
            for a in self.arguments.value:
                symset.add(a.name)
                ar = FuncArgReference(a, a.name, i)
                bundle.symtree.register_symbol(a.name, ar)
                argref.append(ar)
                i += 1
            if len(symset) != len(self.arguments.value):
                raise errors.ParseError(
                    "{}:{} Repeating symbol in function args not allowed".
                    format(self.token.getsourcepos().lineno,
                           self.token.getsourcepos().colno))

        # Eval children
        expr = []
        for e in self.value:
            LOGGER.info("Processing {}".format(e))
            e.eval(bundle, expr)
        leader.append(
            ParseFunction(ExpressionType.FUNCTION,
                          expr,
                          self.token,
                          self.ident,
                          self.private,
                          pre=argref))
        # Pop stack
        bundle.symtree.pop_scope()
Esempio n. 2
0
    def eval(self, bundle, leader):
        bundle.symtree.push_scope(self.name, self.name)
        i = 0
        argref = []
        if not isinstance(self.arguments, EmptyCollection):
            symset = set()
            for a in self.arguments.value:
                symset.add(a.name)
                ar = FuncArgReference(a, a.name, i)
                bundle.symtree.register_symbol(a.name, ar)
                argref.append(ar)
                i += 1
            if len(symset) != len(self.arguments.value):
                raise errors.ParseError(
                    "{}:{} Repeating symbol in lambda args not allowed".format(
                        self.token.getsourcepos().lineno,
                        self.token.getsourcepos().colno))

        # Eval expression
        expr = []
        for e in self.value:
            e.eval(bundle, expr)
        # Append Body
        plambda, plamdaref = refactor_lambda(
            self, self.token.getsourcepos(), argref, expr, [
                FuncArgReference, LetArgReference, MatchExprReference,
                LetResReference, MatchResReference
            ], [LiteralReference, VarReference])
        # [print("Lamb Arg {}".format(n)) for n in plambda.pre]
        bundle.lambdas.append(plambda)
        # Pop stack
        bundle.symtree.pop_scope()
        # Append Reference
        leader.append(plamdaref)
Esempio n. 3
0
    def parse_function(self, token, frame):
        symbol = None
        arguments = None

        # Edge - func token only at end
        if not frame:
            _malformed(token)
        # Check first for private
        if isinstance(frame[0], ast.LiteralReference):
            if frame[0].value == ":private":
                frame.pop(0)
            # Should never get here due to pre-parse, but
            else:
                raise errors.ParseError(
                    "{}:{} Function type only supports :private keyword."
                    " Found {}".format(token.getsourcepos().lineno,
                                       token.getsourcepos().colno,
                                       frame[0].value))
        # Should never get here due to pre-parse, but
        if not frame:
            _panic("{}:{} Missing symbol for {}", token)

        if isinstance(frame[0], ast.Symbol):
            symbol = frame.pop(0)
        else:
            _panic("{}:{} Expected symbol for {}", token)

        # Should never get here due to pre-parse, but
        if not frame:
            _panic("{}:{} Missing function arguments", token)
        elif not isinstance(frame[0], ast.CollectionAst):
            _panic("{}:{} Expected function arguments", token)
        else:
            arguments = frame.pop(0)

        # Verify all in collection are symbols
        if not isinstance(arguments, ast.EmptyCollection):
            for x in arguments.value:
                if ((not isinstance(x, ast.Symbol)
                     or x.token.gettokentype() not in self._strict_symtokens)):
                    _panic("{}:{} Invalid symbol in arguments for {}", token)

        expressions = []
        index = 0
        # Get all expressions in function
        for x in frame:
            if not isinstance(x, self._decl_set):
                expressions.append(x)
                index += 1
            else:
                break
        frame = frame[index:]

        hdr = self._locals.get(symbol.name, None)
        if not hdr:
            _panic("{}:{} Function {} not in symbol table", symbol.token)

        frame.insert(0, ast.Function(hdr, expressions, self.input))
        return frame
Esempio n. 4
0
 def __init__(self, value, token, src):
     super().__init__(token, src)
     self._prefix = None
     if not isinstance(value, EmptyCollection):
         if len(value.value) % 2 != 0:
             raise errors.ParseError("{}:{} Uneven let pairs".format(
                 token.getsourcepos().lineno,
                 token.getsourcepos().colno))
     self.value = value
Esempio n. 5
0
    def __init__(self, symbol, lpairs, expr, token, src):
        super().__init__(token, src)
        self._id = symbol
        self._letpairs = lpairs

        self.value = expr if type(expr[0]) is not list else expr[0]

        if len(self.value) > 1:
            raise errors.ParseError(
                "Let expects 1 expression in body, found {}".format(
                    len(self.value)))
Esempio n. 6
0
 def eval(self, bundle, leader):
     sym = bundle.symtree.resolve_symbol(self.name)
     if sym:
         leader.append(sym)
         # leader.append(
         #     ParseMatchExpression(
         #         ExpressionType.MATCH_EXPR,
         #         [sym],
         #         self.token))
     else:
         raise errors.ParseError(
             "Can not result match expression reference {}".format(
                 self.name))
 def _ParseAttribute(self, suite_element, attribute_name, mandatory,
                     default_value=None):
   if suite_element.hasAttribute(attribute_name):
     value = suite_element.getAttribute(attribute_name)
     if default_value in (True, False):
       value = value.lower() == "true"
   elif mandatory:
     error_msg = ('Could not find attribute %s in %s' %
                  (attribute_name, self.TAG_NAME))
     raise errors.ParseError(msg=error_msg)
   else:
     value = default_value
   return value
Esempio n. 8
0
    def parse_variable(self, token, frame):
        """Variable parse. Productions:

        var symbol
        var symbol expression (not = FUNC, VAR, INCLUDE, MODULE)
        var :private symbol
        var :private symbol expression (not = FUNC, VAR, INCLUDE, MODULE)

        if there is an expression, there must be only one
        """
        symbol = None
        expression = None
        # Edge - var token only at end
        if not frame:
            _malformed(token)

        # Check first for private
        if isinstance(frame[0], ast.LiteralReference):
            if frame[0].value == ":private":
                frame.pop(0)
            # Should never get here due to pre-parse, but
            else:
                raise errors.ParseError(
                    "{}:{} Variable type only supports :private keyword."
                    " Found {}".format(token.getsourcepos().lineno,
                                       token.getsourcepos().colno,
                                       frame[0].value))

        # Should never get here due to pre-parse
        if not frame:
            _panic("{}:{} Missing symbol for {}", token)

        if isinstance(frame[0], ast.Symbol):
            symbol = frame.pop(0)
        # Should never get here due to pre-parse
        else:
            _panic("{}:{} Expected symbol for {}", token)

        # Check for expression existance
        if frame:
            if not isinstance(frame[0], self._decl_set):
                # TODO: Get until var, func, include or end
                expression = [frame.pop(0)]

        hdr = self._locals.get(symbol.name, None)
        if not hdr:
            _panic("{}:{} Variable {} not in symbol table", symbol.token)
        frame.insert(0, ast.Variable(hdr, expression, token, self.input))
        return frame
Esempio n. 9
0
 def __init__(self, vhdr, value, token, src):
     super().__init__(token, src)
     self._name = vhdr.name
     self._private = vhdr.private
     self._vref = vhdr.reference
     self._ident = vhdr.ident
     self._reference = vhdr.get_reference()
     if value:
         if type(value[0]) is list:
             value = value[0]
         if len(value) != 1:
             raise errors.ParseError("variable '{}' at line {} has"
                                     "more than 1 expression".format(
                                         vname.value,
                                         token.getsourcepos().lineno))
     else:
         value = [_NIL]
     self._value = value
Esempio n. 10
0
    def eval(self, bundle, leader):
        # Put symbols in table and evaluate expression

        if not isinstance(self.value, EmptyCollection):
            symset = set()
            for i, j in zip(*[iter(self.value.value)] * 2):
                expr = []
                j.eval(bundle, expr)
                symset.add(i.value)
                ident = self.prefix + "_" + i.value
                lar = LetArgReference(i)
                lar.ident = ident
                bundle.symtree.register_symbol(i.value, lar)
                leader.append(
                    ParseLetPair(ExpressionType.LET_ARG_PAIR,
                                 expr,
                                 res=lar,
                                 name=ident))
            if len(symset) != len(self.value.value) / 2:
                raise errors.ParseError(
                    "{}:{} Repeating symbol in let args not allowed".format(
                        self.token.getsourcepos().lineno,
                        self.token.getsourcepos().colno))
Esempio n. 11
0
 def _eval_partial(self, ktype, array, bundle, leader):
     errors.ParseError("_eval_partial unhandled for {}".format(ktype))
 def _GetRootElement(self, doc):
     root_elements = doc.getElementsByTagName("test-definitions")
     if len(root_elements) != 1:
         error_msg = "expected 1 and only one test-definitions tag"
         raise errors.ParseError(msg=error_msg)
     return root_elements[0]
Esempio n. 13
0
def _unexpected_declaration(token, decltype):
    raise errors.ParseError(
        "{}:{} Unexpected type {} found in expression {}".format(
            token.getsourcepos().lineno,
            token.getsourcepos().colno, decltype, token.gettokentype()))
Esempio n. 14
0
def _panic(cause, token):
    raise errors.ParseError(
        cause.format(token.getsourcepos().lineno,
                     token.getsourcepos().colno, token.gettokentype()))