コード例 #1
0
    def _return_conf_list(self):
        """
        Iterates through the apache config file, building a list whoes entries
        are a tokenized version of each line in the config file.

        :returns: ``list``
        """
        # Variables
        conf_list = []

        # A file path was given
        if self.apache_config_path:
            with open(self.apache_config_path, "r") as apache_config:
                for line in apache_config:
                    parsed_result_line = ungroup(LINE).parseString(line)
                    conf_list.append(parsed_result_line)
        # The file was given as a string
        # TODO: Write tests for a file given as a string!
        elif self.apache_file_as_string:
            conf_file_line_list = self.apache_file_as_string.split("\n")
            for line in conf_file_line_list:
                # Add the delimiter back in
                line = line + "\n"
                parsed_result_line = ungroup(LINE).parseString(line)
                conf_list.append(parsed_result_line)
        return conf_list
コード例 #2
0
    def _return_conf_list(self):
        """
        Iterates through the apache config file, building a list whoes entries
        are a tokenized version of each line in the config file.

        :returns: ``list``
        """
        # Variables
        conf_list = []

        # A file path was given
        if self.apache_config_path:
            with open(self.apache_config_path, "r") as apache_config:
                for line in apache_config:
                    parsed_result_line = ungroup(LINE).parseString(line)
                    conf_list.append(parsed_result_line)
        # The file was given as a string
        # TODO: Write tests for a file given as a string!
        elif self.apache_file_as_string:
            conf_file_line_list = self.apache_file_as_string.split("\n")
            for line in conf_file_line_list:
                # Add the delimiter back in
                line = line + "\n"
                parsed_result_line = ungroup(LINE).parseString(line)
                conf_list.append(parsed_result_line)
        return conf_list
コード例 #3
0
ファイル: survey.py プロジェクト: gn0/honeybee
def parse_survey():
    indent_stack = [1]
    stmt = Forward()

    identifier = Word(alphas, alphanums + "_")
    number = Word(nums)
    doublequoted_string = QuotedString('"', escChar="\\")
    singlequoted_string = QuotedString("'", escChar="\\")
    string = doublequoted_string | singlequoted_string
    value = number | string

    # TODO Parse expressions properly.
    # op = oneOf("+ - * /")
    comp_op = oneOf("= != >= > <= <")

    comment = Group("#" + restOfLine)

    command = Group("@" + identifier + OneOrMore((identifier | value), stopOn=LineEnd()))

    variable = Group("${" + identifier + "}")
    dot = Literal(".")
    expr = Group((variable | dot) + comp_op + value)
    if_cond = "if" + (expr | value)
    if_body = indentedBlock(pp.ungroup(stmt), indent_stack)
    if_block = Group(if_cond + Suppress(":") + if_body)

    q_name = identifier
    q_type = Group(OneOrMore(identifier))
    q_label = string
    q_param = ((identifier + identifier) | (identifier + value))
    q_params = ZeroOrMore(q_param, stopOn=LineEnd())
    question = Group(q_name + q_type + Suppress(":") + Optional(q_label) + q_params)

    group_params = Group(ZeroOrMore(q_param, stopOn=LineEnd()) + Optional(if_cond))

    group_label = string
    group_def = "group" + Group(identifier + Optional(group_label)) + group_params + Optional(if_cond) + Suppress(":")
    group_body = indentedBlock(pp.ungroup(stmt), indent_stack)
    group_block = Group(group_def + group_body)

    repeat_def = "repeat" + Group(identifier + Optional(group_label)) + group_params + Suppress(":")
    repeat_block = Group(repeat_def + group_body)

    # TODO Add + Suppress(LineEnd())?
    stmt <<= (comment | command | if_block | group_block | repeat_block | question)
    stmts = OneOrMore(stmt)

    return stmts
コード例 #4
0
    def __init__(self):

        _by = CaselessLiteral('by')
        _add = Literal('+').suppress()
        _nest = Literal('>').suppress()
        _lpar = Literal('(').suppress()
        _rpar = Literal(')').suppress()
        _variable = ~_by + Word(alphanums + '_' + '.')

        def _p_act(s, l, t):
            # dirty hack to unpack group permutations
            # if has lists in result than it is multidimension cross
            levels_array = list(map(lambda v: isinstance(v, ParseResults), t))
            if sum(levels_array):
                # find where second level of cross starts
                first_cross_starts_at = levels_array.index(True)
                t = t.asList()
                levels = [t[:first_cross_starts_at]]
                levels.extend(t[first_cross_starts_at:])
                return list(map(lambda v: list(v), product(*levels)))
            return t

        self.expr = Forward()
        atom = _variable | (_lpar + ungroup(self.expr) + _rpar)
        cross = Forward()
        cross << (atom +
                  ZeroOrMore(Group(_nest + atom))).setParseAction(_p_act)
        add = Forward()
        add << cross + ZeroOrMore(_add + cross)
        self.expr << Group(add + ZeroOrMore(add))
