Ejemplo n.º 1
0
def parse_type(string: str, location: int, tokens: ParseResults) -> Type:
    try:
        name = tokens[1]
        full_name = f"__PACKAGE__.{name}"
        if tokens[3] == "mod":
            return ModularInteger(full_name, *tokens[4:6])
        if tokens[3] == "range":
            tokens[6] = tokens[6]["size"]
            return RangeInteger(full_name, *tokens[4:7])
        if tokens[3] == "message":
            return MessageSpec(full_name, tokens[4])
        if tokens[3] == "null message":
            return MessageSpec(full_name, [])
        if tokens[3] == "(":
            elements = dict(tokens[4:-2])
            aspects = tokens[-1]
            if len(elements) < len(tokens[4:-2]):
                raise ModelError(f'"{name}" contains duplicate elements')
            if "always_valid" not in aspects:
                aspects["always_valid"] = False
            return Enumeration(full_name, elements, aspects["size"],
                               aspects["always_valid"])
        if tokens[3] == "new":
            return DerivationSpec(full_name, tokens[4])
        if tokens[3] == "array of":
            return Array(
                full_name,
                Reference(tokens[4] if "." in
                          tokens[4] else f"__PACKAGE__.{tokens[4]}"))
    except ModelError as e:
        raise ParseFatalException(string, location, e)
    raise ParseFatalException(string, location, "unexpected type")
Ejemplo n.º 2
0
        def key_parse_action(pstr, loc, toks):
            key = toks[0]

            if key in parse_ctx.keys:
                raise ParseFatalException(
                    pstr, loc,
                    "duplicate key '%s' value definition" % parse_ctx.key)

            parse_ctx.key = key
            parse_ctx.keys.append(key)

            try:
                parse_ctx.key_grammar = parse_ctx.fragment.get_key_grammars(
                )[key]
                key_grammar = parse_ctx.key_grammar.grammar
            except KeyError:
                raise ParseFatalException(
                    pstr, loc, "key '%s' is not supported by fragment" % key)
            except Exception as e:
                raise ParseFatalException(
                    pstr, loc,
                    "unable to parse key '%s'; %s" % (key, e.message))

            key_stmt << (conditional
                         | Group(key_grammar).setResultsName("value"))

            return None
Ejemplo n.º 3
0
def parse_type(string: str, location: int, tokens: list) -> Type:
    try:
        if tokens[3] == 'mod':
            return ModularInteger(tokens[1], *tokens[4:6])
        if tokens[3] == 'range':
            tokens[6] = tokens[6]['size']
            return RangeInteger(tokens[1], *tokens[4:7])
        if tokens[3] == 'message':
            return Message(tokens[1], tokens[4])
        if tokens[3] == '(':
            elements = dict(tokens[4:-2])
            aspects = tokens[-1]
            if len(elements) < len(tokens[4:-2]):
                raise ModelError(f'"{tokens[1]}" contains duplicate elements')
            if 'always_valid' not in aspects:
                aspects['always_valid'] = False
            return Enumeration(tokens[1], elements, aspects['size'],
                               aspects['always_valid'])
        if tokens[3] == 'new':
            if len(tokens) == 7:
                tokens.append(TRUE)
            return Refinement(tokens[1], *tokens[4:])
        if tokens[3] == 'array of':
            return Array(tokens[1], tokens[4])
    except ModelError as e:
        raise ParseFatalException(string, location, e)
    raise ParseFatalException(string, location, 'unexpected type')
Ejemplo n.º 4
0
        def key_body_parsed(pstr, loc, toks):
            stmts = list()
            expand_conditionals(toks, stmts)

            if parse_ctx.key_grammar.min and len(
                    stmts) < parse_ctx.key_grammar.min:
                raise ParseFatalException(
                    pstr, loc,
                    "fragment requires at least %d values for key '%s'" %
                    (parse_ctx.key_grammar.min, parse_ctx.key))

            if parse_ctx.key_grammar.max and len(
                    stmts) > parse_ctx.key_grammar.max:
                raise ParseFatalException(
                    pstr, loc,
                    "fragment requires at most %d values for key '%s'" %
                    (parse_ctx.key_grammar.max, parse_ctx.key))

            try:
                parse_ctx.fragment.set_key_value(parse_ctx.key, stmts)
            except Exception as e:
                raise ParseFatalException(
                    pstr, loc,
                    "unable to add key '%s'; %s" % (parse_ctx.key, e.message))
            return None
        def opt_parse(strng, loc, toks):
            """Check if the option exist and if she's correct for the current instruction"""

            if toks[0] not in OPTIONAL_OPTION_CONFIG:
                raise ParseFatalException(
                    DOCKERFILE_ERROR[216].format(opt=toks[0]), loc=loc)
            if self.currentInstructionName not in OPTIONAL_OPTION_CONFIG[
                    toks[0]]:
                raise ParseFatalException(
                    DOCKERFILE_ERROR[217].format(opt=toks[0]), loc=loc)
