How to use the pyparsing.Regex function in pyparsing

To help you get started, we’ve selected a few pyparsing 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 pyparsing / pyparsing / examples / lucene_grammar.py View on Github external
# at http://svn.apache.org/viewvc/lucene/dev/trunk/lucene/docs/queryparsersyntax.html
#

import pyparsing as pp
from pyparsing import pyparsing_common as ppc

pp.ParserElement.enablePackrat()

COLON, LBRACK, RBRACK, LBRACE, RBRACE, TILDE, CARAT = map(pp.Literal, ":[]{}~^")
LPAR, RPAR = map(pp.Suppress, "()")
and_, or_, not_, to_ = map(pp.CaselessKeyword, "AND OR NOT TO".split())
keyword = and_ | or_ | not_ | to_

expression = pp.Forward()

valid_word = pp.Regex(
    r'([a-zA-Z0-9*_+.-]|\\\\|\\([+\-!(){}\[\]^"~*?:]|\|\||&&))+'
).setName("word")
valid_word.setParseAction(
    lambda t: t[0].replace("\\\\", chr(127)).replace("\\", "").replace(chr(127), "\\")
)

string = pp.QuotedString('"')

required_modifier = pp.Literal("+")("required")
prohibit_modifier = pp.Literal("-")("prohibit")
integer = ppc.integer()
proximity_modifier = pp.Group(TILDE + integer("proximity"))
number = ppc.fnumber()
fuzzy_modifier = TILDE + pp.Optional(number, default=0.5)("fuzzy")

term = pp.Forward().setName("field")
github pkienzle / periodictable / periodictable / formulas.py View on Github external
# Translate isotope
    openiso = Literal('[').suppress()
    closeiso = Literal(']').suppress()
    isotope = Optional(~White()+openiso+Regex("[1-9][0-9]*")+closeiso,
                       default='0')
    isotope = isotope.setParseAction(lambda s, l, t: int(t[0]) if t[0] else 0)

    # Translate ion
    openion = Literal('{').suppress()
    closeion = Literal('}').suppress()
    ion = Optional(~White() +openion +Regex("([1-9][0-9]*)?[+-]") +closeion,
                   default='0+')
    ion = ion.setParseAction(lambda s, l, t: int(t[0][-1]+(t[0][:-1] if len(t[0]) > 1 else '1')))

    # Translate counts
    fract = Regex("(0|[1-9][0-9]*|)([.][0-9]*)")
    fract = fract.setParseAction(lambda s, l, t: float(t[0]) if t[0] else 1)
    whole = Regex("[1-9][0-9]*")
    whole = whole.setParseAction(lambda s, l, t: int(t[0]) if t[0] else 1)
    count = Optional(~White()+(fract|whole), default=1)

    # Convert symbol, isotope, ion, count to (count, isotope)
    element = symbol+isotope+ion+count
    def convert_element(string, location, tokens):
        """interpret string as element"""
        #print "convert_element received", tokens
        symbol, isotope, ion, count = tokens[0:4]
        if isotope != 0:
            symbol = symbol[isotope]
        if ion != 0:
            symbol = symbol.ion[ion]
        return (count, symbol)
github nil0x42 / phpsploit / deps / pyparsing-2.0.2 / examples / lucene_grammar.py View on Github external
not_ = CaselessKeyword("NOT")
to_ = CaselessKeyword("TO")
keyword = and_ | or_ | not_

expression = Forward()

valid_word = Regex(r'([a-zA-Z0-9*_+.-]|\\[!(){}\[\]^"~*?\\:])+').setName("word")
valid_word.setParseAction(
    lambda t : t[0].replace('\\\\',chr(127)).replace('\\','').replace(chr(127),'\\')
    )

string = QuotedString('"')

required_modifier = Literal("+")("required")
prohibit_modifier = Literal("-")("prohibit")
integer = Regex(r"\d+").setParseAction(lambda t:int(t[0]))
proximity_modifier = Group(TILDE + integer("proximity"))
number = Regex(r'\d+(\.\d+)?').setParseAction(lambda t:float(t[0]))
fuzzy_modifier = TILDE + Optional(number, default=0.5)("fuzzy")

term = Forward()
field_name = valid_word.copy().setName("fieldname")
incl_range_search = Group(LBRACK + term("lower") + to_ + term("upper") + RBRACK)
excl_range_search = Group(LBRACE + term("lower") + to_ + term("upper") + RBRACE)
range_search = incl_range_search("incl_range") | excl_range_search("excl_range")
boost = (CARAT + number("boost"))

