Ejemplo n.º 1
0
    def _resolve_variable(cls, config, substitution):
        """
        :param config:
        :param substitution:
        :return: (is_resolved, resolved_variable)
        """
        variable = substitution.variable
        try:
            return True, config.get(variable)
        except ConfigMissingException:
            # default to environment variable
            value = os.environ.get(variable)

            if value is None:
                if substitution.optional:
                    return False, None
                else:
                    raise ConfigSubstitutionException(
                        "Cannot resolve variable ${{{variable}}} (line: {line}, col: {col})"
                        .format(variable=variable,
                                line=lineno(substitution.loc,
                                            substitution.instring),
                                col=col(substitution.loc,
                                        substitution.instring)))
            elif isinstance(value, ConfigList) or isinstance(
                    value, ConfigTree):
                raise ConfigSubstitutionException(
                    "Cannot substitute variable ${{{variable}}} because it does not point to a "
                    "string, int, float, boolean or null {type} (line:{line}, col: {col})"
                    .format(variable=variable,
                            type=value.__class__.__name__,
                            line=lineno(substitution.loc,
                                        substitution.instring),
                            col=col(substitution.loc, substitution.instring)))
            return True, value
Ejemplo n.º 2
0
    def _resolve_variable(config, substitution):
        """
        :param config:
        :param substitution:
        :return: (is_resolved, resolved_variable)
        """
        variable = substitution.variable
        try:
            return True, config.get(variable)
        except ConfigMissingException:
            # default to environment variable
            value = os.environ.get(variable)

            if value is None:
                if substitution.optional:
                    return False, None
                else:
                    raise ConfigSubstitutionException(
                        "Cannot resolve variable ${{{variable}}} (line: {line}, col: {col})".format(
                            variable=variable,
                            line=lineno(substitution.loc, substitution.instring),
                            col=col(substitution.loc, substitution.instring)))
            elif isinstance(value, ConfigList) or isinstance(value, ConfigTree):
                raise ConfigSubstitutionException(
                    "Cannot substitute variable ${{{variable}}} because it does not point to a "
                    "string, int, float, boolean or null {type} (line:{line}, col: {col})".format(
                        variable=variable,
                        type=value.__class__.__name__,
                        line=lineno(substitution.loc, substitution.instring),
                        col=col(substitution.loc, substitution.instring)))
            return True, value
Ejemplo n.º 3
0
    def while_action(self, text, loc, arg):
        logging.getLogger(__name__).debug("while_action {0} {1}".format(
            lineno(loc, text), arg))
        try:
            block0 = [
                "# line {0} while_statement {1}".format(
                    lineno(loc, text), line(loc, text))
            ]
            if 'code' in arg.condition:
                if isinstance(arg.condition.code, list):
                    block1 = arg.condition.code
                    JMPCMD = arg.condition.get('jmpcmd',
                                               {False: "JMPNCMP"})[False]
                else:
                    JMPCMD = arg.condition.code[True]
                    block1 = []
            elif 'rExp' in arg.condition and 'code' in arg.condition.rExp:
                if isinstance(arg.condition.rExp.code, list):
                    block1 = arg.condition.rExp.code
                    JMPCMD = arg.condition.rExp.get('jmpcmd', "JMPNCMP")
                else:
                    JMPCMD = arg.condition.rExp.code[True]
                    block1 = []
            block2 = arg.statementBlock.statementBlock['code']

            arg['code'] = [
                WhileGenerator(self.symbols, JMPCMD, block0, block1, block2)
            ]
            logging.getLogger(__name__).debug("while_action generated code ")
        except Exception as e:
            raise CompileException(text, loc, str(e), self)
        return arg
Ejemplo n.º 4
0
 def store_vector(self, s, l, t):
     q = t.asList()
     self.strg = s
     self.loc = l
     name = q[0]
     arg = q[1:]
     if self.caseless:
         name = name.lower()
     if self.templ is None:
         argt = self.fixate_type(arg[0])
         argt = argt + '_ARRAY'
     else:
         k = self.path[-1].fetch_kw(name)
         if k is None:
             print("Unknown keyword '{}', line: {:d}".format(
                 name, lineno(self.loc, self.strg)))
             if strict:
                 sys.exit(1)
             argt = None
         else:
             if k.nargs == -1:
                 pass
             elif len(arg) != k.nargs:
                 print("Invalid number of elements for key '{}',\
                        line: {:d}".format(name, lineno(self.loc,
                                                        self.strg)))
                 print("  -> {:d} required, {:d} given.".format(
                     k.nargs, len(arg)))
                 if strict:
                     sys.exit(1)
             argt = self.check_type(arg, k.type)
     k = Keyword(name, argt, arg)
     self.cur.add_kwkw(k, set=True)
Ejemplo n.º 5
0
 def addassignement_action(self, text, loc, arg):
     logging.getLogger(__name__).debug(
         "addassignement_action {0} {1}".format(lineno(loc, text), arg))
     try:
         code = [
             "# line {0}: add_assignment: {1}".format(
                 lineno(loc, text), line(loc, text))
         ]
         if arg.rval[0] == '1' and arg.op in ['+=', '-=']:
             self.symbols.getVar(arg.lval)
             if arg.op == "+=":
                 code.append("  INC {0}".format(arg.lval))
             else:
                 code.append("  DEC {0}".format(arg.lval))
         else:
             if 'code' in arg.rval:
                 code += arg.rval.code
                 self.symbols.getVar(arg.lval)
                 if arg.op == "-=":
                     raise CompileException(
                         "-= with expression needs to be fixed in the compiler"
                     )
                 code.append("  {0} {1}".format(opassignmentLookup[arg.op],
                                                arg.lval))
             elif 'identifier' in arg.rval:
                 self.symbols.getVar(arg.rval.identifier)
                 code.append("  LDWR {0}".format(arg.lval))
                 self.symbols.getVar(arg.lval)
                 code.append("  {0} {1}".format(opassignmentLookup[arg.op],
                                                arg.rval.identifier))
         code.append("  STWR {0}".format(arg.lval))
         arg['code'] = code
     except Exception as e:
         raise CompileException(text, loc, str(e), self)
     return arg
