Python pyparsing.Literal() Examples

The following are 30 code examples of pyparsing.Literal(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module pyparsing , or try the search function .
Example #1
Source File: parsers.py    From rekall with GNU General Public License v2.0 7 votes vote down vote up
def anything_beetween(opener_and_closer):
    """Builds a (pyparsing) parser for the content inside delimiters.

    Args:
    opener_and_closer: a string containing two elements: opener and closer

    Returns:
      A (pyparsing) parser for the content inside delimiters.
    """
    opener = pyparsing.Literal(opener_and_closer[0])
    closer = pyparsing.Literal(opener_and_closer[1])
    char_removal_mapping = dict.fromkeys(map(ord, opener_and_closer))
    other_chars = unicode(string.printable).translate(char_removal_mapping)
    word_without_delimiters = pyparsing.Word(other_chars).setName(
        "other_chars")
    anything = pyparsing.Forward()
    delimited_block = opener + anything + closer
    # pylint: disable=expression-not-assigned
    anything << pyparsing.ZeroOrMore(
        word_without_delimiters.setName("word_without_delimiters")
        | delimited_block.setName("delimited_block")
    )

    # Combine all the parts into a single string.
    return pyparsing.Combine(anything) 
Example #2
Source File: ldap3mock.py    From privacyidea with GNU Affero General Public License v3.0 6 votes vote down vote up
def _parse_filter():
        op = pyparsing.oneOf('! & |')
        lpar  = pyparsing.Literal('(').suppress()
        rpar  = pyparsing.Literal(')').suppress()

        k = pyparsing.Word(pyparsing.alphanums)
        # NOTE: We may need to expand on this list, but as this is not a real
        # LDAP server we should be OK.
        # Value to contain:
        #   numbers, upper/lower case letters, astrisk, at symbol, minus, full
        #   stop, backslash or a space
        v = pyparsing.Word(pyparsing.alphanums + "-*@.\\ äöü")
        rel = pyparsing.oneOf("= ~= >= <=")

        expr = pyparsing.Forward()
        atom = pyparsing.Group(lpar + op + expr + rpar) \
                            | pyparsing.Combine(lpar + k + rel + v + rpar)
        expr << atom + pyparsing.ZeroOrMore( expr )

        return expr 
Example #3
Source File: sql_graphviz.py    From auptimizer with GNU General Public License v3.0 6 votes vote down vote up
def grammar():
    parenthesis = Forward()
    parenthesis <<= "(" + ZeroOrMore(CharsNotIn("()") | parenthesis) + ")"

    field_def = OneOrMore(Word(alphanums + "_\"'`:-") | parenthesis)
    field_def.setParseAction(field_act)

    tablename_def = ( Word(alphas + "`_") | QuotedString("\"") )

    field_list_def = field_def + ZeroOrMore(Suppress(",") + field_def)
    field_list_def.setParseAction(field_list_act)

    create_table_def = Literal("CREATE") + "TABLE" + tablename_def.setResultsName("tableName") + "(" + field_list_def.setResultsName("fields") + ")" + ";"
    create_table_def.setParseAction(create_table_act)

    add_fkey_def = Literal("FOREIGN") + "KEY" + "(" + Word(alphanums).setResultsName("keyName") + ")" + "REFERENCES" + Word(alphanums).setResultsName("fkTable") + "(" + Word(alphanums + "_").setResultsName("fkCol") + ")" + Optional(Literal("DEFERRABLE")) + ";"
    add_fkey_def.setParseAction(add_fkey_act)

    other_statement_def = OneOrMore(CharsNotIn(";")) + ";"
    other_statement_def.setParseAction(other_statement_act)

    comment_def = "--" + ZeroOrMore(CharsNotIn("\n"))
    comment_def.setParseAction(other_statement_act)

    return OneOrMore(comment_def | create_table_def | add_fkey_def | other_statement_def) 
Example #4
Source File: yara_support.py    From rekall with GNU General Public License v2.0 6 votes vote down vote up
def anything_beetween(opener_and_closer):
    """Builds a (pyparsing) parser for the content inside delimiters.

    Args:
    opener_and_closer: a string containing two elements: opener and closer

    Returns:
      A (pyparsing) parser for the content inside delimiters.
    """
    opener = pyparsing.Literal(opener_and_closer[0])
    closer = pyparsing.Literal(opener_and_closer[1])
    char_removal_mapping = dict.fromkeys(list(map(ord, opener_and_closer)))
    other_chars = str(string.printable).translate(char_removal_mapping)
    word_without_delimiters = pyparsing.Word(other_chars).setName(
        "other_chars")
    anything = pyparsing.Forward()
    delimited_block = opener + anything + closer
    # pylint: disable=expression-not-assigned
    anything << pyparsing.ZeroOrMore(
        word_without_delimiters.setName("word_without_delimiters")
        | delimited_block.setName("delimited_block")
    )

    # Combine all the parts into a single string.
    return pyparsing.Combine(anything) 
Example #5
Source File: expansions.py    From pyjsgf with MIT License 6 votes vote down vote up
def _collect_from_leaves(e, backtrack):
    result = []
    look_further = True
    for leaf in e.leaves:
        if isinstance(leaf, Dictation):
            # Add regex for a single dictation word.
            result.append(_word_regex_str)
        elif isinstance(leaf, Literal):
            # Add first word of literal.
            result.append(leaf.text.split()[0])
        else:
            # Skip references.
            continue

        # Break out of the loop if the leaf is required.
        if not leaf.is_optional:
            if not backtrack:
                look_further = False
            break
    return result, look_further 
Example #6
Source File: _parser.py    From pingparsing with MIT License 6 votes vote down vote up
def _parse_duplicate(self, line: str) -> Optional[int]:
        if not self._is_support_packet_duplicate:
            return None

        packet_pattern = (
            pp.SkipTo(pp.Word("+" + pp.nums) + pp.Literal("duplicates,"))
            + pp.Word("+" + pp.nums)
            + pp.Literal("duplicates,")
        )

        try:
            duplicate_parse_list = packet_pattern.parseString(_to_unicode(line))
        except pp.ParseException:
            return 0

        return int(duplicate_parse_list[-2].strip("+")) 
Example #7
Source File: expression_parser.py    From rekall with GNU General Public License v2.0 6 votes vote down vote up
def _build_precedence(self, precedence_table):
        # C's & dereference operator.
        precedence = []
        for operators, arity, associativity in precedence_table:
            operators = [pyparsing.Literal(x) for x in operators]

            if arity in [_UNARY, _BINARY]:
                operators = pyparsing.Or(operators)

            precedence.append((
                operators,
                arity,
                associativity,
                self._construct_operator(arity),
            ))
        return precedence 
Example #8
Source File: operators.py    From plyse with MIT License 5 votes vote down vote up
def __init__(self, name, symbols, implicit=False):
        symbols_ = [Literal(s) if len(s) == 1 else CaselessKeyword(s) for s in symbols]
        super(Operator, self).__init__(concatenate(symbols_, operator='OR'))

        self.name = name
        self.implicit = implicit 
Example #9
Source File: parsing.py    From online-ratings with MIT License 5 votes vote down vote up
def _quoted(expr):
    return Combine(Suppress(Literal("'")) + expr + Suppress(Literal("'"))) 
Example #10
Source File: cpu.py    From miasm with GNU General Public License v2.0 5 votes vote down vote up
def literal_list(l):
    l = l[:]
    l.sort()
    l = l[::-1]
    o = pyparsing.Literal(l[0])
    for x in l[1:]:
        o |= pyparsing.Literal(x)
    return o 
Example #11
Source File: cpu.py    From miasm with GNU General Public License v2.0 5 votes vote down vote up
def literal_list(l):
    l = l[:]
    l.sort()
    l = l[::-1]
    o = pyparsing.Literal(l[0])
    for x in l[1:]:
        o |= pyparsing.Literal(x)
    return o 
Example #12
Source File: modulefilter.py    From wfuzz with GNU General Public License v2.0 5 votes vote down vote up
def __init__(self):
        if PYPARSING:
            category = Word(alphas + "_-*", alphanums + "_-*")
            operator = oneOf("and or ,")
            neg_operator = "not"
            elementRef = category
            definition = elementRef + ZeroOrMore(operator + elementRef)
            nestedformula = Group(Suppress(Optional(Literal("("))) + definition + Suppress(Optional(Literal(")"))))
            neg_nestedformula = Optional(neg_operator) + nestedformula
            self.finalformula = neg_nestedformula + ZeroOrMore(operator + neg_nestedformula)

            elementRef.setParseAction(self.__compute_element)
            neg_nestedformula.setParseAction(self.__compute_neg_formula)
            nestedformula.setParseAction(self.__compute_formula)
            self.finalformula.setParseAction(self.__myreduce) 
Example #13
Source File: utilities.py    From pyactr with GNU General Public License v3.0 5 votes vote down vote up
def getrule():
    """
    Using pyparsing, get rule out of a string.
    """
    arrow = pp.Literal("==>")
    buff = pp.Word(pp.alphas, "".join([pp.alphanums, "_"]))
    special_valueLHS = pp.oneOf([x for x in _LHSCONVENTIONS.keys()])
    end_buffer = pp.Literal(">")
    special_valueRHS = pp.oneOf([x for x in _RHSCONVENTIONS.keys()])
    chunk = getchunk()
    rule_reader = pp.Group(pp.OneOrMore(pp.Group(special_valueLHS + buff + end_buffer + pp.Group(pp.Optional(chunk))))) + arrow + pp.Group(pp.OneOrMore(pp.Group(special_valueRHS + buff + end_buffer + pp.Group(pp.Optional(chunk)))))
    return rule_reader 
Example #14
Source File: yara_support.py    From rekall with GNU General Public License v2.0 5 votes vote down vote up
def strings_section():
    return pyparsing.Group(
        pyparsing.Literal("strings") +
        _COLON +
        pyparsing.OneOrMore(statement()).setResultsName("statements")
    ).setResultsName("strings") 
Example #15
Source File: terms.py    From plyse with MIT License 5 votes vote down vote up
def __init__(self, keyword_name, possible_values, separator=':', parse_method=None, allow_other_values=True):

        _values = concatenate(possible_values, operator="LONGEST_OR", class_to_embed_elem=CaselessKeyword)

        if allow_other_values:
            _values ^= SimpleWord()

        super(KeywordTerm, self).__init__(CaselessKeyword(keyword_name) + Literal(separator) + _values)

        self.name = keyword_name
        self.values = possible_values

        if parse_method:
            self.setParseAction(parse_method) 
Example #16
Source File: console.py    From medicare-demo with Apache License 2.0 5 votes vote down vote up
def __hasDeclBegin(self, line):
        if not self.declBegin:
            import pyparsing as p
            self.declBegin = p.stringStart+p.Literal("`")
            self.declBegin.ignore(p.pythonStyleComment)
            self.declBegin.ignore(p.quotedString)

        if self.declBegin.searchString(line.strip()):
            return True
        return False 
Example #17
Source File: console.py    From medicare-demo with Apache License 2.0 5 votes vote down vote up
def __hasDeclEnd(self, line):
        if not self.declEnd: 
            import pyparsing as p
            self.declEnd = p.Literal("`") + p.stringEnd
            self.declEnd.ignore(p.pythonStyleComment)
            self.declEnd.ignore(p.quotedString)

        if self.declEnd.searchString(line.strip()):
            return True
        return False 
Example #18
Source File: pyconsole.py    From medicare-demo with Apache License 2.0 5 votes vote down vote up
def __hasDeclBegin(self, line):
        if not self.declBegin:
            import pyparsing as p
            self.declBegin = p.stringStart+p.Literal("`")
            self.declBegin.ignore(p.pythonStyleComment)
            self.declBegin.ignore(p.quotedString)

        if self.declBegin.searchString(line.strip()):
            return True
        return False 
Example #19
Source File: pyconsole.py    From medicare-demo with Apache License 2.0 5 votes vote down vote up
def __hasDeclEnd(self, line):
        if not self.declEnd: 
            import pyparsing as p
            self.declEnd = p.Literal("`") + p.stringEnd
            self.declEnd.ignore(p.pythonStyleComment)
            self.declEnd.ignore(p.quotedString)

        if self.declEnd.searchString(line.strip()):
            return True
        return False 
Example #20
Source File: console.py    From medicare-demo with Apache License 2.0 5 votes vote down vote up
def __hasDeclBegin(self, line):
        if not self.declBegin:
            import pyparsing as p
            self.declBegin = p.stringStart+p.Literal("`")
            self.declBegin.ignore(p.pythonStyleComment)
            self.declBegin.ignore(p.quotedString)

        if self.declBegin.searchString(line.strip()):
            return True
        return False 
Example #21
Source File: romanNumerals.py    From pyparsing with MIT License 5 votes vote down vote up
def romanNumeralLiteral(numeralString, value):
    return pp.Literal(numeralString).setParseAction(pp.replaceWith(value)) 
Example #22
Source File: strategies.py    From requirementslib with MIT License 5 votes vote down vote up
def flatten_pyparsing_exprs(expr):
    exprs = set()
    for child in expr.exprs:
        if isinstance(child, (Literal, six.string_types)):
            exprs.add(str(child).strip('"'))
        elif isinstance(child, (MatchFirst, ParseExpression, ParserElement)):
            exprs.update(flatten_pyparsing_exprs(child))
    return exprs 
Example #23
Source File: preprocessing_parser.py    From rekall with GNU General Public License v2.0 5 votes vote down vote up
def _define_function_like(self):
        return (
            (_IDENTIFIER.setResultsName("name")
             + _OPEN_PARENTHESES).leaveWhitespace()
            + pyparsing.Optional(
                pyparsing.delimitedList(
                    _IDENTIFIER
                    | pyparsing.Literal("...")  # vararg macro.
                )).setResultsName("arguments")
            + _CLOSE_PARENTHESES
            + pyparsing.restOfLine.setResultsName("replacement")
        ).setParseAction(self._add_function_like) 
Example #24
Source File: expansions.py    From pyjsgf with MIT License 5 votes vote down vote up
def make_expansion(e):
        """
        Take an object, turn it into an Expansion if it isn't one and return it.

        :param e: str | Expansion
        :returns: Expansion
        """
        if isinstance(e, Expansion):
            return e
        elif isinstance(e, string_types):
            return Literal(e)
        else:
            raise TypeError("expected a string or Expansion, got %s instead"
                            % e) 
Example #25
Source File: expansions.py    From pyjsgf with MIT License 5 votes vote down vote up
def __init__(self, text, case_sensitive=False):
        # Set _text and use the text setter to validate the input.
        self._text = ""
        self.text = text
        self._case_sensitive = bool(case_sensitive)
        super(Literal, self).__init__([]) 
Example #26
Source File: expansions.py    From pyjsgf with MIT License 5 votes vote down vote up
def case_sensitive(self):
        """
        Case sensitivity used when matching and compiling :class:`Literal` rule
        expansions.

        This property can be ``True`` or ``False``. Matching and compilation will
        be *case-sensitive* if ``True`` and *case-insensitive* if ``False``. The
        default value is ``False``.

        :rtype: bool
        :returns: literal case sensitivity
        """
        return self._case_sensitive 
Example #27
Source File: expansions.py    From pyjsgf with MIT License 5 votes vote down vote up
def compile(self, ignore_tags=False):
        super(Literal, self).compile()
        if self.tag and not ignore_tags:
            return "%s%s" % (self.text, self.compiled_tag)
        else:
            return self.text 
Example #28
Source File: expansions.py    From pyjsgf with MIT License 5 votes vote down vote up
def _make_matcher_element(self):
        # Return a case-sensitive or case-insensitive pyparsing Literal element.
        text = self._text
        if self.case_sensitive:
            matcher_cls = pyparsing.Literal
        else:
            matcher_cls = pyparsing.CaselessLiteral
        return self._set_matcher_element_attributes(matcher_cls(text)) 
Example #29
Source File: expansions.py    From pyjsgf with MIT License 5 votes vote down vote up
def __init__(self):
        # Pass the empty string to the Literal constructor so that calling compile
        # yields "" or "" + the tag
        super(Dictation, self).__init__("")
        self._use_current_match = False 
Example #30
Source File: case_parser.py    From psst with MIT License 5 votes vote down vote up
def parse_line(attribute, string):

    Grammar = Suppress(Keyword('mpc.{}'.format(attribute)) + Keyword('=')) + String('data') + Suppress(Literal(';') + Optional(Comments))
    result, i, j = Grammar.scanString(string).next()

    return [int_else_float_except_string(s) for s in result['data'].asList()]