Ejemplo n.º 6
0
 def convertToTriple(self,tokenStr,location,group):
     '''
     convert the given token to a triple
     
     Args:
         tokenStr(str): the token string
         location(object): location of the parse process
         group(ParseResults): the expected triple defining group
     ''' 
     tripleKind=group.getName()
     tokens=group[0]
     tokenCount=len(tokens)
     if tokenCount!=3:
         raise ParseException(tokenStr, location, "invalid triple %s: %d tokens found 3 expected" % (tripleKind,tokenCount))  
     e1=tokens[0]
     e2=tokens[1]
     e3=tokens[2]
     if tripleKind=="isValue":
         #'"Paris" is capital of France'
         triple=Triple(e1,e2,e3,location)
     elif tripleKind=="idLink":
         #'Paris capital France'
         triple=Triple(e1,e2,e3,location)
     elif tripleKind=="isLink":
         #'Paris is capital of France'
         triple=Triple(e1,e2,e3,location)
     elif tripleKind=="hasLink":
         #'France has capital Paris'
         triple=Triple(e3,e2,e1,location)
     else:
         raise ParseFatalException(tokenStr, location, "invalid tripleKind %s" %tripleKind)  
     return triple
Ejemplo n.º 7
0
        def parsed_deprecated_mapping(pstr, loc, toks):
            fragment = Mapping()
            fragment.archive = toks[0].archive
            fragment.name = re.sub(r"[^0-9a-zA-Z]+", "_", fragment.archive)

            fragment.entries = set()
            condition_true = False
            for entries in toks[0].entries[0]:
                condition  = next(iter(entries.condition.asList())).strip()
                condition_val = sdkconfig.evaluate_expression(condition)

                if condition_val:
                    for entry in entries[1]:
                        fragment.entries.add((entry.object, None if entry.symbol == '' else entry.symbol, entry.scheme))
                    condition_true = True
                    break

            if not fragment.entries and not condition_true:
                try:
                    entries = toks[0].entries[1][1]
                except IndexError:
                    entries = toks[0].entries[1][0]
                for entry in entries:
                    fragment.entries.add((entry.object, None if entry.symbol == '' else entry.symbol, entry.scheme))

            if not fragment.entries:
                fragment.entries.add(("*", None, "default"))

            dep_warning = str(ParseFatalException(pstr, loc,
                              "Warning: Deprecated old-style mapping fragment parsed in file %s." % fragment_file))

            print(dep_warning)
            return fragment
Ejemplo n.º 8
0
def convert_group(tokens):
    """Converts parseResult from to ConfGroup type."""
    tok = tokens.asList()
    dic = dict(tok)
    if not (len(dic) == len(tok)):
        raise ParseFatalException("Names in group must be unique: %s" % tokens)
    return ConfGroup(dic)
 def error(s, loc, expr, error):
     """Main error template"""
     raise ParseFatalException(DOCKERFILE_ERROR[202].format(
         ligne=self.line_counter,
         colonne=error.loc,
         inst=self.currentInstructionName,
         erreur=error.msg))
Ejemplo n.º 10
0
    def parse_action(s, loc, tokens):
        identifier = tokens[0]
        args = tuple()
        kwargs = {}

        if len(tokens) == 2:
            args, kwargs = tokens[1]
            args = tuple(args)

        if not identifier in Extension.registrar:
            raise ParseException('Unknown extension contract %r' % identifier)

        from contracts.library.separate_context import SeparateContext

        contract_ext = Extension.registrar[identifier]

        if isinstance(contract_ext, CheckCallable):
            callable_thing = contract_ext.callable

            test_args = ('value', ) + args
            from contracts.main import check_callable_accepts_these_arguments
            from contracts.main import InvalidArgs

            try:
                check_callable_accepts_these_arguments(callable_thing,
                                                       test_args, kwargs)

            except InvalidArgs as e:
                msg = 'The callable %s cannot accept these arguments ' % callable_thing
                msg += 'args = %s, kwargs = %s ' % (test_args, kwargs)
                msg += '%s' % e
                raise ParseFatalException(msg)

        where = W(s, loc)
        return Extension(identifier, where, args, kwargs)
