summaryrefslogtreecommitdiff
path: root/examples/lucene_grammar.py
diff options
context:
space:
mode:
authorptmcg <ptmcg@austin.rr.com>2019-01-30 20:41:33 -0600
committerptmcg <ptmcg@austin.rr.com>2019-01-30 20:41:33 -0600
commit3157a77c9584a69839db0f8274c87438b912e99b (patch)
tree5e93a6e21715f9afcbc55eba56f4a5f962b67a46 /examples/lucene_grammar.py
parentf10a02039a6039f8a42b0cc99bea51623676cf01 (diff)
downloadpyparsing-git-3157a77c9584a69839db0f8274c87438b912e99b.tar.gz
Update examples and unit tests to more preferred coding styles, imports for pyparsing_common as ppc and pyparsing_unicode as ppu
Diffstat (limited to 'examples/lucene_grammar.py')
-rw-r--r--examples/lucene_grammar.py57
1 files changed, 28 insertions, 29 deletions
diff --git a/examples/lucene_grammar.py b/examples/lucene_grammar.py
index 6afaad4..07eb319 100644
--- a/examples/lucene_grammar.py
+++ b/examples/lucene_grammar.py
@@ -7,52 +7,51 @@
# at http://svn.apache.org/viewvc/lucene/dev/trunk/lucene/docs/queryparsersyntax.html
#
-from pyparsing import (Literal, CaselessKeyword, Forward, Regex, QuotedString, Suppress,
- Optional, Group, infixNotation, opAssoc, ParserElement,
- pyparsing_common)
-ParserElement.enablePackrat()
+import pyparsing as pp
+from pyparsing import pyparsing_common as ppc
+pp.ParserElement.enablePackrat()
-COLON,LBRACK,RBRACK,LBRACE,RBRACE,TILDE,CARAT = map(Literal,":[]{}~^")
-LPAR,RPAR = map(Suppress,"()")
-and_, or_, not_, to_ = map(CaselessKeyword, "AND OR NOT TO".split())
+COLON,LBRACK,RBRACK,LBRACE,RBRACE,TILDE,CARAT = map(pp.Literal,":[]{}~^")
+LPAR,RPAR = map(pp.Suppress,"()")
+and_, or_, not_, to_ = map(pp.CaselessKeyword, "AND OR NOT TO".split())
keyword = and_ | or_ | not_ | to_
-expression = Forward()
+expression = pp.Forward()
-valid_word = Regex(r'([a-zA-Z0-9*_+.-]|\\\\|\\([+\-!(){}\[\]^"~*?:]|\|\||&&))+').setName("word")
+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 = QuotedString('"')
+string = pp.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")
+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 = Forward()
+term = pp.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)
+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 = 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))
+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))
term.setParseAction(lambda t:[t] if 'field' in t or 'boost' in t else None)
-expression << infixNotation(term,
+expression << pp.infixNotation(term,
[
- (required_modifier | prohibit_modifier, 1, opAssoc.RIGHT),
- ((not_ | '!').setParseAction(lambda: "NOT"), 1, opAssoc.RIGHT),
- ((and_ | '&&').setParseAction(lambda: "AND"), 2, opAssoc.LEFT),
- (Optional(or_ | '||').setParseAction(lambda: "OR"), 2, opAssoc.LEFT),
+ (required_modifier | prohibit_modifier, 1, pp.opAssoc.RIGHT),
+ ((not_ | '!').setParseAction(lambda: "NOT"), 1, pp.opAssoc.RIGHT),
+ ((and_ | '&&').setParseAction(lambda: "AND"), 2, pp.opAssoc.LEFT),
+ (pp.Optional(or_ | '||').setParseAction(lambda: "OR"), 2, pp.opAssoc.LEFT),
])
# test strings taken from grammar description doc, and TestQueryParser.java