How to use the pyparsing.Word 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 datajoint / datajoint-python / datajoint / declare.py View on Github external
def build_foreign_key_parser_old():
    # old-style foreign key parser. Superceded by expression-based syntax. See issue #436
    # This will be deprecated in a future release.
    left = pp.Literal('(').suppress()
    right = pp.Literal(')').suppress()
    attribute_name = pp.Word(pp.srange('[a-z]'), pp.srange('[a-z0-9_]'))
    new_attrs = pp.Optional(left + pp.delimitedList(attribute_name) + right).setResultsName('new_attrs')
    arrow = pp.Literal('->').suppress()
    lbracket = pp.Literal('[').suppress()
    rbracket = pp.Literal(']').suppress()
    option = pp.Word(pp.srange('[a-zA-Z]'))
    options = pp.Optional(lbracket + pp.delimitedList(option) + rbracket).setResultsName('options')
    ref_table = pp.Word(pp.alphas, pp.alphanums + '._').setResultsName('ref_table')
    ref_attrs = pp.Optional(left + pp.delimitedList(attribute_name) + right).setResultsName('ref_attrs')
    return new_attrs + arrow + options + ref_table + ref_attrs
github astropy / pyregion / pyregion / region_numbers.py View on Github external
def _unsigned_simple_number():
    # fnumber : 102.43, 12304.3e10,
    #           .32???
    point = Literal(".")
    e = Literal("e") | Literal("E")  # CaselessLiteral( "E" )
    fnumber = Combine(Word(nums) +
                      Optional(point + Optional(Word(nums))) +
                      Optional(e + Word("+-" + nums, nums)))

    return fnumber  # .setParseAction(lambda s,l,t: (float(t[0]), t[0]))