Ejemplo n.º 11
0
def parse_mathematical_expression(string: str, location: int,
                                  tokens: ParseResults) -> Expr:
    result: List[Expr] = tokens[0]
    while len(result) > 1:
        left = result.pop(0)
        operator = result.pop(0)
        right = result.pop(0)
        assert left.location, f'expression "{left}" without location'
        assert right.location, f'expression "{right}" without location'
        assert left.location.source == right.location.source, "expression with different source"
        locn = Location(left.location.start, left.location.source,
                        left.location.end)
        expression: Expr
        if operator == "+":
            expression = Add(left, right)
            expression.location = locn
        elif operator == "-":
            expression = Sub(left, right, locn)
        elif operator == "*":
            expression = Mul(left, right)
            expression.location = locn
        elif operator == "/":
            expression = Div(left, right, locn)
        elif operator == "**":
            expression = Pow(left, right, locn)
        else:
            raise ParseFatalException(string, location,
                                      "unexpected mathematical operator")
        result.insert(0, expression)
    return result[0]
Ejemplo n.º 12
0
def convert_bool(tokens):
    t = tokens[0].lower()
    vals = {"true": True, "false": False}
    if not t in vals:
        raise ParseFatalException("bool incorrect: %s" % tokens[0])
    else:
        return vals[t]
 def _finish_block(self, parsed_str, location, toks):
     if toks[0] == self._current_block.get_name():
         self._blocks.append(self._current_block)
         self._current_block = None
     else:
         message = 'mismatched begin {0}/end {1}'.format(
             self._current_block.get_name(), toks[0])
         raise ParseFatalException(parsed_str, loc=location, msg=message)
Ejemplo n.º 14
0
def parse_attribute(string: str, location: int, tokens: list) -> Attribute:
    if tokens[2] == 'First':
        return First(tokens[0])
    if tokens[2] == 'Last':
        return Last(tokens[0])
    if tokens[2] == 'Length':
        return Length(tokens[0])
    raise ParseFatalException(string, location, 'unexpected attribute')
Ejemplo n.º 15
0
def parse_term(string: str, location: int,
               tokens: list) -> Union[Attribute, Number]:
    if isinstance(tokens[0], str):
        return Value(tokens[0])
    if isinstance(tokens[0], (Attribute, Number)):
        return tokens[0]
    raise ParseFatalException(string, location,
                              'expected identifier, attribute or number')
Ejemplo n.º 16
0
 def wrapper(string: str, location: int, tokens: ParseResults) -> object:
     try:
         return parse_function(string, location, tokens)
     except (ParseFatalException, RecordFluxError) as e:
         raise e
     except Exception as e:
         raise ParseFatalException(string, location,
                                   f"implementation error ({e})")
Ejemplo n.º 17
0
 def fragment_parse_action(pstr, loc, toks):
     key_grammars = parse_ctx.fragment.get_key_grammars()
     required_keys = set([k for (k,v) in key_grammars.items() if v.required])
     present_keys = required_keys.intersection(set(parse_ctx.keys))
     if present_keys != required_keys:
         raise ParseFatalException(pstr, loc, "required keys %s for fragment not found" %
                                   list(required_keys - present_keys))
     return parse_ctx.fragment
Ejemplo n.º 18
0
 def wrapper(string: str, location: int, tokens: list) -> Any:
     try:
         return parse_function(string, location, tokens)
     except ParseFatalException as e:
         raise e
     except Exception as e:
         raise ParseFatalException(string, location,
                                   f'implementation error ({e})')
Ejemplo n.º 19
0
def verifyLen(s, l, t):
    t = t[0]
    if t.len is not None:
        t1len = len(t[1])
        if t1len != t.len:
            raise ParseFatalException(s,l,\
                    "invalid data of length %d, expected %s" % (t1len, t.len))
    return t[1]
Ejemplo n.º 20
0
 def _handle_feature (self, string, location, tokens, features):
     feature = tokens['feature']
     # Always check base features, since a suprasegmental feature
     # may not have the same name as a base feature.
     if feature in features or feature in self._base_features:
         message = 'Feature name "{}" is duplicated'.format(feature)
         raise ParseFatalException(string, location, message)
     features.append(feature)
Ejemplo n.º 21
0
    def parse_archive(s, loc, toks):
        this = toks[0][0]
        if len(this) != 1:
            raise ParseFatalException(
                s, loc,
                'Could only specify one archive file in one mapping fragment')

        return this[0]
Ejemplo n.º 22
0
def parse_attribute(string: str, location: int,
                    tokens: ParseResults) -> Attribute:
    if tokens[2] == "First":
        return First(tokens[0])
    if tokens[2] == "Last":
        return Last(tokens[0])
    if tokens[2] == "Length":
        return Length(tokens[0])
    raise ParseFatalException(string, location, "unexpected attribute")
