Python pyparsing.OneOrMore() Examples
The following are 28
code examples of pyparsing.OneOrMore().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
pyparsing
, or try the search function
.
Example #1
Source File: parser.py From pdblp with MIT License | 6 votes |
def _parse(mystr): LBRACE, RBRACE, EQUAL = map(pp.Suppress, "{}=") field = pp.Word(pp.printables + ' ', excludeChars='[]=') field.addParseAction(pp.tokenMap(str.rstrip)) string = pp.dblQuotedString().setParseAction(pp.removeQuotes) number = pp.pyparsing_common.number() date_expr = pp.Regex(r'\d\d\d\d-\d\d-\d\d') time_expr = pp.Regex(r'\d\d:\d\d:\d\d\.\d\d\d') nan = pp.Keyword('nan') scalar_value = (string | date_expr | time_expr | number | nan) list_marker = pp.Suppress("[]") value_list = pp.Forward() jobject = pp.Forward() memberDef1 = pp.Group(field + EQUAL + scalar_value) memberDef2 = pp.Group(field + EQUAL + jobject) memberDef3 = pp.Group(field + list_marker + EQUAL + LBRACE + value_list + RBRACE) memberDef = memberDef1 | memberDef2 | memberDef3 value_list <<= (pp.delimitedList(scalar_value, ",") | pp.ZeroOrMore(pp.Group(pp.Dict(memberDef2)))) value_list.setParseAction(lambda t: [pp.ParseResults(t[:])]) members = pp.OneOrMore(memberDef) jobject <<= pp.Dict(LBRACE + pp.ZeroOrMore(memberDef) + RBRACE) # force empty jobject to be a dict jobject.setParseAction(lambda t: t or {}) parser = members parser = pp.OneOrMore(pp.Group(pp.Dict(memberDef))) return parser.parseString(mystr)
Example #2
Source File: sql_graphviz.py From auptimizer with GNU General Public License v3.0 | 6 votes |
def grammar(): parenthesis = Forward() parenthesis <<= "(" + ZeroOrMore(CharsNotIn("()") | parenthesis) + ")" field_def = OneOrMore(Word(alphanums + "_\"'`:-") | parenthesis) field_def.setParseAction(field_act) tablename_def = ( Word(alphas + "`_") | QuotedString("\"") ) field_list_def = field_def + ZeroOrMore(Suppress(",") + field_def) field_list_def.setParseAction(field_list_act) create_table_def = Literal("CREATE") + "TABLE" + tablename_def.setResultsName("tableName") + "(" + field_list_def.setResultsName("fields") + ")" + ";" create_table_def.setParseAction(create_table_act) add_fkey_def = Literal("FOREIGN") + "KEY" + "(" + Word(alphanums).setResultsName("keyName") + ")" + "REFERENCES" + Word(alphanums).setResultsName("fkTable") + "(" + Word(alphanums + "_").setResultsName("fkCol") + ")" + Optional(Literal("DEFERRABLE")) + ";" add_fkey_def.setParseAction(add_fkey_act) other_statement_def = OneOrMore(CharsNotIn(";")) + ";" other_statement_def.setParseAction(other_statement_act) comment_def = "--" + ZeroOrMore(CharsNotIn("\n")) comment_def.setParseAction(other_statement_act) return OneOrMore(comment_def | create_table_def | add_fkey_def | other_statement_def)
Example #3
Source File: c_parser.py From rekall with GNU General Public License v2.0 | 6 votes |
def _element(self): """The parser for all elements.""" self.element = pyparsing.Forward() self.element << ( (~_TYPEDEF) + ( # e.g. int x; self._type_name_with_fields() # e.g. struct s {}; | self._struct_definition_possibly_with_fields() # e.g. enum foo { OPTION = 1 + 2; }; | self._enum_definition() | pyparsing.OneOrMore(_SEMICOLON) ) ) return self.element.setName("element")
Example #4
Source File: macro_expander.py From rekall with GNU General Public License v2.0 | 6 votes |
def expression(self): expression = pyparsing.Forward() # (1 + (2 + 3)) nested_expression = pyparsing.nestedExpr( "(", ")", expression).setParseAction(self._combine_lists) # FOO(2 , 3) function_call = ( _TOKEN().setResultsName("function") + _OPEN_PARENTHESIS() + pyparsing.delimitedList( pyparsing.Combine(expression, adjacent=False, joinString=" "), delim=",").setResultsName("func_args") + _CLOSE_PARENTHESIS() ) expression << pyparsing.OneOrMore( function_call.setParseAction(self._is_known_function) | pyparsing.Group(nested_expression) | _TOKEN() | _NOT_TOKEN() ) return pyparsing.Combine(expression, adjacent=False, joinString=" ")
Example #5
Source File: interpreter.py From design-patterns with MIT License | 6 votes |
def parse(input_string): word = Word(alphanums) command = Group(OneOrMore(word)) token = Suppress("->") device = Group(OneOrMore(word)) argument = Group(OneOrMore(word)) event = command + token + device + Optional(token + argument) parse_results = event.parseString(input_string) cmd = parse_results[0] dev = parse_results[1] cmd_str = "_".join(cmd) dev_str = "_".join(dev) try: val = parse_results[2] except IndexError: val_str = None else: val_str = "_".join(val) return cmd_str, dev_str, val_str
Example #6
Source File: utilities.py From pyactr with GNU General Public License v3.0 | 5 votes |
def getrule(): """ Using pyparsing, get rule out of a string. """ arrow = pp.Literal("==>") buff = pp.Word(pp.alphas, "".join([pp.alphanums, "_"])) special_valueLHS = pp.oneOf([x for x in _LHSCONVENTIONS.keys()]) end_buffer = pp.Literal(">") special_valueRHS = pp.oneOf([x for x in _RHSCONVENTIONS.keys()]) chunk = getchunk() rule_reader = pp.Group(pp.OneOrMore(pp.Group(special_valueLHS + buff + end_buffer + pp.Group(pp.Optional(chunk))))) + arrow + pp.Group(pp.OneOrMore(pp.Group(special_valueRHS + buff + end_buffer + pp.Group(pp.Optional(chunk))))) return rule_reader
Example #7
Source File: api.py From gnocchi with Apache License 2.0 | 5 votes |
def OperationsSchema(v): if isinstance(v, six.text_type): try: v = pyparsing.OneOrMore( pyparsing.nestedExpr()).parseString(v).asList()[0] except pyparsing.ParseException as e: api.abort(400, {"cause": "Invalid operations", "reason": "Fail to parse the operations string", "detail": six.text_type(e)}) return voluptuous.Schema(voluptuous.Any(*OperationsSchemaBase), required=True)(v)
Example #8
Source File: utilities.py From pyactr with GNU General Public License v3.0 | 5 votes |
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
Example #9
Source File: state_transition_diagram.py From pyagram with MIT License | 5 votes |
def lexical_analysis(self, src): string = pp.Regex('[a-zA-Z0-9_{}"=+\-*/\.:;&%@$#<>? a-zA-Zぁ-ゔゞァ-・ヽヾ゛゜ー一-龯]+') blank = pp.LineStart() + pp.LineEnd() start = '[' end = ']' + pp.LineEnd() graph_tag = pp.LineStart() + '@' graph = graph_tag + start + string + end view_tag = pp.LineStart() + '#' view = view_tag + start + string + end server_process_tag = pp.LineStart() + '$' server_process = server_process_tag + start + string + end client_process_tag = pp.LineStart() + '%' client_process = client_process_tag + start + string + end view_transition_identifier = pp.LineStart() + '-->' view_transition = view_transition_identifier + string process_transition_identifier = pp.LineStart() + '==>' process_transition = process_transition_identifier + string state_machine = pp.OneOrMore(graph | view | server_process | client_process | view_transition | process_transition | string | blank) return state_machine.parseString(src)
Example #10
Source File: entity_relationship_diagram.py From pyagram with MIT License | 5 votes |
def lexical_analysis(self, src): delimited = re.sub(r'\s+', ' ', ' '.join(src.strip().split('\n'))).split(';') result = [] for stmt in delimited: if stmt == '': return result string = pp.Regex('[a-zA-Z0-9=_]+') nums = pp.Regex('[0-9]+') ws = pp.OneOrMore(pp.White()).suppress() lp = pp.Regex('[(]').suppress() rp = pp.Regex('[)]').suppress() c = pp.Regex('[,]').suppress() q = pp.Regex("[']").suppress() table_name = string.setResultsName('table_name') create_table = (pp.Keyword('CREATE', caseless = True) + ws + pp.Keyword('TABLE', caseless = True) + ws + pp.Optional(pp.Keyword('IF', caseless = True) + ws + pp.Keyword('NOT', caseless = True) + ws + pp.Keyword('EXISTS', caseless = True))).suppress() + table_name + lp column_name = string.setResultsName('column_name') data_type = string.setResultsName('data_type') length = lp + nums.setResultsName('length') + rp nullable = (pp.Optional(pp.Keyword('NOT', caseless = True) + ws) + pp.Keyword('NULL', caseless = True)).setResultsName('nullable') default_value = pp.Keyword('DEFAULT', caseless = True).suppress() + ws + string.setResultsName('default_value') auto_increment = pp.Keyword('AUTO_INCREMENT', caseless = True).setResultsName('auto_increment') column = pp.Optional(ws) + column_name + ws + data_type + pp.Optional(pp.MatchFirst([length, ws + nullable, ws + default_value, ws + auto_increment])) + pp.Optional(pp.MatchFirst([ws + nullable, ws + default_value, ws + auto_increment])) + pp.Optional(pp.MatchFirst([ws + default_value, ws + auto_increment])) + pp.Optional(ws + auto_increment) + pp.Optional(ws) + c primary_key = pp.Keyword('PRIMARY KEY', caseless = True).suppress() + lp + pp.OneOrMore(q + string.setResultsName('primary_key') + q + pp.Optional(c)) + rp + pp.Optional(c) key = pp.Keyword('KEY', caseless = True).suppress() + lp + q + string.setResultsName('key') + q + pp.Optional(c) + rp + pp.Optional(c) parser = create_table + pp.OneOrMore(pp.Group(column)) + pp.Optional(primary_key) + pp.Optional(key) + rp + pp.OneOrMore(ws + string).suppress() result.append(parser.parseString(stmt, parseAll=True)) return result
Example #11
Source File: aaa.py From ccat with GNU General Public License v3.0 | 5 votes |
def _globalParse___aaa_attributes(line, type, count_aaa): aaa_dict = {} authentication_list = (Suppress('login') + Word(printables)) ('authent_list') authentication_groups = (OneOrMore(Optional(Suppress('group')) + Word(printables))) ('authent_methods') parse_authentication = authentication_list + authentication_groups # parse_authorization_options = MatchFirst(['exec', 'login']) + Word(printables) + OneOrMore(Optional(Suppress('group')) + Word(printables)) accounting_login = (MatchFirst(['exec', 'network', 'connection', 'commands'])) ('acc_login') accounting_list = (Optional(Word(nums)) + Word(printables)) ('acc_list') accounting_record = (MatchFirst(['start-stop', 'stop-only', 'stop'])) ('acc_record') accounting_methods = (OneOrMore(Optional(Suppress('group')) + Word(printables))) ('acc_methods') parse_accounting = accounting_login + accounting_list + accounting_record + accounting_methods if type == 'authentication': result = parse_authentication.parseString(line) aaa_dict.update({'login' + str(count_aaa): {}}) aaa_dict['login' + str(count_aaa)]['list'] = result.authent_list[0] aaa_dict['login' + str(count_aaa)]['methods'] = result.authent_methods.asList() # elif type == 'authorization': # result = parse_authorization_options.parseString(line) # aaa_dict.update({'login' + str(count_aaa): {}}) # aaa_dict['login' + str(count_aaa)]['login'] = result.pop(0) # aaa_dict['login' + str(count_aaa)]['list'] = result.pop(0) # aaa_dict['login' + str(count_aaa)]['methods'] = result.asList() elif type == 'accounting': result = parse_accounting.parseString(line) aaa_dict.update({'login' + str(count_aaa): {}}) aaa_dict['login' + str(count_aaa)]['login'] = result.acc_login aaa_dict['login' + str(count_aaa)]['list'] = result.acc_list.asList() aaa_dict['login' + str(count_aaa)]['record'] = result.acc_record aaa_dict['login' + str(count_aaa)]['methods'] = result.acc_methods.asList() return aaa_dict
Example #12
Source File: core.py From fusesoc with BSD 2-Clause "Simplified" License | 5 votes |
def parse(self, flags): _flags = [] for k, v in flags.items(): if v == True: _flags.append(k) elif v in [False, None]: pass else: _flags.append(k + "_" + v) def cb_conditional(s, l, t): if (t.cond in _flags) != (t.negate == "!"): return t.expr else: return [] word = Word(alphanums + ":<>.[]_-,=~/^~") conditional = Forward() conditional << ( Optional("!")("negate") + word("cond") + Suppress("?") + Suppress("(") + OneOrMore(conditional ^ word)("expr") + Suppress(")") ).setParseAction(cb_conditional) string = word string_list = OneOrMore(conditional ^ string) s = " ".join(string_list.parseString(self.__str__())) logger.debug( "Parsing '{}' with flags {} => {}".format(self.__str__(), str(_flags), s) ) return s
Example #13
Source File: yara_support.py From rekall with GNU General Public License v2.0 | 5 votes |
def rule(): return (_RULE + _IDENTIFIER.setResultsName("name") + _LEFT_CURLY + pyparsing.OneOrMore(section()) + _RIGHT_CURLY)
Example #14
Source File: yara_support.py From rekall with GNU General Public License v2.0 | 5 votes |
def meta_section(): return pyparsing.Group( pyparsing.Literal("meta") + _COLON + pyparsing.OneOrMore( statement() ).setResultsName("statements") ).setResultsName("meta")
Example #15
Source File: expansions.py From pyjsgf with MIT License | 5 votes |
def _make_matcher_element(self): # Define an extra parse action for the child's matcher element. def f(tokens): if tokens.asList(): # Add current match values to the _repetitions_matched list. self._repetitions_matched.append(save_current_matches(self.child)) # Wipe current match values for the next repetition (if any). self.child.reset_for_new_match() return tokens # Get the child's matcher element and add the extra parse action. child_element = self.child.matcher_element.addParseAction(f) # Determine the parser element type to use. type_ = pyparsing.ZeroOrMore if self.is_optional else pyparsing.OneOrMore # Handle the special case of a repetition ancestor, e.g. ((a b)+)+ rep = self.repetition_ancestor if rep: # Check if there are no other branches. c = rep.child only_branch = True while c is not self: if len(c.children) > 1: only_branch = False break else: c = c.children[0] # Use an And element instead if self is the only branch because # it makes no sense to repeat a repeat like this! if only_branch: type_ = pyparsing.And child_element = [child_element] return self._set_matcher_element_attributes(type_(child_element))
Example #16
Source File: case_parser.py From psst with MIT License | 5 votes |
def parse_table(attribute, string): Line = OneOrMore(Float)('data') + Literal(';') + Optional(Comments, default='')('name') Grammar = Suppress(Keyword('mpc.{}'.format(attribute)) + Keyword('=') + Keyword('[') + Optional(Comments)) + OneOrMore(Group(Line)) + Suppress(Keyword(']') + Optional(Comments)) result, i, j = Grammar.scanString(string).next() _list = list() for r in result: _list.append([int_else_float_except_string(s) for s in r['data'].asList()]) return _list
Example #17
Source File: preprocessing_parser.py From rekall with GNU General Public License v2.0 | 5 votes |
def static_function(self): return ( (pyparsing.Keyword("static") | pyparsing.Keyword("inline")) + pyparsing.OneOrMore(pyparsing.Word(pyparsing.alphanums + "_*&")) + parsers.anything_in_parentheses() + parsers.anything_in_curly() ).suppress()
Example #18
Source File: yara_support.py From rekall with GNU General Public License v2.0 | 5 votes |
def yara_parser(): return pyparsing.OneOrMore(rule())
Example #19
Source File: expression_parser.py From rekall with GNU General Public License v2.0 | 5 votes |
def _multiword_argument(self): return pyparsing.Group( self._variable() + pyparsing.OneOrMore(self._variable()) ).setParseAction(util.action(pre_ast.CompositeBlock))
Example #20
Source File: searchparser.py From patzilla with GNU Affero General Public License v3.0 | 4 votes |
def parser(self): """ This function returns a parser. The grammar should be like most full text search engines (Google, Tsearch, Lucene). Grammar: - a query consists of alphanumeric words, with an optional '*' wildcard at the end of a word - a sequence of words between quotes is a literal string - words can be used together by using operators ('and' or 'or') - words with operators can be grouped with parenthesis - a word or group of words can be preceded by a 'not' operator - the 'and' operator precedes an 'or' operator - if an operator is missing, use an 'and' operator """ operatorOr = Forward() operatorWord = Word(wordchars).setResultsName('value') operatorQuotesContent = Forward() operatorQuotesContent << ( (operatorWord + operatorQuotesContent) | operatorWord ) operatorQuotes = Group( Suppress('"') + operatorQuotesContent + Suppress('"') ).setResultsName("quotes") | operatorWord prefix = (Word(alphanums).setResultsName('index') + Word('=').setResultsName('binop')) operatorParenthesis = Group( Optional(prefix) + (Suppress("(") + operatorOr + Suppress(")")) ).setResultsName("parenthesis") | Group(prefix + operatorQuotes).setResultsName('term') | operatorQuotes operatorNot = Forward() operatorNot << (Group( Suppress(Keyword("not", caseless=True)) + operatorNot ).setResultsName("not") | operatorParenthesis) operatorAnd = Forward() operatorAnd << (Group( operatorNot + Suppress(Keyword("and", caseless=True)) + operatorAnd ).setResultsName("and") | Group( operatorNot + OneOrMore(~oneOf("and or", caseless=True) + operatorAnd) ).setResultsName("and") | operatorNot) operatorProximity = Forward() operatorProximity << (Group( operatorParenthesis + Suppress(Literal("near,")) + Word(nums).setResultsName('distance') + operatorParenthesis ).setResultsName("near") | Group( operatorParenthesis + Suppress(Literal("span,")) + Word(nums).setResultsName('distance') + operatorParenthesis ).setResultsName("span") | operatorAnd) operatorOr << (Group( operatorProximity + Suppress(Keyword("or", caseless=True)) + operatorOr ).setResultsName("or") | operatorProximity) return operatorOr.parseString
Example #21
Source File: expansions.py From pyjsgf with MIT License | 4 votes |
def _make_matcher_element(self): # Handle the case where use_current_match is True. if self.use_current_match is True: current_match = self.current_match if current_match is None: result = pyparsing.NoMatch() elif current_match == "": result = pyparsing.Empty() else: result = pyparsing.Literal(self.current_match) # Set the parse action and return the element. return result.setParseAction(self._parse_action) # Otherwise build a list of next possible literals. Make the required stack # of child-parent pairs. stack = [] p1, p2 = self, self.parent while p1 and p2: stack.append((p1, p2)) # Move both pivots further up the tree. p1 = p1.parent p2 = p2.parent # Build a list of next literals using the stack. next_literals, _ = _collect_next_literals(stack, 0, True, False) # De-duplicate the list. next_literals = set(next_literals) word = pyparsing.Regex(_word_regex_str, re.UNICODE) if next_literals: # Check if there is a next dictation literal. If there is, only match # one word for this expansion. if _word_regex_str in next_literals: result = word # Otherwise build an element to match one or more words stopping on # any of the next literals so that they aren't matched as dictation. else: next_literals = list(map(pyparsing.Literal, next_literals)) result = pyparsing.OneOrMore( word, stopOn=pyparsing.Or(next_literals) ) else: # Handle the case of no literals ahead by allowing one or more Unicode # words without restrictions. result = pyparsing.OneOrMore(word) return self._set_matcher_element_attributes(result)
Example #22
Source File: interpreter.py From Python_Master-the-Art-of-Design-Patterns with MIT License | 4 votes |
def main(): word = Word(alphanums) command = Group(OneOrMore(word)) token = Suppress("->") device = Group(OneOrMore(word)) argument = Group(OneOrMore(word)) event = command + token + device + Optional(token + argument) gate = Gate() garage = Garage() airco = Aircondition() heating = Heating() boiler = Boiler() fridge = Fridge() tests = ('open -> gate', 'close -> garage', 'turn on -> aircondition', 'turn off -> heating', 'increase -> boiler temperature -> 20 degrees', 'decrease -> fridge temperature -> 6 degree') open_actions = {'gate':gate.open, 'garage':garage.open, 'aircondition':airco.turn_on, 'heating':heating.turn_on, 'boiler temperature':boiler.increase_temperature, 'fridge temperature':fridge.increase_temperature} close_actions = {'gate':gate.close, 'garage':garage.close, 'aircondition':airco.turn_off, 'heating':heating.turn_off, 'boiler temperature':boiler.decrease_temperature, 'fridge temperature':fridge.decrease_temperature} for t in tests: if len(event.parseString(t)) == 2: # no argument cmd, dev = event.parseString(t) cmd_str, dev_str = ' '.join(cmd), ' '.join(dev) if 'open' in cmd_str or 'turn on' in cmd_str: open_actions[dev_str]() elif 'close' in cmd_str or 'turn off' in cmd_str: close_actions[dev_str]() elif len(event.parseString(t)) == 3: # argument cmd, dev, arg = event.parseString(t) cmd_str, dev_str, arg_str = ' '.join(cmd), ' '.join(dev), ' '.join(arg) num_arg = 0 try: num_arg = int(arg_str.split()[0]) # extract the numeric part except ValueError as err: print("expected number but got: '{}'".format(arg_str[0])) if 'increase' in cmd_str and num_arg > 0: open_actions[dev_str](num_arg) elif 'decrease' in cmd_str and num_arg > 0: close_actions[dev_str](num_arg)
Example #23
Source File: interpreter.py From Python_Master-the-Art-of-Design-Patterns with MIT License | 4 votes |
def main(): word = Word(alphanums) command = Group(OneOrMore(word)) token = Suppress("->") device = Group(OneOrMore(word)) argument = Group(OneOrMore(word)) event = command + token + device + Optional(token + argument) gate = Gate() garage = Garage() airco = Aircondition() heating = Heating() boiler = Boiler() fridge = Fridge() tests = ('open -> gate', 'close -> garage', 'turn on -> aircondition', 'turn off -> heating', 'increase -> boiler temperature -> 5 degrees', 'decrease -> fridge temperature -> 2 degrees') open_actions = {'gate':gate.open, 'garage':garage.open, 'aircondition':airco.turn_on, 'heating':heating.turn_on, 'boiler temperature':boiler.increase_temperature, 'fridge temperature':fridge.increase_temperature} close_actions = {'gate':gate.close, 'garage':garage.close, 'aircondition':airco.turn_off, 'heating':heating.turn_off, 'boiler temperature':boiler.decrease_temperature, 'fridge temperature':fridge.decrease_temperature} for t in tests: if len(event.parseString(t)) == 2: # no argument cmd, dev = event.parseString(t) cmd_str, dev_str = ' '.join(cmd), ' '.join(dev) if 'open' in cmd_str or 'turn on' in cmd_str: open_actions[dev_str]() elif 'close' in cmd_str or 'turn off' in cmd_str: close_actions[dev_str]() elif len(event.parseString(t)) == 3: # argument cmd, dev, arg = event.parseString(t) cmd_str, dev_str, arg_str = ' '.join(cmd), ' '.join(dev), ' '.join(arg) num_arg = 0 try: num_arg = int(arg_str.split()[0]) # extract the numeric part except ValueError as err: print("expected number but got: '{}'".format(arg_str[0])) if 'increase' in cmd_str and num_arg > 0: open_actions[dev_str](num_arg) elif 'decrease' in cmd_str and num_arg > 0: close_actions[dev_str](num_arg)
Example #24
Source File: svg.py From pcbmode with MIT License | 4 votes |
def svg_grammar(): """ Returns the grammar used to parse SVG paths """ # pyparsing grammar comma = PYP.Literal(",").suppress() # supress removes the ',' when captured dot = PYP.Literal(".") space = PYP.Literal(' ') coord = PYP.Regex(r"-?\d+(\.\d*)?([Ee][+-]?\d+)?") one_coord = PYP.Group(coord) xycoords = PYP.Group(coord + PYP.Optional(comma) + coord) two_xycoords = xycoords + PYP.Optional(comma) + xycoords three_xycoords = xycoords + PYP.Optional(comma) + xycoords + PYP.Optional(comma)+xycoords # TODO optimise this; there has to be a more efficient way to describe this c_M = PYP.Literal('M') + PYP.OneOrMore(xycoords) c_m = PYP.Literal('m') + PYP.OneOrMore(xycoords) c_C = PYP.Literal('C') + PYP.OneOrMore(three_xycoords) c_c = PYP.Literal('c') + PYP.OneOrMore(three_xycoords) c_Q = PYP.Literal('Q') + PYP.OneOrMore(two_xycoords) c_q = PYP.Literal('q') + PYP.OneOrMore(two_xycoords) c_T = PYP.Literal('T') + PYP.OneOrMore(xycoords) c_t = PYP.Literal('t') + PYP.OneOrMore(xycoords) c_L = PYP.Literal('L') + PYP.OneOrMore(xycoords) c_l = PYP.Literal('l') + PYP.OneOrMore(xycoords) c_V = PYP.Literal('V') + PYP.OneOrMore(one_coord) c_v = PYP.Literal('v') + PYP.OneOrMore(one_coord) c_H = PYP.Literal('H') + PYP.OneOrMore(one_coord) c_h = PYP.Literal('h') + PYP.OneOrMore(one_coord) c_S = PYP.Literal('S') + PYP.OneOrMore(two_xycoords) c_s = PYP.Literal('s') + PYP.OneOrMore(two_xycoords) c_z = PYP.Literal('z') c_Z = PYP.Literal('Z') path_cmd = c_M | c_m | c_C | c_c | c_Q | c_q | c_T | c_t | c_L | c_l | c_V | c_v | c_H | c_h | c_S | c_s | c_Z | c_z # return the format we're after return PYP.OneOrMore(PYP.Group(path_cmd))
Example #25
Source File: svgpath.py From pcbmode with MIT License | 4 votes |
def _makeSVGGrammar(self): """ Creates an SVG path parsing grammar """ # pyparsing grammar comma = PYP.Literal(",").suppress() # supress removes the ',' when captured dot = PYP.Literal(".") coord = PYP.Regex(r"-?\d+(\.\d*)?([Ee][+-]?\d+)?") one_coord = PYP.Group(coord) xycoords = PYP.Group(coord + PYP.Optional(comma) + coord) two_xycoords = xycoords + PYP.Optional(comma) + xycoords three_xycoords = xycoords + PYP.Optional(comma) + xycoords + PYP.Optional(comma)+xycoords # TODO optimise this; there has to be a more efficient way to describe this c_M = PYP.Literal('M') + PYP.OneOrMore(xycoords) c_m = PYP.Literal('m') + PYP.OneOrMore(xycoords) c_C = PYP.Literal('C') + PYP.OneOrMore(three_xycoords) c_c = PYP.Literal('c') + PYP.OneOrMore(three_xycoords) c_Q = PYP.Literal('Q') + PYP.OneOrMore(two_xycoords) c_q = PYP.Literal('q') + PYP.OneOrMore(two_xycoords) c_T = PYP.Literal('T') + PYP.OneOrMore(xycoords) c_t = PYP.Literal('t') + PYP.OneOrMore(xycoords) c_L = PYP.Literal('L') + PYP.OneOrMore(xycoords) c_l = PYP.Literal('l') + PYP.OneOrMore(xycoords) c_V = PYP.Literal('V') + PYP.OneOrMore(one_coord) c_v = PYP.Literal('v') + PYP.OneOrMore(one_coord) c_H = PYP.Literal('H') + PYP.OneOrMore(one_coord) c_h = PYP.Literal('h') + PYP.OneOrMore(one_coord) c_S = PYP.Literal('S') + PYP.OneOrMore(two_xycoords) c_s = PYP.Literal('s') + PYP.OneOrMore(two_xycoords) c_z = PYP.Literal('z') c_Z = PYP.Literal('Z') path_cmd = c_M | c_m | c_C | c_c | c_Q | c_q | c_T | c_t | c_L | c_l | c_V | c_v | c_H | c_h | c_S | c_s | c_Z | c_z return PYP.OneOrMore(PYP.Group(path_cmd))
Example #26
Source File: interpreter.py From Mastering-Python-Design-Patterns-Second-Edition with MIT License | 4 votes |
def main(): word = Word(alphanums) command = Group(OneOrMore(word)) token = Suppress("->") device = Group(OneOrMore(word)) argument = Group(OneOrMore(word)) event = command + token + device + Optional(token + argument) gate = Gate() garage = Garage() airco = Aircondition() heating = Heating() boiler = Boiler() fridge = Fridge() tests = ('open -> gate', 'close -> garage', 'turn on -> air condition', 'turn off -> heating', 'increase -> boiler temperature -> 5 degrees', 'decrease -> fridge temperature -> 2 degrees') open_actions = {'gate':gate.open, 'garage':garage.open, 'air condition':airco.turn_on, 'heating':heating.turn_on, 'boiler temperature':boiler.increase_temperature, 'fridge temperature':fridge.increase_temperature} close_actions = {'gate':gate.close, 'garage':garage.close, 'air condition':airco.turn_off, 'heating':heating.turn_off, 'boiler temperature':boiler.decrease_temperature, 'fridge temperature':fridge.decrease_temperature} for t in tests: if len(event.parseString(t)) == 2: # no argument cmd, dev = event.parseString(t) cmd_str, dev_str = ' '.join(cmd), ' '.join(dev) if 'open' in cmd_str or 'turn on' in cmd_str: open_actions[dev_str]() elif 'close' in cmd_str or 'turn off' in cmd_str: close_actions[dev_str]() elif len(event.parseString(t)) == 3: # argument cmd, dev, arg = event.parseString(t) cmd_str = ' '.join(cmd) dev_str = ' '.join(dev) arg_str = ' '.join(cmd) num_arg = 0 try: # extract the numeric part num_arg = int(arg_str.split()[0]) except ValueError as err: print(f"expected number but got: '{arg_str[0]}'") if 'increase' in cmd_str and num_arg > 0: open_actions[dev_str](num_arg) elif 'decrease' in cmd_str and num_arg > 0: close_actions[dev_str](num_arg)
Example #27
Source File: shparsers.py From stash with MIT License | 4 votes |
def __init__(self, debug=False): self.debug = debug self.logger = logging.getLogger('StaSh.Parser') escaped = pp.Combine("\\" + pp.Word(pp.printables + ' ', exact=1)).setParseAction(self.escaped_action) escaped_oct = pp.Combine("\\" + pp.Word('01234567', max=3)).setParseAction(self.escaped_oct_action) escaped_hex = pp.Combine("\\x" + pp.Word('0123456789abcdefABCDEF', exact=2)).setParseAction(self.escaped_hex_action) # Some special uq_word is needed, e.g. &3 for file descriptor of Pythonista interactive prompt uq_word = (pp.Literal('&3') | pp.Word(_WORD_CHARS)).setParseAction(self.uq_word_action) bq_word = pp.QuotedString('`', escChar='\\', unquoteResults=False).setParseAction(self.bq_word_action) dq_word = pp.QuotedString('"', escChar='\\', unquoteResults=False).setParseAction(self.dq_word_action) sq_word = pp.QuotedString("'", escChar='\\', unquoteResults=False).setParseAction(self.sq_word_action) # The ^ operator means longest match (as opposed to | which means first match) word = pp.Combine(pp.OneOrMore(escaped ^ escaped_oct ^ escaped_hex ^ uq_word ^ bq_word ^ dq_word ^ sq_word))\ .setParseAction(self.word_action) identifier = pp.Word(pp.alphas + '_', pp.alphas + pp.nums + '_').setParseAction(self.identifier_action) assign_op = pp.Literal('=').setParseAction(self.assign_op_action) assignment_word = pp.Combine(identifier + assign_op + word).setParseAction(self.assignment_word_action) punctuator = pp.oneOf('; &').setParseAction(self.punctuator_action) pipe_op = pp.Literal('|').setParseAction(self.pipe_op_action) io_redirect_op = pp.oneOf('>> >').setParseAction(self.io_redirect_op_action) io_redirect = (io_redirect_op + word)('io_redirect') # The optional ' ' is a workaround to a possible bug in pyparsing. # The position of cmd_word after cmd_prefix is always reported 1 character ahead # of the correct value. cmd_prefix = (pp.OneOrMore(assignment_word) + pp.Optional(' '))('cmd_prefix') cmd_suffix = (pp.OneOrMore(word)('args') + pp.Optional(io_redirect)) ^ io_redirect modifier = pp.oneOf('! \\') cmd_word = (pp.Combine(pp.Optional(modifier) + word) ^ word)('cmd_word').setParseAction(self.cmd_word_action) simple_command = \ (cmd_prefix + pp.Optional(cmd_word) + pp.Optional(cmd_suffix)) \ | (cmd_word + pp.Optional(cmd_suffix)) simple_command = pp.Group(simple_command) pipe_sequence = simple_command + pp.ZeroOrMore(pipe_op + simple_command) pipe_sequence = pp.Group(pipe_sequence) complete_command = pp.Optional(pipe_sequence + pp.ZeroOrMore(punctuator + pipe_sequence) + pp.Optional(punctuator)) # --- special parser for inside double quotes uq_word_in_dq = pp.Word(pp.printables.replace('`', ' ').replace('\\', ''))\ .setParseAction(self.uq_word_action) word_in_dq = pp.Combine(pp.OneOrMore(escaped ^ escaped_oct ^ escaped_hex ^ bq_word ^ uq_word_in_dq)) # --- self.parser = complete_command.parseWithTabs().ignore(pp.pythonStyleComment) self.parser_within_dq = word_in_dq.leaveWhitespace() self.next_word_type = ShParser._NEXT_WORD_CMD self.tokens = [] self.parts = []
Example #28
Source File: searchparser.py From phpsploit with GNU General Public License v3.0 | 4 votes |
def parser(self): """ This function returns a parser. The grammar should be like most full text search engines (Google, Tsearch, Lucene). Grammar: - a query consists of alphanumeric words, with an optional '*' wildcard at the end of a word - a sequence of words between quotes is a literal string - words can be used together by using operators ('and' or 'or') - words with operators can be grouped with parenthesis - a word or group of words can be preceded by a 'not' operator - the 'and' operator precedes an 'or' operator - if an operator is missing, use an 'and' operator """ operatorOr = Forward() operatorWord = Group(Combine(Word(alphanums) + Suppress('*'))).setResultsName('wordwildcard') | \ Group(Word(alphanums)).setResultsName('word') operatorQuotesContent = Forward() operatorQuotesContent << ( (operatorWord + operatorQuotesContent) | operatorWord ) operatorQuotes = Group( Suppress('"') + operatorQuotesContent + Suppress('"') ).setResultsName("quotes") | operatorWord operatorParenthesis = Group( (Suppress("(") + operatorOr + Suppress(")")) ).setResultsName("parenthesis") | operatorQuotes operatorNot = Forward() operatorNot << (Group( Suppress(Keyword("not", caseless=True)) + operatorNot ).setResultsName("not") | operatorParenthesis) operatorAnd = Forward() operatorAnd << (Group( operatorNot + Suppress(Keyword("and", caseless=True)) + operatorAnd ).setResultsName("and") | Group( operatorNot + OneOrMore(~oneOf("and or") + operatorAnd) ).setResultsName("and") | operatorNot) operatorOr << (Group( operatorAnd + Suppress(Keyword("or", caseless=True)) + operatorOr ).setResultsName("or") | operatorAnd) return operatorOr.parseString