, operatorPrecedence - . . -, "" "" . , :
and_tok = pyp.Keyword("and")
or_tok = pyp.Keyword("or")
Word = ~(and_tok | or_tok) + pyp.Word(pyp.alphas)("Word")
"" "" "".
, operatorPrecedence. "" - , . operatorPrecedence . Phrase , , amok. operatorPrecedence Word ( pyp.OneOrMore(Word), ).
, operatorPrecedence , ^ Word. , Phrase Expression operatorPrecedence.
, :
Expression = (
pyp.operatorPrecedence(pyp.OneOrMore(Word), [
(and_tok, 2, pyp.opAssoc.LEFT),
(or_tok, 2, pyp.opAssoc.LEFT)
])
)
:
>>> test("Hello and Bob")
<ITEM>
<ITEM>
<Word>Hello</Word>
<AND>and</AND>
<Word>Bob</Word>
</ITEM>
</ITEM>
>>> test("TestA and TestB and TestC or TestD")
<ITEM>
<ITEM>
<ITEM>
<Word>TestA</Word>
<AND>and</AND>
<Word>TestB</Word>
<AND>and</AND>
<Word>TestC</Word>
</ITEM>
<OR>or</OR>
<Word>TestD</Word>
</ITEM>
</ITEM>
, , , , parseAction (operatorrPrecedence ).
( , test("TestA and TestB and TestC or TestD") . , "" "" , (TestA and TestB and TestC) or TestD, , (TestC or TestD) , "" .)