Ejemplo n.º 6
0
 def assignment_action(self, text, loc, arg):
     logging.getLogger(__name__).debug("assignment_action {0} {1}".format(
         lineno(loc, text), arg))
     try:
         code = [
             "# line {0} assignment {1}".format(lineno(loc, text),
                                                line(loc, text))
         ]
         rval_code = find_and_get(arg.rval, 'code')
         if rval_code is not None:
             code += arg.rval.code
         elif arg.rval == "*P":
             code.append("  LDWI")
         elif 'identifier' in arg:
             self.symbols.getVar(arg.identifier)
             code.append("  LDWR {0}".format(arg.identifier))
         if arg.lval == "*P":
             code.append("  STWI")
         elif arg.lval != "W":
             symbol = self.symbols.getVar(arg.lval)
             code.append("  STWR {0}".format(symbol.name))
         if 'code' in arg:
             arg['code'].extend(code)
         else:
             arg['code'] = code
     except Exception as e:
         raise CompileException(text, loc, str(e), self)
     return arg
Ejemplo n.º 7
0
 def rExp_condition_action(self, text, loc, arg):
     logging.getLogger(__name__).debug(
         "rExp_condition_action {0} {1}".format(lineno(loc, text), arg))
     try:
         code = [
             "# line {0} rExp_condition {1}".format(lineno(loc, text),
                                                    line(loc, text))
         ]
         condition_code = arg.condition.rExp['code']
         if isinstance(condition_code, str):
             if 'not_' in arg['condition']:
                 code += ["  CMPEQUAL NULL"]
             else:
                 code += ["  CMPNOTEQUAL NULL"]
             arg['code'] = code
         else:
             if 'not_' in arg['condition']:
                 arg['code'] = {
                     False: condition_code[True],
                     True: condition_code[False]
                 }
             else:
                 arg['code'] = condition_code
     except Exception as e:
         raise CompileException(text, loc, str(e), self)
     return arg
Ejemplo n.º 8
0
	def check_type(self, arg, argt):
		print "You hit a bug! Yipeee!"
		sys.exit(1)
		if (isinstance(arg, tuple) or isinstance(arg, list)):
			argt=re.sub('_ARRAY', '', argt)
			for i in arg:
				self.check_type(i, argt)

		if argt == 'INT':
			if not ival.match(arg):
				print 'Invalid type on line %d: Not an int: \n -> %s' % (
						lineno(self.loc,self.strg), line(self.loc,
							self.strg).strip())
				sys.exit(1)
		elif argt == 'DBL':
			if not dval.match(arg):
				print 'Invalid type on line %d: Not a float: \n -> %s' % (
						lineno(self.loc,self.strg), line(self.loc,
							self.strg).strip())
				sys.exit(1)
		elif argt == 'BOOL':
			if not lval.match(arg):
				print 'Invalid type on line %d: Not a bool: \n -> %s' % (
						lineno(self.loc,self.strg), line(self.loc,
							self.strg).strip())
				sys.exit(1)
		elif argt != 'STR':
			print 'Invalid type on line %d: Not a %s: \n -> %s' % (
					lineno(self.loc,self.strg), argt, line(self.loc,
						self.strg).strip())
			sys.exit(1)
		return argt
Ejemplo n.º 9
0
	def store_vector(self,s,l,t):
		q=t.asList()
		self.strg=s
		self.loc=l
		name=q[0]
		arg=q[1:]
		if self.caseless:
			name=name.lower()
		if self.templ is None:
			argt=self.guess_vectype(arg)
		else:
			k=self.path[-1].findkw(name)
			if k is None:
				print "Unknown keyword '%s', line: %d" % (name, 
						lineno(self.loc,self.strg))
				if strict:
					sys.exit(1)
				argt=None
			else:
				if k.nargs == -1: 
					pass
				elif len(arg) != k.nargs:
					print "Invalid number of elements for key '%s',\
line: %d" % ( name, lineno(self.loc,self.strg))
					print "  -> %d required, %d given." % (k.nargs, len(arg))
					if strict:
						sys.exit(1)
				argt=self.check_vectype(arg,k.type)
		k=Keyword(name,argt,arg)
		self.cur.add_kwkw(k,set=True)
Ejemplo n.º 10
0
	def check_type(self, arg, argt):
		print "You hit a bug! Yipeee!"
		sys.exit(1)
		if (isinstance(arg, tuple) or isinstance(arg, list)):
			argt=re.sub('_ARRAY', '', argt)
			for i in arg:
				self.check_type(i, argt)

		if argt == 'INT':
			if not ival.match(arg):
				print 'Invalid type on line %d: Not an int: \n -> %s' % (
						lineno(self.loc,self.strg), line(self.loc,
							self.strg).strip())
				sys.exit(1)
		elif argt == 'DBL':
			if not dval.match(arg):
				print 'Invalid type on line %d: Not a float: \n -> %s' % (
						lineno(self.loc,self.strg), line(self.loc,
							self.strg).strip())
				sys.exit(1)
		elif argt == 'BOOL':
			if not lval.match(arg):
				print 'Invalid type on line %d: Not a bool: \n -> %s' % (
						lineno(self.loc,self.strg), line(self.loc,
							self.strg).strip())
				sys.exit(1)
		elif argt != 'STR':
			print 'Invalid type on line %d: Not a %s: \n -> %s' % (
					lineno(self.loc,self.strg), argt, line(self.loc,
						self.strg).strip())
			sys.exit(1)
		return argt
