How to use the bashlex.parser 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 / bashlex / tests / test-parser.py View on Github external
def test_expansion_limit(self):
        '''make sure the expansion limit is working by tracking recursive
        parsing count, and also checking that the word isn't expanded'''
        counter = [0]
        class countingparser(parser._parser):
            def __init__(self, *args, **kwargs):
                super(countingparser, self).__init__(*args, **kwargs)
                counter[0] += 1

        old = parser._parser
        parser._parser = countingparser

        try:
            s = 'a $(b $(c $(d $(e))))'
            self.assertASTEquals(s,
                commandnode(s,
                  wordnode('a'),
                  wordnode('$(b $(c $(d $(e))))', '$(b $(c $(d $(e))))', [
                    comsubnode('$(b $(c $(d $(e))))',
                      commandnode('b $(c $(d $(e)))',
                        wordnode('b'),
github idank / explainshell / explainshell / matcher.py View on Github external
def match(self):
        logger.info('matching string %r', self.s)

        # limit recursive parsing to a depth of 1
        self.ast = bashlex.parser.parsesingle(self.s, expansionlimit=1,
                                              strictmode=False)
        if self.ast:
            self.visit(self.ast)
            assert len(self.groupstack) == 1, 'groupstack should contain only shell group after matching'

            # if we only have one command in there and no shell results/expansions,
            # reraise the original exception
            if (len(self.groups) == 2 and not self.groups[0].results and
                self.groups[1].manpage is None and not self.expansions):
                raise self.groups[1].error
        else:
            logger.warn('no AST generated for %r', self.s)

        def debugmatch():
            s = '\n'.join(['%d) %r = %r' % (i, self.s[m.start:m.end], m.text) for i, m in enumerate(self.allmatches)])
            return s
github idank / bashlex / bashlex / subst.py View on Github external
# TODO: fix this hack that prevents mutual import
    from bashlex import parser

    tok = parserobj.tok

    if tokenizerargs is None:
        tokenizerargs = {'parserstate' : copy.copy(tok._parserstate),
                         'lastreadtoken' : tok._last_read_token,
                         'tokenbeforethat' : tok._token_before_that,
                         'twotokensago' : tok._two_tokens_ago}

    string = base[sindex:]
    newlimit = parserobj._expansionlimit
    if newlimit is not None:
        newlimit -= 1
    p = parser._parser(string, tokenizerargs=tokenizerargs,
                       expansionlimit=newlimit)
    node = p.parse()

    endp = node.pos[1]
    _adjustpositions(node, sindex, len(base))

    return node, endp
github idank / bashlex / bashlex / __init__.py View on Github external
from bashlex import parser, tokenizer

parse = parser.parse
parsesingle = parser.parsesingle
split = parser.split