コード例 #5
0
def _build_ana_rcp_parser():
    separator = pp.Suppress(':')
    key = pp.Word(pp.printables, excludeChars=':')
    value = pp.Regex(r'[^\n\r]*') + pp.LineEnd().suppress()

    block_name = key + separator + pp.LineEnd().suppress()
    platemap_keylist = pp.Literal(
        'platemap_comp4plot_keylist') + separator + pp.delimitedList(
            pp.Word(pp.alphas))
    run_ids = pp.Literal('run_ids') + separator + pp.delimitedList(
        pyparsing_common.integer)
    plate_id = (pp.Literal('plate_ids') |
                pp.Literal('plate_id')) + separator + pyparsing_common.integer

    key_value = (platemap_keylist | run_ids | plate_id
                 | key + separator + value)

    indent_stack = [1]
    block = pp.Forward()
    block_body = (block | key_value)

    indented_block = pp.Dict(
        pp.ungroup(pp.indentedBlock(block_body, indent_stack)))
    block << (block_name + indented_block | key_value)

    return pp.OneOrMore(pp.Dict(pp.Group(block)))
コード例 #6
0
def parse_choices():
    indent_stack = [1]
    stmt = Forward()

    identifier = Word(alphas, alphanums + "_")
    number = Word(nums)
    string = QuotedString('"', escChar="\\")
    value = number | string

    comment = Group("#" + restOfLine)

    command = Group("@" + identifier +
                    OneOrMore((identifier | value), stopOn=LineEnd()))

    choice_param = ((identifier + identifier) | (identifier + value))
    choice_params = Group(ZeroOrMore(choice_param, stopOn=LineEnd()))
    choice_value = value
    choice_label = value
    choice = Group(choice_value + choice_label + choice_params)

    list_name = identifier
    list_def = "list" + list_name + choice_params + Suppress(":")
    list_body = indentedBlock(ungroup(stmt), indent_stack)
    list_block = Group(list_def + list_body)

    stmt <<= (comment | command | list_block | choice)
    stmts = OneOrMore(stmt)

    return stmts
コード例 #7
0
ファイル: csv.py プロジェクト: cjh1/edp-parser
def _build_csv_parser():
    separator = pp.Suppress(':')
    key = pp.Word(pp.printables, excludeChars=':')
    value = pp.Regex(r'[^\n\r]*') + pp.LineEnd().suppress()

    block_name = key + separator  + pp.LineEnd().suppress()

    key_value = key + separator + value

    header = (pp.LineStart().suppress() +  pp.Word(pp.nums) + pp.ZeroOrMore( pp.White().suppress() + pp.Word(pp.nums)) + pp.LineEnd().suppress())

    csv_header = pp.delimitedList(pp.Word(pp.printables, excludeChars=',')) + pp.LineEnd().suppress()

    csv_row = pp.delimitedList(pp.Word(pp.nums + ';.+-e_') | pp.Literal('custom')) + pp.LineEnd().suppress()

    indent_stack = [1]
    block = pp.Forward()
    block_body = ( block | key_value)

    indented_block = pp.Dict(pp.ungroup(pp.indentedBlock(block_body, indent_stack)))
    block << ( block_name + indented_block | key_value)

    return pp.Optional(header) + pp.ZeroOrMore(pp.Dict(pp.Group(block))).setResultsName('meta') + \
        csv_header.setResultsName('csvHeader') + \
        pp.Group(pp.OneOrMore(pp.Group(csv_row))).setResultsName('csvValues')
コード例 #8
0
rr_type_tkey = CaselessLiteral('TKEY')
rr_type_tsig = CaselessLiteral('TSIG')
rr_type_caa = CaselessLiteral('CAA')

rr_type_set = ungroup(rr_type_aaaa
                      | rr_type_a
                      | rr_type_caa
                      | rr_type_cname
                      | rr_type_dnskey
                      | rr_type_ds
                      | rr_type_hinfo
                      | rr_type_location
                      | rr_type_mx
                      | rr_type_nsec3param
                      | rr_type_nsec3
                      | rr_type_nsec
                      | rr_type_ns
                      | rr_type_openpgpkey
                      | rr_type_ptr
                      | rr_type_rrsig
                      | rr_type_soa
                      | rr_type_srv
                      | rr_type_tkey
                      | rr_type_tlsa
                      | rr_type_tsig
                      | rr_type_txt
                      | rr_type_wks
                      | rr_type_x25)('rr_type')
rr_type_set.setName('<rr_type>')

