Example #1
0
    def __init__(self, params):
        self.name = ""
        self.type = None
        self.default_value = None
        self.range = None

        if isinstance(params, list):
            self.name = params[0]
            for attr in params[1:]:
                if attr[0] == 'default':
                    self.default_value = attr[1]
                elif attr[0] == 'type':
                    assert attr[1] == 'string' or attr[1] == 'integer'
                    self.type = attr[1]
                elif attr[0] == 'range':
                    self.range = [attr[1], attr[2]]
                    if int(self.range[0]) >= int(self.range[1]):
                        raise ValueError('Invalid range')

            if not self.default_value is None:
                try:
                    # consistenza di tipo sui valori dello slot corrente
                    parser = Word(alphas) if self.type == 'string' else Word(nums)
                    parser.parseString(self.default_value)
                    # se slot corrente ha un range definito
                    if not self.range is None:
                        spec_range = '{0} >= {1} and {0} <= {2}'.format(self.default_value, self.range[0], self.range[1])
                        if not eval(spec_range):
                            raise ValueError('Default value doesn\'t satisfy range constraints')
                except (ParseException, SyntaxError):
                    raise ValueError('Incorrect default value for slot')
        else:
            self.name = params
Example #2
0
def read_stats(result_dir, stats_file_name):
    stat_rule = Word(printables) + Word('nan.%' + nums) + Optional(restOfLine)

    stats = []

    try:
        with open(path.join(result_dir, stats_file_name)) as stats_file:
            i = 0
            for stat_line in stats_file:
                if len(stats) <= i:
                    stats.append(collections.OrderedDict())

                try:
                    stat = stat_rule.parseString(stat_line)
                    key = stat[0]
                    value = stat[1]

                    stats[i][key] = value
                except ParseException as e:
                    # print(e)
                    pass

                if 'End Simulation Statistics' in stat_line:
                    i += 1
    except Exception as e:
        print(e)
        return None
    else:
        return stats
Example #3
0
 def set_param(self,line):
     # define grammar
     a = line.find("]")
     b = line.find("=")
     if(a>0 and b>0 and (b-a)==1):
         return
     else:
         modele = Word( alphas ) + "[" + Word(nums) + "]" + Word(objectPath) + "=" + Word( divers )
         try:
             pd = modele.parseString( line )
         except ParseException as pe:
             pass
         else:
             obj = pd[0]
             key = pd[4]
             value = pd[6][:len(pd[6])-1]
             nb = int(pd[2])
             if(key[0]=="."):
                 key = key[1:]                           #expect ".keyword"
                 if(key.find(".")<0):                    #a single keyword
                     if(key in ocd_str_param):
                         setattr(self,key,set_str(value))
                         #print("->  ocd[{id}].{key}={value}".format(id=self.id,key=key,value=value))
                     elif(key in ocd_int_param):
                         setattr(self,key,int(value))
                         #print("->  ocd[{id}].{key}={value}".format(id=self.id,key=key,value=value))
                 else:
                     keywords = key.split(".")
                     if(keywords[0]=="likelihood"):
                         self.likelihood.set_param(keywords,value)
                     elif(keywords[0]=="posterior"):
                         self.posterior.set_param(keywords,value)
Example #4
0
    def open(self, id):
        self.id = id
        item = 'BODY'
        typ, data = self.imap.fetch(id, '(' + item + ')')
        assert typ == 'OK'

        inline_parse = Forward()
        inline_parse << Suppress("(") + Group(OneOrMore(Or([Keyword("NIL"), QuotedString('"'), Word(nums), inline_parse ]))) + Suppress(")")
        parse = Word(nums) + Suppress("(") + Keyword(item) + Suppress("(") + Group(OneOrMore(inline_parse)) + ZeroOrMore(Or([Keyword("NIL"), QuotedString('"'), Word(nums)])) +  Suppress(")") + Suppress(")")
        p = parse.parseString(data[0])

        #print data[0]
        #print p
        #print

        self.attachment = []
        for i in p[2]:
            #while 'NIL' in i:
            #    i.remove('NIL')

            a = {
                'type'          : '/'.join(i[0:2]).lower(),
                i[2][0].lower() : i[2][1],
            }
                
            self.attachment.append(a)
Example #5
0
File: io.py Project: izquierdo/kr
def load_variables(filename):
		"""Load random variables definitions from file (in C45 format but with class at the end).
		File must contain information in format 'Variable Name: Values.' as in the example below:
		A: true,false.
		B: 0,1,2.
		C: c1,c2,c3,c4.
		D: one.
		"""
		from DataStructures.randomvariables import RandomVariable
		RV = []
#		variable = Word(caps + lowers + digits).setResultsName("name") + ": " + OneOrMore(Group(Word(caps + lowers + digits) + Optional("." + Word(caps + lowers + digits))) + Suppress(Optional(","))).setResultsName("domain") + "."

		variable = Word(caps + lowers + digits).setResultsName("name") + ": " + OneOrMore(Word(caps + lowers + digits + ".") + Optional(Suppress(","))).setResultsName("domain")  
		for line in file(filename):
			if not line[0] == '#':
				dataline = line[0:(len(line)-2)]
				#print dataline
				rv = variable.parseString(dataline)
				#print rv.name
				domain = []
				for value in rv.domain:
					#print value,
					value = ''.join(value)
					if value.isdigit():
						#print 'lv#', value
						domain.append(int(value))
					else:
						domain.append(value)
				#print
				var = RandomVariable(rv.name,domain)
				RV.append(var)
		return RV
Example #6
0
    def __createGram(self):

        if self.notNeedSpace:
            lNot = Keyword(self.operators['not'])
        else:
            lNot = Literal(self.operators['not'])
        
        lAnd = Literal(self.operators['and'])
        lOr = Literal(self.operators['or'])
        lImp = Literal(self.operators['impL'])
        lEqu = Literal(self.operators['equ'])
        
        lTrue = Keyword(self.constants['true'])
        lFalse = Keyword(self.constants['false'])
        
        lVar = Word(alphas, alphanums+'_')
        
        lVar.setParseAction(self.ffactory.createLogicVariable)
        lTrue.setParseAction(self.ffactory.createLogicTruth)
        lFalse.setParseAction(self.ffactory.createLogicFalse)

        factor = lTrue | lFalse | lVar
        
        expression = myparsing.operatorPrecedence(factor,
        [
         (lNot, 1, opAssoc.RIGHT, self.ffactory.createNotOperation),
         (lAnd, 2, opAssoc.LEFT, self.ffactory.createAndOperation),
         (lOr,  2, opAssoc.LEFT, self.ffactory.createOrOperation),
         (lImp, 2, opAssoc.LEFT, self.ffactory.createImpicationOperation),
         (lEqu, 2, opAssoc.LEFT, self.ffactory.createEquvalenceOperation)
        ],
        [('(', ')'), ('[', ']'), ('{', '}')])
    

        self.final = expression + StringEnd()
Example #7
0
    def __init__(self):
        # literals
        star = Literal('*')
        comma = Suppress(',')

        # indentifiers
        identifier = Word(alphas, alphanums+'_')
        alias      = identifier.copy()

        # select clause
        column_name = Combine(Optional(alias + '.') +
                              identifier +
                              Optional(' as ' + identifier))\
                              .setResultsName('column_name')
        select = Keyword('select', caseless=1)
        select_clause = (star | Group(delimitedList(column_name, comma)))\
            .setResultsName('select_clause')

        # from clause
        from_  = Keyword('from', caseless=1)
        table_name =  delimitedList(identifier + Optional(alias), comma)
        from_clause = table_name.setResultsName('from_clause')

        # select statment
        self.select_stmt = select + select_clause + from_ + from_clause
Example #8
0
def func_tokens(dictionary, parse_action):
    func_name = Word(alphas+'_', alphanums+'_')

    func_ident = Combine('$' + func_name.copy()('funcname'))
    func_tok = func_ident + originalTextFor(nestedExpr())('args')
    func_tok.leaveWhitespace()
    func_tok.setParseAction(parse_action)
    func_tok.enablePackrat()

    rx_tok = Combine(Literal('$').suppress() + Word(nums)('num'))

    def replace_token(tokens):
        index = int(tokens.num)
        return dictionary.get(index, u'')

    rx_tok.setParseAction(replace_token)

    strip = lambda s, l, tok: tok[0].strip()
    text_tok = CharsNotIn(u',').setParseAction(strip)
    quote_tok = QuotedString('"')

    if dictionary:
        arglist = Optional(delimitedList(quote_tok | rx_tok | text_tok))
    else:
        arglist = Optional(delimitedList(quote_tok | text_tok))

    return func_tok, arglist, rx_tok
Example #9
0
	def make_parser(self):
		g = self
		
		lpar = Literal('(').suppress()
		rpar = Literal(')').suppress()
		colon = Literal(':').suppress()
		delimiter = Literal(';').suppress()
		unknown = Literal('?').setParseAction(lambda s,l,t: [0])  # ? -> number 0
		
		number = Word(nums).setParseAction(map_int)

		ident = Word(alphas+'_', alphas+nums+'_')

		label_gen = (
			ident + lpar + Optional(number) + rpar
		).setParseAction(tokenize('LabelGen'))

		label_def = (ident + colon).setParseAction(tokenize('LabelDef'), run(g.gen_label_def))

		label_ref = ident.copy().setParseAction(tokenize('LabelRef'))

		operand = number | label_gen | label_ref | unknown

		instr = (ident + List(operand) + delimiter).setParseAction(tokenize('Instr'), run(g.gen_instr))
		
		entry = instr | label_def

		progr = List(entry).setParseAction(run(self.gen_end_progr))
		
		return progr
Example #10
0
 def set_param(self,line):
     # define grammar
     a = line.find("]")
     b = line.find("=")
     if(a>0 and b>0 and (b-a)==1):
         return
     else:
         modele = Word( alphas ) + "[" + Word(nums) + "]" + Word(objectPath) + "=" + Word( divers )
         try:
             pd = modele.parseString( line )
         except ParseException as pe:
             pass
         else:
             obj = pd[0]
             key = pd[4]
             value = pd[6][:len(pd[6])-1]
             nb = int(pd[2])
             if(key[0]=="."):
                 key = key[1:]                           #expect ".keyword"
                 if(key.find(".")<0):                    #a single keyword
                     if(key in ("ref")):
                         setattr(self,key,set_str(value))
                         #print("->  ocd[{id}].{key}={value}".format(id=self.id,key=key,value=value))
                     elif(key in ("start","resolution")):
                         setattr(self,key,float(value))
                         #print("->  ocd[{id}].{key}={value}".format(id=self.id,key=key,value=value))
                     elif(key in ("bp","sigma")):
                         setattr(self,key,np.array([float(x) for x in commaSeparatedList.parseString(set_str(value))]))
def parse_connection_str(connstr):
    ## Grammar for connection syntax
    digits="0123456789"
    othervalid="_.@"
    identifier= Word(alphas+digits+othervalid)
    nodename=identifier.setResultsName('nodename')

    outputnames = delimitedList( identifier ).setResultsName('outputnames')
    inputnames  = delimitedList( identifier ).setResultsName('inputnames')

    # middle nodes have both inputs and outputs
    middlenode= Group( nodename + Suppress('(') + inputnames
                       + Optional( "|" + outputnames)
                       + Suppress(")") ).setResultsName('middlenode')
    # first node has only outputs
    headnode = (nodename + Suppress("(") + outputnames
                + Suppress(")")).setResultsName('headnode')
    # last node has only inputs
    tailnode = (nodename + Suppress("(") + inputnames
                + Suppress(")")).setResultsName('tailnode')

    # connect head -> [middle ->] tail
    connect= Group( headnode
                    + Group(ZeroOrMore(Suppress("->") \
                        + middlenode + FollowedBy("->") )).setResultsName('middlenodes')
                    + Suppress("->")+tailnode).setResultsName('nodes')

    connectlist = Group( connect + ZeroOrMore( Suppress(";")\
                        + connect )).setResultsName('connects')

    parsed=connectlist.parseString(connstr)
    check_numconnections(parsed)
    return parsed
Example #12
0
    def parse_sexp(data):
        '''parse sexp/S-expression format and return a python list'''
        # define punctuation literals
        LPAR, RPAR, LBRK, RBRK, LBRC, RBRC, VBAR = map(Suppress, "()[]{}|")

        decimal = Word("123456789", nums).setParseAction(lambda t: int(t[0]))
        bytes = Word(printables)
        raw = Group(decimal.setResultsName("len") + Suppress(":") + bytes).setParseAction(OtrPrivateKeys.verifyLen)
        token = Word(alphanums + "-./_:*+=")
        base64_ = Group(Optional(decimal, default=None).setResultsName("len") + VBAR
            + OneOrMore(Word( alphanums +"+/=" )).setParseAction(lambda t: b64decode("".join(t)))
            + VBAR).setParseAction(OtrPrivateKeys.verifyLen)

        hexadecimal = ("#" + OneOrMore(Word(hexnums)) + "#")\
                        .setParseAction(lambda t: int("".join(t[1:-1]),16))
        qString = Group(Optional(decimal, default=None).setResultsName("len") +
                                dblQuotedString.setParseAction(removeQuotes)).setParseAction(OtrPrivateKeys.verifyLen)
        simpleString = raw | token | base64_ | hexadecimal | qString

        display = LBRK + simpleString + RBRK
        string_ = Optional(display) + simpleString

        sexp = Forward()
        sexpList = Group(LPAR + ZeroOrMore(sexp) + RPAR)
        sexp << ( string_ | sexpList )

        try:
            sexpr = sexp.parseString(data)
            return sexpr.asList()[0][1:]
        except ParseFatalException, pfe:
            print("Error:", pfe.msg)
            print(pfe.loc)
            print(pfe.markInputline())
Example #13
0
 def parse(operator, string, digits=None):
     """Parse a valid interval from strings like '1800-1900' or '12'. If operator is one of 
     ('<=', '>=', '>', '<'), only a single number will be parsed and an open interval will be constructed.
     If the optional paramater *digits* is specified, only numbers having exactly this number of digits
     will be considered.
     """
     string = string.strip()
     if digits is not None:
         number = Word(pyparsing.nums, exact=digits)
     else: number = Word(pyparsing.nums)
     try:
         if operator not in ('<=', '>=', '>', '<'):
             # the ^ means xor. | does not work together with parseAll=True (bug in pyparsing?)
             parser = number ^ (number + Suppress('-') + number)
             result = parser.parseString(string, parseAll=True).asList()
             result = [int(r) for r in result]
             if len(result) == 1:
                 return Interval(result[0], result[0])
             else: return Interval(result[0], result[1])
         else:
             result = number.parseString(string)
             date = int(result[0])
             if operator == '>=':
                 return Interval(date, None)
             elif operator == '<=':
                 return Interval(None, date)
             elif operator == '>':
                 return Interval(date+1, None)
             elif operator == '<':
                 return Interval(None, date-1)
             else: assert False
     except pyparsing.ParseException:
         return None