Ejemplo n.º 11
0
	def check_type(self, arg, argt):
		if argt == 'INT':
			if not ival.match(arg):
				print 'Invalid type on line %d: Not an int: \n -> %s' % (
						lineno(self.loc,self.strg), line(self.loc,
							self.strg).strip())
				sys.exit(1)
		elif argt == 'DBL':
			if not dval.match(arg):
				print 'Invalid type on line %d: Not a float: \n -> %s' % (
						lineno(self.loc,self.strg), line(self.loc,
							self.strg).strip())
				sys.exit(1)
		elif argt == 'BOOL':
			if not lval.match(arg):
				print 'Invalid type on line %d: Not a bool: \n -> %s' % (
						lineno(self.loc,self.strg), line(self.loc,
							self.strg).strip())
				sys.exit(1)
		elif argt != 'STR':
			print 'Invalid type on line %d: Not a %s: \n -> %s' % (
					lineno(self.loc,self.strg), argt, line(self.loc,
						self.strg).strip())
			sys.exit(1)
		return argt
Ejemplo n.º 12
0
    def if_action(self, text, loc, arg):
        logging.getLogger(__name__).debug("if_action {0} {1}".format(
            lineno(loc, text), arg))
        try:
            block0 = [
                "# line {0} if statement {1}".format(lineno(loc, text),
                                                     line(loc, text))
            ]
            if isinstance(arg.condition.code, list):
                block0 += arg.condition.code
                JMPCMD = arg.condition.get('jmpcmd', {False: "JMPNCMP"})[False]
            else:
                JMPCMD = arg.condition.code[True]

            if 'elseblock' in arg:
                block1 = arg.ifblock.ifblock.code
                block2 = arg.elseblock.elseblock[
                    'code'] if 'elseblock' in arg.elseblock else arg.elseblock[
                        'code']
            else:
                block1 = arg.ifblock.ifblock['code']
                block2 = None
            arg['code'] = [
                IfGenerator(self.symbols, JMPCMD, block0, block1, block2)
            ]
        except Exception as e:
            raise CompileException(text, loc, str(e), self)
        return arg
Ejemplo n.º 13
0
Archivo: getkw.py Proyecto: lnw/gimic
 def check_vectype(self, arg, argt):
     if argt == 'INT_ARRAY':
         for i in arg:
             if not ival.match(i):
                 print('Invalid type on line %d: Not an int: \n -> %s' %
                       (lineno(self.loc, self.strg),
                        line(self.loc, self.strg).strip()))
                 sys.exit(1)
     elif argt == 'DBL_ARRAY':
         for i in arg:
             if not dval.match(i):
                 print('Invalid type on line %d: Not a float: \n -> %s' %
                       (lineno(self.loc, self.strg),
                        line(self.loc, self.strg).strip()))
                 sys.exit(1)
     elif argt == 'BOOL_ARRAY':
         for i in arg:
             if not lval.match(i):
                 print('Invalid type on line %d: Not a bool: \n -> %s' %
                       (lineno(self.loc, self.strg),
                        line(self.loc, self.strg.strip())))
                 sys.exit(1)
     elif argt != 'STR':
         print('Invalid type on line %d: Not a %s: \n -> %s' % (lineno(
             self.loc, self.strg), argt, line(self.loc, self.strg).strip()))
         sys.exit(1)
     return argt
Ejemplo n.º 14
0
    def var_action(self, text, loc, arg):
        print("var_action", self.currentFile, lineno(loc, text), arg[0:2],
              arg[2].split(",") if len(arg) > 2 else "")
        """ add a variable to the self.variablesdict
        """
        logger = logging.getLogger(__name__)
        logger.debug("{0}:{1} Variable {2}".format(self.currentFile,
                                                   lineno(loc, text), arg))
        var = Variable()
        label, data = arg[:2]
        fields = arg[2].split(",") if len(arg) > 2 else [None] * 3
        fields += [None] * (3 - len(fields))
        var.type, unit, var.encoding = [
            x if x is None or '' else x.strip() for x in fields
        ]
        var.name = label
        var.origin = self.currentFile
        var.enabled = True

        if var.encoding not in encodings:
            raise ppexception(
                "unknown encoding {0} in file '{1}':{2}".format(
                    var.encoding, self.currentFile, lineno(loc, text)),
                self.currentFile, lineno, var.encoding)

        try:
            data = str(eval(data, globals(), self.defines))
        except Exception:
            logger.exception(
                "Evaluation error in file '{0}' on line: '{1}'".format(
                    self.currentFile, data))

        if unit is not None:
            var.value = Q(float(data), unit)
            data = self.convertParameter(var.value, var.encoding)
        else:
            var.value = Q(float(data))
            data = int(round(float(data)))

        if label in self.defines:
            logger.error(
                "Error in file '%s': attempted to reassign '%s' to '%s' (from prev. value of '%s') in a var statement."
                % (self.currentFile, label, data, self.defines[label]))
            raise ppexception("variable redifinition", self.currentFile,
                              lineno, label)
        else:
            self.defines[
                label] = label  # add the variable to the dictionary of definitions to prevent identifiers and variables from having the same name
            # however, we do not want it replaced with a number but keep the name for the last stage of compilation
            pass
        var.data = data
        self.variabledict.update({label: var})
        if var.type == "exitcode":
            self._exitcodes[data & 0x0000ffff] = var
