def parse_element(cls, indent_stack): """Sets ``connect`` attribute to the rule""" return ( Keyword("@connect").suppress() + Literal('(').suppress() + quotedString.setResultsName('field').setParseAction(removeQuotes) + Optional(Literal(',').suppress() + SkipTo(')')).setResultsName('func') + Literal(')').suppress()).setResultsName("connect").setParseAction( lambda toks: { 'connected_field': toks.field, 'update_function': toks.func[0] if toks.func else None })
def parse_element(cls, indent_stack): """Sets ``connect`` attribute to the rule""" return (Keyword("@connect").suppress() + Literal('(').suppress() + quotedString.setResultsName('field') .setParseAction(removeQuotes) + Optional(Literal(',').suppress() + SkipTo(')')).setResultsName('func') + Literal(')').suppress() ).setResultsName("connect").setParseAction( lambda toks: { 'connected_field': toks.field, 'update_function': toks.func[0] if toks.func else None })
def _create_parser() -> ParserElement: """ Creates the parser to use for parsing filter expressions. :return: The created parser. """ # Defining what a variable is. main_variable = '${' + Word(alphanums + '-._ ') + '}' main_variable = main_variable.setResultsName('variable') main_variable.setParseAction(lambda toks: toks[1]) second_variable = main_variable.copy().setResultsName('second_variable') # Defining what an operator is. operator = oneOf(' '.join(Operator.symbols())) operator = operator.setResultsName('operator') # Defining what the different possible values are. real = pyparsing_common.real.setResultsName('real') integer = pyparsing_common.integer.setResultsName('integer') boolean = oneOf('true false', caseless=True) boolean = boolean.setResultsName('boolean') boolean.setParseAction(lambda toks: list(map(lambda b: b.lower() == 'true', toks))) string = quotedString.setResultsName('string') string.setParseAction(lambda toks: list(map(lambda s: s[1:-1], toks))) # Defining what a single value is. single_value = real | integer | boolean | string single_value = single_value.setResultsName('value') # Defining what a list of values is. list_value = '[' + delimitedList(single_value) + ']' list_value = list_value.setResultsName('list') # Defining what a general value is. value = single_value | list_value # Defining what a complete expression is. variable_right = value + operator + main_variable variable_right = variable_right.setResultsName('right') variable_left = main_variable + operator + value variable_left = variable_left.setResultsName('left') both_variable = main_variable + operator + second_variable both_variable = both_variable.setResultsName('both') return variable_right | variable_left | both_variable | main_variable
_sign = Word("+-", exact=1) number = ( Combine(Optional(_sign) + Word(nums) + Optional("." + Optional(Word(nums)))) .setName("number") .setResultsName("number") ) integer = ( Combine(Optional(_sign) + Word(nums)).setName("number").setResultsName("number") ) boolean = (upkey("true") | upkey("false")).setName("bool") binary = Combine("b" + quotedString) value = Forward() json_value = Forward() string = quotedString.setResultsName("str") json_primitive = ( null.setResultsName("null") | number | string | boolean.setResultsName("bool") ) set_primitive = ( number.setResultsName("number") | quotedString.setResultsName("str") | binary.setResultsName("binary") ) primitive = json_primitive | binary.setResultsName("binary") _emptyset = Keyword("()").setResultsName("set") set_ = ( Suppress("(") + delimitedList(Group(set_primitive)) + Suppress(")") ).setResultsName("set") list_ = ( Suppress("[") + Optional(delimitedList(json_value)) + Suppress("]")
diffusion_entry_ = Group((diffusion_constant_2d_.setResultsName('2D') | diffusion_constant_3d_.setResultsName('3D')) + Suppress(equal) + (function_entry_.setResultsName('function') | (identifier | numarg).setResultsName('variable'))) molecule_entry = Group(molecule_definition + Optional(Group(lbrace + Optional(diffusion_entry_.setResultsName('diffusionFunction')) + (ZeroOrMore(statement)).setResultsName('moleculeParameters') + rbrace))) hashed_molecule_section = Group(hashsymbol + Suppress(define_molecules_) + lbrace + OneOrMore(molecule_entry) + rbrace) # hash function entry function_name = Group(identifier + '()') math_function_entry = Group(function_name.setResultsName('functionName') + Suppress(equal) + Group(restOfLine).setResultsName('functionBody')) hashed_function_section = Group(hashsymbol + Suppress(define_functions_) + lbrace + ZeroOrMore(math_function_entry) + rbrace) # hash reaction entry hashed_reaction_section = Group(hashsymbol + Suppress(define_reactions_) + lbrace + OneOrMore(reaction_definition) + rbrace) # hash observable entry count_definition = Group(count_ + lbracket + species_definition.setResultsName('speciesPattern') + Suppress(',') + identifier + rbracket) observable_entry = Group(lbrace + Group(delimitedList(count_definition, delim='+')).setResultsName('patterns') + rbrace + Suppress('=>') + quotedString.setResultsName('outputfile')) bngobservable_entry = Group((species_ | molecules_).setResultsName('obskey') + identifier.setResultsName('obsname') + Group(delimitedList(species_definition, delim=',')).setResultsName('obspatterns')) hashed_observable_section = Group(hashsymbol + Suppress(reaction_data_output_) + lbrace + OneOrMore(statement).setResultsName('statements') + ZeroOrMore(observable_entry).setResultsName('mdlobs') + ZeroOrMore(bngobservable_entry).setResultsName('bngobs') + rbrace) # hash initialization entry key = identifier + Suppress('=') value = restOfLine release_site_definition = Group(identifier.setResultsName('name') + release_site_ + lbrace + dictOf(key, value).setResultsName('entries') + rbrace) object_definition = Group(identifier.setResultsName('compartmentName') + Suppress(object_) + (bracketidentifier | identifier) + (nestedExpr('{', '}', content=statement)).setResultsName('compartmentOptions')) hashed_initialization_section = Group(hashsymbol + Suppress(instantiate_) + identifier.setResultsName('name') + identifier.setResultsName('type') + lbrace + Group(ZeroOrMore(release_site_definition | object_definition)).setResultsName('entries') + rbrace) other_sections = section_enclosure_ # statement = Group(identifier + equal + (quotedString | OneOrMore(mathElements))) + Suppress(LineEnd() | StringEnd()) grammar = ZeroOrMore(Suppress(other_sections) | Suppress(statement) | hashed_system_constants.setResultsName('systemConstants') |
_TABLE_NAME = _SQL_ID.setResultsName("table") _PROCEDURE_NAME = _sql_identifier(Word(alphanums + "_.:").setResultsName("name")).setResultsName('name') _SELECT_COLUMN_LIST = delimitedList(Group(_SELECT_COLUMN), combine=False).setResultsName("columns") _SQL_ARG = Optional(_SQL_DIRECTION, default='IN') + _SQL_ID + _SQL_TYPE _SQL_ARGS = delimitedList(Group(_SQL_ARG), combine=False) _RETURN_TYPE = oneOf("object array", caseless=True).setResultsName("type") # expressions _DEFINE_FUNCTION = _DEFINE + _ID + nestedExpr(content=_ID_LIST, ignoreExpr=None).setResultsName("args") + \ Suppress(White()) + Regex(".*$").setResultsName("body") _DEFINE_VAR = _DEFINE + _ID + _VALUE _UNDEFINE = _UNDEF + _ID _INCLUDE_FILE = _INCLUDE + quotedString.setResultsName("filename") _EXPAND_VAR = Suppress('$') + _ID _EXPAND_FUNC = Suppress('$') + _ID + nestedExpr(content=_VALUE_LIST, ignoreExpr=None).setResultsName("args") _IF_EXPR = _IF + SkipTo(lineEnd, include=True).setResultsName("condition") _ELSE_EXPR = _ELSE + lineEnd _ENDIF_EXPR = _ENDIF + lineEnd _RETURN_HINT_EXPR = _select_hint(Optional(_ID + Suppress(Literal(":"))) + _RETURN_TYPE).setResultsName("hint") _TEMP_TABLE_EXPR = _SQL_ID.setResultsName('table') + \ nestedExpr(content=delimitedList(Group(_SQL_ID + _SQL_TYPE)), ignoreExpr=None).setResultsName('columns') +\ Suppress(Literal(';')) _PROCEDURE_COMMENT_FORMAT = Optional(_TEMP_TABLE_EXPR) + Optional(_RETURNS + oneOf("union", caseless=True).setResultsName("mode")) + lineEnd _CREATE_PROCEDURE = _CREATE + Optional(_DEFINER + '=' + _SQL_ID) + _PROCEDURE + _PROCEDURE_NAME + \
lbrace + ZeroOrMore(math_function_entry) + rbrace) # hash reaction entry hashed_reaction_section = Group(hashsymbol + Suppress(define_reactions_) + lbrace + OneOrMore(reaction_definition) + rbrace) # hash observable entry count_definition = Group(count_ + lbracket + species_definition.setResultsName('speciesPattern') + Suppress(',') + identifier + rbracket) observable_entry = Group(lbrace + Group( delimitedList(count_definition, delim='+')).setResultsName('patterns') + rbrace + Suppress('=>') + quotedString.setResultsName('outputfile')) bngobservable_entry = Group((species_ | molecules_).setResultsName('obskey') + identifier.setResultsName('obsname') + Group(delimitedList(species_definition, delim=',') ).setResultsName('obspatterns')) hashed_observable_section = Group( hashsymbol + Suppress(reaction_data_output_) + lbrace + OneOrMore(statement).setResultsName('statements') + ZeroOrMore(observable_entry).setResultsName('mdlobs') + ZeroOrMore(bngobservable_entry).setResultsName('bngobs') + rbrace) # hash initialization entry key = identifier + Suppress('=') value = restOfLine release_site_definition = Group(
upkey('number') | upkey('binary'))\ .setName('type').setResultsName('type') _sign = Word('+-', exact=1) number = Combine(Optional(_sign) + Word(nums) + Optional('.' + Optional(Word(nums)))) \ .setName('number').setResultsName('number') integer = Combine(Optional(_sign) + Word(nums)) \ .setName('number').setResultsName('number') boolean = (upkey('true') | upkey('false')).setName('bool') binary = Combine('b' + quotedString) value = Forward() json_value = Forward() string = quotedString.setResultsName('str') json_primitive = (null.setResultsName('null') | number | string | boolean.setResultsName('bool')) set_primitive = (number.setResultsName('number') | quotedString.setResultsName('str') | binary.setResultsName('binary')) primitive = (json_primitive | binary.setResultsName('binary')) _emptyset = Keyword('()').setResultsName('set') set_ = (Suppress('(') + delimitedList(Group(set_primitive)) + Suppress(')')).setResultsName('set') list_ = (Suppress('[') + Optional(delimitedList(json_value)) + Suppress(']')).setResultsName('list') key_val = (Group(quotedString.setResultsName('str')) + Suppress(':') + json_value) dict_ = (Suppress('{') + Optional(delimitedList(Group(key_val))) +
_SELECT_COLUMN_LIST = delimitedList(Group(_SELECT_COLUMN), combine=False).setResultsName("columns") _SQL_ARG = Optional(_SQL_DIRECTION, default='IN') + _SQL_ID + _SQL_TYPE _SQL_ARGS = delimitedList(Group(_SQL_ARG), combine=False) _RETURN_TYPE = oneOf("object array", caseless=True).setResultsName("type") # expressions _DEFINE_FUNCTION = _DEFINE + _ID + nestedExpr(content=_ID_LIST, ignoreExpr=None).setResultsName("args") + \ Suppress(White()) + Regex(".*$").setResultsName("body") _DEFINE_VAR = _DEFINE + _ID + SkipTo(lineEnd, include=True).setResultsName("value") _UNDEFINE = _UNDEF + _ID _INCLUDE_FILE = _INCLUDE + quotedString.setResultsName("filename") _EXPAND_VAR = Suppress('$') + _ID _EXPAND_FUNC = Suppress('$') + _ID + nestedExpr( content=_VALUE_LIST, ignoreExpr=None).setResultsName("args") _IF_EXPR = _IF + SkipTo(lineEnd, include=True).setResultsName("condition") _ELSE_EXPR = _ELSE + lineEnd _ENDIF_EXPR = _ENDIF + lineEnd _RETURN_HINT_EXPR = _select_hint( Optional(_ID + Suppress(Literal(":"))) + _RETURN_TYPE).setResultsName("hint") _TEMP_TABLE_EXPR = _SQL_ID.setResultsName('table') + \ nestedExpr(content=delimitedList(Group(_SQL_ID + _SQL_TYPE)), ignoreExpr=None).setResultsName('columns') +\ Suppress(Literal(';'))
# pylint: disable=C0103 backtickString = Regex(r'`[^`]*`').setName("string enclosed in backticks") and_, from_, into, in_, table_key, null, where_ = \ map(upkey, ['and', 'from', 'into', 'in', 'table', 'null', 'where']) var = Word(alphas, alphanums + '_-').setName('variable').setResultsName('var') expr = Combine(Optional('m') + backtickString).setName('python expression').setResultsName('python') table = var.setResultsName('table') type_ = (upkey('string') | upkey('number') | upkey('binary'))\ .setName('type').setResultsName('type') _sign = Word('+-', exact=1) num = Combine(Optional(_sign) + Word(nums) + Optional('.' + Optional(Word(nums)))).setName('number') primitive = (null.setResultsName('null') | num.setResultsName('number') | quotedString.setResultsName('str') | Combine('b' + quotedString).setResultsName('binary')) _emptyset = Keyword('()').setResultsName('set') set_ = (Suppress('(') + delimitedList(Group(primitive)) + Suppress(')')).setResultsName('set') value = Group(primitive | expr | set_ | _emptyset).setName('value') # Wrap these in a group so they can be used independently primitive = Group(primitive | expr).setName('primitive') set_ = Group(set_ | _emptyset | expr).setName('set')
diffusion_entry_ = Group((diffusion_constant_2d_.setResultsName('2D') | diffusion_constant_3d_.setResultsName('3D')) + Suppress(equal) + (function_entry_.setResultsName('function') | (identifier | numarg).setResultsName('variable'))) molecule_entry = Group(molecule_definition + Optional(Group(lbrace + Optional(diffusion_entry_.setResultsName('diffusionFunction')) + (ZeroOrMore(statement)).setResultsName('moleculeParameters') + rbrace))) hashed_molecule_section = Group(hashsymbol + Suppress(define_molecules_) + lbrace + OneOrMore(molecule_entry) + rbrace) #hash function entry function_name = Group(identifier + '()') math_function_entry = Group(function_name.setResultsName('functionName') + Suppress(equal) + Group(restOfLine).setResultsName('functionBody')) hashed_function_section = Group(hashsymbol + Suppress(define_functions_) + lbrace + ZeroOrMore(math_function_entry) +rbrace) # hash reaction entry hashed_reaction_section = Group(hashsymbol + Suppress(define_reactions_) + lbrace + OneOrMore(reaction_definition) + rbrace) # hash observable entry count_definition = Group(count_ + lbracket + species_definition.setResultsName('speciesPattern') + Suppress(',') + identifier + rbracket) observable_entry = Group(lbrace + Group(delimitedList(count_definition, delim='+')).setResultsName('patterns') + rbrace + Suppress('=>') + quotedString.setResultsName('outputfile')) bngobservable_entry = Group((species_ | molecules_).setResultsName('obskey') + identifier.setResultsName('obsname') + Group(delimitedList(species_definition, delim=',')).setResultsName('obspatterns')) hashed_observable_section = Group(hashsymbol + Suppress(reaction_data_output_) + lbrace + OneOrMore(statement).setResultsName('statements') + ZeroOrMore(observable_entry).setResultsName('mdlobs') + ZeroOrMore(bngobservable_entry).setResultsName('bngobs') + rbrace) # hash initialization entry key = identifier + Suppress('=') value = restOfLine release_site_definition = Group(identifier.setResultsName('name') + release_site_ + lbrace + dictOf(key,value).setResultsName('entries') + rbrace) object_definition = Group(identifier.setResultsName('compartmentName') + Suppress(object_) + (bracketidentifier | identifier) + (nestedExpr('{', '}',content=statement)).setResultsName('compartmentOptions')) hashed_initialization_section = Group(hashsymbol + Suppress(instantiate_) + identifier.setResultsName('name') + identifier.setResultsName('type') + lbrace + Group(ZeroOrMore(release_site_definition | object_definition)).setResultsName('entries') + rbrace ) other_sections = section_enclosure_ #statement = Group(identifier + equal + (quotedString | OneOrMore(mathElements))) + Suppress(LineEnd() | StringEnd()) grammar = ZeroOrMore(Suppress(other_sections) | Suppress(statement) | hashed_system_constants.setResultsName('systemConstants')