How to use the bashlex.errors 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-tokenizer.py View on Github external
def test_escape_error(self):
        return # TODO

        s = "a b\\"

        self.assertRaisesRegexp(errors.ParsingError, "No escaped character.*position 2", tokenize, s)
github idank / explainshell / tests / test-matcher.py View on Github external
def test_unparsed(self):
        cmd = '(bar; bar) c'
        self.assertRaises(bashlex.errors.ParsingError,
                          matcher.matcher(cmd, s).match)
github idank / bashlex / tests / test-parser.py View on Github external
def test_malformed_if(self):
        s = 'if foo; bar; fi'
        self.assertRaisesRegexp(errors.ParsingError, "unexpected token 'fi'.*position 13", parse, s)

        s = 'if foo; then bar;'
        self.assertRaisesRegexp(errors.ParsingError, "unexpected EOF.*position 17", parse, s)

        s = 'if foo; then bar; elif baz; fi'
        self.assertRaisesRegexp(errors.ParsingError, "unexpected token 'fi'.*position 28", parse, s)
github idank / bashlex / bashlex / parser.py View on Github external
def p_error(p):
    assert isinstance(p, tokenizer.token)

    if p.ttype == tokenizer.tokentype.EOF:
        raise errors.ParsingError('unexpected EOF',
                                  p.lexer.source,
                                  len(p.lexer.source))
    else:
        raise errors.ParsingError('unexpected token %r' % p.value,
                                  p.lexer.source, p.lexpos)
github cgat-developers / cgat-core / scripts / cgat_check_deps.py View on Github external
'transform',
                  'uncompress',
                  'execglam2',
                  'execglam2scan',
                  'CGATparameter',
                  'checkpoint',
                  'for']

    for statement in statements:
        # use bashlex to parse statements
        commands = []
        try:
            # print(statement)
            parts = bashlex.parse(statement)
            get_cmd_names(parts[0], commands)
        except bashlex.errors.ParsingError:
            pass

        for command in commands:
            # print(command)
            if command.lower() not in exceptions:
                if command not in deps:
                    deps[command] = 1
                else:
                    deps[command] += 1

    # list of unmet dependencies
    check_path_failures = []

    # print dictionary ordered by value
    for k in sorted(deps, key=deps.get, reverse=True):
        if shutil.which(k) is None:
github idank / bashlex / bashlex / subst.py View on Github external
elif c == '$' and len(string) > 1:
            tindex = sindex[0]
            node, sindex[0] = _paramexpand(parserobj, string, sindex[0])
            if node:
                parts.append(node)
            istring += string[tindex:sindex[0]]
        elif c == '`':
            tindex = sindex[0]
            # bare instance of ``
            if nextchar() == '`':
                sindex[0] += 1
                istring += '``'
            else:
                x = _stringextract(string, sindex[0], "`")
                if x == -1:
                    raise errors.ParsingError('bad substitution: no closing "`" '
                                              'in %s' % string)
                else:
                    if wordtoken.flags & flags.word.NOCOMSUB:
                        pass
                    else:
                        sindex[0] = x

                        word = string[tindex+1:sindex[0]]
                        command, ttindex = _recursiveparse(parserobj, word, 0)
                        _adjustpositions(command, tindex+1, len(string))
                        ttindex += 1 # ttindex is on the closing char

                        # assert sindex[0] == ttindex
                        # go one past the closing `
                        sindex[0] += 1
github idank / bashlex / bashlex / heredoc.py View on Github external
fullline = fullline[1:]

        if not fullline:
            continue

        if fullline[:-1] == redirword and fullline[len(redirword)] == '\n':
            document.append(fullline[:-1])
            # document_done
            break

        document.append(fullline)
        #fullline = self.readline(bool(redirnode.flags & flags.word.QUOTED))
        fullline = tokenizer.readline(False)

    if not fullline:
        raise errors.ParsingError("here-document at line %d delimited by end-of-file (wanted %r)" % (lineno, redirword), tokenizer._shell_input_line, tokenizer._shell_input_line_index)

    document = ''.join(document)
    endpos = tokenizer._shell_input_line_index - 1

    assert hasattr(redirnode, 'heredoc')
    redirnode.heredoc = ast.node(kind='heredoc', value=document,
                                 pos=(startpos, endpos))

    # if the heredoc immediately follows this node, fix its end pos
    if redirnode.pos[1] + 1 == startpos:
        redirnode.pos = (redirnode.pos[0], endpos)

    return document
github idank / bashlex / bashlex / tokenizer.py View on Github external
"while" : tokentype.WHILE,
    "until" : tokentype.UNTIL,
    "do" : tokentype.DO,
    "done" : tokentype.DONE,
    "in" : tokentype.IN,
    "function" : tokentype.FUNCTION,
    "time" : tokentype.TIME,
    "{" : tokentype.LEFT_CURLY,
    "}" : tokentype.RIGHT_CURLY,
    "!" : tokentype.BANG,
    "[[" : tokentype.COND_START,
    "]]" : tokentype.COND_END,
    "coproc" : tokentype.COPROC
}

class MatchedPairError(errors.ParsingError):
    def __init__(self, startline, message, tokenizer):
        # TODO use startline?
        super(MatchedPairError, self).__init__(message,
                                               tokenizer.source,
                                               tokenizer._shell_input_line_index - 1)

wordflags = flags.word
parserflags = flags.parser

class token(object):
    def __init__(self, type_, value, pos=None, flags=None):
        if type_ is not None:
            assert isinstance(type_, tokentype)

        if flags is None:
            flags = set()
github idank / bashlex / bashlex / parser.py View on Github external
def p_error(p):
    assert isinstance(p, tokenizer.token)

    if p.ttype == tokenizer.tokentype.EOF:
        raise errors.ParsingError('unexpected EOF',
                                  p.lexer.source,
                                  len(p.lexer.source))
    else:
        raise errors.ParsingError('unexpected token %r' % p.value,
                                  p.lexer.source, p.lexpos)
github idank / explainshell / explainshell / web / views.py View on Github external
command = command[:1000] # trim commands longer than 1000 characters
    if '\n' in command:
        return render_template('errors/error.html', title='parsing error!',
                               message='no newlines please')

    s = store.store('explainshell', config.MONGO_URI)
    try:
        matches, helptext = explaincommand(command, s)
        return render_template('explain.html',
                               matches=matches,
                               helptext=helptext,
                               getargs=command)

    except errors.ProgramDoesNotExist, e:
        return render_template('errors/missingmanpage.html', title='missing man page', e=e)
    except bashlex.errors.ParsingError, e:
        logger.warn('%r parsing error: %s', command, e.message)
        return render_template('errors/parsingerror.html', title='parsing error!', e=e)
    except NotImplementedError, e:
        logger.warn('not implemented error trying to explain %r', command)
        msg = ("the parser doesn't support %r constructs in the command you tried. you may "
               "<a href="https://github.com/idank/explainshell/issues">report a "
               "bug</a> to have this added, if one doesn't already exist.") % e.args[0]

        return render_template('errors/error.html', title='error!', message=msg)
    except:
        logger.error('uncaught exception trying to explain %r', command, exc_info=True)
        msg = 'something went wrong... this was logged and will be checked'
        return render_template('errors/error.html', title='error!', message=msg)