Ejemplo n.º 15
0
def pyparsingLoggingSuccessDebugAction(instring, startloc, endloc, expr, toks):

    row_start = pyparsing.lineno(startloc, instring)
    col_start = pyparsing.col(startloc, instring)

    row_end = pyparsing.lineno(endloc, instring)
    col_end = pyparsing.col(endloc, instring)

    pplogger.debug(
        strip_margin('''Match Success: expr: `%s`, startLoc: `%s`,
        | startRow: `%s`, startCol: `%s`, endLoc: `%s`, endRow: `%s`, endCol:
        | `%s`, -> toks: `%s` tokens'''), expr, startloc, row_start, col_start,
        endloc, row_end, col_end, len(toks.asList()))
    def _line(cls):
        text = cls._free_form_text()

        physical_line = text + cls.EOL
        physical_line.setParseAction(lambda origString, loc, tokens: (tokens[
            0][0].rstrip(), pp.lineno(loc, origString)))
        logical_line = pp.OneOrMore(text + cls.CONTINUATION) + physical_line
        logical_line.setParseAction(lambda origString, loc, tokens: (''.join(
            [x[0].lstrip() for x in tokens]), pp.lineno(loc, origString)))

        line = physical_line | logical_line | cls.EOL

        return line
Ejemplo n.º 17
0
    def transform(self):
        def determine_type(token):
            return ConfigTree if isinstance(token, ConfigTree) else ConfigList if isinstance(token, list) else str

        def format_str(v):
            return "" if v is None else str(v)

        if self.has_substitution():
            return self

        # remove None tokens
        tokens = [token for token in self.tokens if token is not None]

        if not tokens:
            return None

        # check if all tokens are compatible
        first_tok_type = determine_type(tokens[0])
        for index, token in enumerate(tokens[1:]):
            tok_type = determine_type(token)
            if first_tok_type is not tok_type:
                raise ConfigWrongTypeException(
                    "Token '{token}' of type {tok_type} (index {index}) must be of type {req_tok_type} (line: {line}, col: {col})".format(
                        token=token,
                        index=index + 1,
                        tok_type=tok_type.__name__,
                        req_tok_type=first_tok_type.__name__,
                        line=lineno(self._loc, self._instring),
                        col=col(self._loc, self._instring),
                    )
                )

        if first_tok_type is ConfigTree:
            result = ConfigTree()
            for token in tokens:
                for key, val in token.items():
                    # update references for substituted contents
                    if isinstance(val, ConfigValues):
                        val.parent = result
                        val.key = key
                    result[key] = val
            return result
        elif first_tok_type is ConfigList:
            result = []
            main_index = 0
            for sublist in tokens:
                sublist_result = ConfigList()
                for token in sublist:
                    if isinstance(token, ConfigValues):
                        token.parent = result
                        token.key = main_index
                    main_index += 1
                    sublist_result.append(token)
                result.extend(sublist_result)
            return [result]
        else:
            if len(tokens) == 1:
                return tokens[0]
            else:
                return "".join(format_str(token) for token in tokens[:-1]) + format_str(tokens[-1])
Ejemplo n.º 18
0
    def resolve_substitutions(config):
        ConfigParser._fixup_self_references(config)
        substitutions = ConfigParser._find_substitutions(config)
        if len(substitutions) > 0:
            unresolved = True
            any_unresolved = True
            _substitutions = []
            while any_unresolved and len(substitutions) > 0 and set(substitutions) != set(_substitutions):
                unresolved = False
                any_unresolved = True
                _substitutions = substitutions[:]

                for substitution in _substitutions:
                    is_optional_resolved, resolved_value = ConfigParser._resolve_variable(config, substitution)

                    # if the substitution is optional
                    if not is_optional_resolved and substitution.optional:
                        resolved_value = None

                    unresolved, new_substitutions, result = ConfigParser._do_substitute(substitution, resolved_value, is_optional_resolved)
                    any_unresolved = unresolved or any_unresolved
                    substitutions.extend(new_substitutions)
                    if not isinstance(result, ConfigValues):
                        substitutions.remove(substitution)

            ConfigParser._final_fixup(config)
            if unresolved:
                raise ConfigSubstitutionException("Cannot resolve {variables}. Check for cycles.".format(
                    variables=', '.join('${{{variable}}}: (line: {line}, col: {col})'.format(
                        variable=substitution.variable,
                        line=lineno(substitution.loc, substitution.instring),
                        col=col(substitution.loc, substitution.instring)) for substitution in substitutions)))

        return config
Ejemplo n.º 19
0
    def _setLineAndLineNumberParseAction(
            self, s: str, loc: int,
            toks: pyparsing.ParseResults) -> pyparsing.ParseResults:
        '''
        helper that is meant to be used with `<ParserElement>.setParseAction`, which means this function gets called
        whenever a match for each line of the Telegram TL file gets hit.

        Here we add stuff to the ParseResults that it passes in

        we add the source line and source line number to each match

        @param s is the original parse string
        @param loc is the location in the string where matching started
        @param toks is the list of the matched tokens, packaged as a ParseResults object
        @return a ParseResults if you are modifying it, else None
        '''

        t = toks
        orig_line = pyparsing.line(loc, s)
        line_number = pyparsing.lineno(loc, s)
        column = pyparsing.col(loc, s)

        logger.debug(
            "_setLineAndLineNumberParseAction: line_number: `%s`, column: `%s`,  line: `%s`",
            line_number, column, orig_line)

        t[constants.RESULT_NAME_SOURCE_LINE] = orig_line
        t[constants.RESULT_NAME_SOURCE_LINE_NUMBER] = line_number

        return t
Ejemplo n.º 20
0
def pyparsingLoggingStartDebugAction(instring, loc, expr):

    row = pyparsing.lineno(loc, instring)
    col = pyparsing.col(loc, instring)

    pplogger.debug("Match Start: expr: `%s` at loc `%s`, row: `%d`, col: `%d`",
                   expr, loc, row, col)
Ejemplo n.º 21
0
 def __init__(self, s, loc, toks):
     self.cmd = str(toks[0])[1:]
     #print 'cmd', self.cmd
     self.args = toks[1].asList()
     self.params = toks[2].asList()
     self.lineno = lineno(loc, s)
     self.filename = None