Ejemplo n.º 23
0
    def parse(cls, string, location, tokens):
        if len(tokens) > 3:
            msg = ('Cannot stack dice operators! Try disabiguating your '
                   'expression with parentheses,')
            raise ParseFatalException(string, tokens[3].location, msg)

        amount, kind, dicetype = tokens
        try:
            ret = dice_switch(amount, dicetype, kind)
            return ret.set_parse_attributes(string, location, tokens)
        except ValueError as e:
            if len(e.args) > 1:
                if type(e.args[1]) is int:
                    location = tokens[e.args[1]].location
                # unused as of yet
                # elif isinstance(e.args[1], Element):
                #     location = e.args[1].location
            raise ParseFatalException(string, location, e.args[0])
        def instructions_parse(strng, loc, toks):
            """Check if the instruction exist in the config file"""

            self.currentInstructionName = toks[0]

            if toks[0] not in INSTRUCTION_CONFIG_LIST:
                raise ParseFatalException(DOCKERFILE_ERROR[211], loc=loc)

            self.currentInstruction = INSTRUCTION_CONFIG_LIST[toks[0]]
        def args_num_parse(strng, loc, toks):
            """Check if the number of arguments is correct"""

            minArg = self.currentInstruction[1]
            maxArg = self.currentInstruction[2]
            nbArgs = len(toks)
            if (not minArg <= nbArgs <= maxArg):
                raise ParseFatalException(DOCKERFILE_ERROR[215].format(
                    nombre=nbArgs, min=minArg, max=maxArg),
                                          loc=loc)
Ejemplo n.º 26
0
    def parse(s, loc, toks):
        this = toks[0]

        name = this[0]
        entries = {entry for entry in this[1] if entry}

        if not entries:
            raise ParseFatalException(s, loc, 'Sections entries shouldn\'t be empty')

        return Sections(name, entries)
Ejemplo n.º 27
0
def convert_num(tokens):
    try:
        if sys.version_info[0] < 3 or tokens[0][-1] != 'L':
            res = literal_eval(tokens[0])
        else:
            ## Python3 does not support anymore L suffix for long integers
            res = literal_eval(tokens[0][:-1])
    except (SyntaxError, ValueError):
        raise ParseFatalException("Number incorrect: %s" % tokens[0])
    return res
Ejemplo n.º 28
0
def handleMacro(toks):
    macroChar = toks[0][1]
    if macroChar == "d":
        return CharacterRangeEmitter("0123456789")
    elif macroChar == "w":
        return CharacterRangeEmitter(srange("[A-Za-z0-9_]"))
    elif macroChar == "s":
        return LiteralEmitter(" ")
    else:
        raise ParseFatalException("",0,"unsupported macro character (" + macroChar + ")")
Ejemplo n.º 29
0
def handleMacro(toks):
    macroChar = toks[0][1]
    if macroChar == "d":
        return CharSetRandomizer(srange("[0-9]"))
    elif macroChar == "w":
        return CharSetRandomizer(srange("[A-Za-z0-9_]"))
    elif macroChar == "s":
        return CharSetRandomizer(" ")
    else:
        raise ParseFatalException(
            "", 0, "unsupported macro character (" + macroChar + ")")
Ejemplo n.º 30
0
def finish_block(parsed_str, location, token):
    global current_block, blocks
    if token[0] == current_block.get_name():
        blocks.append(current_block)
        current_block = None
    else:
        message = 'mismatched begin {0}/end {1}'
        block_name = current_block.get_name()
        raise ParseFatalException(parsed_str,
                                  loc=location,
                                  msg=message.format(block_name, token[0]))
Ejemplo n.º 31
0
	def __init__( self, tokens, line, pos, stack ):
		super(WhereBase, self).__init__(line, pos, stack);

		self.Modifier = self.Automatic;

		try:
			if( tokens[0].lower() in self.Modifiers.keys() ):
				self.Modifier = tokens[0].lower();
				self.AddItems( tokens[2:] );
			else:
				self.AddItems( tokens[1:] );

		except ValueError as ve:
			e = ParseFatalException(ve.item.pi['line'], ve.item.pi['pos'], str(ve), self);
			e.stack = ve.item.pi['stack'];
			raise e;

		# Validate sub-class has implemented all Matches* functions
		for Modifier in self.Modifiers.keys():
			if( 'Matches%s' % Modifier.title() not in dir( self ) ):
				raise AssertionError( '%s has not implemented method Matches%s for modifier %s.' % ( self.ClassName, Modifier.title(), Modifier ) );