Example #14
0
    def init_parser(self):

        INTEGER = Word(nums)
        INTEGER.setParseAction(lambda x: int(x[0]))

        header = INTEGER("species_count") + INTEGER("sequence_length") +\
            Suppress(restOfLine)
        header.setParseAction(self.set_header)

        sequence_name = Word(
            alphas + nums + "!#$%&\'*+-./;<=>?@[\\]^_`{|}~",
            max=100)

        # Take a copy and disallow line breaks in the bases
        bases = self.BASES.copy()
        bases.setWhitespaceChars(" \t")
        seq_start = sequence_name("species") + bases(
            "sequence") + Suppress(LineEnd())
        seq_start.setParseAction(self.set_seq_start)
        seq_start_block = OneOrMore(seq_start)
        seq_start_block.setParseAction(self.set_start_block)

        seq_continue = bases("sequence") + Suppress(LineEnd())
        seq_continue.setParseAction(self.set_seq_continue)

        seq_continue_block = Suppress(LineEnd()) + OneOrMore(seq_continue)
        seq_continue_block.setParseAction(self.set_continue_block)

        return header + seq_start_block + ZeroOrMore(seq_continue_block)
Example #15
0
def _construct_grammar():
    logical_operator = get_logical_operator()
    logical_expression = get_logical_expression()

    facets_expression = get_facet_expression()
    highlight_expression = get_highlight_expression()
    sort_expression = get_sort_expression()
    aggs_expression = get_aggregations_expression()
    nested_expression = get_nested_expression()

    # The below line describes how the type expression should be.
    type_expression = Word('type')\
        + Word(':').suppress()\
        + Word(srange("[a-zA-Z0-9_]"))\
        + Optional(CaselessLiteral('AND')).suppress()
    type_expression.setParseAction(parse_type_expression)

    base_expression = Optional(highlight_expression)\
        + Optional(sort_expression)\
        + Optional(type_expression)\
        + ZeroOrMore(
            (facets_expression
             | aggs_expression
             | nested_expression
             | logical_expression)
            + Optional(logical_operator)
        ).setParseAction(parse_one_or_more_logical_expressions)
    base_expression.setParseAction(parse_type_logical_facets_expression)

    return base_expression
def parse_morse_letter(line):
    """Parse a line of input from the morse code table file and convert it into a parsetree."""
    letter = Word(alphas)
    token = Word(alphas)
    morse_letter_expression = letter.setResultsName('letter')\
                            + OneOrMore(token).setResultsName('tokens')
    return morse_letter_expression.parseString(line)
Example #17
0
def getLogLineBNF_DBpedia36():
    global logLineBNF
    
    if logLineBNF is None:
        integer = Word( nums )
        ipAddress = delimitedList( integer, ".", combine=True )
        hashipAddress = Word(nums+alphas)
        timeZoneOffset = Word("+-",nums)
        month = Word(string.uppercase, string.lowercase, exact=3)
        serverDateTime = Group( Suppress("[") + 
                                Combine( integer + "/" + month + "/" + integer +
                                        " " + integer + ":" + integer + ":" + integer ) +
                                timeZoneOffset + 
                                Suppress("]") )
                         
        logLineBNF = ( hashipAddress.setResultsName("ipAddr") + 
                       Suppress("-") +
                       ("-" | Word( alphas+nums+"@._" )).setResultsName("auth") +
                       serverDateTime.setResultsName("timestamp") + 
                       dblQuotedString.setResultsName("cmd").setParseAction(getCmdFields2) +
                       (integer | "-").setResultsName("statusCode") + 
                       (integer | "-").setResultsName("numBytesSent")  + 
                       dblQuotedString.setResultsName("referrer").setParseAction(removeQuotes) +
                       dblQuotedString.setResultsName("clientSfw").setParseAction(removeQuotes) )
    return logLineBNF
def parse_ampersand_comment(s):
    import pyparsing
    pyparsing.ParserElement.enablePackrat()
    from pyparsing import Word, Literal, QuotedString, CaselessKeyword, \
         OneOrMore, Group, Optional, Suppress, Regex, Dict
    word = Word(string.letters+string.digits+"%_")
    key = word.setResultsName("key") + Suppress("=")
    single_value = (Word(string.letters+string.digits+"-.") |
                    QuotedString("'") |
                    QuotedString('"'))
    range_value = Group(Suppress("{") +
                        single_value.setResultsName("min") +
                        Suppress(",") +
                        single_value.setResultsName("max") +
                        Suppress("}"))
    pair = (key + (single_value | range_value).setResultsName("value"))
    g = OneOrMore(pair)
    d = []
    for x in g.searchString(s):
        v = x.value
        if type(v) == str:
            try: v = float(v)
            except ValueError: pass
        else:
            try: v = map(float, v.asList())
            except ValueError: pass
        d.append((x.key, v))
    return d
def nexus_iter(infile):
    import pyparsing
    pyparsing.ParserElement.enablePackrat()
    from pyparsing import Word, Literal, QuotedString, CaselessKeyword, \
         OneOrMore, Group, Optional, Suppress, Regex, Dict
    ## beginblock = Suppress(CaselessKeyword("begin") +
    ##                       CaselessKeyword("trees") + ";")
    ## endblock = Suppress((CaselessKeyword("end") |
    ##                      CaselessKeyword("endblock")) + ";")
    comment = Optional(Suppress("[&") + Regex(r'[^]]+') + Suppress("]"))
    ## translate = CaselessKeyword("translate").suppress()
    name = Word(string.letters+string.digits+"_.") | QuotedString("'")
    ## ttrec = Group(Word(string.digits).setResultsName("number") +
    ##               name.setResultsName("name") +
    ##               Optional(",").suppress())
    ## ttable = Group(translate + OneOrMore(ttrec) + Suppress(";"))
    newick = Regex(r'[^;]+;')
    tree = (CaselessKeyword("tree").suppress() +
            Optional("*").suppress() +
            name.setResultsName("tree_name") +
            comment.setResultsName("tree_comment") +
            Suppress("=") +
            comment.setResultsName("root_comment") +
            newick.setResultsName("newick"))
    ## treesblock = Group(beginblock +
    ##                    Optional(ttable.setResultsName("ttable")) +
    ##                    Group(OneOrMore(tree)) +
    ##                    endblock)

    def not_begin(s): return s.strip().lower() != "begin trees;"
    def not_end(s): return s.strip().lower() not in ("end;", "endblock;")
    def parse_ttable(f):
        ttable = {}
        while True:
            s = f.next().strip()
            if not s: continue
            if s.lower() == ";": break
            if s[-1] == ",": s = s[:-1]
            k, v = s.split()
            ttable[k] = v
            if s[-1] == ";": break
        return ttable
            
    # read lines between "begin trees;" and "end;"
    f = itertools.takewhile(not_end, itertools.dropwhile(not_begin, infile))
    s = f.next().strip().lower()
    if s != "begin trees;":
        print sys.stderr, "Expecting 'begin trees;', got %s" % s
        raise StopIteration
    ttable = {}
    while True:
        try: s = f.next().strip()
        except StopIteration: break
        if not s: continue
        if s.lower() == "translate":
            ttable = parse_ttable(f)
            print "ttable: %s" % len(ttable)
        elif s.split()[0].lower()=='tree':
            match = tree.parseString(s)
            yield nexus.Newick(match, ttable)
Example #20
0
def _define_grammar():
    """
    Creates and returns a copy of the selector grammar.

    Wrapped in a function to avoid polluting the module namespace.
    """
    expr = Forward()

    label_name = Word(LABEL_CHARS)
    label_name.setParseAction(LabelNode)

    string_literal = QuotedString('"') | QuotedString("'")
    string_literal.setParseAction(LiteralNode)

    set_literal = (Suppress("{") +
                   delimitedList(QuotedString('"') | QuotedString("'"), ",") +
                   Suppress("}"))
    set_literal.setParseAction(SetLiteralNode)

    eq_comparison = label_name + Suppress("==") + string_literal
    eq_comparison.setParseAction(LabelToLiteralEqualityNode)

    not_eq_comparison = label_name + Suppress("!=") + string_literal
    not_eq_comparison.setParseAction(InequalityNode)

    in_comparison = label_name + Suppress(Keyword("in")) + set_literal
    in_comparison.setParseAction(LabelInSetLiteralNode)

    not_in = Suppress(Keyword("not") + Keyword("in"))
    not_in_comparison = label_name + not_in + set_literal
    not_in_comparison.setParseAction(NotInNode)

    has_check = (Suppress("has(") +
                 Word(LABEL_CHARS) +
                 Suppress(")"))
    has_check.setParseAction(HasNode)

    comparison = (eq_comparison |
                  not_eq_comparison |
                  in_comparison |
                  not_in_comparison |
                  has_check)

    paren_expr = (Suppress("(") + expr + Suppress(")"))

    value = comparison | paren_expr

    and_expr = value + ZeroOrMore(Suppress("&&") + value)
    and_expr.setParseAction(simplify_and_node)

    or_expr = and_expr + ZeroOrMore(Suppress("||") + and_expr)
    or_expr.setParseAction(simplify_or_node)

    expr << or_expr

    grammar = expr + StringEnd()
    return grammar
Example #21
0
def get_highlight_expression():
    field_expression = Word(srange("[a-zA-Z0-9_.*]"))
    field_expression.setParseAction(parse_highlight_field_expression)
    fields_expression = OneOrMore(
        field_expression + Optional(',').suppress())
    fields_expression.setParseAction(parse_highlight_expression)
    highlight_expression = Word('highlight:').suppress() \
        + Word('[').suppress() \
        + fields_expression + Word(']').suppress()
    return highlight_expression
Example #22
0
    def setup(self):
        # some expressions that will be reused
        units = []
        for unit in time_units:
            units.append(Keyword(unit))
        units = get_match_first(units)
        units = units.setResultsName("unit")
        units.setParseAction(lambda s, l, tok: time_units[tok[0]])

        multiplier = Word(nums)
        multiplier = multiplier.setResultsName("multiply")
        multiplier.setParseAction(self.parseMulti)

        adder = []
        for add in add_modifiers:
            adder.append(CL(add))
        adder = get_match_first(adder)
        adder = adder.setResultsName("add")
        adder.setParseAction(self.parseAdd)
        modifier = (multiplier | adder)  # + FollowedBy(units)

        # ago
        #
        # e.g 5 days ago
        ago = Optional(modifier) + units + Suppress(Word("ago"))
        ago.setParseAction(self.parseAgo)

        # time range
        #
        # e.g in the lat 10 days
        time_range = Suppress(Optional(
            CL("in the"))) + \
            Suppress(Word("last") |
                     Word("past")) + \
            Optional(modifier) + \
            units
        time_range.setParseAction(self.parseRange)

        # special keyword handling
        #
        # e.g yesterday
        # only handles yesterday right now, maybe need to be modified to do
        # more
        special_expr = []
        for expr in special:
            special_expr.append(
                Keyword(expr).setParseAction(
                    lambda s, l, tok: special[tok[0]]))
        special_expr = get_match_first(special_expr)
        special_expr = special_expr.setResultsName("unit")
        special_expr.setParseAction(self.parseAgo)

        parser = (special_expr | ago | time_range)

        return parser
Example #23
0
def eval_query(query_str):
    global WORD_CHARS
    boolOperand = Word(WORD_CHARS)
    boolOperand.setParseAction(BoolOperand)

    boolExpr = infixNotation(
        boolOperand,
        [("not", 1, opAssoc.RIGHT, BoolNot), ("and", 2, opAssoc.LEFT, BoolAnd), ("or", 2, opAssoc.LEFT, BoolOr)],
    )

    return boolExpr.parseString(query_str.lower())[0].calcop()
    def _getPattern(self):
        arith_expr = Forward()
        comp_expr = Forward()
        logic_expr = Forward()
        LPAR, RPAR, SEMI = map(Suppress, "();")
        identifier = Word(alphas+"_", alphanums+"_")
        multop = oneOf('* /')
        plusop = oneOf('+ -')
        expop = Literal( "^" )
        compop = oneOf('> < >= <= != ==')
        andop = Literal("AND")
        orop = Literal("OR")
        current_value = Literal( "." )
        assign = Literal( "=" )
        # notop = Literal('NOT')
        function = oneOf(' '.join(self.FUNCTIONS))
        function_call = Group(function.setResultsName('fn') + LPAR + Optional(delimitedList(arith_expr)) + RPAR)
        aggregate_column = QuotedString(quoteChar='{', endQuoteChar='}')
        single_column = QuotedString(quoteChar='[', endQuoteChar=']')
        integer = Regex(r"-?\d+")
        real = Regex(r"-?\d+\.\d*")

        # quotedString enables strings without quotes to pass

        operand = \
            function_call.setParseAction(self.__evalFunction) | \
            aggregate_column.setParseAction(self.__evalAggregateColumn) | \
            single_column.setParseAction(self.__evalSingleColumn) | \
            ((real | integer).setParseAction(self.__evalConstant)) | \
            quotedString.setParseAction(self.__evalString).addParseAction(removeQuotes) | \
            current_value.setParseAction(self.__evalCurrentValue) | \
            identifier.setParseAction(self.__evalString)

        arith_expr << operatorPrecedence(operand,
            [
             (expop, 2, opAssoc.LEFT, self.__expOp),
             (multop, 2, opAssoc.LEFT, self.__multOp),
             (plusop, 2, opAssoc.LEFT, self.__addOp),
            ])

        # comp_expr = Group(arith_expr + compop + arith_expr)
        comp_expr << operatorPrecedence(arith_expr,
            [
                (compop, 2, opAssoc.LEFT, self.__evalComparisonOp),
            ])

        logic_expr << operatorPrecedence(comp_expr,
            [
                (andop, 2, opAssoc.LEFT, self.__evalLogicOp),
                (orop, 2, opAssoc.LEFT, self.__evalLogicOp)
            ])

        pattern = logic_expr + StringEnd()
        return pattern
Example #25
0
 def ValidateName(self,Nn,T):
     from pyparsing import Word,alphas,nums,ParseException
     G=Word(alphas+"_", alphas+nums+"_")
     G.setDebug(False)
     try:
         ps=G.parseString(Nn, parseAll=True)
         Nn=ps[0]
     except ParseException,PE:
         self.PRINT(PE.line)
         self.PRINT(" "*(PE.column-1) + "^")
         self.ERROR(64,T,PE)