Ejemplo n.º 22
0
 def __init__(self, st, locn, tokString):
     self.token_string = tokString
     self.loc = locn
     self.before_line = line(locn - 1, st)
     self.source_line = line(locn, st)
     self.line_num = lineno(locn, st)
     self.col = col(locn, st)
Ejemplo n.º 23
0
def line_col(primitive):
    loc = location(primitive)
    src = source(primitive)
    if src and loc:
        return lineno(loc, src), col(loc, src)
    else:
        return None, None
Ejemplo n.º 24
0
 def __init__(self, s, loc, toks):
     self.cmd = str(toks[0])[1:]
     #print 'cmd', self.cmd
     self.args = toks[1].asList()
     self.params = toks[2].asList()
     self.lineno = lineno(loc, s)
     self.filename = None
Ejemplo n.º 25
0
    def transform(self):
        def determine_type(token):
            return ConfigTree if isinstance(
                token,
                ConfigTree) else ConfigList if isinstance(token, list) else str

        def format_str(v):
            return '' if v is None else str(v)

        if self.has_substitution():
            return self

        # remove None tokens
        tokens = [token for token in self.tokens if token is not None]

        if not tokens:
            return None

        # check if all tokens are compatible
        first_tok_type = determine_type(tokens[0])
        for index, token in enumerate(tokens[1:]):
            tok_type = determine_type(token)
            if first_tok_type is not tok_type:
                raise ConfigWrongTypeException(
                    "Token '{token}' of type {tok_type} (index {index}) must be of type {req_tok_type} (line: {line}, col: {col})"
                    .format(token=token,
                            index=index + 1,
                            tok_type=tok_type.__name__,
                            req_tok_type=first_tok_type.__name__,
                            line=lineno(self._loc, self._instring),
                            col=col(self._loc, self._instring)))

        if first_tok_type is ConfigTree:
            result = ConfigTree()
            for token in tokens:
                for key, val in token.items():
                    # update references for substituted contents
                    if isinstance(val, ConfigValues):
                        val.parent = result
                        val.key = key
                    result[key] = val
            return result
        elif first_tok_type is ConfigList:
            result = []
            for sublist in tokens:
                sublist_result = ConfigList()
                for index, token in enumerate(sublist):
                    if isinstance(token, ConfigValues):
                        token.parent = result
                        token.key = index
                    sublist_result.append(token)
                result.extend(sublist_result)
            return [result]
        else:
            if len(tokens) == 1:
                return tokens[0]
            else:
                return ''.join(
                    token if isinstance(token, str) else format_str(token) +
                    ' ' for token in tokens[:-1]) + format_str(tokens[-1])
Ejemplo n.º 26
0
			def getMessage(pstr, pos, filepath=''):
				line = pyparsing.line(pos, pstr);
				lineno = pyparsing.lineno(pos, pstr);
				col = pyparsing.col(pos, pstr)
				arrow = ( '-'  * (col-1) + '^');
				ls = os.linesep + '    ';
				return '  in file {filepath} (line: {lineno}, col: {col}){ls}{line}{ls}{arrow}'.format(**locals());
Ejemplo n.º 27
0
 def _parse_action_block(self, source, idx, tokin):
     value = tokin[0].asList()
     return [{
         'type': 'block',
         'line': pp.lineno(idx, source),
         'col': pp.col(idx, source),
         'statements': value
     }]
Ejemplo n.º 28
0
def _location (orig_string, locn):
    if not _debug_info_enabled:
        return None

    assert _source_file is not None
    return instruction.SourceLocation (srcfile=os.path.abspath (_source_file),
                                       line=pp.lineno (locn, orig_string),
                                       column=pp.col (locn, orig_string))
Ejemplo n.º 29
0
def set_line_number(string, location, tokens):
    if len(tokens) == 1:
        line_number = lineno(location, string)
        tokens_cache[tokens[0]] = line_number
        tokens.line_number = line_number
    else:
        for item in tokens:
            tokens.line_number = tokens_cache.get(item)
Ejemplo n.º 30
0
 def _parse_action_obj(self, source, idx, tokin):
     value = tokin[0]
     return [{
         'type': 'obj',
         'line': pp.lineno(idx, source),
         'col': pp.col(idx, source),
         'key': (value[0], value[1])
     }]
Ejemplo n.º 31
0
 def _parse_action_obj(self, source, idx, tokin):
     value = tokin[0].asDict()
     return [{'type': 'obj',
              'line': pp.lineno(idx, source),
              'col': pp.col(idx, source),
              'id_type': value.get('id_type'),
              'id_fixed': value.get('id_fixed'),
              'key': (value.get('class'), value.get('id_fixed', 'xxx'))}]
Ejemplo n.º 32
0
 def _parse_action_group(self, source, idx, tokin):
     value = tokin
     return [{
         'type': 'group',
         'line': pp.lineno(idx, source),
         'col': pp.col(idx, source),
         'key': (value[0], )
     }]
Ejemplo n.º 33
0
 def opexpression_action(self, text, loc, arg):
     try:
         logging.getLogger(__name__).debug(
             "opexpression_action {0} {1}".format(lineno(loc, text), arg))
         code = [
             "# line {0}: shiftexpression {1}".format(
                 lineno(loc, text),
                 line(loc, text)), "  LDWR {0}".format(arg.operand),
             "  {0} {1}".format(shiftLookup[arg.op],
                                arg.argument.identifier)
         ]
         arg['code'] = code
         logging.getLogger(__name__).debug(
             "shiftexpression generated code {0}".format(code))
     except Exception as e:
         raise CompileException(text, loc, str(e), self)
     return arg
Ejemplo n.º 34
0
def find_cursor(source):
    """Return (source, line, col) based on the | character, stripping the source."""
    source = source.strip()
    i = source.index('|')
    assert i != -1
    l = pyparsing.lineno(i, source)
    c = pyparsing.col(i, source)
    return source.replace('|', ''), l, c