github oubiwann-unsupported / pymon / sandbox / oubiwann / pymon_shell / shellparser.py View on Github external
# commands
        serviceNodeActionsToken = oneOf(
            "add del delete update replace append").setResultsName(
            "action")
        serviceNodeShowActionToken = Literal(
            "show").setResultsName(
            "action")
        memActionsToken = oneOf(
            "write clear").setResultsName(
            "action")
        showActionsToken = oneOf(
            "nodes services lists").setResultsName(
            "action")

        # node args
        nodeNameToken = Word(alphanums + '_').setResultsName(
            "name")
        nodeUriToken = Word(alphanums + '/:@_-.').setResultsName(
            "uri")

        # general service args
        true = CaselessLiteral("true")
        false = CaselessLiteral("false")
        enabledToken = Optional(
            Literal("enabled") +
            (true | false).setResultsName(
            "enabled"))
        orgToken = Optional(
            Literal("org") + 
            Word(alphanums + """.'",:;!?()@#$%&*<>/\\""").setResultsName(
            "org"))
        intervalToken = Optional(
github thombashi / tcconfig / tcconfig / parser / _filter.py View on Github external
from .._network import sanitize_network


class TcFilterParser(AbstractParser):
    class FilterMatchIdIpv4(object):
        INCOMING_NETWORK = 12
        OUTGOING_NETWORK = 16
        PORT = 20

    class FilterMatchIdIpv6(object):
        INCOMING_NETWORK_LIST = [8, 12, 16, 20]
        OUTGOING_NETWORK_LIST = [24, 28, 32, 36]
        PORT = 40

    __FILTER_FLOWID_PATTERN = (
        pp.Literal("filter parent") + pp.SkipTo("flowid", include=True) + pp.Word(pp.hexnums + ":")
    )
    __FILTER_PROTOCOL_PATTERN = (
        pp.Literal("filter parent") + pp.SkipTo("protocol", include=True) + pp.Word(pp.alphanums)
    )
    __FILTER_PRIORITY_PATTERN = (
        pp.Literal("filter parent") + pp.SkipTo("pref", include=True) + pp.Word(pp.nums)
    )
    __FILTER_ID_PATTERN = (
        pp.Literal("filter parent") + pp.SkipTo("fh", include=True) + pp.Word(pp.hexnums + ":")
    )
    __FILTER_MATCH_PATTERN = (
        pp.Literal("match") + pp.Word(pp.alphanums + "/") + pp.Literal("at") + pp.Word(pp.nums)
    )
    __FILTER_MANGLE_MARK_PATTERN = (
        pp.Literal("filter parent")
        + pp.SkipTo("handle", include=True)
github shinichi-takii / ddlparse / ddlparse / ddlparse.py View on Github external
\s*\bCOMMENT\b\s+(
                \'(\\\'|[^\']|,)+\'
                |
                \"(\\\"|[^\"]|,)+\"
                |
                [^,]+
            )
        )?
    )
    """

    _COLUMN_CONSTRAINT = re.sub(r"(^\s+|\n)", r"", _COLUMN_CONSTRAINT_BASE, flags=re.MULTILINE)


    _CREATE_TABLE_STATEMENT = Suppress(_CREATE) + Optional(_TEMP)("temp") + Suppress(_TABLE) + Optional(Suppress(CaselessKeyword("IF NOT EXISTS"))) \
        + Optional(_SUPPRESS_QUOTE) + Optional(Word(alphanums + "_")("schema") + Optional(_SUPPRESS_QUOTE) + _DOT + Optional(_SUPPRESS_QUOTE)) + Word(alphanums + "_<>")("table") + Optional(_SUPPRESS_QUOTE) \
        + _LPAR \
        + delimitedList(
            OneOrMore(
                _COMMENT
                |
                # Ignore Index
                Suppress(_KEY + Word(alphanums + "_'`() "))
                |
                Group(
                    Optional(Suppress(_CONSTRAINT) + Optional(_SUPPRESS_QUOTE) + Word(alphanums + "_")("name") + Optional(_SUPPRESS_QUOTE))
                    + (
                        (
                            (_PRIMARY_KEY ^ _UNIQUE ^ _UNIQUE_KEY ^ _NOT_NULL)("type")
                            + Optional(_SUPPRESS_QUOTE) + Optional(Word(alphanums + "_"))("name") + Optional(_SUPPRESS_QUOTE)
                            + _LPAR + Group(delimitedList(Optional(_SUPPRESS_QUOTE) + Word(alphanums + "_") + Optional(_SUPPRESS_QUOTE)))("constraint_columns") + _RPAR
                        )
github google / encrypted-bigquery-client / src / query_parser.py View on Github external
# an alphabetical character and contain alphanumeric characters
  # and underscores (i.e. function or variable names).
  label = pp.Word(pp.alphas, pp.alphas + pp.nums + '_' + '.')

  # A single/double quote surrounded string.
  string = pp.quotedString

  # Various number representations.
  integer = pp.Word(pp.nums)
  decimal_type1 = pp.Combine(integer + decimal + pp.Optional(integer))
  decimal_type2 = pp.Combine(decimal + integer)
  real = decimal_type1 | decimal_type2
  exponent = exponent_literal + pp.Word('+-' + pp.nums, pp.nums)
  number_without_exponent = real | integer
  number = pp.Combine(number_without_exponent + pp.Optional(exponent))
  integer_argument = pp.Word(pp.nums)
  integer_argument.setParseAction(PushSingleToken)

  # Forward declaration for recusive grammar. We assume that full_expression can
  # represent any expression that is valid.
  full_expression = pp.Forward()

  # Aggregation function definitions.
  avg_function = pp.CaselessKeyword('AVG') + lp + full_expression + rp
  count_star.setParseAction(PushCountStar)
  count_argument = ((pp.Optional(distinct_keyword) + full_expression) |
                    count_star)
  count_function = (pp.CaselessKeyword('COUNT') + lp +
                    count_argument + pp.Optional(comma + integer_argument) + rp)
  quantiles_function = (pp.CaselessKeyword('QUANTILES') + lp + full_expression +
                        pp.Optional(comma + integer_argument) + rp)
  stddev_function = pp.CaselessKeyword('STDDEV') + lp + full_expression + rp
github enthought / enable / enthought / savage / svg / css / colour.py View on Github external
#percentage values, ie 100%, 50%
    Group(colorPerc + comma + colorPerc + comma + colorPerc)
    )
    +
    Literal(")").suppress() + StringEnd()
)

def parseShortHex(t):
    return tuple(int(x*2, 16) for x in t[0])


doubleHex = Word(hexnums, exact=2).setParseAction(lambda t: int(t[0], 16))
hexLiteral = (Literal("#").setParseAction(lambda t: "RGB") +
    (
    Group(doubleHex + doubleHex + doubleHex) |
    Word(hexnums, exact=3).setParseAction(parseShortHex)
    ) + StringEnd()
)

def parseNamedColour(t):
    try:
        return ["RGB", NamedColours[t[0].lower()]]
    except KeyError:
        return ["RGB", (0,0,0)]

namedColour = Word(alphas).setParseAction(parseNamedColour)


colourValue = rgb | hexLiteral | namedColour


##constants
github robocomp / robocomp / tools / robocompdsl / parseIDSL.py View on Github external
opp       = Suppress(Word("("))
		clp       = Suppress(Word(")"))
		lt        = Suppress(Word("<"))
		gt        = Suppress(Word(">"))
		eq        = Suppress(Word("="))
		identifier        = Word(alphas+"_",alphanums+"_")
		typeIdentifier    = Word(alphas+"_",alphanums+"_:")
		structIdentifer   = Group(typeIdentifier.setResultsName('type') + identifier.setResultsName('identifier') + Optional(eq) + Optional(CharsNotIn(";").setResultsName('defaultValue')) + semicolon)
		structIdentifers  = Group(OneOrMore(structIdentifer))

		## Imports
		idslImport  = Suppress(Word("import")) + quote +  CharsNotIn("\";").setResultsName('path') + quote + semicolon
		idslImports = ZeroOrMore(idslImport)

		structDef     = Word("struct").setResultsName('type') + identifier.setResultsName('name') + op + structIdentifers.setResultsName("structIdentifiers") + cl + semicolon
		dictionaryDef = Word("dictionary").setResultsName('type') + lt + CharsNotIn("<>").setResultsName('content') + gt + identifier.setResultsName('name') + semicolon
		sequenceDef   = Word("sequence").setResultsName('type')   + lt + typeIdentifier.setResultsName('typeSequence') + gt + identifier.setResultsName('name') + semicolon
		enumDef       = Word("enum").setResultsName('type')       + identifier.setResultsName('name') + op + CharsNotIn("{}").setResultsName('content') + cl + semicolon
		exceptionDef  = Word("exception").setResultsName('type')  + identifier.setResultsName('name') + op + CharsNotIn("{}").setResultsName('content') + cl + semicolon

		raiseDef       = Suppress(Word("throws")) + typeIdentifier + ZeroOrMore( Literal(',') + typeIdentifier )
		decoratorDef    = Literal('idempotent') | Literal('out')
		retValDef       = typeIdentifier.setResultsName('ret')

		firstParam    = Group( Optional(decoratorDef.setResultsName('decorator')) + typeIdentifier.setResultsName('type') + identifier.setResultsName('name'))
		nextParam     = Suppress(Word(',')) + firstParam
		params        = firstParam + ZeroOrMore(nextParam)


		remoteMethodDef  = Group(Optional(decoratorDef.setResultsName('decorator')) + retValDef.setResultsName('ret') + typeIdentifier.setResultsName('name') + opp + Optional(          params).setResultsName('params') + clp + Optional(raiseDef.setResultsName('raise')) + semicolon )
		interfaceDef    = Word('interface').setResultsName('type')  + typeIdentifier.setResultsName('name') + op + Group(ZeroOrMore(remoteMethodDef)).setResultsName('methods') + cl + semicolon
github barsch / seishub.core / seishub / core / xmldb / xpath.py View on Github external
pp.srange("[\uF900-\uFDCF]") + \
                           pp.srange("[\uFDF0-\uFFFD]") + \
                           pp.srange("[\u10000-\uEFFFF]")
        xmlNameChar = xmlNameStartChar + "-" + "." + pp.nums + \
                      unichr(0xB7) + pp.srange("[\u0300-\u036F]") + \
                      pp.srange("[\u203F-\u2040]")
        # custom tokens
        wildcard = pp.Literal(self.WILDCARD)        # node wildcard operator
        sep = pp.Literal(self.SEP)                  # path separator
        selfNd = pp.Literal('.').suppress()         # current node
        parentNd = pp.Literal(self.PARENT)          # parent of current node
        lpar = pp.Literal('(').suppress()           # left parenthesis literal
        rpar = pp.Literal(')').suppress()           # right parenthesis literal
        pstart = pp.Literal('[').suppress()         # beginning of predicates
        pend = pp.Literal(']').suppress()           # end of predicates
        ncPrefix = pp.Word(xmlNameStartChar, xmlNameChar) + ':' # namespace prefix
        # node name, may contain a namespace prefix and may start with '@' for
        # attribute nodes
        ndName = pp.Combine(pp.Optional('@') + pp.Optional(ncPrefix) + \
                 pp.Word(xmlNameStartChar, xmlNameChar))
        node = wildcard | parentNd | selfNd | ndName # node
        literalValue = pp.Literal('"').suppress() + \
                           pp.CharsNotIn('"') + \
                       pp.Literal('"').suppress() \
                       | \
                       pp.Literal("'").suppress() + \
                           pp.CharsNotIn("'") + \
                       pp.Literal("'").suppress()   # literal value delimited
                                                    # by either "" or ''
        numericValue = pp.Combine(pp.Optional('-') + \
                                  pp.Word(pp.nums) + \
                                  pp.Optional('.' + pp.Word(pp.nums)))# Numbers
github GeneralizedLearningUtilities / SuperGLU / python_module / SuperGLU / Services / TextProcessing / Speech_Processing.py View on Github external
def makeBNFParser(self):
        # Basic Components
        point = Literal(".")
        integer = Word(nums)
        fNumber = Combine(integer + point + integer)
        number = fNumber | integer
        unaryStarts = self.makeCombo(self.UNARY_OPS_START, Literal)
        unaryEnds = self.makeCombo(self.UNARY_OPS_END, Literal)
        ops = self.makeCombo(self.BINARY_OPS, Literal)
        kwds = self.makeCombo(self.SPECIAL_KWDS, CaselessKeyword)
        kwdLits = self.makeCombo(self.SPECIAL_KWDS, CaselessLiteral)
        functs = self.makeCombo(self.SPECIAL_FUNCT, CaselessKeyword)
        functLits = self.makeCombo(self.SPECIAL_FUNCT, CaselessLiteral)
        anyLits = Word(printables)
        variable = Word(alphas)
        startDelim = self.makeCombo(self.START_DELIMS, Literal, True)
        endDelim = self.makeCombo(self.END_DELIMS, Literal, True)

        # Expression
        group = Forward()
        factor = Forward()
        expr = Forward()
        val = (startDelim |
               #unaryStarts.setParseAction(self.pushUnarySt) |
               kwds.setParseAction(self.pushKeyword) |
               functs.setParseAction(self.pushFunct) |
               number.setParseAction(self.pushNum) |
               kwdLits.setParseAction(self.pushKeyword) |
               functLits.setParseAction(self.pushFunct) |
               unaryEnds.setParseAction(self.pushUnaryEnd) |