我们从Python开源项目中,提取了以下11个代码示例,用于说明如何使用pyparsing.QuotedString()。
def _build_parser(): date_literal = pp.Regex(r'(?P<year>\d{4})/(?P<month>\d{2})/(?P<day>\d{2})') \ .setParseAction(lambda s,l,t: schema.Date(t.year, t.month, t.day)) dollars_literal = pp.Regex(r'\$\d+(\.\d{2})') \ .setParseAction(lambda s,l,t: schema.Dollars(t[0])) string_literal = (pp.QuotedString('"', escChar='\\') | pp.QuotedString("'", escChar='\\')) \ .setParseAction(lambda s,l,t: schema.String(t[0])) literal = date_literal | dollars_literal | string_literal ident = pp.Word(pp.alphas) match_op = pp.oneOf(operator_map.keys()) match = ident + match_op + literal assign_op = pp.Literal('=') assign = ident + assign_op + literal part = (match | assign).setParseAction(lambda s,l,t: [t]) rule = pp.delimitedList(part) + pp.LineEnd() return rule
def parser(self): connector_name = pp.Word(pp.alphas, pp.alphanums + "_$") connector_type = pp.Word(pp.alphas) connector_kwarg = (pp.Word(pp.alphas, pp.alphanums + "_$") + pp.Suppress("=") + pp.QuotedString(quoteChar="'")) connector_kwarg.setParseAction(lambda x: {x[0]: x[1]}) conn_kwarg_list = pp.delimitedList(connector_kwarg) conn_kwarg_list.setParseAction(lambda x: dict(pair for d in x for pair in d.items())) single_connector = (connector_name + pp.Suppress("<-") + connector_type + pp.Suppress("(") + conn_kwarg_list + pp.Suppress(")")) single_connector.setParseAction(lambda x: self._add_connector(conn_name=x[0], conn_type=x[1], conn_kwargs=x[2])) connector_block = pp.OneOrMore(single_connector) return connector_block
def parse_format(format): definition = [] # define pattern grammar variable_ptn = pp.QuotedString("{", endQuoteChar="}")("variable") escape_open_ptn = pp.Literal("{{")("escape_open") escape_close_ptn = pp.Literal("}}")("escape_close") escape_ptn = escape_open_ptn | escape_close_ptn literal_ptn = pp.CharsNotIn("{}")("literal") element_ptn = escape_ptn | variable_ptn | literal_ptn for toks, start, end in element_ptn.leaveWhitespace().scanString(format): try: definition.append({ "literal": lambda: Literal(toks[0]), "variable": lambda: Variable.create(toks[0]), "escape_open": lambda: OpenBrace(), "escape_close": lambda: CloseBrace(), }[toks.items()[0][0]]()) except KeyError: raise FormatStringError return definition
def split_by_commas(value): """Split values by commas and quotes according to api-wg :param value: value to be split .. versionadded:: 3.17 """ word = (pp.QuotedString(quoteChar='"', escChar='\\') | pp.Word(pp.printables, excludeChars='",')) grammar = pp.stringStart + pp.delimitedList(word) + pp.stringEnd try: return list(grammar.parseString(value)) except pp.ParseException: raise ValueError("Invalid value: %s" % value)
def parse_filter_str(self, filter_str): """ method to parse filter string """ prop = pp.WordStart(pp.alphas) + pp.Word(pp.alphanums + "_").setResultsName("prop") value = (pp.QuotedString("'") | pp.QuotedString('"') | pp.Word( pp.printables, excludeChars=",")).setResultsName("value") types_ = pp.oneOf("re eq ne gt ge lt le").setResultsName("types") flags = pp.oneOf("C I").setResultsName("flags") comma = pp.Literal(',') quote = (pp.Literal("'") | pp.Literal('"')).setResultsName("quote") type_exp = pp.Group(pp.Literal("type") + pp.Literal( "=") + quote + types_ + quote).setResultsName("type_exp") flag_exp = pp.Group(pp.Literal("flag") + pp.Literal( "=") + quote + flags + quote).setResultsName("flag_exp") semi_expression = pp.Forward() semi_expression << pp.Group(pp.Literal("(") + prop + comma + value + pp.Optional(comma + type_exp) + pp.Optional(comma + flag_exp) + pp.Literal(")") ).setParseAction( self.parse_filter_obj).setResultsName("semi_expression") expr = pp.Forward() expr << pp.operatorPrecedence(semi_expression, [ ("not", 1, pp.opAssoc.RIGHT, self.not_operator), ("and", 2, pp.opAssoc.LEFT, self.and_operator), ("or", 2, pp.opAssoc.LEFT, self.or_operator) ]) result = expr.parseString(filter_str) return result
def getchunk(): """ Using pyparsing, create chunk reader for chunk strings. """ slot = pp.Word("".join([pp.alphas, "_"]), "".join([pp.alphanums, "_"])) special_value = pp.Group(pp.oneOf([ACTRVARIABLE, "".join([ACTRNEG, ACTRVARIABLE]), ACTRNEG, VISIONGREATER, VISIONSMALLER, "".join([VISIONGREATER, ACTRVARIABLE]), "".join([VISIONSMALLER, ACTRVARIABLE])])\ + pp.Word("".join([pp.alphanums, "_", '"', "'"]))) strvalue = pp.QuotedString('"', unquoteResults=False) strvalue2 = pp.QuotedString("'", unquoteResults=False) varvalue = pp.Word("".join([pp.alphanums, "_"])) value = varvalue | special_value | strvalue | strvalue2 chunk_reader = pp.OneOrMore(pp.Group(slot + value)) return chunk_reader
def parse_search_query(query): unicode_printables = u''.join(unichr(c) for c in xrange(65536) if not unichr(c).isspace()) word = TextNode.group(Word(unicode_printables)) exact = ExactNode.group(QuotedString('"', unquoteResults=True, escChar='\\')) term = exact | word comparison_name = Word(unicode_printables, excludeChars=':') comparison = ComparisonNode.group(comparison_name + Literal(':') + term) content = OneOrMore(comparison | term) return content.parseString(query)
def parser(cls): unpack = pp.Suppress("unpack") packed_col_name = common_parsers.column dict_key = pp.Suppress("[") + pp.QuotedString(quoteChar="'") + pp.Suppress("]") dict_key_grp = pp.Group(pp.OneOrMore(dict_key)) new_col_name = common_parsers.column unpack_arg = new_col_name + pp.Suppress("=") + packed_col_name + dict_key_grp unpack_arg.setParseAction(lambda x: {'packed_col': x[1], 'key_list': x[2], 'new_col_name': x[0]}) parser = unpack + pp.Suppress("(") + pp.delimitedList(unpack_arg) + pp.Suppress(")") parser.setParseAction(lambda x: Unpack(unpack_list=x)) return parser
def statement(): return pyparsing.Group( _IDENTIFIER.setResultsName("lhs") + _EQUALS + pyparsing.Combine( (anything_in_curly() | pyparsing.QuotedString("'", escChar="\\", unquoteResults=False) | pyparsing.QuotedString("\"", escChar="\\", unquoteResults=False) | _REGEX) + pyparsing.ZeroOrMore(_KEYWORD), adjacent=False, joinString=" ", ).setResultsName("rhs") )
def parse(cls, search=False): """Parse the main query text. This method will also set the class attribute `parsed_search` to the parsed query, and it will return it too. :param cls: The class object, since it is a static method :type cls: object :param search: Search text string if a custom search string is to be used. False if the `cls.search` class attribute is to be used. :type search: str :returns: Parsed query :rtype: list >>> print(DocMatcher.parse('hello author = einstein')) [['hello'], ['author', '=', 'einstein']] >>> print(DocMatcher.parse('')) [] >>> print(\ DocMatcher.parse(\ '"hello world whatever =" tags = \\\'hello ====\\\'')) [['hello world whatever ='], ['tags', '=', 'hello ====']] >>> print(DocMatcher.parse('hello')) [['hello']] """ import pyparsing cls.logger.debug('Parsing search') search = search or cls.search papis_alphas = pyparsing.printables.replace('=', '') papis_key = pyparsing.Word(pyparsing.alphanums + '-') papis_value = pyparsing.QuotedString( quoteChar='"', escChar='\\', escQuote='\\' ) ^ pyparsing.QuotedString( quoteChar="'", escChar='\\', escQuote='\\' ) ^ papis_key equal = pyparsing.ZeroOrMore(" ") + \ pyparsing.Literal('=') + \ pyparsing.ZeroOrMore(" ") papis_query = pyparsing.ZeroOrMore( pyparsing.Group( pyparsing.ZeroOrMore( papis_key + equal ) + papis_value ) ) parsed = papis_query.parseString(search) cls.logger.debug('Parsed search = %s' % parsed) cls.parsed_search = parsed return cls.parsed_search