Ejemplo n.º 35
0
def pyparsingLoggingExceptionDebugAction(instring, loc, expr, exc):

    row = pyparsing.lineno(loc, instring)
    col = pyparsing.col(loc, instring)

    pplogger.error(
        "Caught Exception: expr: `%s`, loc: `%s`,  row: `%d`, col: `%d`, exception: `%s`",
        expr, loc, row, col, exc)
Ejemplo n.º 36
0
    def transform(self):
        def determine_type(token):
            return ConfigTree if isinstance(token, ConfigTree) else ConfigList if isinstance(token, list) else str

        def format_str(v, last=False):
            if isinstance(v, ConfigQuotedString):
                return v.value + ('' if last else v.ws)
            else:
                return '' if v is None else str(v)

        if self.has_substitution():
            return self

        # remove None tokens
        tokens = [token for token in self.tokens if token is not None]

        if not tokens:
            return None

        # check if all tokens are compatible
        first_tok_type = determine_type(tokens[0])
        for index, token in enumerate(tokens[1:]):
            tok_type = determine_type(token)
            if first_tok_type is not tok_type:
                raise ConfigWrongTypeException(
                    "Token '{token}' of type {tok_type} (index {index}) must be of type {req_tok_type} (line: {line}, col: {col})".format(
                        token=token,
                        index=index + 1,
                        tok_type=tok_type.__name__,
                        req_tok_type=first_tok_type.__name__,
                        line=lineno(self._loc, self._instring),
                        col=col(self._loc, self._instring)))

        if first_tok_type is ConfigTree:
            result = ConfigTree()
            for token in tokens:
                ConfigTree.merge_configs(result, token, copy_trees=True)
            return result
        elif first_tok_type is ConfigList:
            result = []
            main_index = 0
            for sublist in tokens:
                sublist_result = ConfigList()
                for token in sublist:
                    if isinstance(token, ConfigValues):
                        token.parent = result
                        token.key = main_index
                    main_index += 1
                    sublist_result.append(token)
                result.extend(sublist_result)
            return result
        else:
            if len(tokens) == 1:
                if isinstance(tokens[0], ConfigQuotedString):
                    return tokens[0].value
                return tokens[0]
            else:
                return ''.join(format_str(token) for token in tokens[:-1]) + format_str(tokens[-1], True)
Ejemplo n.º 37
0
    def transform(self):
        def determine_type(token):
            return ConfigTree if isinstance(token, ConfigTree) else ConfigList if isinstance(token, list) else str

        def format_str(v, last=False):
            if isinstance(v, ConfigQuotedString):
                return v.value + ('' if last else v.ws)
            else:
                return '' if v is None else str(v)

        if self.has_substitution():
            return self

        # remove None tokens
        tokens = [token for token in self.tokens if token is not None]

        if not tokens:
            return None

        # check if all tokens are compatible
        first_tok_type = determine_type(tokens[0])
        for index, token in enumerate(tokens[1:]):
            tok_type = determine_type(token)
            if first_tok_type is not tok_type:
                raise ConfigWrongTypeException(
                    "Token '{token}' of type {tok_type} (index {index}) must be of type {req_tok_type} (line: {line}, col: {col})".format(
                        token=token,
                        index=index + 1,
                        tok_type=tok_type.__name__,
                        req_tok_type=first_tok_type.__name__,
                        line=lineno(self._loc, self._instring),
                        col=col(self._loc, self._instring)))

        if first_tok_type is ConfigTree:
            result = ConfigTree()
            for token in tokens:
                ConfigTree.merge_configs(result, token, copy_trees=True)
            return result
        elif first_tok_type is ConfigList:
            result = []
            main_index = 0
            for sublist in tokens:
                sublist_result = ConfigList()
                for token in sublist:
                    if isinstance(token, ConfigValues):
                        token.parent = result
                        token.key = main_index
                    main_index += 1
                    sublist_result.append(token)
                result.extend(sublist_result)
            return result
        else:
            if len(tokens) == 1:
                if isinstance(tokens[0], ConfigQuotedString):
                    return tokens[0].value
                return tokens[0]
            else:
                return ''.join(format_str(token) for token in tokens[:-1]) + format_str(tokens[-1], True)
Ejemplo n.º 38
0
 def _parse_action_attr(self, source, idx, tokin):
     value = tokin[0]
     tokout = {'type': 'attr',
               'line': pp.lineno(idx, source),
               'col': pp.col(idx, source),
               'key': (value[0], value[1])}
     if len(value) > 2:
         tokout['comment'] = value[2][1:].strip()
     return [tokout]
Ejemplo n.º 39
0
    def resolve_substitutions(cls, config, accept_unresolved=False):
        has_unresolved = False
        cls._fixup_self_references(config, accept_unresolved)
        substitutions = cls._find_substitutions(config)
        if len(substitutions) > 0:
            unresolved = True
            any_unresolved = True
            _substitutions = []
            cache = {}
            while any_unresolved and len(substitutions) > 0 and set(substitutions) != set(_substitutions):
                unresolved = False
                any_unresolved = True
                _substitutions = substitutions[:]

                for substitution in _substitutions:
                    is_optional_resolved, resolved_value = cls._resolve_variable(config, substitution)

                    # if the substitution is optional
                    if not is_optional_resolved and substitution.optional:
                        resolved_value = None
                    if isinstance(resolved_value, ConfigValues):
                        parents = cache.get(resolved_value)
                        if parents is None:
                            parents = []
                            link = resolved_value
                            while isinstance(link, ConfigValues):
                                parents.append(link)
                                link = link.overriden_value
                            cache[resolved_value] = parents

                    if isinstance(resolved_value, ConfigValues) \
                       and substitution.parent in parents \
                       and hasattr(substitution.parent, 'overriden_value') \
                       and substitution.parent.overriden_value:

                        # self resolution, backtrack
                        resolved_value = substitution.parent.overriden_value

                    unresolved, new_substitutions, result = cls._do_substitute(
                        substitution, resolved_value, is_optional_resolved)
                    any_unresolved = unresolved or any_unresolved
                    substitutions.extend(new_substitutions)
                    if not isinstance(result, ConfigValues):
                        substitutions.remove(substitution)

            cls._final_fixup(config)
            if unresolved:
                has_unresolved = True
                if not accept_unresolved:
                    raise ConfigSubstitutionException("Cannot resolve {variables}. Check for cycles.".format(
                        variables=', '.join('${{{variable}}}: (line: {line}, col: {col})'.format(
                            variable=substitution.variable,
                            line=lineno(substitution.loc, substitution.instring),
                            col=col(substitution.loc, substitution.instring)) for substitution in substitutions)))

        cls._final_fixup(config)
        return has_unresolved