#  a series of RR types (without a semicolon separator)
rr_type_series = OneOrMore(
コード例 #9
0
in_ = CK("in").setParseAction(pp.replaceWith(1))
from_ = CK("from").setParseAction(pp.replaceWith(1))
before = CK("before").setParseAction(pp.replaceWith(-1))
after = CK("after").setParseAction(pp.replaceWith(1))
ago = CK("ago").setParseAction(pp.replaceWith(-1))
next_ = CK("next").setParseAction(pp.replaceWith(1))
last_ = CK("last").setParseAction(pp.replaceWith(-1))
at_ = CK("at")
on_ = CK("on")

couple = (pp.Optional(CK("a")) + CK("couple") +
          pp.Optional(CK("of"))).setParseAction(pp.replaceWith(2))
a_qty = (CK("a") | CK("an")).setParseAction(pp.replaceWith(1))
the_qty = CK("the").setParseAction(pp.replaceWith(1))
qty = pp.ungroup(integer | couple | a_qty | the_qty).setName("qty")
time_ref_present = pp.Empty().addParseAction(
    pp.replaceWith(True))("time_ref_present")


def fill_24hr_time_fields(t):
    t["HH"] = t[0]
    t["MM"] = t[1]
    t["SS"] = 0
    t["ampm"] = ("am", "pm")[t.HH >= 12]


def fill_default_time_fields(t):
    for fld in "HH MM SS".split():
        if fld not in t:
            t[fld] = 0
コード例 #10
0
ファイル: tokens.py プロジェクト: oerc0122/QASMParser
def _setup_QASMParser():
    """
    Routine to initialise and return parsing blocks
    """
    class _Op:
        """ Class to set up quantum operations """
        def __init__(self,
                     name,
                     argParser,
                     version="OPENQASM 2.0",
                     qop=False,
                     keyOverride=None):
            global cops
            global qops
            global _reservedKeys
            if name in qops or name in cops:
                raise IOError(dupTokenWarning.format("Operation", name))
            self.operation = name
            if keyOverride is not None:
                self.parser = (keyOverride + argParser).addParseAction(
                    lambda s, l, t: _override_keyword(t, name))
            else:
                self.parser = CaselessKeyword(name)("keyword") + argParser

            self.version = parse_version(version)
            self.parser.addParseAction(
                lambda s, l, t: _set_version(t, self.version))

            _reservedKeys.append(name)
            if qop:
                qops[name] = self
            else:
                cops[name] = self

    class _Routine():
        """ Class to set up quantum gates, circuits, etc. """
        def __init__(self,
                     name,
                     pargs=False,
                     spargs=False,
                     gargs=False,
                     qargs=False,
                     returnables=False,
                     prefixes=None,
                     version="OPENQASM 2.0"):
            global blocks
            global _reservedKeys
            if name in qops or name in cops:
                raise IOError(dupTokenWarning.format("Routine", name))
            self.operation = name

            self.parser = Keyword(name)("keyword") + validName("gateName")

            if prefixes:
                localPrefixParser = Each(map(Optional, map(
                    Keyword, prefixes))).addParseAction(prefix_setter)
            else:
                localPrefixParser = prefixParser
            self.parser = localPrefixParser + self.parser

            # Handle different args
            req = []
            if pargs:
                req.append(Optional(pargParser)("pargs"))
            if spargs:
                req.append(Optional(spargParser)("spargs"))
            if gargs:
                req.append(Optional(gargParser)("gargs"))
            self.parser = self.parser + Each(req)
            if qargs:
                self.parser = self.parser + qargParser("qargs")
            if returnables:
                self.parser = self.parser + Optional(returnParser)

            self.version = parse_version(version)
            self.parser.addParseAction(
                lambda s, l, t: _set_version(t, self.version))

            _reservedKeys.append(name)
            blocks[name] = self

    class _Block():
        """ Class to set up blocks such as if, for, etc. """
        def __init__(self, name, detParser, version="OPENQASM 2.0"):
            global blocks
            global _reservedKeys
            self.operation = name
            self.parser = Keyword(name)("keyword") + detParser

            self.version = parse_version(version)
            self.parser.addParseAction(
                lambda s, l, t: _set_version(t, self.version))

            _reservedKeys.append(name)
            blocks[name] = self

    sign = Word("+-", exact=1)
    number = Word(nums)
    expo = Combine(CaselessLiteral("e") + Optional(sign) +
                   number).setResultsName("exponent")

    pi = CaselessKeyword("pi")

    bitstring = Combine(OneOrMore(oneOf("0 1")) + Literal("b"))

    integer = Combine(number + Optional(expo))
    real = Combine(
        Optional(sign) + (("." + number) ^ (number + "." + Optional(number))) +
        Optional(expo))
    validName = Forward()
    lineEnd = Literal(";")

    _is_ = Keyword("to").suppress()
    _in_ = Keyword("in")
    _to_ = Literal("->").suppress()

    commentSyntax = "//"
    commentOpenStr = "/*"
    commentCloseStr = "*/"
    commentOpenSyntax = Literal(commentOpenStr)
    commentCloseSyntax = Literal(commentCloseStr)

    dirSyntax = "***"
    dirOpenStr = f"{dirSyntax} begin"
    dirCloseStr = f"{dirSyntax} end"

    dirSyntax = Keyword(dirSyntax)
    dirOpenSyntax = CaselessLiteral(dirOpenStr)
    dirCloseSyntax = CaselessLiteral(dirCloseStr)

    intFunc = oneOf("abs powrem countof fllog")
    realFunc = oneOf("abs powrem arcsin arccos arctan sin cos tan exp ln sqrt")
    boolFunc = oneOf("andof orof xorof")

    inL, inS, inR = map(Suppress, "[:]")
    vBar = Suppress("|")
    bSlash = Suppress("\\")
    brL, brR = map(Suppress, "()")

    intExp = Forward()
    realExp = Forward()
    boolExp = Forward()

    index = intExp.setResultsName("index")
    interval = Optional(intExp.setResultsName("start"), default=None) + inS \
        + Optional(intExp.setResultsName("end"), default=None) \
        + Optional(inS + Optional(intExp.setResultsName("step"), default=1))
    interRef = Group(inL + interval + inR)
    loopRef = Group(
        inL + intExp.setResultsName("start") + inS +
        intExp.setResultsName("end") +
        Optional(inS + Optional(intExp.setResultsName("step"), default=1)) +
        inR)
    ref = inL + Group(delimitedList(index ^ interval))("ref") + inR
    regNoRef = validName("var")
    regRef = Group(validName("var") + Optional(ref))
    regMustRef = Group(validName("var") + ref)
    regListNoRef = Group(delimitedList(regNoRef))
    regListRef = Group(delimitedList(regRef))

    inPlaceAlias = vBar + regListRef + vBar
    validQarg = regRef | inPlaceAlias
    aliasQarg = Group(regRef) | inPlaceAlias

    inPlaceCreg = bSlash + delimitedList(regRef | bitstring) + bSlash
    validCreg = (regRef | inPlaceCreg)

    def set_maths_type(toks, mathsType):
        """ Set logical or integer or floating point """
        toks["type"] = mathsType

    intVar = integer | regRef
    realVar = real | integer | pi | regRef
    boolVar = interRef | regRef | realExp | intExp | validCreg | bitstring
    intFuncVar = (intFunc + brL +
                  Group(Optional(delimitedList(intVar)))("args") +
                  brR).setParseAction(Function)
    realFuncVar = ((realFunc ^ intFunc) + brL +
                   Group(Optional(delimitedList(realVar)))("args") +
                   brR).setParseAction(Function)
    boolFuncVar = (boolFunc + brL +
                   Group(Optional(delimitedList(boolVar)))("args") +
                   brR).setParseAction(Function)

    mathOp = [(oneOf("- +"), 1, opAssoc.RIGHT, Binary),
              (oneOf("^"), 2, opAssoc.LEFT, Binary),
              (oneOf("* / div"), 2, opAssoc.LEFT, Binary),
              (oneOf("+ -"), 2, opAssoc.LEFT, Binary)]
    logOp = [(oneOf("! not"), 1, opAssoc.RIGHT, Binary),
             (oneOf("and or xor"), 2, opAssoc.LEFT, Binary),
             (oneOf("< <= == != >= >"), 2, opAssoc.LEFT, Binary),
             (oneOf("in"), 2, opAssoc.LEFT, Binary)]

    intExp <<= infixNotation(
        intFuncVar | intVar,
        mathOp).setParseAction(lambda s, l, t: set_maths_type(t, "int"))

    realExp <<= infixNotation(
        realFuncVar | realVar,
        mathOp).setParseAction(lambda s, l, t: set_maths_type(t, "float"))

    boolExp <<= infixNotation(
        boolFuncVar | boolVar,
        logOp).setParseAction(lambda s, l, t: set_maths_type(t, "bool"))

    mathExp = intExp ^ realExp ^ boolExp
    cregExp = bitstring("bit") ^ validCreg("reg")

    prefixes = ["unitary"]
    callMods = ["CTRL", "INV"]

    def prefix_setter(toks):
        """ Pull out prefixes of gate calls and add them into list """
        for prefix in prefixes:
            toks[prefix] = prefix in toks.asList()

    prefixParser = Each(map(Optional,
                            map(Keyword,
                                prefixes))).addParseAction(prefix_setter)

    pargParser = brL + delimitedList(validName)("pargs") + brR
    spargParser = inL + delimitedList(validName)("spargs") + inR
    gargParser = ungroup(
        nestedExpr("<", ">", delimitedList(ungroup(validName)), None))
    qargParser = delimitedList(regRef)

    callQargParser = delimitedList(validQarg)
    callPargParser = brL + delimitedList(realExp) + brR
    callSpargParser = inL + delimitedList(intExp) + inR

    fullArgParser = Each(
        (Optional(pargParser("pargs")), Optional(spargParser("spargs")),
         Optional(gargParser("gargs"))))

    callArgParser = Each(
        (Optional(callPargParser("pargs")),
         Optional(callSpargParser("spargs")), Optional(gargParser("gargs"))))

    returnParser = Optional(_to_ + validCreg("byprod"))

    modifiers = ZeroOrMore(Combine(oneOf(callMods) + Suppress("-")))

    commentLine = Literal(commentSyntax).suppress() + restOfLine("comment")
    commentBlock = cStyleComment("comment").addParseAction(
        removeQuotes).addParseAction(removeQuotes)
    comment = commentLine | commentBlock
    comment.addParseAction(lambda s, l, t: _set_version(t, (0, 0, 0)))

    directiveName = Word(alphas).setParseAction(downcaseTokens)
    directiveArgs = CharsNotIn(";")

    _Op("directive",
        directiveName("directive") + Suppress(White() * (1, )) +
        directiveArgs("args"),
        version="REQASM 1.0",
        keyOverride=(~dirOpenSyntax + ~dirCloseSyntax + dirSyntax))

    def split_args(toks):
        """ Split directive arguments out """
        toks[0]["keyword"] = "directive"
        toks[0]["args"] = toks[0]["args"].strip().split(" ")

    directiveStatement = directiveName("directive") + restOfLine("args") + \
        Group(ZeroOrMore(Combine(Optional(White(" ")) + ~dirCloseSyntax + Word(printables+" "))))("block")

    directiveBlock = ungroup(
        nestedExpr(
            dirOpenSyntax,
            dirCloseSyntax,
            content=directiveStatement,
            ignoreExpr=(comment | quotedString
                        )).setWhitespaceChars("\n").setParseAction(split_args))
    directiveBlock.addParseAction(lambda s, l, t: _set_version(t, (2, 1, 0)))

    # Programming lines
    _Op("version",
        Empty(),
        version=(0, 0, 0),
        keyOverride=Combine(
            oneOf(versions)("type") + White() +
            real("versionNumber"))("version"))
    _Op("include", quotedString("file").addParseAction(removeQuotes))

    # Gate-like structures
    _Op("opaque",
        validName("name") + fullArgParser + Optional(qargParser("qargs")) +
        returnParser,
        keyOverride=prefixParser + "opaque")
    _Routine("gate", pargs=True, qargs=True)
    _Routine("circuit",
             pargs=True,
             qargs=True,
             spargs=True,
             returnables=True,
             version="REQASM 1.0")

    # Variable-like structures
    _Op("creg", regRef("arg"))
    _Op("qreg", regRef("arg"))
    _Op("cbit", Group(regNoRef)("arg"), version="REQASM 1.0")
    _Op("qbit", Group(regNoRef)("arg"), version="REQASM 1.0")
    _Op("defAlias",
        regMustRef("alias"),
        keyOverride="alias",
        version="REQASM 1.0")

    # No more on-definition aliases
    _Op("alias",
        regRef("alias") + _is_ + aliasQarg("target"),
        keyOverride="set",
        version="REQASM 1.0")
    _Op("val",
        validName("var") + Literal("=").suppress() + mathExp("val"),
        version="REQASM 1.0")

    _Op("set", (Group(regRef)("var") ^ inPlaceCreg("var")) +
        Literal("=").suppress() + cregExp("val"),
        version="REQASM 1.0")

    # Operations-like structures
    _Op("measure", regRef("qreg") + _to_ + regRef("creg"), qop=True)
    _Op("barrier", regListNoRef("args"))
    _Op("output", regRef("value"), qop=True, version="REQASM 1.0")
    _Op("reset", regRef("qreg"))
    _Op("exit", Empty(), version="REQASM 1.0")

    _Op("free", validName("target"), version="REQASM 1.0")
    _Op("next", validName("loopVar"), qop=True, version="REQASM 1.0")
    _Op("finish", (Literal("quantum process") | validName)("loopVar"),
        qop=True,
        version="REQASM 1.0")
    _Op("end", validName("process"), qop=True, version="REQASM 1.0")

    # Special gate call handler
    callGate = Combine(Group(modifiers)("mods") + \
                       validName("gate")) + \
                       callArgParser + \
                       callQargParser("qargs").addParseAction(lambda s, l, t: _override_keyword(t, "call")) + \
                       returnParser
    callGate.addParseAction(lambda s, l, t: _set_version(t, (1, 2, 0)))

    # Block structures
    _Block("for",
           validName("var") + _in_ + loopRef("range"),
           version="REQASM 1.0")
    _Block("if", "(" + boolExp("cond") + ")", version="REQASM 1.0")
    _Block("while", "(" + boolExp("cond") + ")", version="OMEQASM 1.0")

    qopsParsers = list(map(lambda qop: qop.parser,
                           qops.values())) + [callGate, directiveBlock]
    blocksParsers = list(map(lambda block: block.parser, blocks.values()))

    _Op("if",
        blocks["if"].parser + Group(Group(Group(Or(qopsParsers))))("block"),
        version="OPENQASM 2.0",
        keyOverride=Empty())
    _Op("for",
        blocks["for"].parser + Group(Group(Group(Or(qopsParsers))))("block"),
        version="REQASM 1.0",
        keyOverride=Empty())
    _Op("while",
        blocks["while"].parser + Group(Group(Group(Or(qopsParsers))))("block"),
        version="OMEQASM 1.0",
        keyOverride=Empty())

    # Set-up line parsers
    reservedNames = Or(map(Keyword, _reservedKeys))
    validName <<= (~reservedNames) + Word(alphas, alphanums + "_")

    copsParsers = list(map(lambda cop: cop.parser, cops.values()))

    operations = ((
        (Or(copsParsers) ^ Or(qopsParsers)) |  # Classical/Quantum Operations
        callGate |  # Gate parsers
        White()  # Blank Line
    ) + lineEnd.suppress()) ^ directiveBlock  # ; or Directives

    validLine = Forward()
    codeBlock = nestedExpr("{", "}",
                           Suppress(White()) ^ Group(validLine),
                           (quotedString))

    validLine <<= (
        ((operations + Optional(comment)) ^
         (Or(blocksParsers) + codeBlock("block") + Optional(lineEnd))
         ^ comment))  # Whole line comment

    testLine = Forward()
    dummyCodeBlock = nestedExpr(
        "{", "}", testLine,
        (directiveBlock | quotedString | comment)) + Optional(lineEnd)

    ignoreSpecialBlocks = (~commentOpenSyntax + ~commentCloseSyntax +
                           ~dirOpenSyntax + ~dirCloseSyntax)

    testLine <<= (
        comment |  # Comments
        directiveBlock |  # Directives
        (ignoreSpecialBlocks + ZeroOrMore(CharsNotIn("{}")) + dummyCodeBlock)
        |  # Block operations
        (ignoreSpecialBlocks + ZeroOrMore(CharsNotIn("{};")) + lineEnd)
    )  # QASM Instructions

    testKeyword = (dirSyntax.setParseAction(
        lambda s, l, t: _override_keyword(t, "directive"))
                   | Word(alphas)("keyword"))

    code = (Group(directiveBlock)) | Group(validLine)

    return code, testLine, testKeyword, reservedNames, mathExp
コード例 #11
0
ファイル: isc_inet.py プロジェクト: egberts/bind9_parser
ip6_addr_list = ip6_addr + semicolon

# Used by server-addresses
ip46_addr_list = Group(ip46_addr + semicolon)

ip46_addr_and_port_list = ((ip46_addr('addr') +
                            Optional(inet_ip_port_keyword_and_number_element) +
                            semicolon)('ip46_addr_port'))('')

### SERIES ####

# 999.999.999.999; [ 999.999.999.999; ]*
ip4_addr_list_series = Group(ip4_addr_list + ZeroOrMore(ip4_addr_list))

# Example: 1.1.1.1/1; 2.2.2.2/2; 3.3.3.3/3;
ip4s_prefix_list_series = Group(ip4s_prefix_list +
                                ZeroOrMore(ip4s_prefix_list))

# Example: 4321::1; 5432::7; 6543::8;
ip6_addr_list_series = Group(ip6_addr_list + ZeroOrMore(ip6_addr_list))

# Example: 3210::3; 1.1.1.1;
ip46_addr_list_series = (
    OneOrMore(ungroup(ip46_addr_list))
    # + ZeroOrMore(ip46_addr_list)
)

# Example: 3210::3; 123.123.123.1/24; 1.1.1.1;
ip_addr_list = ip46_addr_or_prefix + semicolon
ip_addr_semicolon_series = Group(ip_addr_list + ZeroOrMore(ip_addr_list))
コード例 #12
0
    BEXPR <<= infixNotation(
        Group(BEXPRATOM),
        [(Keyword("and"), 2, opAssoc.LEFT, to_prefix_expr),
         (Keyword("or"), 2, opAssoc.LEFT, to_prefix_expr)
         # TODO other operators
         ]).setParseAction(unwrap_tf)
    return Group(EXPR), BEXPR


EXPR, BEXPR = makeExprParsers(baseVarRefParser)
VARREF = baseVarRefParser(EXPR)
_, LINKBEXPR = makeExprParsers(linkVarRefParser)

INITIALIZER = (
    LBRACK + delimitedList(ungroup(EXPR)) + RBRACK | Group(
        (ungroup(EXPR) + Suppress("..") + ungroup(EXPR)).setParseAction(
            to_sexpr("#range"))) |  # noqa: E501
    ungroup(EXPR))

PROC = Forward()


def group_list(toks):
    x = to_list(toks)
    try:
        if x and x[0] == "list":
            return [x]
        else:
            return x
    except TypeError:
コード例 #13
0
ファイル: isc_utils.py プロジェクト: bopopescu/bind9_parser
dlz_name_type = Word(alphanums + '_-.', max=63)('dlz_name')
database_name_type = Word(alphanums + '_-.', max=63)('dlz_name')

# #############################################################
# Series
# #############################################################

# '<key_id>;'
key_id_list = key_id + semicolon

# '<key_id>; <key_id>; ...'
key_id_list_series = (
    Group(
        OneOrMore(
            ungroup(key_id_list)
        )
    )('key_ids')
)
key_id_list_series.setName('<key_list>;')


test_data_boolean_passing = [
    'yes',
    'YES',
    'no',
    'No',
    '0',
    '1',
    'True',
    'False',
コード例 #14
0
optview_stmt_empty_contact = (
    Keyword('empty-contact').suppress() -
    Group(soa_name_type('soa_contact_name') + semicolon)(
        'empty_contact')  # Dict (not a multiple-statement)
)('')

optview_stmt_empty_zones_enable = (Keyword('empty-zones-enable').suppress() -
                                   isc_boolean('empty_zones_enable') +
                                   semicolon)

optview_stmt_fetch_glue = (Keyword('fetch-glue').suppress() -
                           isc_boolean('fetch_glue') + semicolon
                           )  # v8.1 to v9.7.0

optview_stmt_files = (Keyword('files').suppress() - Group(
    (ungroup(number_type(''))
     | Literal('unlimited')('')
     | Keyword('default'))('files_count'))('files') + semicolon)('')

optview_stmt_heartbeat_interval = (Keyword('heartbeat-interval').suppress() -
                                   minute_type('heartbeat_interval') +
                                   semicolon)

#  hostname ( none | quoted_fqdn );  # [ Opt View ]
optview_stmt_hostname = (Keyword('hostname').suppress() - Group(
    Literal('none')('none')
    | quoted_domain_generic_fqdn('hostname')('name')
    | domain_generic_fqdn('hostname')('name'))('hostname') + semicolon)

optview_stmt_lame_ttl = (Keyword('lame-ttl').suppress() -
                         number_type('lame_ttl') + semicolon)
コード例 #15
0
charset_master_name_base = alphanums + '_-'
master_name_base = Word(charset_master_name_base, max=62)

master_name = (master_name_base)('master_name')
master_name.setName('<master_name>')

# #############################################################
# Series
# #############################################################

# '<key_id>;'
key_id_list = key_id + semicolon

# '<key_id>; <key_id>; ...'
key_id_list_series = (Group(OneOrMore(ungroup(key_id_list)))('key_ids'))
key_id_list_series.setName('<key_list>;')

test_data_boolean_passing = [
    'yes',
    'YES',
    'no',
    'No',
    '0',
    '1',
    'True',
    'False',
    'FALSE',
    'TRUE',
    'true',
    'false',
コード例 #16
0
ファイル: pactl.py プロジェクト: schaney/dotfiles
def get_syntax():
    """
    Get the syntax suitable for parsing `pactl list` output

    The syntax parses valid text and produces a list of :class:`Record` objects
    """
    # The syntax uses indenting so a shared indent stack is required to parse
    # output properly. See indentedBlock() documentation for details
    indent_stack = [1]
    # VALUE
    # VALUE = p.Regex(".+").setResultsName("VALUE")
    property_value = (
        p.indentedBlock(
            p.Word(p.alphanums + ".").setResultsName("PROP_NAME")
            + p.Suppress('=')
            + p.QuotedString('"').setResultsName("PROP_VALUE"),
            indent_stack
        ).setResultsName("prop-value")
    )
    simple_value = p.ungroup(
        p.Combine(
            p.Regex(r".*")
            + p.Optional(
                p.indentedBlock(p.Regex(".+"), indent_stack)
            ),
            joinString='\n',
            adjacent=False
        )
    ).setResultsName("simple-value")
    value = p.Or(
        [property_value, simple_value]
    ).setResultsName("value")
    # NAME
    NAME = p.Regex("[^:]+").setResultsName("NAME")
    # entry: NAME ':' [VALUE] [indented<VALUE>]
    entry = (
        NAME + p.Suppress(":") + value
    ).setResultsName(
        "entry"
    ).setParseAction(
        Entry.from_tokens
    )
    # HEADER
    HEADER = p.restOfLine.setResultsName("HEADER")
    # record: HEADER '\n' indented<entry>
    record = (
        HEADER
        + p.Suppress(p.lineEnd)
        + p.indentedBlock(entry, indent_stack).setResultsName("entry-list")
    ).setResultsName(
        "record"
    ).setParseAction(
        Record.from_tokens
    )
    # record_list: record+
    record_list = p.OneOrMore(
        record
    ).setResultsName(
        "record-list"
    )
    syntax = record_list
    syntax.enablePackrat()
    return syntax
コード例 #17
0
ファイル: isc_zone.py プロジェクト: egberts/bind9_parser
# Only found in secondary, slave, mirror, stub, redirect, zone-stub or zone-slave
# masters [ port <integer> ] [ dscp <integer> ]
#         {
#             (
#               <ipv4_address> [ port <integer> ]
#               | <ipv6_address> [ port <integer> ]
#               | <masters>
#             )
#             [ key <string> ]
#             ;
#             ...
#         };

zone_masters_set = (
    ((ungroup(ip4_addr -
              Optional(inet_ip_port_keyword_and_number_element('ip_port')))
      ('ip4')
      ^ ungroup(ip6_addr -
                Optional(inet_ip_port_keyword_and_number_element('ip_port')))
      ('ip6') ^ master_name('master_name'))('') -
     Optional(key_id_keyword_and_name_pair))('') + semicolon)('')

zone_masters_series = (
    OneOrMore(Group(zone_masters_set(''))(''))(
        'zone_master_list'
    )  # without that PyParsing label, we cannot get standalone grouping, must be labeled.
)

# 'masters' clause has a name field for the 1st argument;
#     'masters' statement in the zone clause does not have a <master_name> field but it has 'port' or 'dscp'
zone_stmt_masters = (
コード例 #18
0
in_ = CK("in").setParseAction(pp.replaceWith(1))
from_ = CK("from").setParseAction(pp.replaceWith(1))
before = CK("before").setParseAction(pp.replaceWith(-1))
after = CK("after").setParseAction(pp.replaceWith(1))
ago = CK("ago").setParseAction(pp.replaceWith(-1))
next_ = CK("next").setParseAction(pp.replaceWith(1))
last_ = CK("last").setParseAction(pp.replaceWith(-1))
at_ = CK("at")
on_ = CK("on")

couple = (pp.Optional(CK("a")) + CK("couple") +
          pp.Optional(CK("of"))).setParseAction(pp.replaceWith(2))
a_qty = (CK("a") | CK("an")).setParseAction(pp.replaceWith(1))
the_qty = CK("the").setParseAction(pp.replaceWith(1))
qty = pp.ungroup(integer | couple | a_qty | the_qty)
time_ref_present = pp.Empty().addParseAction(
    pp.replaceWith(True))('time_ref_present')


def fill_24hr_time_fields(t):
    t['HH'] = t[0]
    t['MM'] = t[1]
    t['SS'] = 0
    t['ampm'] = ('am', 'pm')[t.HH >= 12]


def fill_default_time_fields(t):
    for fld in 'HH MM SS'.split():
        if fld not in t:
            t[fld] = 0
コード例 #19
0
ファイル: parsing.py プロジェクト: stjordanis/icl
from typing import Tuple

from pyparsing import Word, Literal, nums, alphanums, OneOrMore, Optional,\
    SkipTo, ParseException, Group, Combine, delimitedList, quotedString,\
    nestedExpr, ParseResults, oneOf, ungroup, Keyword

# define punctuation - reuse of expressions helps packratting work better
LPAR, RPAR, LBRACK, RBRACK, COMMA, EQ = map(Literal, "()[],=")

# Qualifier to go in front of type in the argument list (unsigned const int foo)
qualifier = OneOrMore(
    Keyword('const') ^ Keyword('typename') ^ Keyword('struct')
    ^ Keyword('enum'))
qualifier = ungroup(qualifier.addParseAction(' '.join))


def turn_parseresults_to_list(s, loc, toks):
    return ParseResults(normalise_templates(toks[0].asList()))


def normalise_templates(toks):
    s_list = ['<']
    s_list_append = s_list.append  # lookup append func once, instead of many times
    for tok in toks:
        if isinstance(tok, str):  # See if it's a string
            s_list_append(' ' + tok)
        else:
            # If it's not a string
            s_list_append(normalise_templates(tok))
    s_list_append(' >')
    return ''.join(s_list)
コード例 #20
0
from_ = CK("from").setParseAction(pp.replaceWith(1))
before = CK("before").setParseAction(pp.replaceWith(-1))
after = CK("after").setParseAction(pp.replaceWith(1))
ago = CK("ago").setParseAction(pp.replaceWith(-1))
next_ = CK("next").setParseAction(pp.replaceWith(1))
last_ = CK("last").setParseAction(pp.replaceWith(-1))
at_ = CK("at")
on_ = CK("on")

couple = ((pp.Optional(CK("a")) +
           CK("couple") + pp.Optional(CK("of"))).setParseAction(
               pp.replaceWith(2)).setName("couple"))

a_qty = (CK("a") | CK("an")).setParseAction(pp.replaceWith(1))
the_qty = CK("the").setParseAction(pp.replaceWith(1))
qty = pp.ungroup((integer | couple | a_qty
                  | the_qty).setName("qty_expression")).setName("qty")
time_ref_present = pp.Empty().addParseAction(
    pp.replaceWith(True))("time_ref_present")


def fill_24hr_time_fields(t):
    t["HH"] = t[0]
    t["MM"] = t[1]
    t["SS"] = 0
    t["ampm"] = ("am", "pm")[t.HH >= 12]


def fill_default_time_fields(t):
    for fld in "HH MM SS".split():
        if fld not in t:
            t[fld] = 0