Example #26
0
 def check_slot_value(self, slot_value):
     try:
         if not self.type is None:
             parser = Word(alphas) if self.type == 'string' else Word(nums)
             parser.parseString(slot_value)
         if not self.range is None and self.type != 'string':
             spec_range = '{0} >= {1} and {0} <= {2}'.format(slot_value, self.range[0], self.range[1])
             if not eval(spec_range):
                 return False
         return True
     except (ParseException, SyntaxError):
         return False
Example #27
0
File: io.py Project: izquierdo/kr
def load_c45_header(filename):
		"""Load random variables definitions from file (in C45 format).
		File must contain information in format 'Variable Name: Values.' as in the example below:
		0,1.
		A: true,false.
		B: 0,1,2.
		C: c1,c2,c3,c4.
		D: one.
		The first line is related to the class object (expressed in last position at the output header)
		"""
		from DataStructures.randomvariables import RandomVariable
		RV = []

		cvariable = OneOrMore(Word(caps + lowers + digits + ".") + Optional(Suppress(","))).setResultsName("domain")  
		variable = Word(caps + lowers + digits).setResultsName("name") + ": " + OneOrMore(Word(caps + lowers + digits + ".") + Optional(Suppress(","))).setResultsName("domain")  
		class_variable = None
		for line in file(filename):
			if not line[0] == '#' and len(line) > 1:
				if class_variable is None:
					dataline = line[0:(len(line)-2)]
					#print dataline
					rv = cvariable.parseString(dataline)
					domain = []
					for value in rv.domain:
						#print value,
						value = ''.join(value)
						if value.isdigit():
							#print 'lv#', value
							domain.append(int(value))
						else:
							domain.append(value)
					#print
					class_variable = RandomVariable('class',domain)
				else:	
					dataline = line[0:(len(line)-2)]
					#print dataline
					rv = variable.parseString(dataline)
					#print rv.name
					domain = []
					for value in rv.domain:
						#print value,
						value = ''.join(value)
						if value.isdigit():
							#print 'lv#', value
							domain.append(int(value))
						else:
							domain.append(value)
					#print
					var = RandomVariable(rv.name,domain)
					RV.append(var)
		RV.append(class_variable)
		return RV
Example #28
0
    def __init__(self, EvaluateVariableChild=None, EvaluateNumberChild=None):
        EvaluateVariableChild = EvaluateVariableChild or EvaluateVariable
        EvaluateNumberChild = EvaluateNumberChild or EvaluateNumber
        # what is a float number
        floatNumber = Regex(r'[-]?\d+(\.\d*)?([eE][-+]?\d+)?')
        # a variable is a combination of letters, numbers, and underscor
        variable = Word(alphanums + "_")
        # a sign is plus or minus
        signOp = oneOf('+ -')
        # an operand is a variable or a floating point number
        operand = floatNumber ^ variable
        # when a floatNumber is found, parse it with evaluate number
        floatNumber.setParseAction(EvaluateNumberChild)
        # when a variable is found, parse it with the EvaluateVariableChild
        # or EvaluateVariable
        variable.setParseAction(EvaluateVariableChild)
        # comparisons include lt,le,gt,ge,eq,ne
        comparisonOp = oneOf("< <= > >= == !=")
        # negation of the boolean is !
        notOp = oneOf("!")
        # an expression is a either a comparison or
        # a NOT operation (where NOT a is essentially (a == False))
        comparisonExpression = operatorPrecedence(operand,
                                                  [
                                                   (comparisonOp,
                                                    2,
                                                    opAssoc.LEFT,
                                                    EvaluateComparison
                                                    ),
                                                   (notOp,
                                                    1,
                                                    opAssoc.RIGHT,
                                                    EvaluateNot
                                                    ),
                                                  ])

        # boolean logic of AND or OR
        boolOp = oneOf("& |")

        # a bool expression contains a nested bool expression or a comparison,
        # joined with a boolean operation
        boolExpression = Forward()
        boolPossible = boolExpression | comparisonExpression
        self.boolExpression = operatorPrecedence(boolPossible,
                                                 [
                                                  (boolOp,
                                                   2,
                                                   opAssoc.RIGHT,
                                                   EvaluateOrAnd
                                                   ),
                                                 ])
        return
Example #29
0
def main(s):
    lpar = Literal('(').suppress()
    rpar = Literal(')').suppress()
    integer = Word(nums)
    element = Word(alphas, exact=1)
    formula = Forward()
    term = Group((element | Group(lpar + formula + rpar)('subgroup')) +
            Optional(integer, default=1)('mult'))
    formula << OneOrMore(term)
    integer.setParseAction(process_integer)
    term.setParseAction(process_term)
    formula.setParseAction(process_formula)
    return formula.parseString(s)[0]
Example #30
0
class NodeParser:
    def __init__(self):
        self.num = Word(nums)

        self.header = Regex(r"^UCLA.*")
        self.comment = Regex(r"#.*")
        self.bkid = Word(alphanums)

        self.num_nodes = Literal("NumNodes") + Literal(":") + self.num("NumNodes")
        self.num_terminals = Literal("NumTerminals") + Literal(":") + self.num("NumTerminals")
        self.size = Group(self.num("width") + self.num("height"))
        self.terminal = Optional(Literal("terminal"))
        self.node = self.bkid("id") + self.size("size") + self.terminal
        self.node_grammar = (
            self.header + ZeroOrMore(self.comment) + self.num_nodes + self.num_terminals + OneOrMore(self.node)
        )

        self.coordinate = Group(self.num("x") + self.num("y"))
        self.pl = (
            self.bkid("id") + self.coordinate("coordinate") + Suppress(Literal(": N") + Optional(Literal(r"/FIXED")))
        )
        self.pl_grammar = self.header + ZeroOrMore(self.comment) + OneOrMore(self.pl)

    def compute_chip_size(self, benchmark):
        benchmark_path = pathlib.Path(os.environ["BENCHMARK"])
        node_file = benchmark_path / "ispd2005/{0}/{0}.nodes".format(benchmark)
        pl_file = benchmark_path / "ispd2005/{0}/{0}.pl".format(benchmark)
        print(node_file.as_posix())
        print(pl_file.as_posix())

        x_max = 0
        y_max = 0
        sizes = []
        coordinates = []

        self.size.setParseAction(lambda tokens: sizes.append([tokens.width, tokens.height]))
        self.coordinate.setParseAction(lambda tokens: coordinates.append((tokens.x, tokens.y)))
        self.bkid.setParseAction(lambda tokens: print(tokens[0]))

        self.node_grammar.parseFile(node_file.as_posix())
        self.pl_grammar.parseFile(pl_file.as_posix())

        for i in range(len(sizes)):
            print(i)
            if coordinates[i][0] + sizes[i][0] > x_max:
                x_max = coordinates[i][0] + sizes[i][0]
            if coordinates[i][1] + sizes[i][1] > y_max:
                y_max = coordinates[i][1] + sizes[i][1]

        return x_max, y_max
Example #31
0
def _create_grammar_6_0():
    """Create the SYM 6.0 grammar.

    """

    word = Word(printables.replace(';', '').replace(':', ''))
    positive_integer = Word(nums)
    number = Word(nums + '.Ee-+')
    lp = Suppress(Literal('('))
    rp = Suppress(Literal(')'))
    lb = Suppress(Literal('['))
    rb = Suppress(Literal(']'))
    name = Word(alphas + nums + '_-').setWhitespaceChars(' ')
    assign = Suppress(Literal('='))
    comma = Suppress(Literal(','))
    type_ = name

    version = Group(Keyword('FormatVersion') - assign - Keyword('6.0'))

    title = Group(Keyword('Title') - assign - QuotedString('"'))

    enum_value = Group(number + assign + QuotedString('"'))

    enum = Group(
        Suppress(Keyword('Enum')) - assign - name - Suppress(lp) +
        Group(delimitedList(enum_value)) - Suppress(rp))

    sig_unit = Group(Literal('/u:') + word)
    sig_factor = Group(Literal('/f:') + word)
    sig_offset = Group(Literal('/o:') + word)
    sig_min = Group(Literal('/min:') + word)
    sig_max = Group(Literal('/max:') + word)
    sig_default = Group(Literal('/d:') + word)
    sig_long_name = Group(Literal('/ln:') + word)
    sig_enum = Group(Literal('/e:') + word)

    signal = Group(
        Suppress(Keyword('Sig')) - Suppress(assign) - name - type_ +
        Group(Optional(positive_integer)) + Group(Optional(Keyword('-m'))) +
        Group(
            Optional(sig_unit) + Optional(sig_factor) + Optional(sig_offset) +
            Optional(sig_min) + Optional(sig_max) + Optional(sig_default) +
            Optional(sig_long_name) + Optional(sig_enum)))

    symbol = Group(
        Suppress(lb) - name - Suppress(rb) -
        Group(Optional(Keyword('ID') + assign + word)) -
        Group(Keyword('Len') + assign + positive_integer) + Group(
            Optional(
                Keyword('Mux') + assign + word + positive_integer + comma +
                positive_integer + positive_integer)) +
        Group(Optional(Keyword('CycleTime') + assign + positive_integer)) +
        Group(Optional(Keyword('Timeout') + assign + positive_integer)) +
        Group(Optional(Keyword('MinInterval') + assign + positive_integer)) +
        Group(
            ZeroOrMore(Group(
                Keyword('Sig') + assign + name + positive_integer))))

    enums = Group(Keyword('{ENUMS}') + Group(ZeroOrMore(enum)))
    signals = Group(Keyword('{SIGNALS}') + Group(ZeroOrMore(signal)))
    send = Group(Keyword('{SEND}') + Group(ZeroOrMore(symbol)))
    receive = Group(Keyword('{RECEIVE}') + Group(ZeroOrMore(symbol)))
    sendreceive = Group(Keyword('{SENDRECEIVE}') + Group(ZeroOrMore(symbol)))

    section = (enums | signals | send | receive | sendreceive)

    grammar = (version - title + Group(OneOrMore(section)) + StringEnd())
    grammar.ignore(dblSlashComment)

    return grammar
Example #32
0
        return 'Notice( volume=%s, page=%s )' % (repr(
            self.volume), repr(self.page))

    def __eq__(self, other):
        return isinstance(other, Notice) and repr(self) == repr(other)


class Delayed:
    pass


effective_date = (utils.Marker("effective") +
                  utils.Marker("date")).setParseAction(lambda: EffectiveDate())

notice_citation = (
    Word(string.digits) + utils.Marker('FR') +
    Word(string.digits)).setParseAction(lambda m: Notice(int(m[0]), int(m[1])))

delayed = utils.Marker("delayed").setParseAction(lambda: Delayed())


def int2Month(m):
    month = date(2000, m, 1)
    month = month.strftime('%B')
    token = utils.Marker(month)
    return token.setParseAction(lambda: m)


months = reduce(lambda l, r: l | r, map(int2Month, range(2, 13)))