Ejemplo n.º 40
0
 def insert_action( self, text, loc, arg ):
     oldfile = self.currentFile
     print("insert_action", lineno(loc, text), arg)
     myprogram = self.program.copy()
     self.currentFile = arg[0][1:-1]
     result = myprogram.parseFile( self.currentFile )
     self.currentFile = oldfile
     print(result)
     return result
Ejemplo n.º 41
0
 def action(s, loc, token):
     _lineno = lineno(loc, s)
     _colno = col(loc, s) - 1
     logger.info("log={0} (lineno={1}, col={2})".format(loc, _lineno, _colno))
     logger.debug("comment: {0}".format(token[0]))
     logger.debug("comment: {0}".format(nodeInfo(token)))
     _comment = ast.Comment(token[0][2:])
     _comment.loc_info = (_lineno, _colno)
     return _comment
Ejemplo n.º 42
0
    def resolve_substitutions(cls, config, accept_unresolved=False):
        has_unresolved = False
        cls._fixup_self_references(config, accept_unresolved)
        substitutions = cls._find_substitutions(config)
        if len(substitutions) > 0:
            unresolved = True
            any_unresolved = True
            _substitutions = []
            cache = {}
            while any_unresolved and len(substitutions) > 0 and set(substitutions) != set(_substitutions):
                unresolved = False
                any_unresolved = True
                _substitutions = substitutions[:]

                for substitution in _substitutions:
                    is_optional_resolved, resolved_value = cls._resolve_variable(config, substitution)

                    # if the substitution is optional
                    if not is_optional_resolved and substitution.optional:
                        resolved_value = None
                    if isinstance(resolved_value, ConfigValues):
                        parents = cache.get(resolved_value)
                        if parents is None:
                            parents = []
                            link = resolved_value
                            while isinstance(link, ConfigValues):
                                parents.append(link)
                                link = link.overriden_value
                            cache[resolved_value] = parents

                    if isinstance(resolved_value, ConfigValues) \
                       and substitution.parent in parents \
                       and hasattr(substitution.parent, 'overriden_value') \
                       and substitution.parent.overriden_value:

                        # self resolution, backtrack
                        resolved_value = substitution.parent.overriden_value

                    unresolved, new_substitutions, result = cls._do_substitute(substitution, resolved_value, is_optional_resolved)
                    any_unresolved = unresolved or any_unresolved
                    substitutions.extend(new_substitutions)
                    if not isinstance(result, ConfigValues):
                        substitutions.remove(substitution)

            cls._final_fixup(config)
            if unresolved:
                has_unresolved = True
                if not accept_unresolved:
                    raise ConfigSubstitutionException("Cannot resolve {variables}. Check for cycles.".format(
                        variables=', '.join('${{{variable}}}: (line: {line}, col: {col})'.format(
                            variable=substitution.variable,
                            line=lineno(substitution.loc, substitution.instring),
                            col=col(substitution.loc, substitution.instring)) for substitution in substitutions)))

        cls._final_fixup(config)
        return has_unresolved
Ejemplo n.º 43
0
	def push_sect(self,k):
		self.stack.append(k)
		self.cur=self.stack[-1]
		if self.templ is not None:
			x=self.path[-1].findsect(k.name)
			if x is None:
				print "Invalid section on line %d: \n%s" % (
						lineno(self.loc,self.strg), line(self.loc,self.strg))
				sys.exit(1)
			self.path.append(x)
Ejemplo n.º 44
0
 def __str__(self):
     #TODO: create error message which is understood by Pydev. Format:
     #File "/home/eike/codedir/freeode/trunk/freeode_py/simlparser.py", line 956, in createProcess
     if self.str == None:
         return 'Error! ' + self.message + '\n At position: ' + str(self.loc)
     else:
         lineno = pyparsing.lineno(self.loc, self.str)
         col = pyparsing.col(self.loc, self.str)
         return 'Error! %s \n' % self.message +\
                'Line: %d, Column: %d' % (lineno, col)
Ejemplo n.º 45
0
 def __init__(self, message, print_location=True):
     super(SemanticException, self).__init__()
     self._message = message
     self.location = exshared.location
     self.print_location = print_location
     if exshared.location is not None:
         self.line = lineno(exshared.location, exshared.text)
         self.col = col(exshared.location, exshared.text)
         self.text = line(exshared.location, exshared.text)
     else:
         self.line = self.col = self.text = None
Ejemplo n.º 46
0
 def __str__(self):
     #TODO: create error message which is understood by Pydev.
     errMsg = 'Error!\n'
     for msg1, loc1 in self.errTupList:
         lineno, col = 0, 0
         if self.str:
             lineno = pyparsing.lineno(loc1, self.str)
             col = pyparsing.col(loc1, self.str)
         errMsg += '%s \n' % msg1 +\
                   'Line: %d, Column: %d\n' % (lineno, col)
     return errMsg  
