How to use the bashlex.parser.parse 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
import unittest, functools

from bashlex import parser, state, flags, ast, errors

parse = functools.partial(parser.parse, convertpos=True)

def reservedwordnode(word, s):
    return ast.node(kind='reservedword', word=word, s=s)

def commandnode(s, *parts):
    return ast.node(kind='command', s=s, parts=list(parts))

def wordnode(word, s=None, parts=None):
    if s is None:
        s = word
    if parts is None:
        parts = []
    return ast.node(kind='word', word=word, s=s, parts=list(parts))

def assignmentnode(word, s=None, parts=None):
    node = wordnode(word, s, parts)
github idank / bashlex / examples / commandsubstitution-remover.py View on Github external
group = argparser.add_mutually_exclusive_group()
    group.add_argument('file', metavar='file', type=file, nargs='?',
                       help='file to parse')
    group.add_argument('-c', dest='expression',
                       help='string to parse')

    args = argparser.parse_args()

    if args.expression:
        s = args.expression
    elif args.file:
        s = args.file.read()
    else:
        s = sys.stdin.read()

    trees = parser.parse(s)
    positions = []
    for tree in trees:
        visitor = nodevisitor(positions)
        visitor.visit(tree)

    # do replacements from the end so the indicies will be correct
    positions.reverse()

    postprocessed = list(s)

    for start, end in positions:
        # replace the portion of the input where the substitution occurred
        # with the replacement string
        postprocessed[start:end] = args.replacement

    print ''.join(postprocessed)
github nickdiego / compiledb / compiledb / parser.py View on Github external
return []
        for tree in trees:
            svisitor = SubstCommandVisitor()
            svisitor.visit(tree)
            substs = svisitor.substs
            substs.reverse()
            preprocessed = list(line)
            for s in substs:
                start, end = s.command.pos
                s_cmd = line[start:end]
                out = run_cmd(s_cmd, shell=True, cwd=wd)
                start, end = s.pos
                preprocessed[start:end] = out.strip()
            preprocessed = ''.join(preprocessed)

        trees = bashlex.parser.parse(preprocessed)
        processor = CommandProcessor(preprocessed, wd)
        for tree in trees:
            processor.do_process(tree)
        return processor.commands
github nickdiego / compiledb / compiledb / parser.py View on Github external
def process(line, wd):
        trees = bashlex.parser.parse(line)
        if not trees:
            return []
        for tree in trees:
            svisitor = SubstCommandVisitor()
            svisitor.visit(tree)
            substs = svisitor.substs
            substs.reverse()
            preprocessed = list(line)
            for s in substs:
                start, end = s.command.pos
                s_cmd = line[start:end]
                out = run_cmd(s_cmd, shell=True, cwd=wd)
                start, end = s.pos
                preprocessed[start:end] = out.strip()
            preprocessed = ''.join(preprocessed)
github idank / bashlex / bashlex / __init__.py View on Github external
from bashlex import parser, tokenizer

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