How to use the bashlex.ast.findfirstkind function in bashlex

To help you get started, we’ve selected a few bashlex examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github idank / explainshell / explainshell / matcher.py View on Github external
if _iscompoundopenclosecurly(body):
            # create a matchresult until after the first {
            mr = matchresult(node.pos[0], body.list[0].pos[1],
                             helpconstants._function, None)
            self.groups[0].results.append(mr)

            # create a matchresult for the closing }
            mr = matchresult(body.list[-1].pos[0], body.list[-1].pos[1],
                             helpconstants._function, None)
            self.groups[0].results.append(mr)

            # visit anything in between the { }
            for part in body.list[1:-1]:
                self.visit(part)
        else:
            beforebody = bashlex.ast.findfirstkind(parts, 'compound') - 1
            assert beforebody > 0
            beforebody = parts[beforebody]

            # create a matchresult ending at the node before body
            mr = matchresult(node.pos[0], beforebody.pos[1],
                             helpconstants._function, None)
            self.groups[0].results.append(mr)

            self.visit(body)

        return False
github idank / explainshell / explainshell / matcher.py View on Github external
def visitcommand(self, node, parts):
        assert parts

        # look for the first WordNode, which might not be at parts[0]
        idxwordnode = bashlex.ast.findfirstkind(parts, 'word')
        if idxwordnode == -1:
            logger.info('no words found in command (probably contains only redirects)')
            return

        wordnode = parts[idxwordnode]

        # check if this refers to a previously defined function
        if wordnode.word in self.functions:
            logger.info('word %r is a function, not trying to match it or its '
                        'arguments', wordnode)

            # first, add a matchresult for the function call
            mr = matchresult(wordnode.pos[0], wordnode.pos[1],
                             helpconstants._functioncall % wordnode.word, None)
            self.matches.append(mr)
github idank / explainshell / explainshell / matcher.py View on Github external
def startcommand(self, commandnode, parts, endword, addgroup=True):
        logger.info('startcommand commandnode=%r parts=%r, endword=%r, addgroup=%s',
                    commandnode, parts, endword, addgroup)
        idxwordnode = bashlex.ast.findfirstkind(parts, 'word')
        assert idxwordnode != -1

        wordnode = parts[idxwordnode]
        if wordnode.parts:
            logger.info('node %r has parts (it was expanded), no point in looking'
                        ' up a manpage for it', wordnode)

            if addgroup:
                mg = matchgroup(self._generatecommandgroupname())
                mg.manpage = None
                mg.suggestions = None
                self.groups.append(mg)
                self.groupstack.append((commandnode, mg, endword))

            return False
github idank / explainshell / explainshell / matcher.py View on Github external
# add a group for this command, we'll mark it as unknown
                # when visitword is called
                logger.info('no manpage found for %r, adding a group for it',
                            wordnode.word)

                mg = matchgroup(self._generatecommandgroupname())
                mg.error = e
                mg.manpage = None
                mg.suggestions = None
                self.groups.append(mg)
                self.groupstack.append((commandnode, mg, endword))

            return False

        manpage = mps[0]
        idxnextwordnode = bashlex.ast.findfirstkind(parts, 'word')

        # check the next word for a possible multicommand if:
        # - the matched manpage says so
        # - we have another word node
        # - the word node has no expansions in it
        if manpage.multicommand and idxnextwordnode != -1 and not parts[idxnextwordnode].parts:
            nextwordnode = parts[idxnextwordnode]
            try:
                multi = '%s %s' % (wordnode.word, nextwordnode.word)
                logger.info('%r is a multicommand, trying to get another token and look up %r', manpage, multi)
                mps = self.findmanpages(multi)
                manpage = mps[0]
                # we consume this node here, pop it from parts so we
                # don't visit it again as an argument
                parts.pop(idxnextwordnode)
                endpos = nextwordnode.pos[1]
github idank / bashlex / bashlex / parser.py View on Github external
def p_function_def(p):
    '''function_def : WORD LEFT_PAREN RIGHT_PAREN newline_list function_body
                    | FUNCTION WORD LEFT_PAREN RIGHT_PAREN newline_list function_body
                    | FUNCTION WORD newline_list function_body'''
    parts = _makeparts(p)
    body = parts[-1]
    name = parts[ast.findfirstkind(parts, 'word')]

    p[0] = ast.node(kind='function', name=name, body=body, parts=parts,
                    pos=_partsspan(parts))