Ejemplo n.º 47
0
 def start_action(self, instring, loc, expr):
     self.writer.startElement(
         u"attempt",
         attributes={
             u"class": unicode(expr.__class__.__name__),
             u"loc": unicode(repr(loc)),
             u"expr": unicode(repr(expr)),
             u"lineno": unicode(lineno(loc, instring)),
             u"col": unicode(col(loc, instring)),
         },
     )
Ejemplo n.º 48
0
def set_line_number(string, location, tokens):
    # check and limit CPU usage
    context.check_and_limit_cpu_consumption()

    if len(tokens) == 1:
        line_number = lineno(location, string)
        tokens_cache[tokens[0]] = line_number
        tokens.line_number = line_number
    else:
        for item in tokens:
            tokens.line_number = tokens_cache.get(item)
Ejemplo n.º 49
0
 def const_action( self, text, loc, arg ):
     """ add the const to the self.constDict dictionary
     """
     logger = logging.getLogger(__name__)
     logger.debug("{0}:{1} const {2}".format(self.currentFile, lineno(loc, text), arg))
     label, value = arg
     if label in self.constDict:
         logger.error( "Error parsing const in file '{0}': attempted to redefine'{1}' to '{2}' from '{3}'".format(self.currentFile, label, value, self.constDict[label]) )
         raise ppexception("Redefining variable", self.currentFile, lineno, label)    
     else:
         self.constDict[label] = int(value)
Ejemplo n.º 50
0
    def var_action( self, text, loc, arg):
        print("var_action", self.currentFile, lineno(loc, text), arg[0:2], arg[2].split(",") if len(arg)>2 else "") 
        """ add a variable to the self.variablesdict
        """
        logger = logging.getLogger(__name__)
        logger.debug( "{0}:{1} Variable {2}".format( self.currentFile, lineno(loc, text), arg ) )
        var = Variable()
        label, data = arg[:2]
        fields = arg[2].split(",") if len(arg)>2 else [None]*3
        fields += [None]*(3-len(fields))
        var.type, unit, var.encoding = [ x if x is None or '' else x.strip() for x in fields ]
        var.name = label
        var.origin = self.currentFile
        var.enabled = True

        if var.encoding not in encodings:
            raise ppexception("unknown encoding {0} in file '{1}':{2}".format(var.encoding, self.currentFile, lineno(loc, text)), self.currentFile, lineno, var.encoding)

        try:
            data = str(eval(data, globals(), self.defines))
        except Exception:
            logger.exception( "Evaluation error in file '{0}' on line: '{1}'".format(self.currentFile, data) )

        if unit is not None:
            var.value = Q(float(data), unit)
            data = self.convertParameter( var.value, var.encoding )
        else:
            var.value = Q(float(data))
            data = int(round(float(data)))

        if label in self.defines:
            logger.error( "Error in file '%s': attempted to reassign '%s' to '%s' (from prev. value of '%s') in a var statement." %(self.currentFile, label, data, self.defines[label]) )
            raise ppexception("variable redifinition", self.currentFile, lineno, label)
        else:
            self.defines[label] = label # add the variable to the dictionary of definitions to prevent identifiers and variables from having the same name
                                        # however, we do not want it replaced with a number but keep the name for the last stage of compilation
            pass
        var.data = data
        self.variabledict.update({ label: var})
        if var.type == "exitcode":
            self._exitcodes[data & 0x0000ffff] = var
Ejemplo n.º 51
0
Archivo: parser.py Proyecto: tdi/pyPEPA
 def parse(self,string):
     try:
         self.gramma().parseString(string, parseAll=True)
     except ParseException as e:
         raise
     seen_procs = [str(x) for x in self._processes]
     for seen in self._seen:
         if seen not in seen_procs:
             line = lineno(self._seen[seen][0], self._seen[seen][1])
             column =col(self._seen[seen][0], self._seen[seen][1])
             raise ProcessNotDefinedError("{} process not defined - possible deadlock, line {}, col {}".format(seen,line, column))
     return (self._processes, self._var_stack, self._systemeq, self._actions)
Ejemplo n.º 52
0
 def __init__(self, filename, string,
              character=None, line=None, column=None):
     self.filename = filename
     self.string = string
     if character is None:
         assert line is not None and column is not None
         self.line = line
         self.col = column
         self.character = None
     else:
         assert line is None and column is None
         self.character = character
         self.line = lineno(character, string)
         self.col = col(character, string)
Ejemplo n.º 53
0
        def remove_indent(multiline, indent):
            """
            Generate the lines removing the indent
            """

            for line in multiline.splitlines():
                if line and not line[:indent].isspace():
                    warn("%s: %s: under-indented multiline string "
                         "truncated: '%s'" %
                         (lineno(loc, s), col(loc, s), line),
                         LettuceSyntaxWarning)

                # for those who are surprised by this, slicing a string
                # shorter than indent will yield empty string, not IndexError
                yield line[indent:]
 def associate(s,l,t):
     t = list(t)
     line = pp.lineno(l, s)
     while len(t) > 1:
         if assoc == 'none':
             if len(t) != 3:
                 raise pp.ParseException(s, l, name+' is not associative')
             t[0:3] = [dobinop(line, *t[0:3])]
         elif assoc == 'left':
             t[0:3] = [dobinop(line, *t[0:3])]
         elif assoc == 'right':
             t[-4:-1] = [dobinop(line, *t[-4:-1])]
         else:
             assert not "unhandled associativity"
     return t[0]
Ejemplo n.º 55
0
 def assertParses(self, syntax, text):
     try:
         return self.parse(syntax, text)
     except p.ParseBaseException as exc:
         if hasattr(exc, 'col') and hasattr(exc, 'lineno'):
             lineno = exc.lineno
             col = exc.col
         else:
             lineno = p.lineno(exc.loc, text)
             col = p.col(exc.loc, text)
         print()
         print("Parse error on line {} column {}: {}".format(
             lineno, col, exc))
         self._show_text(text, lineno, col, context=3)
         raise