string_expr = Group(string + proximity_modifier) | string
word_expr = Group(valid_word + fuzzy_modifier) | valid_word
term << (Optional(field_name("field") + COLON) + 
         (word_expr | string_expr | range_search | Group(LPAR + expression + RPAR)) +
         Optional(boost))
github bdcht / amoco / amoco / system / structs.py View on Github external
"""StructDefine is a decorator class used for defining structures
    by parsing a simple intermediate language input decorating a StructFormatter class.
    """
    All = {}
    rawtypes   = ('x','c','b','B','h','H','i','I','l','L','f','d','s','n','N','p','P','q','Q')
    alignments = {'x':1, 'c':1, 'b':1, 'B':1, 's':1,
                  'h':2, 'H':2,
                  'i':4, 'I':4, 'l':4, 'L':4, 'f':4,
                  'q':8, 'Q':8, 'd':8,
                  'P':8}
    integer    = pp.Regex(r'[1-9][0-9]*')
    integer.setParseAction(lambda r: int(r[0]))
    symbol     = pp.Regex(r'[A-Za-z_][A-Za-z0-9_]*')
    comment    = pp.Suppress(';')+pp.restOfLine
    fieldname  = pp.Suppress(':')+pp.Group(pp.Optional(pp.Literal('>')|pp.Literal('<'),default=None)+symbol)
    inf        = pp.Regex(r'~[bBhHiI]?')
    length     = integer|symbol|inf
    typename   = pp.Group(symbol+pp.Optional(pp.Suppress('*')+length,default=0))
    structfmt  = pp.OneOrMore(pp.Group(typename+fieldname+pp.Optional(comment,default='')))

    def __init__(self,fmt,**kargs):
        self.fields = []
        self.source = fmt
        self.packed = kargs.get('packed',False)
        if 'alignments' in kargs:
           self.alignments = kargs['alignments']
        for l in self.structfmt.parseString(fmt,True):
            f_type,f_name,f_comment = l
            f_order,f_name = f_name
            f_type,f_count = f_type
            if f_order is None and 'order' in kargs:
                f_order=kargs['order']
github pkienzle / periodictable / periodictable / formulas.py View on Github external
def convert_by_volume(string, location, tokens):
        """convert mixture by %vol"""
        #print "by volume", tokens
        piece = tokens[1:-1:2] + [tokens[-1]]
        fract = [float(v) for v in tokens[:-1:2]]
        fract.append(100-sum(fract))
        #print piece, fract
        if len(piece) != len(fract):
            raise ValueError("Missing base component of mixture "+string)
        if fract[-1] < 0:
            raise ValueError("Formula percentages must sum to less than 100%")
        return _mix_by_volume_pairs(zip(piece, fract))
    mixture_by_volume = by_volume.setParseAction(convert_by_volume)

    mixture_by_layer = Forward()
    layer_thick = Group(count + Regex(LENGTH_RE) + space)
    layer_part = (layer_thick + mixture) | (opengrp + mixture_by_layer + closegrp +count)
    mixture_by_layer << layer_part + ZeroOrMore(partsep + layer_part)
    def convert_by_layer(string, location, tokens):
        """convert layer thickness '# nm material'"""
        if len(tokens) < 2:
            return tokens
        piece = []
        fract = []
        for p1, p2 in zip(tokens[0::2], tokens[1::2]):
            if isinstance(p1, Formula):
                f = p1.absthick * float(p2)
                p = p1
            else:
                f = float(p1[0]) * LENGTH_UNITS[p1[1]]
                p = p2
            piece.append(p)
github nerdvegas / rez / to_delete / parse_request.py View on Github external
def __init__(self):
        import pyparsing as pp
        _pkg = pp.Regex("[a-zA-Z_0-9~<=^\\.\\-\\!\\+]+").setParseAction(self._parse_pkg)

        _subshell_label = pp.Regex("[a-zA-Z0-9_]+")
        _subshell_label_decl = (_subshell_label + ':').setParseAction(self._parse_subshell_label)
        _subshell_body = (_subshell_label_decl * (0, 1)) + pp.OneOrMore(_pkg)
        _subshell_prefix = (pp.Regex("[a-zA-Z0-9_]+\\(") ^ '(').setParseAction(self._parse_subshell_prefix)
        _subshell_suffix = (pp.Regex("\\)[a-zA-Z0-9_]+") ^ ')').setParseAction(self._parse_subshell_suffix)
        _subshell = _subshell_prefix + _subshell_body + _subshell_suffix

        _request = pp.OneOrMore(_pkg ^ _subshell).setParseAction(self._parse_subshell_request)
        self.expr = _request + pp.ZeroOrMore('|' + _request)
