Ejemplo n.º 1
0
    nestedExpr,
)
from undebt.pattern.common import (
    NL,
    ANY_CHAR,
    WHITE,
    NUM,
)
from undebt.cmd.logic import (
    create_find_and_replace,
    parse_grammar,
    _transform_results,
)

# util:
NUM = originalTextFor(NUM)

REST_OF_LINE = originalTextFor(NL | (ANY_CHAR | WHITE) + SkipTo(NL) + NL)


@tokens_as_list(assert_len=1)
def trim(tokens):
    return tokens[0][1:-1]


BRACES = attach(originalTextFor(nestedExpr("{", "}", ignoreExpr=NL)), trim)

BRACKETS = attach(originalTextFor(nestedExpr("[", "]", ignoreExpr=NL)), trim)

# setup:
patterns_list = []
Ejemplo n.º 2
0
from undebt.pattern.util import condense
from undebt.pattern.util import in_string
from undebt.pattern.util import tokens_as_list
from undebt.pyparsing import Keyword
from undebt.pyparsing import Literal
from undebt.pyparsing import Optional
from undebt.pyparsing import originalTextFor
from undebt.pyparsing import ZeroOrMore


unicode_literals = Keyword("unicode_literals")
non_unicode_literals_name = ~unicode_literals + NAME
import_grammar = originalTextFor(
    Keyword("from") + Keyword("__future__") + Keyword("import")
    + ZeroOrMore(non_unicode_literals_name + COMMA)
    + unicode_literals
    + ZeroOrMore(COMMA + non_unicode_literals_name)
    + Optional(COMMA)
)


@tokens_as_list(assert_len=1)
def identity_replace(tokens):
    return tokens[0]


u_string_grammar = Literal("u").suppress() + condense(Optional(Literal("r")) + STRING)


@tokens_as_list(assert_len=1)
def u_string_replace(original, location, tokens):
Ejemplo n.º 3
0
def quoted(string):
    """Match a string containing the given string."""
    return originalTextFor(
        Combine("'" + string + "'") | Combine('"' + string + '"'))
Ejemplo n.º 4
0
    Word,
    Regex,
)
from undebt.pattern.common import (
    NL,
    ANY_CHAR,
    WHITE,
)
from undebt.cmd.logic import (
    create_find_and_replace,
    parse_grammar,
    _transform_results,
)

# util:
NUM = originalTextFor(Word("0123456789"))

REST_OF_LINE = originalTextFor(NL | (ANY_CHAR | WHITE) + SkipTo(NL) + NL)


@tokens_as_list(assert_len=1)
def trim(tokens):
    return tokens[0][1:-1]


PARENS = attach(
    originalTextFor(
        Literal("(") + OneOrMore(~NL + ~Literal(")") + ANY_CHAR) +
        Literal(")")), trim)
BRACKETS = attach(
    originalTextFor(
Ejemplo n.º 5
0
def _untouched_nested_expr(left, right):
    """Matches a nested expression without changing the original text."""
    return originalTextFor(nestedExpr(left, right, ignoreExpr=NL | STRING))
Ejemplo n.º 6
0
def _untouched_string(quote):
    """Matches a string from quote to quote. Does not handle escapes."""
    return originalTextFor(Literal(quote) + SkipTo(quote) + Literal(quote))
Ejemplo n.º 7
0
START_OF_FILE = StringStart().suppress()
END_OF_FILE = StringEnd().suppress()

NL = Literal("\n")
DOT = Literal(".")
LPAREN = Literal("(")
RPAREN = Literal(")")
COMMA = Literal(",")
COLON = Literal(":")

LINE_START = NL | fixto(START_OF_FILE, "")
NO_BS_NL = Regex(r"(?<!\\)").suppress() + NL

SKIP_TO_TEXT = SkipTo(CharsNotIn(WHITESPACE_OR_NL_CHARS))
SKIP_TO_TEXT_OR_NL = SkipTo(CharsNotIn(WHITESPACE_CHARS))
INDENT = originalTextFor(LINE_START + SKIP_TO_TEXT_OR_NL)

WHITE = ~START_OF_FILE + Word(WHITESPACE_CHARS).setWhitespaceChars("")
NL_WHITE = ~START_OF_FILE + Word(WHITESPACE_OR_NL_CHARS).setWhitespaceChars("")

COMMA_IND = condense(COMMA + Optional(INDENT))
LPAREN_IND = condense(LPAREN + Optional(INDENT))
IND_RPAREN = condense(Optional(INDENT) + RPAREN)


def _untouched_string(quote):
    """Matches a string from quote to quote. Does not handle escapes."""
    return originalTextFor(Literal(quote) + SkipTo(quote) + Literal(quote))


TRIPLE_DBL_QUOTE_STRING = _untouched_string('"""')
Ejemplo n.º 8
0
    | Keyword("and")
    | Keyword("or")
    | addspace(OneOrMore(Keyword("is")
                         | Keyword("not")
                         | Keyword("in"))))

OP = ASSIGN_OP | UNARY_OP | BINARY_OP

TRAILER = DOT + NAME | PARENS | BRACKETS
TRAILERS = condense(ZeroOrMore(TRAILER))

ATOM_BASE = NAME | NUM | PARENS | BRACKETS | BRACES | STRING
ATOM = condense(ATOM_BASE + TRAILERS)
UNARY_OP_ATOM = addspace(Optional(UNARY_OP) + ATOM)

EXPR = addspace(UNARY_OP_ATOM + ZeroOrMore(BINARY_OP + UNARY_OP_ATOM))
EXPR_LIST = condense(EXPR + ZeroOrMore(addspace(COMMA + EXPR)) +
                     Optional(COMMA))
EXPR_IND_LIST = originalTextFor(EXPR + ZeroOrMore(COMMA_IND + EXPR) +
                                Optional(COMMA_IND))

PARAM = condense(Optional(Word("*") | NAME + Literal("=")) + EXPR)
PARAMS = originalTextFor(PARAM + ZeroOrMore(COMMA_IND + PARAM) +
                         Optional(COMMA_IND))

HEADER = originalTextFor(START_OF_FILE + ZeroOrMore(SKIP_TO_TEXT + (
    STRING
    | pythonStyleComment
    | Optional(Keyword("from") + SkipTo("import")) + Keyword("import") +
    (PARENS | SkipTo(NO_BS_NL))) + NL))