Exemplo n.º 1
0
    def parse_string(self, string):
        '''Populate a new object from a string.
        
        Parsing is hard, so we're going to call out to the pyparsing
        library here.  I hope you installed it!
        '''
        from pyparsing import Suppress, Regex, quotedString, restOfLine, Keyword, nestedExpr, Group, OneOrMore, Word, Literal, alphanums, removeQuotes, replaceWith
        gr_eq = Literal('=')
        gr_stripped_string = quotedString.copy().setParseAction(removeQuotes)
        gr_opt_quoted_string = gr_stripped_string | restOfLine
        gr_name = Keyword('name', caseless=True) + gr_eq + gr_opt_quoted_string
        gr_name.setParseAction(lambda x, y=self: y._set_name(x[2]))
        gr_yn = Keyword('yes', caseless=True).setParseAction(
            replaceWith('1')) | Keyword('no', caseless=True).setParseAction(
                replaceWith('0'))
        gr_phrase = Group(
            OneOrMore(gr_stripped_string | Word(alphanums)) + gr_eq +
            gr_opt_quoted_string)

        def np(words, fn=gr_opt_quoted_string, action=print):
            p = Keyword(words[0], caseless=True)
            for w in words[1:]:
                p = p | Keyword(w, caseless=True)
            p = p + gr_eq + fn
            p.setParseAction(action)
            return p

        gr_ifsc = np(PList('Ignore File Set Changes'),
                     gr_yn,
                     action=self._parse_setter(IGNORECHANGES))
        gr_evss = np(PList('Enable VSS'),
                     gr_yn,
                     action=self._parse_setter(VSSENABLED))

        gr_i_option = Group(
            Keyword(OPTIONS, caseless=True) +
            nestedExpr('{', '}', Regex('[^\}]+', re.MULTILINE)))
        gr_e_option = gr_i_option.copy()
        gr_i_file = gr_phrase.copy()
        gr_e_file = gr_phrase.copy()

        gr_inc = Keyword('include', caseless=True) + nestedExpr(
            '{', '}', OneOrMore(gr_i_option | gr_i_file))
        gr_inc.addParseAction(self._parse_add_entry)
        gr_exc = Keyword('exclude', caseless=True) + nestedExpr(
            '{', '}', OneOrMore(gr_e_option | gr_e_file))
        gr_exc.addParseAction(self._parse_add_entry)

        gr_res = OneOrMore(gr_name | gr_inc | gr_exc | gr_ifsc | gr_evss)
        result = gr_res.parseString(string, parseAll=True)
        return 'Fileset: ' + self[NAME]
Exemplo n.º 2
0
    def parse_string(self, string):
        '''Populate a new object from a string.
        
        Parsing is hard, so we're going to call out to the pyparsing
        library here.  I hope you installed it!
        '''
        from pyparsing import Suppress, Regex, quotedString, restOfLine, Keyword, nestedExpr, Group, OneOrMore, Word, Literal, alphanums, removeQuotes, replaceWith
        gr_eq = Literal('=')
        gr_stripped_string = quotedString.copy().setParseAction( removeQuotes )
        gr_opt_quoted_string = gr_stripped_string | restOfLine
        gr_name = Keyword('name', caseless=True) + gr_eq + gr_opt_quoted_string
        gr_name.setParseAction(lambda x, y=self: y._set_name(x[2]))
        gr_yn = Keyword('yes', caseless=True).setParseAction(replaceWith('1')) | Keyword('no', caseless=True).setParseAction(replaceWith('0'))
        gr_phrase = Group(OneOrMore(gr_stripped_string | Word(alphanums)) + gr_eq + gr_opt_quoted_string)

        def np(words, fn = gr_opt_quoted_string, action=print):
            p = Keyword(words[0], caseless=True)
            for w in words[1:]:
                p = p | Keyword(w, caseless=True)
            p = p + gr_eq + fn
            p.setParseAction(action)
            return p
        
        gr_ifsc = np(PList('Ignore File Set Changes'), gr_yn, action=self._parse_setter(IGNORECHANGES))
        gr_evss = np(PList('Enable VSS'), gr_yn, action=self._parse_setter(VSSENABLED))

        gr_i_option = Group(Keyword(OPTIONS, caseless=True) + nestedExpr('{','}', Regex('[^\}]+', re.MULTILINE)))
        gr_e_option = gr_i_option.copy()
        gr_i_file = gr_phrase.copy()
        gr_e_file = gr_phrase.copy()

        gr_inc = Keyword('include', caseless=True) + nestedExpr('{','}', OneOrMore(gr_i_option | gr_i_file))
        gr_inc.addParseAction(self._parse_add_entry)
        gr_exc = Keyword('exclude', caseless=True) + nestedExpr('{','}', OneOrMore(gr_e_option | gr_e_file))
        gr_exc.addParseAction(self._parse_add_entry)

        gr_res = OneOrMore(gr_name | gr_inc | gr_exc | gr_ifsc | gr_evss)
        result = gr_res.parseString(string, parseAll=True)
        return 'Fileset: ' + self[NAME]
Exemplo n.º 3
0
    tok = tok[0].asList()
    return (int(tok[0]), int(tok[1]))

prime_entry = Word(nums).addParseAction(lambda tok: (int(tok[0]), 1)) ^ \
              Group(Word(nums) + Suppress(":") + integer).addParseAction(parse_numbered)

prime_list = Group(delimitedList(
    prime_entry, delim=",")).addParseAction(lambda tok: tok.asList())

param_node = (Literal("P{").addParseAction(lambda tok: "P") + prime_list +
              Suppress("}")).addParseAction(lambda toks: (toks[0], toks[1]))

lambda_node = Literal("L").addParseAction(lambda tok: ("L", ))

primeable_node = sync_node ^ transition_node ^ call_node ^ compressed_node
prime_token = Group(primeable_node + ZeroOrMore(Literal("'")).addParseAction(
    lambda toks: len(toks))).addParseAction(lambda toks: tuple(*toks))
node_token = prime_token ^ param_node ^ lambda_node

node_choice = Forward()
transitive = Group(
    Suppress("(") + delimitedList(Group(OneOrMore(node_choice)), delim="|") +
    Suppress(")"))
node_choice <<= (transitive ^ node_token)

plain_string = node_token + ZeroOrMore(node_choice)

rec_tree = StringStart() + (
    plain_string
    ^ transitive.copy().addParseAction(lambda t: t[0])) + StringEnd()