expression = pp.Forward() valid_word = pp.Regex( r'([a-zA-Z0-9*_+.-]|\\\\|\\([+\-!(){}\[\]^"~*?:]|\|\||&&))+' ).setName("word") valid_word.setParseAction( lambda t: t[0].replace("\\\\", chr(127)).replace("\\", "").replace(chr(127), "\\") ) string = pp.QuotedString('"') required_modifier = pp.Literal("+")("required") prohibit_modifier = pp.Literal("-")("prohibit") integer = ppc.integer() proximity_modifier = pp.Group(TILDE + integer("proximity")) number = ppc.fnumber() fuzzy_modifier = TILDE + pp.Optional(number, default=0.5)("fuzzy") term = pp.Forward() field_name = valid_word().setName("fieldname") incl_range_search = pp.Group(LBRACK - term("lower") + to_ + term("upper") + RBRACK) excl_range_search = pp.Group(LBRACE - term("lower") + to_ + term("upper") + RBRACE) range_search = incl_range_search("incl_range") | excl_range_search("excl_range") boost = CARAT - number("boost") string_expr = pp.Group(string + proximity_modifier) | string word_expr = pp.Group(valid_word + fuzzy_modifier) | valid_word term << ( pp.Optional(field_name("field") + COLON) + (word_expr | string_expr | range_search | pp.Group(LPAR + expression + RPAR)) + pp.Optional(boost)
keyword = and_ | or_ | not_ | to_ expression = Forward() valid_word = Regex(r'([a-zA-Z0-9*_+.-]|\\[!(){}\[\]^"~*?\\:])+').setName( "word") valid_word.setParseAction(lambda t: t[0].replace('\\\\', chr(127)).replace( '\\', '').replace(chr(127), '\\')) string = QuotedString('"') required_modifier = Literal("+")("required") prohibit_modifier = Literal("-")("prohibit") integer = Regex(r"\d+").setParseAction(lambda t: int(t[0])) proximity_modifier = Group(TILDE + integer("proximity")) number = pyparsing_common.fnumber() fuzzy_modifier = TILDE + Optional(number, default=0.5)("fuzzy") term = Forward() field_name = valid_word().setName("fieldname") incl_range_search = Group(LBRACK + term("lower") + to_ + term("upper") + RBRACK) excl_range_search = Group(LBRACE + term("lower") + to_ + term("upper") + RBRACE) range_search = incl_range_search("incl_range") | excl_range_search( "excl_range") boost = (CARAT + number("boost")) string_expr = Group(string + proximity_modifier) | string word_expr = Group(valid_word + fuzzy_modifier) | valid_word term << (Optional(field_name("field") + COLON) +
# in order to use the int, we need to parse it manually... print(2 * int(res[2])) # option 2 (using named parser parts) nam = Word(alphas) integer = Word(nums) assgnmt_v2 = nam("varname") + "=" + integer("value") res = assgnmt_v2.parseString(assgnmt_str) print(assgnmt_str, "->", res, repr(res)) print(res["varname"], res["value"]) # option 3 from pyparsing import pyparsing_common as cm # there is a predefined f(loating?)number available... assgnmt_v3 = Word(alphas) + "=" + cm.fnumber() assgnmt_str2 = "a = 34.67" res = assgnmt_v3.parseString(assgnmt_str) print(res, 2 * res[2]) res = assgnmt_v3.parseString(assgnmt_str2) print(res, 2 * res[2]) # run several parsing problems as test at once assgnmt_v3.runTests("""\ a = 1.2 b = -4 a = 1.2e4 a = 0.001""")
keyword = and_ | or_ | not_ | to_ expression = Forward() valid_word = Regex(r'([a-zA-Z0-9*_+.-]|\\[!(){}\[\]^"~*?\\:])+').setName("word") valid_word.setParseAction( lambda t : t[0].replace('\\\\',chr(127)).replace('\\','').replace(chr(127),'\\') ) string = QuotedString('"') required_modifier = Literal("+")("required") prohibit_modifier = Literal("-")("prohibit") integer = Regex(r"\d+").setParseAction(lambda t:int(t[0])) proximity_modifier = Group(TILDE + integer("proximity")) number = pyparsing_common.fnumber() fuzzy_modifier = TILDE + Optional(number, default=0.5)("fuzzy") term = Forward() field_name = valid_word().setName("fieldname") incl_range_search = Group(LBRACK + term("lower") + to_ + term("upper") + RBRACK) excl_range_search = Group(LBRACE + term("lower") + to_ + term("upper") + RBRACE) range_search = incl_range_search("incl_range") | excl_range_search("excl_range") boost = (CARAT + number("boost")) string_expr = Group(string + proximity_modifier) | string word_expr = Group(valid_word + fuzzy_modifier) | valid_word term << (Optional(field_name("field") + COLON) + (word_expr | string_expr | range_search | Group(LPAR + expression + RPAR)) + Optional(boost)) term.setParseAction(lambda t:[t] if 'field' in t or 'boost' in t else None)