def len_nonAnsi(string): ''' Gets the nonAnsi length of a string, removes additional length of the Ansi formatting of certain strings. ''' ESC = pp.Literal('\x1b') integer = pp.Word(pp.nums) escapeSeq = pp.Combine(ESC + '[' + pp.Optional(pp.delimitedList(integer, ';')) + pp.oneOf(list(pp.alphas))) return len(pp.Suppress(escapeSeq).transformString(string))
def _build_assignments_grammar(): key = InstructionBase.pp_word( without="=", unquote=True) + ~pp.FollowedBy(pp.White()) value = InstructionBase.pp_word(unquote=True) assignment = ( key + pp.Combine(pp.Suppress("=") + pp.Optional(value, default=""))).setName("assignment") assignments = pp.OneOrMore(assignment).setParseAction( InstructionBase.dictionize) assignments = assignments.setResultsName("assignments") return assignments
def non_ascii(string: str) -> str: esc = pyparsing.Literal('\x1b') escape_sequence = pyparsing.Combine(esc + '[' + \ pyparsing.Optional( pyparsing.delimitedList( pyparsing.Word(pyparsing.nums), ';') ) + \ pyparsing.oneOf(list(pyparsing.alphas))) return \ pyparsing.Suppress(escape_sequence)\ .transformString(string)
def swallow_errors(rule): """Extend the production rule by potentially eating errors. This does not return a p.NoMatch() because that messes up the error messages. """ ret = rule if allow_errors: # Synchronize on the first semicolon or the first unbalanced closing curly ret = rule | pattern( 'catch_errors', parseWithLocation(p.Suppress(catch_errors), UnparseableNode)) return ret
def __init__(self): """constructor""" """make LAD parser""" self.NwNumber = pp.Word(pp.nums, max=1).setParseAction(pp.tokenMap(int)).setBreak(False) self.Nw = pp.CaselessLiteral('NW:') + self.NwNumber + pp.Suppress(pp.lineEnd()) self.Ope_I = pp.Combine(pp.CaselessLiteral('I') + pp.Word(pp.nums, max=2)) self.Ope_O = pp.Combine(pp.CaselessLiteral('O') + pp.Word(pp.nums, max=2)) self.Ope_M = pp.Combine(pp.CaselessLiteral('M') + pp.Word(pp.nums, max=2)) self.Ope = self.Ope_I | self.Ope_O | self.Ope_M self.Command_LD = (pp.CaselessKeyword('LDN') | pp.CaselessKeyword ('LD')) + self.Ope + pp.Suppress(pp.lineEnd()) self.Command_AND = (pp.CaselessKeyword('ANDN') | pp.CaselessKeyword ('AND')) + self.Ope + pp.Suppress(pp.lineEnd()) self.Command_OR = (pp.CaselessKeyword('ORN') | pp.CaselessKeyword('OR')) + self.Ope + pp.Suppress(pp.lineEnd()) self.Command_OUT = pp.CaselessKeyword('OUT') + self.Ope + pp.Suppress(pp.lineEnd()) self.Command_BSAND = pp.CaselessKeyword('BSAND') + pp.Suppress(pp.lineEnd()) self.Command_BFAND = pp.CaselessKeyword('BFAND') + pp.Suppress(pp.lineEnd()) self.Command_BSOR = pp.CaselessKeyword('BSOR') + pp.Suppress(pp.lineEnd()) self.Command_BFOR = pp.CaselessKeyword('BFOR') + pp.Suppress(pp.lineEnd()) self.Command_LDOR = self.Command_LD + self.Command_OR * (0, 7) self.Command_ANDOR = self.Command_AND + self.Command_OR * (0, 7) self.Command_LDAND = self.Command_LDOR + self.Command_ANDOR * (0, 7) self.Complex = pp.Forward() self.Block = pp.Group((self.Complex | self.Command_LDAND) + pp.Optional(self.Command_ANDOR * (0, 7))) self.ComplexOR = self.Command_BSOR + self.Block + self.Block + self.Command_BFOR self.ComplexAND = self.Command_BSAND + self.Block + self.Block + self.Command_BFAND self.Complex <<= self.ComplexOR | self.ComplexAND self.NwProgram = pp.Group(self.Nw + self.Block + self.Command_OUT) self.Program = pp.OneOrMore(self.NwProgram)
class Port(Node): """ Description of a port on a sink """ __fragments__ = { 'name': 'port-name', 'label': 'port-label', 'priority': 'port-priority', 'availability': 'port-availability' } __syntax__ = ( p.Word(p.alphanums + "-;").setResultsName('port-name') + p.Suppress(':') # This part was very tricky to write. The label is basically # arbitrary localized Unicode text. We want to grab all of it in # one go but without consuming the upcoming '(' character or the # space that comes immediately before. # # The syntax here combines a sequence of words, as defined by # anything other than a space and '(', delimited by a single # whitespace. + p.delimitedList(p.Regex('[^ (\n]+'), ' ', combine=True).setResultsName('port-label') + p.Suppress('(') + p.Keyword('priority').suppress() + p.Suppress(':') + p.Word(p.nums).setParseAction(lambda t: int(t[0])).setResultsName( 'port-priority') + p.MatchFirst([ p.Suppress(',') + p.Literal('not available'), p.Suppress(',') + p.Literal('available'), p.Empty().setParseAction(lambda t: '') ]).setResultsName('port-availability') + p.Suppress(')')).setResultsName("port")
def parseBib(filename, language): pp.ParserElement.setDefaultWhitespaceChars(" \n\t") entry = returnList( pp.Word('@', pp.alphanums) + sl('{') + pp.Word(pp.alphanums + "_") + sl(',') + CommaList( returnTuple( pp.Word(pp.alphanums) + sl('=') + pp.QuotedString('{', endQuoteChar='}'))) + pp.Suppress(pp.Optional(',')) + sl('}')) r = (pp.ZeroOrMore(entry) | pp.Suppress('#' + pp.ZeroOrMore(pp.CharsNotIn('\n'))) + pp.StringEnd()).parseFile(filename) bibliography = QOpen(os.path.join(language, "bibliography.rst"), 'wt') print >> bibliography, "Bibliography" print >> bibliography, "============" print >> bibliography for _, e in sorted([(str(x[1]), x) for x in r]): (etype, tag, attrs) = str(e[0][1:]), str(e[1]), dict([ (str(a), str(b)) for (a, b) in e[2] ]) representations = { 'article': '$author, "$title". $journal $volume $number, pp $pages ($year)', 'inproceedings': '$author "$title", $booktitle, $year', 'misc': '$author "$title", $year', 'techreport': '$author "$title", $edition, $edition ($year)', } if etype in representations: if 0: print >> bibliography, tag print >> bibliography, "^" * len(tag) print >> bibliography print >> bibliography, ".. [%s] %s" % ( tag, Template(representations[etype]).safe_substitute(attrs)) print >> bibliography bibliography.close()
def parse_first_case_line(first_case_line): data = {"case_order": first_case_line[0]} gender = p.Suppress(p.Literal("(")) + p.Word( p.alphas, exact=1).setResultsName("gender") + p.Suppress( p.Literal(")")) dob = p.Suppress( p.Literal("DOB:")) + date.setResultsName("dob") + p.Suppress( p.Literal("Age:")) + p.Word(p.nums).setResultsName("age") linked_case = p.Suppress(p.Literal("LINKED CASE")) provisional = p.Suppress(p.Literal("PROVISIONAL")) first_case_line_detail = p.And([ p.SkipTo(p.White(" ", min=10) ^ gender).setResultsName("name"), p.Optional(gender), p.Optional(dob), p.Optional(linked_case), p.Optional(provisional), p.Word(p.nums), ]) for key, value in first_case_line_detail.parseString( first_case_line[1]).asDict().items(): data[key] = value.strip() return data
def parseTreeVariables(expression, counts=None, verbose=0): """ parseTreeExpression and fill flat list with tree variable needed for evaluation Used in getAndTestVariableList :param verbose: verbosity :param expression: expression to parse e.g. expr="x>1 & x>0 | y==1 |x+1>2| (x2<2) | (x1*2)<2| sin(x)<1" :param counts: :return: :type counts: dict Example usage: :parseVariables("x>1 & x>0 | y==1 |x+1>2| (x2<2) | (x1*2)<2| sin(x)<1") ==> {'sin': 1, 'x': 4, 'x1': 1, 'x2': 1, 'y': 1} """ if verbose: print("expression", expression) if counts is None: counts = dict() varList = [] theContent = pyparsing.Word(pyparsing.alphanums + "._") | pyparsing.Suppress(',') | pyparsing.Suppress('|') | pyparsing.Suppress('&') | pyparsing.Suppress('!') \ | pyparsing.Suppress('>') | pyparsing.Suppress('=') | pyparsing.Suppress('+') |pyparsing.Suppress('-') | pyparsing.Suppress('<') | pyparsing.Suppress('*') \ | pyparsing.Suppress('*') | pyparsing.Suppress(':') parents = pyparsing.nestedExpr('(', ')', content=theContent) res = parents.parseString("(" + expression + ")") __parseVariableList(res, varList) for i in varList: counts[i] = counts.get(i, 0) + 1 return counts
def parseTreeVariables(expression, counts=None, verbose=0): r""" ParseTreeExpression and fill flat list with tree variable needed for evaluation * Used in getAndTestVariableList :param verbose: verbosity :param expression: expression to parse :param counts: :return: dictionary with pairs variable:count :type counts: dict Example usage: >>> parseVariables("x>1 & x>0 | y==1 |x+1>2| (x2<2) | (x1*2)<2| sin(x)<1") ==> {'sin': 1, 'x': 4, 'x1': 1, 'x2': 1, 'y': 1} """ if verbose: logging.info("expression", expression) if counts is None: counts = dict() varList = [] theContent = pyparsing.Word(pyparsing.alphanums + "._") | pyparsing.Suppress(',') | pyparsing.Suppress('|') | pyparsing.Suppress('&') | pyparsing.Suppress('!') \ | pyparsing.Suppress('>') | pyparsing.Suppress('=') | pyparsing.Suppress('+') | pyparsing.Suppress('-') | pyparsing.Suppress('<') | pyparsing.Suppress('*') \ | pyparsing.Suppress('*') | pyparsing.Suppress(':') parents = pyparsing.nestedExpr('(', ')', content=theContent) try: res = parents.parseString("(" + expression + ")") __parseVariableList(res, varList) except: logging.error("Oops! That was no valid number. Try again...", expression) for i in varList: counts[i] = counts.get(i, 0) + 1 return counts
class Property(Node): """ A key=value pair. A list of properties is a possible syntax for Attribute value. """ __fragments__ = {'name': 'property-name', 'value': 'property-value'} __syntax__ = (p.Word(p.alphanums + "-_.").setResultsName("property-name") + p.Suppress('=') + p.QuotedString('"').setResultsName("property-value") ).setResultsName('property')
def _parse(self, df=None, independent_param_vals=None): expr_evaluator = Evaluator(df=df, name_dict=independent_param_vals) param_expr = expr_evaluator.parser() render_as_type = pp.Word(pp.alphas, pp.alphanums + "_$") render_as_type.setParseAction( lambda x: self._set_render_type(value=x[0])) container_type = pp.Optional(pp.Word(pp.alphas, pp.alphanums + "_$") + pp.Suppress(":"), default=None) container_type.setParseAction( lambda x: self._set_container_type(value=x[0])) parser = param_expr + pp.Suppress( "->") + container_type + render_as_type try: parser.parseString(self.parameter_str) except pp.ParseException, e: raise ParameterRenderError( "Error parsing parameter string: \n %s" % e)
def parse_design(self): # GLOBALS for this class EOL = pp.LineEnd().suppress() linebreak = pp.Suppress(";" + pp.LineEnd()) identifier = pp.Word(pp.alphanums + '._“!<>/[]$#$%&‘*+,/:<=>?@[\]^_`{|}~') # CONFLICT with '();' number = pp.pyparsing_common.number word = pp.Word(pp.alphas) LPAR = pp.Suppress('(') RPAR = pp.Suppress(')') ORIENT = (pp.Keyword('N') | pp.Keyword('S') | pp.Keyword('E') | pp.Keyword('W') | pp.Keyword('FN') | pp.Keyword('FS') | pp.Keyword('FE') | pp.Keyword('FW')) pt = LPAR + pp.OneOrMore(number | pp.Keyword('*')) + RPAR # pair of x,y design_id = pp.Keyword('DESIGN') design = design_id + identifier('DESIGN') + linebreak self.events[0].set() # event[0] (parse_dbuPerMicron) has priority return design
def __init__(self, parser, public='commands'): self.parser = parser self.public = public self.rules = {} self.commands = Commands(self) ruleName = pp.Combine( pp.Suppress('<') + pp.Word(pp.alphanums + '_.') + pp.Suppress('>')) ruleName.setParseAction(lambda toks: self[toks[0]]) expr = pp.Forward() seq = pp.delimitedList(expr, delim=pp.Empty()) seq.setParseAction(lambda toks: pp.And(toks.asList()) if len(toks.asList()) > 1 else None) self.rule = alt = pp.delimitedList(seq, delim='|') alt.setParseAction(lambda toks: pp.Or(toks.asList()) if len(toks.asList()) > 1 else None) groupExpr = pp.Suppress('(') + alt + pp.Suppress(')') groupExpr.setParseAction(lambda toks: Grouping(toks[0])) word = pp.Word(pp.alphanums + r".&'\"") word.setParseAction(lambda toks: pp.Keyword(toks[0])) token = groupExpr | ruleName | word optExpr = pp.Suppress('[') + alt + pp.Suppress(']') optExpr.setParseAction(lambda toks: pp.Optional(toks[0])) zeroOrMore = token + pp.Suppress(pp.Literal('*')) zeroOrMore.setParseAction(lambda toks: pp.ZeroOrMore(toks[0])) oneOrMore = token + pp.Suppress(pp.Literal('+')) oneOrMore.setParseAction(lambda toks: pp.OneOrMore(toks[0])) elem = zeroOrMore | oneOrMore | optExpr | token tagged = elem + pp.Combine(pp.Suppress('/') + pp.Word(pp.alphanums)).setResultsName('tag') tagged.setParseAction(self.parseExpr) expr << (tagged | elem)
def makeAliasAnyTree(key, parent, aliases): """ build recursive alias anytree :param key: - start key :param parent: - initial node :param aliases: - alias dictionary :return: anytree object """ theContent = pyparsing.Word( pyparsing.alphanums + ".+-=_><") | pyparsing.Suppress(',') | pyparsing.Suppress( '||') | pyparsing.Suppress('&&') | pyparsing.Suppress('!') parents = pyparsing.nestedExpr('(', ')', content=theContent) res = parents.parseString("(" + aliases[key] + ")")[0] for subExpression in res: if len(subExpression) == 0: continue for a in subExpression: if a in aliases: newNode = Node(a, parent=parent, content=aliases[a]) makeAliasAnyTree(a, newNode, aliases) else: Node(a, parent=parent)
def getSklWriteResGroupDefParser(): writeResGroup = pp.Word(pp.alphanums) resources = pp.SkipTo("]") latency = pp.Word(pp.nums) microOps = pp.Word(pp.nums) resourceCycles = pp.SkipTo("]") return pp.Suppress("def ") + writeResGroup("SKLWriteResGroup") + pp.Suppress(": SchedWriteRes<[") + resources("Resources") + pp.Suppress(pp.restOfLine) + ( pp.Suppress("let Latency = ") + latency("Latency") + pp.Suppress(pp.restOfLine) + pp.Suppress("let NumMicroOps = ") + microOps("NumMicroOps") + pp.Suppress(pp.restOfLine) + pp.Suppress("let ResourceCycles = [") + resourceCycles("ResourceCycles") + pp.Suppress(pp.restOfLine) )
def get_address(filename_string): sample = filename_string sample = sample[:len(sample) - 4] zipcode_pp = Word(nums, min=5) + pp.Suppress('_') zipp = zipcode_pp.searchString(sample) zipcode = zipp[0][0] address1 = pp.OneOrMore( Word(pp.alphanums + '()') + pp.Suppress('_') + ~Word(zipcode, min=5)) address2 = Word(pp.alphanums + '()') + pp.Suppress('_') + pp.Suppress( Word(zipcode, min=5)) add1 = list(address1.parseString(sample)) add2 = list(address2.searchString(sample)) add1.append(add2[0][0]) address = " ".join(add1) return address, zipcode
def gto_basis_parser(): """ Gaussian-type orbital basis parser with pyparsing Basis structure in CRYSTAL is as follows: NUM NSHELLS <ECP_PART> <SHELL_PART> <PRIMITIVE_PART> :return: basis parser """ header = 2 * pc.integer ecp_part = pp.Word(pp.alphas) + pp.Optional( pp.Group(pc.real + 6 * pc.integer) + pp.Group(pp.OneOrMore(pp.Group(2 * pc.real + pc.signed_integer)))) bs_head = pp.Group(3 * pc.integer + 2 * pc.number) bs_part = pp.OneOrMore( pp.Group(bs_head + pp.ZeroOrMore( pp.Group((3 * pc.number + pp.Suppress(pp.LineEnd())) ^ (2 * pc.number + pp.Suppress(pp.LineEnd())))))) return pp.SkipTo(header) + header('header') + pp.Optional( ecp_part('ecp')) + bs_part('bs')
def _parse_map_tables(report_str: str) -> Dict[str, str]: """ Parse the tables from a ISE map report. Keys are the title of the table, values are the table body. """ # Capture the title from section headings like: # # Section 12 - Control Set Information # ------------------------------------ title = ( pp.lineStart() + "Section" + ppc.integer + "-" + pp.SkipTo(pp.lineEnd())("title").setParseAction(pp.tokenMap(str.strip)) + pp.lineEnd() ) sec_hline = pp.Suppress(pp.lineStart() + pp.Word("-") + pp.lineEnd() * (1,)) # Table horizontal lines like # +-------------------------------+ hline = pp.lineStart() + pp.Word("+", "+-") + pp.lineEnd() # Most tables will have the format # +-----------------------+ # | Col 1 | Col 2 | Col 3 | # +-----------------------+ # | D1 | D2 | D3 | # ... # +-----------------------+ # # However "Control Set Information" appears to use horizontal lines to # separate clocks within the data section. Therefore, just grab # everything until a horizontal line followed by a blank line rather # than something more precise. table = pp.Combine(hline + pp.SkipTo(hline + pp.LineEnd(), include=True))( "body" ) table_section = title + sec_hline + table # Make line endings significant table_section.setWhitespaceChars(" \t") result = {t.title: t.body for t in table_section.searchString(report_str)} return result
def parse(s): code = pp.Forward() opcode = pp.Or([ pp.Literal('+'), pp.Literal('-'), pp.Literal('*'), pp.Literal('/'), pp.Literal('_'), pp.Literal('='), pp.Literal('>'), pp.Literal('&'), pp.Literal('|'), pp.Literal('~'), pp.Literal('$'), pp.Literal('%'), pp.Literal('\\'), pp.Literal('@'), pp.Literal('ø'), pp.Literal('p'), pp.Literal(':'), pp.Literal(';'), pp.Literal('!'), pp.Literal('?'), pp.Literal('#'), ]).setParseAction(lambda toks: ast.Opcode(toks[0])) number = (pp.Word('1234567890').setParseAction( lambda toks: ast.Number(int(toks[0])))) str_def = ((pp.Literal('"') + pp.SkipTo(pp.Literal('"'), include=True) ).setParseAction(lambda toks: ast.String(toks[1]))) varname = (pp.Word( 'qwertyuiopasdfghjklzxcvbnm', exact=1).setParseAction(lambda toks: ast.Varname(toks[0]))) fn_def = pp.Suppress(pp.Literal('[')) + code + pp.Suppress(pp.Literal(']')) expr = pp.Or([opcode, number, varname, str_def, fn_def]) atom = pp.Or([expr]) code << pp.ZeroOrMore(atom) code.setParseAction(lambda toks: ast.Function(toks)) return code.parseString(s)[0]
def _generate_grammar(*, debug_parser: bool = False): pp.ParserElement.setDefaultWhitespaceChars(" \t") EOL = pp.Optional(pp.pythonStyleComment()) + pp.LineEnd() LC = pp.Suppress(pp.OneOrMore(EOL) + pp.White(ws=" \t", min=4)) Identifier = pp.Word(initChars=pp.alphas, bodyChars=pp.alphanums + "_-") MultilineArgument = pp.QuotedString(quoteChar="<<<<", endQuoteChar=">>>>", multiline=True) SingleQuotedArgument = pp.QuotedString(quoteChar="'", escChar="\\") DoubleQuotedArgument = pp.QuotedString(quoteChar='"', escChar="\\") QuotedArgument = (SingleQuotedArgument | DoubleQuotedArgument | MultilineArgument)("quoted") SimpleArgument = pp.Word(pp.alphanums + "_-+*!$%&/()[]{}.,;:")("simple") Argument = (QuotedArgument | SimpleArgument) + pp.Optional(LC) KwArgument = pp.Combine(Identifier("key") + "=" + Argument) ArgumentList = pp.Group(pp.ZeroOrMore(pp.Group(KwArgument | Argument))) Command = (pp.locatedExpr(Identifier)("command") + pp.Optional(LC) + ArgumentList("args")) Grammar = pp.ZeroOrMore(pp.Group(pp.Optional(Command) + pp.Suppress(EOL))) if debug_parser: for expr_name in ("Grammar Command ArgumentList KwArgument Argument " "SimpleArgument QuotedArgument DoubleQuotedArgument " "SingleQuotedArgument MultilineArgument " "Identifier LC EOL".split()): expr = locals()[expr_name] expr.setName(expr_name) expr.setDebug() Grammar.parseWithTabs() # Keep tabs unexpanded! return Grammar
def parseWidgetString(self, widgetString): toParse = "(" + widgetString + ")" theContent = pyparsing.Word(pyparsing.alphanums + ".+-_") | '#' | pyparsing.Suppress( ',') | pyparsing.Suppress(':') widgetParser = pyparsing.nestedExpr('(', ')', content=theContent) widgetList0 = widgetParser.parseString(toParse)[0] for widgetTitle, iWidget in izip(*[iter(widgetList0)] * 2): name = widgetTitle.split('.') if name[0] == 'accordion': if findInList(name[1], self.accordArray) == -1: self.accordArray.append([name[1]]) for name, param in izip(*[iter(iWidget)] * 2): self.fillArray([name, param], self.accordArray[findInList( name[1], self.accordArray)]) elif name[0] == 'tab': if findInList(name[1], self.tabArray) == -1: self.tabArray.append([name[1]]) for name, param in izip(*[iter(iWidget)] * 2): self.fillArray([name, param], self.tabArray[findInList( name[1], self.tabArray)]) else: self.fillArray([widgetTitle, iWidget], self.widgetArray)
def _parse_twr_stats(report_str: str) -> pp.ParseResults: """ Parse the design statistics from an ISE timing report. Design statistics: :: Minimum period: 11.343ns{1} (Maximum frequency: 88.160MHz) """ header = pp.Suppress(pp.SkipTo("Design statistics:", include=True)) period = ( pp.Suppress("Minimum period:") + ppc.number("min period") + pp.Suppress("ns{1}") ) freq = ( pp.Suppress("(Maximum frequency:") + ppc.number("max clock") + pp.Suppress("MHz)") ) stat = header + period + freq return stat.parseString(report_str)
def make_expression(arities): """Takes dictionary with keys as operation names an arities as values.""" expr = pp.Forward() op_list = [] for op in arities: arity = arities[op] if arity == 0: op_expr = op + pp.Suppress('()') else: # op(expr,expr,... arity times) op_expr = op + pp.Suppress('(') + expr for i in range(arity - 1): op_expr = op_expr + pp.Suppress(',') + expr op_expr = op_expr + pp.Suppress(')') op_list.append(op_expr.setName(op)) combine = op_list[0] for op in op_list[1:]: combine = combine | op combine = combine | pp.Word('xyz_' + pp.nums) # expr << combine return expr
def parser(self): query_key = pp.Keyword("QUERY") query_value = pp.Suppress("|") + pp.SkipTo(pp.Suppress(";"), include=True) fields_key = pp.Keyword("FIELDS") field_name = common_parsers.column field_name_list = pp.Group(pp.delimitedList( field_name, delim=",")).setParseAction(lambda x: x.asList()) fields_block = (pp.Suppress(fields_key) + field_name_list) connector_name = pp.Word(pp.alphas, pp.alphanums + "_$") using_block = pp.Suppress("USING") + connector_name then_key = pp.Suppress("THEN") manipulation_set = pp.Suppress("|") + pp.SkipTo(pp.Suppress(";"), include=True) then_block = then_key + manipulation_set as_key = pp.Suppress("AS") node_name = pp.Word(pp.alphas, pp.alphanums + "_$") as_block = as_key + node_name query_node_block = (pp.Suppress(query_key) + query_value + pp.Optional(fields_block, default=None) + using_block + pp.Optional(then_block, default=None) + as_block) query_node_block.setParseAction( lambda x: self._add_query_node(query_value=x[0], connector_name=x[2], node_name=x[4], fields=x[1], manipulation_set=x[3])) single_query_node = query_node_block + pp.Optional(pp.Suppress("---")) retrieve_block = pp.OneOrMore(single_query_node) return retrieve_block
def _generate_grammar(*, debug_parser: bool = False): pp.ParserElement.setDefaultWhitespaceChars(' \t') EOL = pp.Optional(pp.pythonStyleComment()) + pp.LineEnd() LC = pp.Suppress(pp.OneOrMore(EOL) + pp.White(ws=' \t', min=4)) Identifier = pp.Word(initChars=pp.alphas, bodyChars=pp.alphanums + '_-') MultilineArgument = pp.QuotedString(quoteChar = '<<<<', endQuoteChar = '>>>>', multiline=True) SingleQuotedArgument = pp.QuotedString(quoteChar = '\'', escChar = '\\') DoubleQuotedArgument = pp.QuotedString(quoteChar = '"', escChar = '\\') QuotedArgument = (SingleQuotedArgument | DoubleQuotedArgument | MultilineArgument)('quoted') SimpleArgument = pp.Word(pp.alphanums + '_-+*!$%&/()[]{}.,;:')('simple') Argument = (QuotedArgument | SimpleArgument) + pp.Optional(LC) KwArgument = pp.Combine(Identifier('key') + '=' + Argument) ArgumentList = pp.Group(pp.ZeroOrMore(pp.Group(KwArgument | Argument))) Command = pp.locatedExpr(Identifier)('command') + pp.Optional(LC) \ + ArgumentList('args') Grammar = pp.ZeroOrMore(pp.Group(pp.Optional(Command) + pp.Suppress(EOL))) if debug_parser: for ename in 'Grammar Command ArgumentList KwArgument Argument ' \ 'SimpleArgument QuotedArgument DoubleQuotedArgument ' \ 'SingleQuotedArgument MultilineArgument ' \ 'Identifier LC EOL'.split(): expr = locals()[ename] expr.setName(ename) expr.setDebug() Grammar.parseWithTabs() # Keep tabs unexpanded! return Grammar
def parseWidgetString(widgetString): r''' Parse widget string and convert it to nested lists :param widgetString: Example: https://github.com/miranov25/RootInteractiveTest/blob/master/JIRA/ADQT-3/tpcQADemoWithStatus.ipynb >>> from InteractiveDrawing.bokeh.bokehTools import * >>> widgets="tab.sliders(slider.meanMIP(45,55,0.1,45,55),slider.meanMIPele(50,80,0.2,50,80), slider.resolutionMIP(0,0.15,0.01,0,0.15))," >>> widgets+="tab.checkboxGlobal(slider.global_Warning(0,1,1,0,1),checkbox.global_Outlier(0))," >>> widgets+="tab.checkboxMIP(slider.MIPquality_Warning(0,1,1,0,1),checkbox.MIPquality_Outlier(0), checkbox.MIPquality_PhysAcc(1))" >>> print(parseWidgetString(widgets)) >>> ['tab.sliders', ['slider.meanMIP', ['45', '55', '0.1', '45', '55'], 'slider.meanMIPele', ['50', '80', '0.2', '50', '80'], ....] :return: Nested lists of strings to create widgets ''' toParse = "(" + widgetString + ")" theContent = pyparsing.Word(pyparsing.alphanums + ".+-_[]{}") | '#' | pyparsing.Suppress( ',') | pyparsing.Suppress(':') widgetParser = pyparsing.nestedExpr('(', ')', content=theContent) widgetList = widgetParser.parseString(toParse)[0] return widgetList
def _parse_to_delimiter( expression, delimiter, include, ignore=pyparsing.cppStyleComment, fallback_function=None, ): if ignore: expression = expression.copy().ignore(ignore) to_delimiter_parser = pyparsing.SkipTo(delimiter).setParseAction( _parse_result(expression, fallback_function), ) if include: to_delimiter_parser += pyparsing.Suppress(delimiter) return to_delimiter_parser
def _build_expr_parser(): atom = ppc.fnumber + pp.Optional(pp.oneOf('m h d w')) def do_atom(t): if len(t) == 2: if t[1] == 'm': return t[0] / 60 if t[1] == 'h': return t[0] if t[1] == 'd': return t[0] * 24 if t[1] == 'w': return t[0] * 24 * 7 return t[0] atom.addParseAction(do_atom) term = atom + pp.ZeroOrMore(pp.Suppress('*') + atom) term.addParseAction(partial(reduce, mul)) expr = term + pp.ZeroOrMore(pp.Suppress('+') + term) expr.addParseAction(sum) return expr
def create_parser_element(): """ Parser to read the basis set of a given element in the following format: # Basis set for element : O NewGTO O S 5 1 2266.1767785000 -0.0053893504 2 340.8701019100 -0.0402347214 3 77.3631351670 -0.1800818421 4 21.4796449400 -0.4682885766 5 6.6589433124 -0.4469261716 S 1 1 0.8097597567 1.0000000000 S 1 1 0.2553077223 1.0000000000 P 3 1 17.7215043170 0.0626302488 2 3.8635505440 0.3333113849 3 1.0480920883 0.7414863830 P 1 1 0.2764154441 1.0000000000 D 1 1 1.2000000000 1.0000000000 end; """ header = pa.Suppress(pa.Literal('# Basis set for element :')) + \ pa.Word(pa.alphas) + skipLine parseCGF = pa.Group( pa.Word(pa.alphas, exact=1) + natural + pa.Group(pa.OneOrMore(pa.Suppress(natural) + floatNumber * 2))) return pa.Group(header + pa.OneOrMore(parseCGF) + pa.Suppress(pa.Literal('end;')))