date_parser = (months + Word(string.digits) + Suppress(Optional(",")) + Word(
Example #33
0
# vim: set encoding=utf-8
from pyparsing import (LineStart, Literal, OneOrMore, Optional, Regex, SkipTo,
                       srange, Suppress, Word, ZeroOrMore)

from regparser.grammar import atomic, unified
from regparser.grammar.utils import DocLiteral, keep_pos, Marker

smart_quotes = (Suppress(DocLiteral(u'“', "left-smart-quote")) +
                SkipTo(DocLiteral(u'”', "right-smart-quote")).setParseAction(
                    keep_pos).setResultsName("term"))

e_tag = (Suppress(Regex(r"<E[^>]*>")) + OneOrMore(Word(
    srange("[a-zA-Z-]"))).setParseAction(keep_pos).setResultsName("term") +
         Suppress(Literal("</E>")))

xml_term_parser = (
    LineStart() + Optional(Suppress(unified.any_depth_p)) +
    Suppress(ZeroOrMore(Word(srange("[a-zA-Z]") + '.,'))) +
    e_tag.setResultsName("head") + ZeroOrMore(
        (atomic.conj_phrases + e_tag).setResultsName("tail",
                                                     listAllMatches=True)) +
    Suppress(ZeroOrMore(Regex(r",[a-zA-Z ]+,"))) +
    Suppress(ZeroOrMore((Marker("this") | Marker("the")) + Marker("term"))) +
    ((Marker("mean") | Marker("means"))
     | (Marker("refers") + ZeroOrMore(Marker("only")) + Marker("to"))
     | ((Marker("has") | Marker("have")) + Marker("the") + Marker("same") +
        Marker("meaning") + Marker("as"))))

scope_term_type_parser = (
    Marker("purposes") + Marker("of") + Optional(Marker("this")) +
    SkipTo(",").setResultsName("scope") + Literal(",") +
Example #34
0
""".upper()

from pyparsing import (
    Literal,
    Word,
    delimitedList,
    alphas,
    alphanums,
    OneOrMore,
    ZeroOrMore,
    CharsNotIn,
    replaceWith,
)

skobki = "(" + ZeroOrMore(CharsNotIn(")")) + ")"
field_def = OneOrMore(Word(alphas, alphanums + "_\"':-") | skobki)


def field_act(s, loc, tok):
    return ("<" + tok[0] + "> " + " ".join(tok)).replace('"', '\\"')


field_def.setParseAction(field_act)

field_list_def = delimitedList(field_def)


def field_list_act(toks):
    return " | ".join(toks)

Example #35
0
        test = instring[loc:loc + self.matchLen]
        if test.upper() == self.match:
            return loc + self.matchLen, test
        #~ raise ParseException( instring, loc, self.errmsg )
        exc = self.myException
        exc.loc = loc
        exc.pstr = instring
        raise exc


def Sequence(token):
    """ A sequence of the token"""
    return OneOrMore(token + maybeComma)


digit_sequence = Word(nums)

sign = oneOf("+ -")


def convertToFloat(s, loc, toks):
    try:
        return float(toks[0])
    except:
        raise ParseException(loc, "invalid float format %s" % toks[0])


exponent = CaselessLiteral("e") + Optional(sign) + Word(nums)

#note that almost all these fields are optional,
#and this can match almost anything. We rely on Pythons built-in
Example #36
0
    # Reference Point Usage
    "1": "1",  # Use rp1
    "2": "0",  # Use rp2
    # Minimum Distance flag
    ">": "1",  # Obey minimum distance
    "<": "0",  # Do not obey minimum distance
    # Color (Distance Type)
    "Gr": "00",  # Gray
    "Bl": "01",  # Black
    "Wh": "10",  # White
}


alpha_upper = string.ascii_uppercase

mnemonic = Word(alpha_upper, bodyChars=alpha_upper + nums).setResultsName("mnemonic")

# XXX can't use pyparsing_common.signedInteger as the latest pyparsing 2.1.5
# has a bug which always converts them to floats. Remove this once 2.1.6 is
# published on PyPI.
signed_integer = (
    Regex(r"[+-]?\d+").setName("signed integer").setParseAction(tokenMap(int))
)

variable = Word(alphas, bodyChars=alphanums)

stack_item = Suppress(",") + (signed_integer | Suppress("*") | variable)

flag = oneOf(list(VTT_MNEMONIC_FLAGS.keys()))
# convert flag to binary string
flag.setParseAction(tokenMap(lambda t: VTT_MNEMONIC_FLAGS[t]))
Example #37
0
def pyparsing_parse(text):
    """
    >>> import os
    >>> dirname = os.path.join(os.path.dirname(__file__), "data")
    >>> filename = os.path.join(dirname, "error1.blk")
    >>> with open(filename, encoding="utf8") as file:
    ...     pyparsing_parse(file.read())
    Traceback (most recent call last):
    ...
    ValueError: Error {0}: syntax error, line 8
    >>> filename = os.path.join(dirname, "error2.blk")
    >>> with open(filename, encoding="utf8") as file:
    ...     pyparsing_parse(file.read())
    Traceback (most recent call last):
    ...
    ValueError: Error {0}: syntax error, line 1
    >>> filename = os.path.join(dirname, "error3.blk")
    >>> with open(filename, encoding="utf8") as file:
    ...     pyparsing_parse(file.read())
    Traceback (most recent call last):
    ...
    ValueError: Error {0}: syntax error, line 4
    >>> expected = "[white: ]\\n[lightblue: Director]\\n/\\n/\\n[white: ]\\n[lightgreen: Secretary]\\n/\\n/\\n[white: Minion #1]\\n[white: ]\\n[white: Minion #2]"
    >>> filename = os.path.join(dirname, "hierarchy.blk")
    >>> with open(filename, encoding="utf8") as file:
    ...     blocks = pyparsing_parse(file.read())
    >>> str(blocks).strip() == expected
    True

    >>> expected = "[#00CCDE: MessageBox Window\\n[lightgray: Frame\\n[white: ]\\n[white: Message text]\\n/\\n/\\n[goldenrod: OK Button]\\n[white: ]\\n[#ff0505: Cancel Button]\\n/\\n[white: ]\\n]\\n]"
    >>> filename = os.path.join(dirname, "messagebox.blk")
    >>> with open(filename, encoding="utf8") as file:
    ...     blocks = pyparsing_parse(file.read())
    >>> str(blocks).strip() == expected
    True
    """
    def add_block(tokens):
        return Block.Block(tokens.name,
                           tokens.color if tokens.color else "white")

    left_bracket, right_bracket = map(Suppress, "[]")
    new_rows = Word("/")("new_rows").setParseAction(
        lambda tokens: len(tokens.new_rows))
    name = CharsNotIn("[]/\n")("name").setParseAction(
        lambda tokens: tokens.name.strip())
    color = (Word("#", hexnums, exact=7) | Word(alphas, alphanums))("color")
    empty_node = (left_bracket +
                  right_bracket).setParseAction(lambda: EmptyBlock)
    nodes = Forward()
    node_data = Optional(color + Suppress(":")) + Optional(name)
    node_data.setParseAction(add_block)
    node = left_bracket - node_data + nodes + right_bracket
    nodes << Group(
        ZeroOrMore(Optional(new_rows) + OneOrMore(node | empty_node)))
    stack = [Block.get_root_block()]
    try:
        results = nodes.parseString(text, parseAll=True)
        assert len(results) == 1
        items = results.asList()[0]
        populate_children(items, stack)
    except (ParseException, ParseSyntaxException) as err:
        raise ValueError("Error {{0}}: syntax error, line "
                         "{0}".format(err.lineno))
    return stack[0]
Example #38
0
    def parse(cls,
              content,
              basedir=None,
              resolve=True,
              unresolved_value=DEFAULT_SUBSTITUTION):
        """parse a HOCON content

        :param content: HOCON content to parse
        :type content: basestring
        :param resolve: if true, resolve substitutions
        :type resolve: boolean
        :param unresolved_value: assigned value value to unresolved substitution.
        If overriden with a default value, it will replace all unresolved value to the default value.
        If it is set to to pyhocon.STR_SUBSTITUTION then it will replace the value by its substitution expression (e.g., ${x})
        :type unresolved_value: boolean
        :return: a ConfigTree or a list
        """

        unescape_pattern = re.compile(r'\\.')

        def replace_escape_sequence(match):
            value = match.group(0)
            return cls.REPLACEMENTS.get(value, value)

        def norm_string(value):
            return unescape_pattern.sub(replace_escape_sequence, value)

        def unescape_string(tokens):
            return ConfigUnquotedString(norm_string(tokens[0]))

        def parse_multi_string(tokens):
            # remove the first and last 3 "
            return tokens[0][3:-3]

        def convert_number(tokens):
            n = tokens[0]
            try:
                return int(n, 10)
            except ValueError:
                return float(n)

        def safe_convert_number(tokens):
            n = tokens[0]
            try:
                return int(n, 10)
            except ValueError:
                try:
                    return float(n)
                except ValueError:
                    return n

        def convert_period(tokens):

            period_value = int(tokens.value)
            period_identifier = tokens.unit

            period_unit = next((single_unit for single_unit, values in
                                cls.get_supported_period_type_map().items()
                                if period_identifier in values))

            return period(period_value, period_unit)

        # ${path} or ${?path} for optional substitution
        SUBSTITUTION_PATTERN = r"\$\{(?P<optional>\?)?(?P<variable>[^}]+)\}(?P<ws>[ \t]*)"

        def create_substitution(instring, loc, token):
            # remove the ${ and }
            match = re.match(SUBSTITUTION_PATTERN, token[0])
            variable = match.group('variable')
            ws = match.group('ws')
            optional = match.group('optional') == '?'
            substitution = ConfigSubstitution(variable, optional, ws, instring,
                                              loc)
            return substitution

        # ${path} or ${?path} for optional substitution
        STRING_PATTERN = '"(?P<value>(?:[^"\\\\]|\\\\.)*)"(?P<ws>[ \t]*)'

        def create_quoted_string(instring, loc, token):
            # remove the ${ and }
            match = re.match(STRING_PATTERN, token[0])
            value = norm_string(match.group('value'))
            ws = match.group('ws')
            return ConfigQuotedString(value, ws, instring, loc)

        def include_config(instring, loc, token):
            url = None
            file = None
            required = False

            if token[0] == 'required':
                required = True
                final_tokens = token[1:]
            else:
                final_tokens = token

            if len(final_tokens) == 1:  # include "test"
                value = final_tokens[0].value if isinstance(
                    final_tokens[0], ConfigQuotedString) else final_tokens[0]
                if value.startswith("http://") or value.startswith(
                        "https://") or value.startswith("file://"):
                    url = value
                else:
                    file = value
            elif len(final_tokens) == 2:  # include url("test") or file("test")
                value = final_tokens[1].value if isinstance(
                    token[1], ConfigQuotedString) else final_tokens[1]
                if final_tokens[0] == 'url':
                    url = value
                else:
                    file = value

            if url is not None:
                logger.debug('Loading config from url %s', url)
                obj = ConfigFactory.parse_URL(url,
                                              resolve=False,
                                              required=required,
                                              unresolved_value=NO_SUBSTITUTION)
            elif file is not None:
                path = file if basedir is None else os.path.join(basedir, file)
                logger.debug('Loading config from file %s', path)
                obj = ConfigFactory.parse_file(
                    path,
                    resolve=False,
                    required=required,
                    unresolved_value=NO_SUBSTITUTION)
            else:
                raise ConfigException(
                    'No file or URL specified at: {loc}: {instring}',
                    loc=loc,
                    instring=instring)

            return ConfigInclude(obj if isinstance(obj, list) else obj.items())

        @contextlib.contextmanager
        def set_default_white_spaces():
            default = ParserElement.DEFAULT_WHITE_CHARS
            ParserElement.setDefaultWhitespaceChars(' \t')
            yield
            ParserElement.setDefaultWhitespaceChars(default)

        with set_default_white_spaces():
            assign_expr = Forward()
            true_expr = Keyword("true", caseless=True).setParseAction(
                replaceWith(True))
            false_expr = Keyword("false", caseless=True).setParseAction(
                replaceWith(False))
            null_expr = Keyword("null", caseless=True).setParseAction(
                replaceWith(NoneValue()))
            # key = QuotedString('"', escChar='\\', unquoteResults=False) | Word(alphanums + alphas8bit + '._- /')
            key = QuotedString('"', escChar='\\', unquoteResults=False) | \
                  Word("0123456789.").setParseAction(safe_convert_number) | Word(alphanums + alphas8bit + '._- /')

            eol = Word('\n\r').suppress()
            eol_comma = Word('\n\r,').suppress()
            comment = (Literal('#') | Literal('//')) - SkipTo(eol
                                                              | StringEnd())
            comment_eol = Suppress(Optional(eol_comma) + comment)
            comment_no_comma_eol = (comment | eol).suppress()
            number_expr = Regex(
                r'[+-]?(\d*\.\d+|\d+(\.\d+)?)([eE][+\-]?\d+)?(?=$|[ \t]*([\$\}\],#\n\r]|//))',
                re.DOTALL).setParseAction(convert_number)

            period_types = itertools.chain.from_iterable(
                cls.get_supported_period_type_map().values())
            period_expr = Regex(r'(?P<value>\d+)\s*(?P<unit>' +
                                '|'.join(period_types) +
                                ')$').setParseAction(convert_period)

            # multi line string using """
            # Using fix described in http://pyparsing.wikispaces.com/share/view/3778969
            multiline_string = Regex(
                '""".*?"*"""',
                re.DOTALL | re.UNICODE).setParseAction(parse_multi_string)
            # single quoted line string
            quoted_string = Regex(
                r'"(?:[^"\\\n]|\\.)*"[ \t]*',
                re.UNICODE).setParseAction(create_quoted_string)
            # unquoted string that takes the rest of the line until an optional comment
            # we support .properties multiline support which is like this:
            # line1  \
            # line2 \
            # so a backslash precedes the \n
            unquoted_string = Regex(
                r'(?:[^^`+?!@*&"\[\{\s\]\}#,=\$\\]|\\.)+[ \t]*',
                re.UNICODE).setParseAction(unescape_string)
            substitution_expr = Regex(r'[ \t]*\$\{[^\}]+\}[ \t]*'
                                      ).setParseAction(create_substitution)
            string_expr = multiline_string | quoted_string | unquoted_string

            value_expr = period_expr | number_expr | true_expr | false_expr | null_expr | string_expr

            include_content = (quoted_string | (
                (Keyword('url') | Keyword('file')) - Literal('(').suppress() -
                quoted_string - Literal(')').suppress()))
            include_expr = (Keyword("include", caseless=True).suppress() +
                            (include_content |
                             (Keyword("required") - Literal('(').suppress() -
                              include_content - Literal(')').suppress()))
                            ).setParseAction(include_config)

            root_dict_expr = Forward()
            dict_expr = Forward()
            list_expr = Forward()
            multi_value_expr = ZeroOrMore(comment_eol | include_expr
                                          | substitution_expr | dict_expr
                                          | list_expr | value_expr
                                          | (Literal('\\') - eol).suppress())
            # for a dictionary : or = is optional
            # last zeroOrMore is because we can have t = {a:4} {b: 6} {c: 7} which is dictionary concatenation
            inside_dict_expr = ConfigTreeParser(
                ZeroOrMore(comment_eol | include_expr | assign_expr
                           | eol_comma))
            inside_root_dict_expr = ConfigTreeParser(
                ZeroOrMore(comment_eol | include_expr | assign_expr
                           | eol_comma),
                root=True)
            dict_expr << Suppress('{') - inside_dict_expr - Suppress('}')
            root_dict_expr << Suppress('{') - inside_root_dict_expr - Suppress(
                '}')
            list_entry = ConcatenatedValueParser(multi_value_expr)
            list_expr << Suppress('[') - ListParser(list_entry - ZeroOrMore(
                eol_comma - list_entry)) - Suppress(']')

            # special case when we have a value assignment where the string can potentially be the remainder of the line
            assign_expr << Group(key - ZeroOrMore(comment_no_comma_eol) - (
                dict_expr | (Literal('=') | Literal(':') | Literal('+=')) -
                ZeroOrMore(comment_no_comma_eol) -
                ConcatenatedValueParser(multi_value_expr)))

            # the file can be { ... } where {} can be omitted or []
            config_expr = ZeroOrMore(comment_eol | eol) + (
                list_expr | root_dict_expr
                | inside_root_dict_expr) + ZeroOrMore(comment_eol | eol_comma)
            config = config_expr.parseString(content, parseAll=True)[0]

            if resolve:
                allow_unresolved = resolve and unresolved_value is not DEFAULT_SUBSTITUTION and unresolved_value is not MANDATORY_SUBSTITUTION
                has_unresolved = cls.resolve_substitutions(
                    config, allow_unresolved)
                if has_unresolved and unresolved_value is MANDATORY_SUBSTITUTION:
                    raise ConfigSubstitutionException(
                        'resolve cannot be set to True and unresolved_value to MANDATORY_SUBSTITUTION'
                    )

            if unresolved_value is not NO_SUBSTITUTION and unresolved_value is not DEFAULT_SUBSTITUTION:
                cls.unresolve_substitutions_to_value(config, unresolved_value)
        return config
Example #39
0
    def _create_tokenizer(self):
        general_comp = oneOf('< > = != <= >=')
        bool_and = Literal('and')
        bool_or = Literal('or')
        bool_not = Literal('not')
        lpar, rpar = map(Suppress, '()')
        tick = Literal("'")
        minus = Literal('-')
        plus = Literal('+')
        mul = Literal('*')
        mod = Literal('mod')
        comma = Suppress(',')

        alphabet = 'абвгдеёжзийклмнопрстуфхцчшщъыьэюя'
        element = (Word(alphabet + alphabet.upper() + nums + '@/:._- ')
                   .setParseAction(self._push))
        integer = Word(nums).setParseAction(self._push)
        floats = (Combine(Word(nums) + '.' + Optional(Word(nums)))
                  .setParseAction(self._push))
        string = Word(alphabet + alphabet.upper() +
                      nums + srange('[a-zA-Z]' + '.'))
        quoted_string = Combine(tick + string + tick).setParseAction(self._push)
        date = Combine(tick + Word(nums, exact=2) + '.' +
                       Word(nums, exact=2) + '.' + Word(nums, exact=4) +
                       tick).setParseAction(self._push)

        expr = Forward()
        node = element + ZeroOrMore(((mul | minus) + element)
                                    .setParseAction(self._push))
        parenthesized_node = Group(lpar + node + rpar)
        node_or_parnode = node | parenthesized_node
        # Переменное (не менее двух) количество узлов, разделённых запятыми
        variadic_node = Group(node_or_parnode + comma + node_or_parnode +
                              ZeroOrMore(comma + node_or_parnode))
        parenthesized_expr = Group(lpar + expr + rpar)

        count_func = Literal('count') + parenthesized_node
        round_func = Literal('round') + parenthesized_node
        sum_func = Literal('sum') + parenthesized_node
        number_func = Literal('number') + parenthesized_expr
        substring_func = (Literal('substring') +
                          Group(lpar + node + comma + integer +
                                Optional(comma + integer) + rpar))
        concat_func = (Literal('concat') + Group(lpar + variadic_node + rpar))
        usch_filename = Literal('usch:getFileName') + Group(lpar + rpar)
        usch_iif = (Literal('usch:iif') +
                   Group(lpar + expr + comma + expr + comma + expr + rpar))
        usch_compare_date = (Literal('usch:compareDate') +
                             Group(lpar + node + comma + node + rpar))
        funcs = (count_func | round_func | sum_func | number_func |
                 substring_func | concat_func | usch_filename |
                 usch_iif | usch_compare_date).setParseAction(self._push)

        atom = (funcs | node | (Optional(bool_not) + parenthesized_expr)
                .setParseAction(self._push_not))
        left_expr = atom + ZeroOrMore(((mul | minus | plus | mod) + atom)
                                      .setParseAction(self._push))

        factor = (left_expr +
                  ZeroOrMore((general_comp +
                              (floats | integer | atom | quoted_string | date))
                             .setParseAction(self._push)))
        term = factor + ZeroOrMore((bool_and + factor)
                                   .setParseAction(self._push))
        expr <<= term + ZeroOrMore((bool_or + term).setParseAction(self._push))
        return expr
Example #40
0
    foreign key
    (student_id) references students(student_id);

alter table only student_registrations
    add constraint classes_link
    foreign key
    (class_id) references classes(class_id);
""".upper()

from pyparsing import Literal, Word, delimitedList \
    , alphas, alphanums \
    , OneOrMore, ZeroOrMore, CharsNotIn \
    , replaceWith

skobki = "(" + ZeroOrMore(CharsNotIn(")")) + ")"
field_def = OneOrMore(Word(alphas, alphanums + "_\"':-") | skobki)


def field_act(s, loc, tok):
    return ("<" + tok[0] + "> " + " ".join(tok)).replace("\"", "\\\"")


field_def.setParseAction(field_act)

field_list_def = delimitedList(field_def)


def field_list_act(toks):
    return " | ".join(toks)


def asFloat(s, l, t):
    return float(t[0])


def asFloatOrInt(s, l, t):
    """ Return an int if possible, otherwise a float"""
    v = t[0]
    try:
        return int(v)
    except ValueError:
        return float(v)


integer = Word("0123456789").setParseAction(asInt)

number = Combine(
    Optional(Word("0123456789")) + Literal(".") + Word("01234567890") |
    integer)
number.setName('number')

sign = oneOf("+ -")

signedNumber = Combine(Optional(sign) + number).setParseAction(asFloat)

lengthValue = Combine(Optional(sign) + number).setParseAction(asFloatOrInt)
lengthValue.setName('lengthValue')

lengthUnit = oneOf(
    ['em', 'ex', 'px', 'pt', 'in', 'cm', 'mm', 'pc', '%'], caseless=True)
Example #42
0
# collection of all data in VCD file (parsed) as Python list items
VCD_alldata = []

SCOPE, VAR, UPSCOPE, END, ENDDEFINITIONS = map(
    Suppress, "$scope $var $upscope $end $enddefinitions".split())

# these in Literal so they show as "indexes"
DATE, VERSION, TIMESCALE, MODULE, WIRE, REG = map(
    Literal, "$date $version $timescale module wire reg".split())

# note [1]
DATE.setParseAction(lambda t: "date")
VERSION.setParseAction(lambda t: "version")
TIMESCALE.setParseAction(lambda t: "timescale")

scope_header = Group(SCOPE + MODULE + Word(printables) + END)
wordortwonotend = (OneOrMore(~END + Word(printables)))
wordortwonotend.setParseAction(
    lambda t: ' '.join(t)
)  # to put possible multi words in the same string (field) [for signal names with spaces]
wire_map = Group(VAR + WIRE + Word(alphanums) + Word(printables) +
                 wordortwonotend + END)
reg_map = Group(VAR + REG + Word(alphanums) + Word(printables) +
                wordortwonotend + END)
var_map = (wire_map | reg_map)
scope_footer = (UPSCOPE + END)
enddefs_footer = (ENDDEFINITIONS + END)
#~ wire_map.setDebug()

# note [3]
var_map.setParseAction(lambda t: VCD_allvars.append(t.asList()[0]))
Example #43
0
#
from pyparsing import Literal, Word, Group, Dict, ZeroOrMore, alphas, nums, delimitedList, pyparsing_common as ppc

testData = """
+-------+------+------+------+------+------+------+------+------+
|       |  A1  |  B1  |  C1  |  D1  |  A2  |  B2  |  C2  |  D2  |
+=======+======+======+======+======+======+======+======+======+
| min   |   7  |  43  |   7  |  15  |  82  |  98  |   1  |  37  |
| max   |  11  |  52  |  10  |  17  |  85  | 112  |   4  |  39  |
| ave   |   9  |  47  |   8  |  16  |  84  | 106  |   3  |  38  |
| sdev  |   1  |   3  |   1  |   1  |   1  |   3  |   1  |   1  |
+-------+------+------+------+------+------+------+------+------+
"""

# define grammar for datatable
underline = Word("-=")
number = ppc.integer

vert = Literal("|").suppress()

rowDelim = ("+" + ZeroOrMore(underline + "+")).suppress()
columnHeader = Group(vert + vert + delimitedList(Word(alphas + nums), "|") +
                     vert)

heading = rowDelim + columnHeader("columns") + rowDelim
rowData = Group(vert + Word(alphas) + vert + delimitedList(number, "|") + vert)
trailing = rowDelim

datatable = heading + Dict(ZeroOrMore(rowData)) + trailing

# now parse data and print results
Example #44
0
def CORBA_IDL_BNF():
    global bnf
    
    if not bnf:

        # punctuation
        colon  = Literal(":")
        lbrace = Literal("{")
        rbrace = Literal("}")
        lbrack = Literal("[")
        rbrack = Literal("]")
        lparen = Literal("(")
        rparen = Literal(")")
        equals = Literal("=")
        comma  = Literal(",")
        dot    = Literal(".")
        slash  = Literal("/")
        bslash = Literal("\\")
        star   = Literal("*")
        semi   = Literal(";")
        langle = Literal("<")
        rangle = Literal(">")
        
        # keywords
        any_       = Keyword("any")
        attribute_ = Keyword("attribute")
        boolean_   = Keyword("boolean")
        case_      = Keyword("case")
        char_      = Keyword("char")
        const_     = Keyword("const")
        context_   = Keyword("context")
        default_   = Keyword("default")
        double_    = Keyword("double")
        enum_      = Keyword("enum")
        exception_ = Keyword("exception")
        false_     = Keyword("FALSE")
        fixed_     = Keyword("fixed")
        float_     = Keyword("float")
        inout_     = Keyword("inout")
        interface_ = Keyword("interface")
        in_        = Keyword("in")
        long_      = Keyword("long")
        module_    = Keyword("module")
        object_    = Keyword("Object")
        octet_     = Keyword("octet")
        oneway_    = Keyword("oneway")
        out_       = Keyword("out")
        raises_    = Keyword("raises")
        readonly_  = Keyword("readonly")
        sequence_  = Keyword("sequence")
        short_     = Keyword("short")
        string_    = Keyword("string")
        struct_    = Keyword("struct")
        switch_    = Keyword("switch")
        true_      = Keyword("TRUE")
        typedef_   = Keyword("typedef")
        unsigned_  = Keyword("unsigned")
        union_     = Keyword("union")
        void_      = Keyword("void")
        wchar_     = Keyword("wchar")
        wstring_   = Keyword("wstring")
        
        identifier = Word( alphas, alphanums + "_" ).setName("identifier")
        
        #~ real = Combine( Word(nums+"+-", nums) + dot + Optional( Word(nums) ) 
                        #~ + Optional( CaselessLiteral("E") + Word(nums+"+-",nums) ) )
        real = Regex(r"[+-]?\d+\.\d*([Ee][+-]?\d+)?").setName("real")
        #~ integer = ( Combine( CaselessLiteral("0x") + Word( nums+"abcdefABCDEF" ) ) |
                    #~ Word( nums+"+-", nums ) ).setName("int")
        integer = Regex(r"0x[0-9a-fA-F]+|[+-]?\d+").setName("int")

        udTypeName = delimitedList( identifier, "::", combine=True ).setName("udType")
        # have to use longest match for type, in case a user-defined type name starts with a keyword type, like "stringSeq" or "longArray"
        typeName = ( any_ ^ boolean_ ^ char_ ^ double_ ^ fixed_ ^ 
                    float_ ^ long_ ^ octet_ ^ short_ ^ string_ ^ 
                    wchar_ ^ wstring_ ^ udTypeName ).setName("type")
        sequenceDef = Forward().setName("seq")
        sequenceDef << Group( sequence_ + langle + ( sequenceDef | typeName ) + rangle )
        typeDef = sequenceDef | ( typeName + Optional( lbrack + integer + rbrack ) )
        typedefDef = Group( typedef_ + typeDef + identifier + semi ).setName("typedef")

        moduleDef = Forward()
        constDef = Group( const_ + typeDef + identifier + equals + ( real | integer | quotedString ) + semi ) #| quotedString )
        exceptionItem = Group( typeDef + identifier + semi )
        exceptionDef = ( exception_ + identifier + lbrace + ZeroOrMore( exceptionItem ) + rbrace + semi )
        attributeDef = Optional( readonly_ ) + attribute_ + typeDef + identifier + semi
        paramlist = delimitedList( Group( ( inout_ | in_ | out_ ) + typeName + identifier ) ).setName( "paramlist" )
        operationDef = ( ( void_ ^ typeDef ) + identifier + lparen + Optional( paramlist ) + rparen + \
                        Optional( raises_ + lparen + Group( delimitedList( typeName ) ) + rparen ) + semi )
        interfaceItem = ( constDef | exceptionDef | attributeDef | operationDef )
        interfaceDef = Group( interface_ + identifier  + Optional( colon + delimitedList( typeName ) ) + lbrace + \
                        ZeroOrMore( interfaceItem ) + rbrace + semi ).setName("opnDef")
        moduleItem = ( interfaceDef | exceptionDef | constDef | typedefDef | moduleDef )
        moduleDef << module_ + identifier + lbrace + ZeroOrMore( moduleItem ) + rbrace + semi

        bnf = ( moduleDef | OneOrMore( moduleItem ) )
        
        singleLineComment = "//" + restOfLine
        bnf.ignore( singleLineComment )
        bnf.ignore( cStyleComment )
        
    return bnf
Example #45
0
def _create_grammar():
    """Create the DBC grammar.

    """

    word = Word(printables.replace(';', '').replace(':', ''))
    integer = Group(Optional('-') + Word(nums))
    positive_integer = Word(nums).setName('positive integer')
    number = Word(nums + '.Ee-+')
    colon = Suppress(Literal(':'))
    scolon = Suppress(Literal(';'))
    pipe = Suppress(Literal('|'))
    at = Suppress(Literal('@'))
    sign = Literal('+') | Literal('-')
    lp = Suppress(Literal('('))
    rp = Suppress(Literal(')'))
    lb = Suppress(Literal('['))
    rb = Suppress(Literal(']'))
    comma = Suppress(Literal(','))
    node = Word(alphas + nums + '_-').setWhitespaceChars(' ')
    frame_id = Word(nums).setName('frame id')

    version = Group(Keyword('VERSION') - QuotedString('"', multiline=True))
    version.setName(VERSION)

    symbol = Word(alphas + '_') + Suppress(LineEnd())

    symbols = Group(Keyword('NS_') - colon - Group(ZeroOrMore(symbol)))
    symbols.setName('NS_')

    discard = Suppress(Keyword('BS_') - colon).setName('BS_')

    nodes = Group(Keyword('BU_') - colon - Group(ZeroOrMore(node)))
    nodes.setName('BU_')

    signal = Group(
        Keyword(SIGNAL) - Group(word + Optional(word)) - colon -
        Group(positive_integer - pipe - positive_integer - at -
              positive_integer - sign) -
        Group(lp - number - comma - number - rp) -
        Group(lb - number - pipe - number - rb) -
        QuotedString('"', multiline=True) - Group(delimitedList(node)))
    signal.setName(SIGNAL)

    message = Group(
        Keyword(MESSAGE) - frame_id - word - colon - positive_integer - word -
        Group(ZeroOrMore(signal)))
    message.setName(MESSAGE)

    event = Suppress(
        Keyword(EVENT) - word - colon - positive_integer - lb - number - pipe -
        number - rb - QuotedString('"', multiline=True) - number - number -
        word - node - scolon)
    event.setName(EVENT)

    comment = Group(
        Keyword(COMMENT) -
        ((Keyword(MESSAGE) - frame_id - QuotedString('"', multiline=True) -
          scolon).setName(MESSAGE)
         | (Keyword(SIGNAL) - frame_id - word -
            QuotedString('"', multiline=True) - scolon).setName(SIGNAL)
         | (Keyword(NODES) - word - QuotedString('"', multiline=True) -
            scolon).setName(NODES)
         | (Keyword(EVENT) - word - QuotedString('"', multiline=True) -
            scolon).setName(EVENT)
         |
         (QuotedString('"', multiline=True) - scolon).setName('QuotedString')))
    comment.setName(COMMENT)

    attribute_definition = Group(
        Keyword(ATTRIBUTE_DEFINITION) -
        ((QuotedString('"', multiline=True))
         | (Keyword(SIGNAL)
            | Keyword(MESSAGE)
            | Keyword(EVENT)
            | Keyword(NODES)) + QuotedString('"', multiline=True)) - word -
        (scolon
         | (Group(
             ZeroOrMore(
                 Group((comma | Empty()) +
                       QuotedString('"', multiline=True)))) + scolon)
         | (Group(ZeroOrMore(number)) + scolon)))
    attribute_definition.setName(ATTRIBUTE_DEFINITION)

    attribute_definition_default = Group(
        Keyword(ATTRIBUTE_DEFINITION_DEFAULT) -
        QuotedString('"', multiline=True) -
        (number | QuotedString('"', multiline=True)) - scolon)
    attribute_definition_default.setName(ATTRIBUTE_DEFINITION_DEFAULT)

    attribute = Group(
        Keyword(ATTRIBUTE) - QuotedString('"', multiline=True) - Group(
            Optional((Keyword(MESSAGE) + frame_id)
                     | (Keyword(SIGNAL) + frame_id + word)
                     | (Keyword(NODES) + word))) -
        (QuotedString('"', multiline=True) | number) - scolon)
    attribute.setName(ATTRIBUTE)

    choice = Group(
        Keyword(CHOICE) - Group(Optional(frame_id)) - word -
        Group(OneOrMore(Group(integer + QuotedString('"', multiline=True)))) -
        scolon)
    choice.setName(CHOICE)

    value_table = Group(
        Keyword(VALUE_TABLE) - word -
        Group(OneOrMore(Group(integer + QuotedString('"', multiline=True)))) -
        scolon)
    value_table.setName(VALUE_TABLE)

    signal_type = Group(
        Keyword(SIGNAL_TYPE) - frame_id - word - colon - positive_integer -
        scolon)
    signal_type.setName(SIGNAL_TYPE)

    signal_multiplexer_values = Group(
        Keyword(SIGNAL_MULTIPLEXER_VALUES) - frame_id - word - word - Group(
            delimitedList(positive_integer - Suppress('-') -
                          Suppress(positive_integer))) - scolon)
    signal_multiplexer_values.setName(SIGNAL_MULTIPLEXER_VALUES)

    message_add_sender = Group(
        Keyword(MESSAGE_TX_NODE) - frame_id - colon -
        Group(delimitedList(node)) - scolon)
    message_add_sender.setName(MESSAGE_TX_NODE)

    attribute_definition_rel = Group(
        Keyword(ATTRIBUTE_DEFINITION_REL) -
        (QuotedString('"', multiline=True)
         | (Keyword(NODES_REL) + QuotedString('"', multiline=True))) - word -
        (scolon
         | (Group(
             ZeroOrMore(
                 Group((comma | Empty()) +
                       QuotedString('"', multiline=True)))) + scolon)
         | (Group(ZeroOrMore(number)) + scolon)))
    attribute_definition_rel.setName(ATTRIBUTE_DEFINITION_REL)

    attribute_definition_default_rel = Group(
        Keyword(ATTRIBUTE_DEFINITION_DEFAULT_REL) -
        QuotedString('"', multiline=True) -
        (number | QuotedString('"', multiline=True)) - scolon)
    attribute_definition_default_rel.setName(ATTRIBUTE_DEFINITION_DEFAULT_REL)

    attribute_rel = Group(
        Keyword(ATTRIBUTE_REL) - QuotedString('"', multiline=True) -
        Keyword(NODES_REL) - word - Keyword(SIGNAL) - frame_id - word -
        (positive_integer | QuotedString('"')) - scolon)
    attribute_rel.setName(ATTRIBUTE_REL)

    signal_group = Group(
        Keyword(SIGNAL_GROUP) - frame_id - word - integer - colon -
        OneOrMore(word) - scolon)
    signal_group.setName(SIGNAL_GROUP)

    entry = (version
             | symbols
             | discard
             | nodes
             | message
             | comment
             | attribute_definition
             | attribute_definition_default
             | attribute
             | choice
             | value_table
             | signal_type
             | signal_multiplexer_values
             | message_add_sender
             | attribute_definition_rel
             | attribute_definition_default_rel
             | attribute_rel
             | signal_group
             | event)

    frame_id.setParseAction(lambda _s, _l, t: int(t[0]))

    return OneOrMore(entry) + StringEnd()
Example #46
0

def checkLine_part2(strt, stp, char, passwd):
    # check whether line corresponds to a valid passwordin part 1
    val1 = passwd[strt - 1]  # password char @ position 1
    val2 = passwd[stp - 1]  # ==     //      ==        2
    # a valid password is one where one password char matches the given char and the other one not (and vice versa => therefore two conditions combined via OR)
    res = ((val1 == char) and (val2 != char)) or ((val1 != char) and
                                                  (val2 == char))
    return (res)


# we want to parse the format used in AoC 2020, day 2

# this matches one line
grmr_line = Word(nums) + "-" + Word(nums) + Word(alphas) + ":" + Word(alphas)

cnt_part1 = 0
cnt_part2 = 0

f = open('AoC_2020_P02_input.txt')
#f = open('AoC_2020_P02_test.txt')

for line in f:
    res = grmr_line.parseString(line)
    # print(res)
    chk_part1 = checkLine_part1(int(res[0]), int(res[2]), res[3], res[5])
    chk_part2 = checkLine_part2(int(res[0]), int(res[2]), res[3], res[5])
    print(chk_part2)
    if (chk_part1 == True):
        cnt_part1 = cnt_part1 + 1
Example #47
0
        print("pushing ", toks[0], "str is ", str)
    exprStack.append(toks[0])


def _assignVar(str, loc, toks):
    global targetvar
    targetvar = toks[0]


# -----------------------------------------------------------------------------
# The following statements define the grammar for the parser.

point = Literal(".")
e = CaselessLiteral("E")
plusorminus = Literal("+") | Literal("-")
number = Word(nums)
integer = Combine(Optional(plusorminus) + number)
floatnumber = Combine(integer + Optional(point + Optional(number)) +
                      Optional(e + integer))

lbracket = Literal("[")
rbracket = Literal("]")
ident = Forward()
## The definition below treats array accesses as identifiers. This means your expressions
## can include references to array elements, rows and columns, e.g., a = b[i] + 5.
## Expressions within []'s are not presently supported, so a = b[i+1] will raise
## a ParseException.
ident = Combine(
    Word(alphas + "-", alphanums + "_") +
    ZeroOrMore(lbracket +
               (Word(alphas + "-", alphanums + "_") | integer) + rbracket))
Example #48
0
SEMI = Literal(";").suppress()
COLON = Literal(":").suppress()

EVENT = Literal("VEVENT").suppress()
CALENDAR = Literal("VCALENDAR").suppress()
ALARM = Literal("VALARM").suppress()

# TOKENS

CALPROP = oneOf("VERSION PRODID METHOD")
ALMPROP = oneOf("TRIGGER")
EVTPROP = oneOf("X-MOZILLA-RECUR-DEFAULT-INTERVAL \
                     X-MOZILLA-RECUR-DEFAULT-UNITS \
                     UID DTSTAMP LAST-MODIFIED X RRULE EXDATE")

propval = Word(valstr)
typeval = Word(valstr)
typename = oneOf("VALUE MEMBER FREQ UNTIL INTERVAL")

proptype = Group(SEMI + typename + EQ + typeval).suppress()

calprop = Group(CALPROP + ZeroOrMore(proptype) + COLON + propval)
almprop = Group(ALMPROP + ZeroOrMore(proptype) + COLON + propval)
evtprop = Group(EVTPROP + ZeroOrMore(proptype) + COLON + propval).suppress() \
   | "CATEGORIES" + COLON + propval.setResultsName("categories") \
   | "CLASS" + COLON + propval.setResultsName("class") \
   | "DESCRIPTION" + COLON + propval.setResultsName("description") \
   | "DTSTART" + proptype + COLON + propval.setResultsName("begin") \
   | "DTEND" + proptype + COLON + propval.setResultsName("end") \
   | "LOCATION" + COLON + propval.setResultsName("location") \
   | "PRIORITY" + COLON + propval.setResultsName("priority") \
Example #49
0
from pyparsing import (  # noqa
    Combine, Literal as L, Optional, ParseException, Regex, Word, ZeroOrMore,
    originalTextFor, stringEnd, stringStart,
)

from .markers import MARKER_EXPR, Marker
from .specifiers import LegacySpecifier, Specifier, SpecifierSet


class InvalidRequirement(ValueError):
    """
    An invalid requirement was found, users should refer to PEP 508.
    """


ALPHANUM = Word(string.ascii_letters + string.digits)

LBRACKET = L("[").suppress()
RBRACKET = L("]").suppress()
LPAREN = L("(").suppress()
RPAREN = L(")").suppress()
COMMA = L(",").suppress()
SEMICOLON = L(";").suppress()
AT = L("@").suppress()

PUNCTUATION = Word("-_.")
IDENTIFIER_END = ALPHANUM | (ZeroOrMore(PUNCTUATION) + ALPHANUM)
IDENTIFIER = Combine(ALPHANUM + ZeroOrMore(IDENTIFIER_END))

NAME = IDENTIFIER("name")
EXTRA = IDENTIFIER
Example #50
0
FLOAT = 'float'
INTEGER = 'integer'
BOOLEAN = 'boolean'

# Definitions of equation structure for parsing with pyparsing
# TODO: Maybe move them somewhere else to not pollute the namespace here?
#       Only IDENTIFIER and EQUATIONS are ever used later
###############################################################################
# Basic Elements
###############################################################################

# identifiers like in C: can start with letter or underscore, then a
# combination of letters, numbers and underscores
# Note that the check_identifiers function later performs more checks, e.g.
# names starting with underscore should only be used internally
IDENTIFIER = Word(string.ascii_letters + '_', string.ascii_letters +
                  string.digits + '_').setResultsName('identifier')

# very broad definition here, expression will be analysed by sympy anyway
# allows for multi-line expressions, where each line can have comments
EXPRESSION = Combine(OneOrMore(
    (CharsNotIn(':#\n') + Suppress(Optional(LineEnd()))).ignore('#' +
                                                                restOfLine)),
                     joinString=' ').setResultsName('expression')

# a unit
# very broad definition here, again. Whether this corresponds to a valid unit
# string will be checked later
UNIT = Word(string.ascii_letters + string.digits +
            '*/.- ').setResultsName('unit')

# a single Flag (e.g. "const" or "event-driven")
Example #51
0
from distutils.version import StrictVersion
from pyparsing import (ParserElement, Forward, Combine, Optional, Word,
                       Literal, CaselessKeyword, CaselessLiteral, Group,
                       FollowedBy, LineEnd, OneOrMore, ZeroOrMore, nums,
                       alphas, alphanums, printables, delimitedList,
                       quotedString, __version__)

ParserElement.enablePackrat()
grammar = Forward()

expression = Forward()

# Literals
intNumber = Combine(Optional('-') + Word(nums))('integer')

floatNumber = Combine(Optional('-') + Word(nums) + Literal('.') +
                      Word(nums))('float')

sciNumber = Combine((floatNumber | intNumber) + CaselessLiteral('e') +
                    intNumber)('scientific')

aString = quotedString('string')

# Use lookahead to match only numbers in a list (can't remember why this
# is necessary)
afterNumber = FollowedBy(",") ^ FollowedBy(")") ^ FollowedBy(LineEnd())
number = Group((sciNumber + afterNumber) | (floatNumber + afterNumber)
               | (intNumber + afterNumber))('number')

boolean = Group(CaselessKeyword("true") | CaselessKeyword("false"))('boolean')
Example #52
0
def Verilog_BNF():
    global verilogbnf

    if verilogbnf is None:

        # compiler directives
        compilerDirective = Combine( "`" + \
            oneOf("define undef ifdef else endif default_nettype "
                  "include resetall timescale unconnected_drive "
                  "nounconnected_drive celldefine endcelldefine") + \
            restOfLine ).setName("compilerDirective")

        # primitives
        SEMI, COLON, LPAR, RPAR, LBRACE, RBRACE, LBRACK, RBRACK, DOT, COMMA, EQ = map(
            Literal, ";:(){}[].,=")

        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")  #.setDebug()
        identifier = identifier1 | identifier2
        assert (identifier2 == r'\abc')

        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( DOT + 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(LBRACE + delimitedList(expr) + RBRACE)
        multiConcat = Group("{" + expr + concat + "}").setName("multiConcat")
        funcCall = Group(identifier + LPAR + Optional(delimitedList(expr)) +
                         RPAR).setName("funcCall")

        subscrRef = Group(LBRACK + delimitedList(expr, COLON) + RBRACK)
        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 + COLON + expr + COLON +
                              expr).setName("mintypmax")
        primary = (number | (LPAR + mintypmaxExpr + RPAR) |
                   (LPAR + Group(expr) + RPAR).setName("nestedExpr")
                   | multiConcat | concat | dblQuotedString | funcCall
                   | subscrIdentifier)

        unop = oneOf("+  -  !  ~  &  ~&  |  ^|  ^  ~^").setName("unop")
        binop = oneOf(
            "+  -  *  /  %  ==  !=  ===  !==  &&  "
            "||  <  <=  >  >=  &  |  ^  ^~  >>  << ** <<< >>>").setName(
                "binop")

        expr << ((unop + expr) |  # must be first!
                 (primary + "?" + expr + COLON + expr) |
                 (primary + Optional(binop + expr)))

        lvalue = subscrIdentifier | concat

        # keywords
        if_ = Keyword("if")
        else_ = Keyword("else")
        edge = Keyword("edge")
        posedge = Keyword("posedge")
        negedge = Keyword("negedge")
        specify = Keyword("specify")
        endspecify = Keyword("endspecify")
        fork = Keyword("fork")
        join = Keyword("join")
        begin = Keyword("begin")
        end = Keyword("end")
        default = Keyword("default")
        forever = Keyword("forever")
        repeat = Keyword("repeat")
        while_ = Keyword("while")
        for_ = Keyword("for")
        case = oneOf("case casez casex")
        endcase = Keyword("endcase")
        wait = Keyword("wait")
        disable = Keyword("disable")
        deassign = Keyword("deassign")
        force = Keyword("force")
        release = Keyword("release")
        assign = Keyword("assign")

        eventExpr = Forward()
        eventTerm = (posedge + expr) | (negedge + expr) | expr | (
            LPAR + eventExpr + RPAR)
        eventExpr << (Group(delimitedList(eventTerm, Keyword("or"))))
        eventControl = Group("@" + (
            (LPAR + eventExpr + RPAR) | identifier | "*")).setName("eventCtrl")

        delayArg = (
            number | Word(alphanums + "$_") |  #identifier |
            (LPAR + Group(delimitedList(mintypmaxExpr | expr)) +
             RPAR)).setName("delayArg")  #.setDebug()
        delay = Group("#" + delayArg).setName("delay")  #.setDebug()
        delayOrEventControl = delay | eventControl

        assgnmt = Group(lvalue + EQ + Optional(delayOrEventControl) +
                        expr).setName("assgnmt")
        nbAssgnmt = Group((lvalue + "<=" + Optional(delay) + expr)
                          | (lvalue + "<=" + Optional(eventControl) +
                             expr)).setName("nbassgnmt")

        range = LBRACK + expr + COLON + expr + RBRACK

        paramAssgnmt = Group(identifier + EQ + expr).setName("paramAssgnmt")
        parameterDecl = Group("parameter" + Optional(range) +
                              delimitedList(paramAssgnmt) +
                              SEMI).setName("paramDecl")

        inputDecl = Group("input" + Optional(range) +
                          delimitedList(identifier) + SEMI)
        outputDecl = Group("output" + Optional(range) +
                           delimitedList(identifier) + SEMI)
        inoutDecl = Group("inout" + Optional(range) +
                          delimitedList(identifier) + SEMI)

        regIdentifier = Group(identifier +
                              Optional(LBRACK + expr + COLON + expr + RBRACK))
        regDecl = Group("reg" + Optional("signed") + Optional(range) +
                        delimitedList(regIdentifier) + SEMI).setName("regDecl")
        timeDecl = Group("time" + delimitedList(regIdentifier) + SEMI)
        integerDecl = Group("integer" + delimitedList(regIdentifier) + SEMI)

        strength0 = oneOf("supply0  strong0  pull0  weak0  highz0")
        strength1 = oneOf("supply1  strong1  pull1  weak1  highz1")
        driveStrength = Group(LPAR + ((strength0 + COMMA + strength1)
                                      | (strength1 + COMMA + strength0)) +
                              RPAR).setName("driveStrength")
        nettype = oneOf(
            "wire  tri  tri1  supply0  wand  triand  tri0  supply1  wor  trior  trireg"
        )
        expandRange = Optional(oneOf("scalared vectored")) + range
        realDecl = Group("real" + delimitedList(identifier) + SEMI)

        eventDecl = Group("event" + delimitedList(identifier) + SEMI)

        blockDecl = (parameterDecl | regDecl | integerDecl | realDecl
                     | timeDecl | eventDecl)

        stmt = Forward().setName("stmt")  #.setDebug()
        stmtOrNull = stmt | SEMI
        caseItem = ( delimitedList( expr ) + COLON + stmtOrNull ) | \
                   ( default + Optional(":") + stmtOrNull )
        stmt << Group(
            (begin + Group(ZeroOrMore(stmt)) + end).setName("begin-end")
            | (if_ + Group(LPAR + expr + RPAR) + stmtOrNull +
               Optional(else_ + stmtOrNull)).setName("if")
            | (delayOrEventControl + stmtOrNull)
            | (case + LPAR + expr + RPAR + OneOrMore(caseItem) + endcase)
            | (forever + stmt) | (repeat + LPAR + expr + RPAR + stmt)
            | (while_ + LPAR + expr + RPAR + stmt)
            | (for_ + LPAR + assgnmt + SEMI + Group(expr) + SEMI + assgnmt +
               RPAR + stmt) | (fork + ZeroOrMore(stmt) + join) |
            (fork + COLON + identifier + ZeroOrMore(blockDecl) +
             ZeroOrMore(stmt) + end) | (wait + LPAR + expr + RPAR + stmtOrNull)
            | ("->" + identifier + SEMI) | (disable + identifier + SEMI)
            | (assign + assgnmt + SEMI) | (deassign + lvalue + SEMI)
            | (force + assgnmt + SEMI) | (release + lvalue + SEMI)
            | (begin + COLON + identifier + ZeroOrMore(blockDecl) +
               ZeroOrMore(stmt) + end).setName("begin:label-end") |
            # these  *have* to go at the end of the list!!!
            (assgnmt + SEMI) | (nbAssgnmt + SEMI)
            | (Combine(Optional("$") + identifier) +
               Optional(LPAR + delimitedList(expr | empty) + RPAR) +
               SEMI)).setName("stmtBody")
        """
        x::=<blocking_assignment> ;
        x||= <non_blocking_assignment> ;
        x||= if ( <expression> ) <statement_or_null>
        x||= if ( <expression> ) <statement_or_null> else <statement_or_null>
        x||= case ( <expression> ) <case_item>+ endcase
        x||= casez ( <expression> ) <case_item>+ endcase
        x||= casex ( <expression> ) <case_item>+ endcase
        x||= forever <statement>
        x||= repeat ( <expression> ) <statement>
        x||= while ( <expression> ) <statement>
        x||= for ( <assignment> ; <expression> ; <assignment> ) <statement>
        x||= <delay_or_event_control> <statement_or_null>
        x||= wait ( <expression> ) <statement_or_null>
        x||= -> <name_of_event> ;
        x||= <seq_block>
        x||= <par_block>
        x||= <task_enable>
        x||= <system_task_enable>
        x||= disable <name_of_task> ;
        x||= disable <name_of_block> ;
        x||= assign <assignment> ;
        x||= deassign <lvalue> ;
        x||= force <assignment> ;
        x||= release <lvalue> ;
        """
        alwaysStmt = Group("always" + Optional(eventControl) +
                           stmt).setName("alwaysStmt")
        initialStmt = Group("initial" + stmt).setName("initialStmt")

        chargeStrength = Group(LPAR + oneOf("small medium large") +
                               RPAR).setName("chargeStrength")

        continuousAssign = Group(assign + Optional(driveStrength) +
                                 Optional(delay) + delimitedList(assgnmt) +
                                 SEMI).setName("continuousAssign")

        tfDecl = (parameterDecl | inputDecl | outputDecl | inoutDecl | regDecl
                  | timeDecl | integerDecl | realDecl)

        functionDecl = Group("function" +
                             Optional(range | "integer" | "real") +
                             identifier + SEMI + Group(OneOrMore(tfDecl)) +
                             Group(ZeroOrMore(stmt)) + "endfunction")

        inputOutput = oneOf("input output")
        netDecl1Arg = (nettype + Optional(expandRange) + Optional(delay) +
                       Group(delimitedList(~inputOutput + identifier)))
        netDecl2Arg = ("trireg" + Optional(chargeStrength) +
                       Optional(expandRange) + Optional(delay) +
                       Group(delimitedList(~inputOutput + identifier)))
        netDecl3Arg = (nettype + Optional(driveStrength) +
                       Optional(expandRange) + Optional(delay) +
                       Group(delimitedList(assgnmt)))
        netDecl1 = Group(netDecl1Arg + SEMI).setName("netDecl1")
        netDecl2 = Group(netDecl2Arg + SEMI).setName("netDecl2")
        netDecl3 = Group(netDecl3Arg + SEMI).setName("netDecl3")

        gateType = oneOf("and  nand  or  nor xor  xnor buf  bufif0 bufif1 "
                         "not  notif0 notif1  pulldown pullup nmos  rnmos "
                         "pmos rpmos cmos rcmos   tran rtran  tranif0  "
                         "rtranif0  tranif1 rtranif1")
        gateInstance = Optional( Group( identifier + Optional( range ) ) ) + \
                        LPAR + Group( delimitedList( expr ) ) + RPAR
        gateDecl = Group(gateType + Optional(driveStrength) + Optional(delay) +
                         delimitedList(gateInstance) + SEMI)

        udpInstance = Group(
            Group(identifier + Optional(range | subscrRef)) + LPAR +
            Group(delimitedList(expr)) + RPAR)
        udpInstantiation = Group(identifier - Optional(driveStrength) +
                                 Optional(delay) + delimitedList(udpInstance) +
                                 SEMI).setName("udpInstantiation")

        parameterValueAssignment = Group(
            Literal("#") + LPAR + Group(delimitedList(expr)) + RPAR)
        namedPortConnection = Group(DOT + identifier + LPAR +
                                    expr + RPAR).setName(
                                        "namedPortConnection")  #.setDebug()
        assert (r'.\abc (abc )' == namedPortConnection)
        modulePortConnection = expr | empty
        #~ moduleInstance = Group( Group ( identifier + Optional(range) ) +
        #~ ( delimitedList( modulePortConnection ) |
        #~ delimitedList( namedPortConnection ) ) )
        inst_args = Group(LPAR + (delimitedList(namedPortConnection)
                                  | delimitedList(modulePortConnection)) +
                          RPAR).setName("inst_args")
        moduleInstance = Group(
            Group(identifier + Optional(range)) + inst_args).setName(
                "moduleInstance")  #.setDebug()

        moduleInstantiation = Group(
            identifier + Optional(parameterValueAssignment) +
            delimitedList(moduleInstance).setName("moduleInstanceList") +
            SEMI).setName("moduleInstantiation")

        parameterOverride = Group("defparam" + delimitedList(paramAssgnmt) +
                                  SEMI)
        task = Group("task" + identifier + SEMI + ZeroOrMore(tfDecl) +
                     stmtOrNull + "endtask")

        specparamDecl = Group("specparam" + delimitedList(paramAssgnmt) + SEMI)

        pathDescr1 = Group(LPAR + subscrIdentifier + "=>" + subscrIdentifier +
                           RPAR)
        pathDescr2 = Group(LPAR + Group(delimitedList(subscrIdentifier)) +
                           "*>" + Group(delimitedList(subscrIdentifier)) +
                           RPAR)
        pathDescr3 = Group(LPAR + Group(delimitedList(subscrIdentifier)) +
                           "=>" + Group(delimitedList(subscrIdentifier)) +
                           RPAR)
        pathDelayValue = Group((
            LPAR + Group(delimitedList(mintypmaxExpr | expr)) + RPAR)
                               | mintypmaxExpr | expr)
        pathDecl = Group((pathDescr1 | pathDescr2 | pathDescr3) + EQ +
                         pathDelayValue + SEMI).setName("pathDecl")

        portConditionExpr = Forward()
        portConditionTerm = Optional(unop) + subscrIdentifier
        portConditionExpr << portConditionTerm + Optional(binop +
                                                          portConditionExpr)
        polarityOp = oneOf("+ -")
        levelSensitivePathDecl1 = Group(if_ + Group(LPAR + portConditionExpr +
                                                    RPAR) + subscrIdentifier +
                                        Optional(polarityOp) + "=>" +
                                        subscrIdentifier + EQ +
                                        pathDelayValue + SEMI)
        levelSensitivePathDecl2 = Group(
            if_ + Group(LPAR + portConditionExpr + RPAR) + LPAR +
            Group(delimitedList(subscrIdentifier)) + Optional(polarityOp) +
            "*>" + Group(delimitedList(subscrIdentifier)) + RPAR + EQ +
            pathDelayValue + SEMI)
        levelSensitivePathDecl = levelSensitivePathDecl1 | levelSensitivePathDecl2

        edgeIdentifier = posedge | negedge
        edgeSensitivePathDecl1 = Group(
            Optional(if_ + Group(LPAR + expr + RPAR)) + LPAR +
            Optional(edgeIdentifier) + subscrIdentifier + "=>" + LPAR +
            subscrIdentifier + Optional(polarityOp) + COLON + expr + RPAR +
            RPAR + EQ + pathDelayValue + SEMI)
        edgeSensitivePathDecl2 = Group(
            Optional(if_ + Group(LPAR + expr + RPAR)) + LPAR +
            Optional(edgeIdentifier) + subscrIdentifier + "*>" + LPAR +
            delimitedList(subscrIdentifier) + Optional(polarityOp) + COLON +
            expr + RPAR + RPAR + EQ + pathDelayValue + SEMI)
        edgeSensitivePathDecl = edgeSensitivePathDecl1 | edgeSensitivePathDecl2

        edgeDescr = oneOf("01 10 0x x1 1x x0").setName("edgeDescr")

        timCheckEventControl = Group(posedge | negedge
                                     | (edge + LBRACK +
                                        delimitedList(edgeDescr) + RBRACK))
        timCheckCond = Forward()
        timCondBinop = oneOf("== === != !==")
        timCheckCondTerm = (expr + timCondBinop +
                            scalarConst) | (Optional("~") + expr)
        timCheckCond << ((LPAR + timCheckCond + RPAR) | timCheckCondTerm)
        timCheckEvent = Group(
            Optional(timCheckEventControl) + subscrIdentifier +
            Optional("&&&" + timCheckCond))
        timCheckLimit = expr
        controlledTimingCheckEvent = Group(timCheckEventControl +
                                           subscrIdentifier +
                                           Optional("&&&" + timCheckCond))
        notifyRegister = identifier

        systemTimingCheck1 = Group("$setup" + LPAR + timCheckEvent + COMMA +
                                   timCheckEvent + COMMA + timCheckLimit +
                                   Optional(COMMA + notifyRegister) + RPAR +
                                   SEMI)
        systemTimingCheck2 = Group("$hold" + LPAR + timCheckEvent + COMMA +
                                   timCheckEvent + COMMA + timCheckLimit +
                                   Optional(COMMA + notifyRegister) + RPAR +
                                   SEMI)
        systemTimingCheck3 = Group("$period" + LPAR +
                                   controlledTimingCheckEvent + COMMA +
                                   timCheckLimit +
                                   Optional(COMMA + notifyRegister) + RPAR +
                                   SEMI)
        systemTimingCheck4 = Group("$width" + LPAR +
                                   controlledTimingCheckEvent + COMMA +
                                   timCheckLimit +
                                   Optional(COMMA + expr + COMMA +
                                            notifyRegister) + RPAR + SEMI)
        systemTimingCheck5 = Group("$skew" + LPAR + timCheckEvent + COMMA +
                                   timCheckEvent + COMMA + timCheckLimit +
                                   Optional(COMMA + notifyRegister) + RPAR +
                                   SEMI)
        systemTimingCheck6 = Group("$recovery" + LPAR +
                                   controlledTimingCheckEvent + COMMA +
                                   timCheckEvent + COMMA + timCheckLimit +
                                   Optional(COMMA + notifyRegister) + RPAR +
                                   SEMI)
        systemTimingCheck7 = Group("$setuphold" + LPAR + timCheckEvent +
                                   COMMA + timCheckEvent + COMMA +
                                   timCheckLimit + COMMA + timCheckLimit +
                                   Optional(COMMA + notifyRegister) + RPAR +
                                   SEMI)
        systemTimingCheck = (
            FollowedBy('$') +
            (systemTimingCheck1 | systemTimingCheck2 | systemTimingCheck3
             | systemTimingCheck4 | systemTimingCheck5 | systemTimingCheck6
             | systemTimingCheck7)).setName("systemTimingCheck")
        sdpd = if_ + Group(LPAR + expr + RPAR) + \
            ( pathDescr1 | pathDescr2 ) + EQ + pathDelayValue + SEMI

        specifyItem = ~Keyword("endspecify") + (
            specparamDecl | pathDecl | levelSensitivePathDecl
            | edgeSensitivePathDecl | systemTimingCheck | sdpd)
        """
        x::= <specparam_declaration>
        x||= <path_declaration>
        x||= <level_sensitive_path_declaration>
        x||= <edge_sensitive_path_declaration>
        x||= <system_timing_check>
        x||= <sdpd>
        """
        specifyBlock = Group("specify" + ZeroOrMore(specifyItem) +
                             "endspecify").setName("specifyBlock")

        moduleItem = ~Keyword("endmodule") + (
            parameterDecl | inputDecl | outputDecl | inoutDecl | regDecl |
            netDecl3 | netDecl1 | netDecl2 | timeDecl | integerDecl | realDecl
            | eventDecl | gateDecl | parameterOverride | continuousAssign
            | specifyBlock | initialStmt | alwaysStmt | task | functionDecl |
            # these have to be at the end - they start with identifiers
            moduleInstantiation | udpInstantiation)
        """  All possible moduleItems, from Verilog grammar spec
        x::= <parameter_declaration>
        x||= <input_declaration>
        x||= <output_declaration>
        x||= <inout_declaration>
        ?||= <net_declaration>  (spec does not seem consistent for this item)
        x||= <reg_declaration>
        x||= <time_declaration>
        x||= <integer_declaration>
        x||= <real_declaration>
        x||= <event_declaration>
        x||= <gate_declaration>
        x||= <UDP_instantiation>
        x||= <module_instantiation>
        x||= <parameter_override>
        x||= <continuous_assign>
        x||= <specify_block>
        x||= <initial_statement>
        x||= <always_statement>
        x||= <task>
        x||= <function>
        """
        portRef = subscrIdentifier
        portExpr = portRef | Group(LBRACE + delimitedList(portRef) + RBRACE)
        port = portExpr | Group((DOT + identifier + LPAR + portExpr + RPAR))

        moduleHdr = Group(
            oneOf("module macromodule") + identifier + Optional(LPAR + Group(
                Optional(
                    delimitedList(
                        Group(
                            oneOf("input output") +
                            (netDecl1Arg | netDecl2Arg | netDecl3Arg))
                        | port))) + RPAR) + SEMI).setName("moduleHdr")

        module = Group(moduleHdr + Group(ZeroOrMore(moduleItem)) +
                       "endmodule").setName("module")  #.setDebug()

        udpDecl = outputDecl | inputDecl | regDecl
        #~ udpInitVal = oneOf("1'b0 1'b1 1'bx 1'bX 1'B0 1'B1 1'Bx 1'BX 1 0 x X")
        udpInitVal = (Regex("1'[bB][01xX]")
                      | Regex("[01xX]")).setName("udpInitVal")
        udpInitialStmt = Group("initial" + identifier + EQ + udpInitVal +
                               SEMI).setName("udpInitialStmt")

        levelSymbol = oneOf("0   1   x   X   ?   b   B")
        levelInputList = Group(OneOrMore(levelSymbol).setName("levelInpList"))
        outputSymbol = oneOf("0   1   x   X")
        combEntry = Group(levelInputList + COLON + outputSymbol + SEMI)
        edgeSymbol = oneOf("r   R   f   F   p   P   n   N   *")
        edge = Group( LPAR + levelSymbol + levelSymbol + RPAR ) | \
               Group( edgeSymbol )
        edgeInputList = Group(
            ZeroOrMore(levelSymbol) + edge + ZeroOrMore(levelSymbol))
        inputList = levelInputList | edgeInputList
        seqEntry = Group(inputList + COLON + levelSymbol + COLON +
                         (outputSymbol | "-") + SEMI).setName("seqEntry")
        udpTableDefn = Group("table" + OneOrMore(combEntry | seqEntry) +
                             "endtable").setName("table")
        """
        <UDP>
        ::= primitive <name_of_UDP> ( <name_of_variable> <,<name_of_variable>>* ) ;
                <UDP_declaration>+
                <UDP_initial_statement>?
                <table_definition>
                endprimitive
        """
        udp = Group("primitive" + identifier + LPAR +
                    Group(delimitedList(identifier)) + RPAR + SEMI +
                    OneOrMore(udpDecl) + Optional(udpInitialStmt) +
                    udpTableDefn + "endprimitive")

        verilogbnf = OneOrMore(module | udp) + StringEnd()

        verilogbnf.ignore(cppStyleComment)
        verilogbnf.ignore(compilerDirective)

    return verilogbnf
Example #53
0
from utils import choose_one, error_exit


REGEX_SPECIAL_CHARS = r'([\.\*\+\?\|\(\)\{\}\[\]])'
REGEX_LOG_FORMAT_VARIABLE = r'\$([a-z0-9\_]+)'
LOG_FORMAT_COMBINED = '$remote_addr - $remote_user [$time_local] ' \
                      '"$request" $status $body_bytes_sent ' \
                      '"$http_referer" "$http_user_agent"'
LOG_FORMAT_COMMON   = '$remote_addr - $remote_user [$time_local] ' \
                      '"$request" $status $body_bytes_sent ' \
                      '"$http_x_forwarded_for"'

# common parser element
semicolon = Literal(';').suppress()
# nginx string parameter can contain any character except: { ; " '
parameter = Word(''.join(c for c in printables if c not in set('{;"\'')))
# which can also be quoted
parameter = parameter | quotedString.setParseAction(removeQuotes)


def detect_config_path():
    """
    Get nginx configuration file path based on `nginx -V` output
    :return: detected nginx configuration file path
    """
    try:
        proc = subprocess.Popen(['nginx', '-V'], stderr=subprocess.PIPE)
    except OSError:
        error_exit('Access log file or format was not set and nginx config file cannot be detected. ' +
                   'Perhaps nginx is not in your PATH?')
Example #54
0
def rc_statement():
    """
    Generate a RC statement parser that can be used to parse a RC file

    :rtype: pyparsing.ParserElement
    """

    one_line_comment = "//" + restOfLine

    comments = cStyleComment ^ one_line_comment

    precompiler = Word("#", alphanums) + restOfLine

    language_definition = (
        "LANGUAGE"
        + Word(alphas + "_").setResultsName("language")
        + Optional("," + Word(alphas + "_").setResultsName("sublanguage"))
    )

    block_start = (Keyword("{") | Keyword("BEGIN")).setName("block_start")
    block_end = (Keyword("}") | Keyword("END")).setName("block_end")

    reserved_words = block_start | block_end

    name_id = ~reserved_words + Word(alphas, alphanums + "_").setName("name_id")

    numbers = Word(nums)

    integerconstant = numbers ^ Combine("0x" + numbers)

    constant = Combine(
        Optional(Keyword("NOT")) + (name_id | integerconstant),
        adjacent=False,
        joinString=" ",
    )

    combined_constants = delimitedList(constant, "|")

    concatenated_string = OneOrMore(quotedString)

    block_options = Optional(
        SkipTo(Keyword("CAPTION"), failOn=block_start)("pre_caption")
        + Keyword("CAPTION")
        + quotedString("caption")
    ) + SkipTo(block_start)("post_caption")

    undefined_control = Group(
        name_id.setResultsName("id_control")
        + delimitedList(
            concatenated_string ^ constant ^ numbers ^ Group(combined_constants)
        ).setResultsName("values_")
    )

    block = block_start + ZeroOrMore(undefined_control)("controls") + block_end

    dialog = (
        name_id("block_id")
        + (Keyword("DIALOGEX") | Keyword("DIALOG"))("block_type")
        + block_options
        + block
    )

    string_table = Keyword("STRINGTABLE")("block_type") + block_options + block

    menu_item = Keyword("MENUITEM")("block_type") + (
        commaSeparatedList("values_") | Keyword("SEPARATOR")
    )

    popup_block = Forward()

    popup_block <<= Group(
        Keyword("POPUP")("block_type")
        + Optional(quotedString("caption"))
        + block_start
        + ZeroOrMore(Group(menu_item | popup_block))("elements")
        + block_end
    )("popups*")

    menu = (
        name_id("block_id")
        + Keyword("MENU")("block_type")
        + block_options
        + block_start
        + ZeroOrMore(popup_block)
        + block_end
    )

    statem = comments ^ precompiler ^ language_definition ^ dialog ^ string_table ^ menu

    return statem
Example #55
0
    def __init__(self):
        """
        Initialize Numeric Parser.

        expop   :: '^'
        multop  :: '*' | '/'
        addop   :: '+' | '-'
        integer :: ['+' | '-'] '0'..'9'+
        atom    :: PI | E | real | fn '(' expr ')' | '(' expr ')'
        factor  :: atom [ expop factor ]*
        term    :: factor [ multop factor ]*
        expr    :: term [ addop term ]*
        """
        point = Literal(".")
        e = CaselessLiteral("E")
        fnumber = Combine(
            Word("+-" + nums, nums) + Optional(point + Optional(Word(nums))) +
            Optional(e + Word("+-" + nums, nums)))
        ident = Word(alphas, alphas + nums + "_$")
        plus = Literal("+")
        minus = Literal("-")
        mult = Literal("*")
        div = Literal("/")
        lpar = Literal("(").suppress()
        rpar = Literal(")").suppress()
        addop = plus | minus
        multop = mult | div
        expop = Literal("^")
        pi = CaselessLiteral("PI")
        expr = Forward()
        atom = ((Optional(oneOf("- +")) +
                 (ident + lpar + expr + rpar | pi | e
                  | fnumber).setParseAction(self.pushFirst))
                | Optional(oneOf("- +")) +
                Group(lpar + expr + rpar)).setParseAction(self.pushUMinus)
        # by defining exponentiation as "atom [ ^ factor ]..." instead of
        # "atom [ ^ atom ]...", we get right-to-left exponents, instead of left-to-right
        # that is, 2^3^2 = 2^(3^2), not (2^3)^2.
        factor = Forward()
        factor << atom + \
            ZeroOrMore((expop + factor).setParseAction(self.pushFirst))
        term = factor + \
            ZeroOrMore((multop + factor).setParseAction(self.pushFirst))
        expr << term + \
            ZeroOrMore((addop + term).setParseAction(self.pushFirst))
        # addop_term = ( addop + term ).setParseAction( self.pushFirst )
        # general_term = term + ZeroOrMore( addop_term ) | OneOrMore( addop_term)
        # expr <<  general_term
        self.bnf = expr
        # map operator symbols to corresponding arithmetic operations
        epsilon = 1e-12
        self.opn = {
            "+": operator.add,
            "-": operator.sub,
            "*": operator.mul,
            "/": operator.truediv,
            "^": operator.pow
        }
        self.fn = {
            "sin": math.sin,
            "cos": math.cos,
            "tan": math.tan,
            "exp": math.exp,
            "abs": abs,
            "trunc": lambda a: int(a),
            "round": round,
            "sgn": lambda a: abs(a) > epsilon and cmp(a, 0) or 0
        }
Example #56
0
from typing import List, Iterable

from pyparsing import (Group, LineEnd, OneOrMore, Suppress, Word, alphas,
                       restOfLine, ParseResults)
from scm.plams import Atom, Molecule

from .parser import floatNumber, natural
from ..common import AtomXYZ
from ..type_hints import PathLike


# =============================================================================

header = natural + LineEnd() + restOfLine

label = Word(alphas, max=2)

xyz = floatNumber * 3

atomParser = label.setResultsName("label") + xyz.setResultsName("xyz")

parser_xyz = Suppress(header) + OneOrMore(Group(atomParser))

# ==============================<>====================================


def parse_string_xyz(xs: str) -> List[AtomXYZ]:
    """Read a molecula geometry in XYZ format from a string.

    :param: xs
    :type:  string
Example #57
0
    if string[0] in 'aeiou':
        return 'an ' + string
    return 'a ' + string


def capitalise(s):
    """Capitalise in BrE."""
    #we do it -better- than s.capitalize() - that lowercases the rest.
    if not s:
        #don't blow up on the empty string - otherwise we get IndexError
        return ''
    return s[0].upper() + s[1:]


#it takes prefixes of symbols to be the 'head word'.
_hwspattern = (Word(punctuation) + Optional(Word(alnumspace))) ^ \
              (Optional(Word(nwprintable)) + Optional(Word(printable)))

#XXX: tabs. watch it blow up!


def head_word_split(string):
    """Split off the first word or group of non-whitespace punctuation."""
    res = _hwspattern.parseString(string)
    if len(res) == 0:
        return ('', '')
    elif len(res) == 1:
        return (res[0], '')
    else:
        return tuple(res)
    alphanums,
    Regex,
    Suppress,
    Forward,
    Group,
    oneOf,
    ZeroOrMore,
    Optional,
    delimitedList,
    restOfLine,
    quotedString,
    Dict,
    Keyword,
)

ident = Word(alphas + "_", alphanums + "_").setName("identifier")
integer = Regex(r"[+-]?\d+")

LBRACE, RBRACE, LBRACK, RBRACK, LPAR, RPAR, EQ, SEMI = map(
    Suppress, "{}[]()=;")

kwds = """message required optional repeated enum extensions extends extend
          to package service rpc returns true false option import syntax"""
for kw in kwds.split():
    exec("{}_ = Keyword('{}')".format(kw.upper(), kw))

messageBody = Forward()

messageDefn = MESSAGE_ - ident("messageId") + LBRACE + messageBody(
    "body") + RBRACE
Example #59
0
import dateutil.parser
import functools
import requests
import platform
import urllib3
import urllib
import click
import pyke
import copy
import json
import sys
import re
import os

ESC = Literal('\x1b')
integer = Word(nums)
escapeSeq = Combine(ESC + '[' + Optional(delimitedList(integer, ';')) +
                    oneOf(list(alphas)))

uncolor = lambda s: Suppress(escapeSeq).transformString(s)

verify_tls = os.environ.get('LUMA_CLI_VERIFY_TLS', None)
if verify_tls == 'false' or verify_tls == 'False':
    verify_tls = False
    urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
else:
    verify_tls = None


def list_options(f):
    options = [
Example #60
0
        f = self.get_static_route_fact(prefix)
        if is_ipv4(tokens[3]):
            f.next_hop = tokens[3]
        else:
            f.interface = tokens[3]

    def parse_port_security(self, tokens):
        """
        config port_security ports 1:1-1:25 admin_state disable max_learning_addr 1 lock_address_mode DeleteOnReset
        """
        ports = self.next_item(tokens, "ports") or ""
        ps_max = self.next_item(tokens, "max_learning_addr")
        for p in self.iter_ports(ports):
            si = self.get_subinterface_fact(p)
            si.port_security = True
            if ps_max is not None:
                si.port_security_max = ps_max


# Port expression parser
DIGITS = Word(nums)
PORT = Combine(DIGITS + Optional(Literal(":") + DIGITS))
# 1:(2,3,10-20)
PORT_RANGE_PT = Group(DIGITS + Literal(":(") + delimitedList(
    Group(DIGITS + Suppress(Literal("-")) + DIGITS) | DIGITS, delim=",") +
                      Suppress(Literal(")")))
# 1:2-1:5
PORT_RANGE = Group(PORT + Suppress(Literal("-")) + PORT)
# Port expression
PORT_EXPR = delimitedList(PORT_RANGE_PT | PORT_RANGE | PORT, delim=",")