我们从Python开源项目中,提取了以下9个代码示例,用于说明如何使用pyparsing.Or()。
def __init__(self): # Uncomment this line to disable Python-style comments but still allow C-style comments # self.commentGrammars = pyparsing.Or([pyparsing.cStyleComment]) # Create command aliases which are shorter self.shortcuts.update({'ap': 'aprint', 'op': 'oprint'}) # Make sure to call this super class __init__ *after* setting commentGrammars and/or updating shortcuts cmd2.Cmd.__init__(self) # NOTE: It is critical that the super class __init__ method be called AFTER updating certain parameters which # are not settable at runtime. This includes the commentGrammars, shortcuts, multilineCommands, etc.
def _build_precedence(self, precedence_table): # C's & dereference operator. precedence = [] for operators, arity, associativity in precedence_table: operators = [pyparsing.Literal(x) for x in operators] if arity in [_UNARY, _BINARY]: operators = pyparsing.Or(operators) precedence.append(( operators, arity, associativity, self._construct_operator(arity), )) return precedence
def _complete_statement(self, line): """Keep accepting lines of input until the command is complete.""" if not line or (not pyparsing.Or(self.commentGrammars).setParseAction(lambda x: '').transformString(line)): raise EmptyStatement() statement = self.parser_manager.parsed(line) while statement.parsed.multilineCommand and (statement.parsed.terminator == ''): statement = '%s\n%s' % (statement.parsed.raw, self.pseudo_raw_input(self.continuation_prompt)) statement = self.parser_manager.parsed(statement) if not statement.parsed.command: raise EmptyStatement() return statement
def _last_matching(self, arg): """Return the last item from the history list that matches arg. Or if arg not provided, return last item. If not match is found, return None. :param arg: str - text to search for in history :return: str - last match, last item, or None, depending on arg. """ try: if arg: return self.history.get(arg)[-1] else: return self.history[-1] except IndexError: return None
def __update_grammar(self, grammar): registers = list(self.registers) grammar.kRegisterNames << pp.Or(map(pp.Literal, list(self.registers))) return grammar
def __init__(self, rule): self.rule = rule self.opener = self.rule['opener'] self.closer = self.rule['closer'] self.columns = self.rule.get('columns', -1) nested = nestedExpr(opener=self.opener, closer=self.closer, content=CharsNotIn(self.opener + self.closer)) if self.columns < 0: self.nested = OneOrMore(nested) else: self.nested = nested * self.columns + Or([CharsNotIn('\n'), Empty()])
def complete_statement(self, line): """Keep accepting lines of input until the command is complete.""" if not line or (not pyparsing.Or(self.commentGrammars).setParseAction(lambda x: '').transformString(line)): raise EmptyStatement() statement = self.parsed(line) while statement.parsed.multilineCommand and (statement.parsed.terminator == ''): statement = '%s\n%s' % (statement.parsed.raw, self.pseudo_raw_input(self.continuation_prompt)) statement = self.parsed(statement) if not statement.parsed.command: raise EmptyStatement() return statement
def __init__(self, comm_file_path): expression_spaced = Forward() expression = Forward() args_spaced = Forward() cb = Optional(',') + ')' # closing_brackets might include a ',' ob = Optional(' ') + '(' + Optional(' ') # closing_brackets might include a ' ' value = (Or([pyparsing_common.identifier.copy().setResultsName('id'), pyparsing_common.number.copy().setResultsName('number'), QuotedString("'").setResultsName('string')])).setParseAction(Value).setResultsName('value') values = (ZeroOrMore(value.setResultsName('valueList', listAllMatches=True) + Optional(','))).setParseAction( Values) keyword = pyparsing_common.identifier.copy() keyword_argument = ( keyword.setResultsName('keyword') + '=' + expression_spaced.setResultsName('expression') ).setParseAction(Keyword_argument) keyword_arguments = ( keyword_argument.setResultsName('keyword_argument', listAllMatches=True) + ZeroOrMore(',' + keyword_argument.setResultsName('keyword_argument', listAllMatches=True)) ).setParseAction(Keyword_arguments) expression << (Or([ value, (ob + values.setResultsName('values') + cb), '_F' + ob + keyword_arguments.setResultsName('keyword_arguments') + cb, ob + expression.setResultsName('expression') + cb ])).setParseAction(Expression) expression_spaced << (Or([expression, ob + expression_spaced + cb])) left_side = pyparsing_common.identifier.setResultsName('left_side') operator_name = pyparsing_common.identifier.setResultsName('operator_name') paragraph = (Optional(left_side + "=") + operator_name + ob + Optional(keyword_arguments .setResultsName( 'keyword_arguments')) + cb + Optional(';')).setParseAction(Paragraph) file = OneOrMore(paragraph).setResultsName('paragraphs').setParseAction(File) self.beam_data_model = file.parseFile(comm_file_path)