github yellekelyk / PyVerilog / verilogParse.py View on Github external
identLead = alphas+"$_"
        identBody = alphanums+"$_"
        identifier1 = Regex( r"\.?["+identLead+"]["+identBody+"]*(\.["+identLead+"]["+identBody+"]*)*"
                            ).setName("baseIdent")
        identifier2 = Regex(r"\\\S+").setParseAction(lambda t:t[0][1:]).setName("escapedIdent")
        identifier = identifier1 | identifier2
        
        hexnums = nums + "abcdefABCDEF" + "_?"
        base = Regex("'[bBoOdDhH]").setName("base")
        basedNumber = Combine( Optional( Word(nums + "_") ) + base + Word(hexnums+"xXzZ"),
                               joinString=" ", adjacent=False ).setName("basedNumber")
        #~ number = ( basedNumber | Combine( Word( "+-"+spacedNums, spacedNums ) +
                           #~ Optional( "." + Optional( Word( spacedNums ) ) ) +
                           #~ Optional( e + Word( "+-"+spacedNums, spacedNums ) ) ).setName("numeric") )
        number = ( basedNumber | \
                   Regex(r"[+-]?[0-9_]+(\.[0-9_]*)?([Ee][+-]?[0-9_]+)?") \
                  ).setName("numeric")
        #~ decnums = nums + "_"
        #~ octnums = "01234567" + "_"
        expr = Forward().setName("expr")
        concat = Group( "{" + delimitedList( expr ) + "}" )
        multiConcat = Group("{" + expr + concat + "}").setName("multiConcat")
        funcCall = Group(identifier + "(" + Optional( delimitedList( expr ) ) + ")").setName("funcCall")

        subscrRef = Group("[" + delimitedList( expr, ":" ) + "]")
        subscrIdentifier = Group( identifier + Optional( subscrRef ) )
        #~ scalarConst = "0" | (( FollowedBy('1') + oneOf("1'b0 1'b1 1'bx 1'bX 1'B0 1'B1 1'Bx 1'BX 1") ))
        scalarConst = Regex("0|1('[Bb][01xX])?")
        mintypmaxExpr = Group( expr + ":" + expr + ":" + expr ).setName("mintypmax")
        primary = (
                  number |
                  ("(" + mintypmaxExpr + ")" ) |
github flavour / ifrc / modules / s3 / s3query.py View on Github external
selector.setParseAction(context)

        keyword = lambda x, y: x | pp.Keyword(y) if x else pp.Keyword(y)

        # Expression Syntax
        function = reduce(keyword, S3FieldSelector.OPERATORS)
        expression = function + \
                     pp.Literal("(").suppress() + \
                     selector + \
                     pp.Literal(")").suppress()

        # Comparison Syntax
        comparison = reduce(keyword, S3ResourceQuery.COMPARISON)

        # Value Syntax
        number = pp.Regex(r"[+-]?\d+(:?\.\d*)?(:?[eE][+-]?\d+)?")
        value = number | \
                pp.Keyword("NONE") | \
                pp.quotedString | \
                pp.Word(pp.alphanums + pp.printables)
        qe = pp.Group(pp.Group(expression | selector) +
                      comparison +
                      pp.originalTextFor(pp.delimitedList(value, combine=True)))

        parser = pp.operatorPrecedence(qe, [("not", 1, pp.opAssoc.RIGHT, ),
                                            ("and", 2, pp.opAssoc.LEFT, ),
                                            ("or", 2, pp.opAssoc.LEFT, ),
                                            ])

        self.parser = parser
        self.ParseResults = pp.ParseResults
        self.ParseException = pp.ParseException
github rix0rrr / gcl / gcl / ast.py View on Github external
def make_grammar(allow_errors):
  """Make the part of the grammar that depends on whether we swallow errors or not."""
  if allow_errors in GRAMMAR_CACHE:
    return GRAMMAR_CACHE[allow_errors]

  tuple = p.Forward()
  catch_errors = p.Forward()
  catch_errors << (p.Regex('[^{};]*') - p.Optional(tuple) - p.Regex('[^;}]*'))

  def swallow_remainder():
    if allow_errors:
      return pattern('swallow_remainder', p.Suppress(catch_errors))
    return p.Empty()

  def swallow_errors(rule):
    """Extend the production rule by potentially eating errors.

    This does not return a p.NoMatch() because that messes up the error messages.
    """
    ret = rule
    if allow_errors:
      # Synchronize on the first semicolon or the first unbalanced closing curly
      ret = rule | pattern('catch_errors', parseWithLocation(p.Suppress(catch_errors), UnparseableNode))
    return ret