def initial(): return OneOrMore([nclasses,enumeration]),database_config, EOF def nclasses(): return OneOrMore(nclass)
def attributes(): return "[", OneOrMore(attribute), Optional(attribute_label),"]" def attribute(): return attribute_key, "=", attribute_value
def recordDef(): return (defHeader, OneOrMore(defField), Optional('}'), Optional(')'), Optional(']'))
def arrayOfDef(): return ( ZeroOrMore(RegExMatch(r'[\w]+')), # name OneOrMore(RegExMatch(r'.*\]')), # data up to comment ZeroOrMore(RegExMatch(r';[\s*\w]+')))
def pattern_with_last(): return ("(", OneOrMore(pattern), ".", pattern, ")") def pattern_datum(): return [string, character, boolean, number]
def command_or_definition(): return [expression, definition, syntax_definition, ("(", "begin", OneOrMore(command_or_definition), ")")] def definition(): return [ ("(", "define", variable, expression, ")"),
def syntax(): # match - syntax = ['"]SYNTAX['"](;) return RegExMatch(r'syntax\s?=\s?[\'\"].*[\'\"]\;?'), OneOrMore( endLine)
def cond(): return "(", "cond", OneOrMore([cond_clause, cond_call]), Optional(else_clause), ")" def cond_clause(): return "(", expression, ZeroOrMore(expression), ")"
def citeString(): return (keywords, [(Optional(date), OneOrMore(cite)), partialCite], oComma, EOF)
def oneofDef(): return ('oneof', defHeader, OneOrMore(defField), '}')
def intraCite(): return OneOrMore( (supraSection, Optional(section), oComma, Optional(inPara)))
def claims(): return OneOrMore(claim), EOF class ClaimVisitor(PTNodeVisitor):
def repeat_modifiers(): return '[', OneOrMore([simple_match, 'eolterm']), ']'
def visit_assignment(self, node, children): """ Create parser rule for assignments and register attribute types on metaclass. """ attr_name = children[0] op = children[1] rhs_rule, modifiers = children[2] cls = self._current_cls target_cls = None if self.debug: self.dprint("Processing assignment {}{}...".format(attr_name, op)) if self.debug: self.dprint("Creating attribute {}:{}".format( cls.__name__, attr_name)) self.dprint("Assignment operation = {}".format(op)) if attr_name in cls._tx_attrs: # If attribute already exists in the metamodel it is # multiple assignment to the same attribute. # Cannot use operator ?= on multiple assignments if op == '?=': line, col = self.grammar_parser.pos_to_linecol(node.position) raise TextXSemanticError( 'Cannot use "?=" operator on multiple' ' assignments for attribute "{}" at {}'.format( attr_name, (line, col)), line, col) cls_attr = cls._tx_attrs[attr_name] else: cls_attr = self.metamodel._new_cls_attr(cls, name=attr_name, position=node.position) # Keep track of metaclass references and containments if type(rhs_rule) is tuple and rhs_rule[0] == "obj_ref": cls_attr.cont = False cls_attr.ref = True # Override rhs by its PEG rule for further processing rhs_rule = rhs_rule[1] # Target class is not the same as target rule target_cls = rhs_rule.cls base_rule_name = rhs_rule.rule_name if op == '+=': assignment_rule = OneOrMore(nodes=[rhs_rule], rule_name='__asgn_oneormore', root=True) cls_attr.mult = MULT_ONEORMORE elif op == '*=': assignment_rule = ZeroOrMore(nodes=[rhs_rule], rule_name='__asgn_zeroormore', root=True) if cls_attr.mult is not MULT_ONEORMORE: cls_attr.mult = MULT_ZEROORMORE elif op == '?=': assignment_rule = Optional(nodes=[rhs_rule], rule_name='__asgn_optional', root=True) cls_attr.mult = MULT_OPTIONAL base_rule_name = 'BOOL' # ?= assigment should have default value of False. # so we shall mark it as such. cls_attr.bool_assignment = True else: assignment_rule = Sequence(nodes=[rhs_rule], rule_name='__asgn_plain', root=True) # Modifiers if modifiers: modifiers, position = modifiers # Sanity check. Modifiers do not make # sense for ?= and = operator at the moment. if op == '?=' or op == '=': line, col = self.grammar_parser.pos_to_linecol(position) raise TextXSyntaxError( 'Modifiers are not allowed for "{}" operator at {}'.format( op, text((line, col))), line, col) # Separator modifier assignment_rule.sep = modifiers.get('sep', None) # End of line termination modifier if 'eolterm' in modifiers: assignment_rule.eolterm = True if target_cls: attr_type = target_cls else: # Use STRING as default attr class attr_type = base_rule_name if base_rule_name else 'STRING' if not cls_attr.cls: cls_attr.cls = ClassCrossRef(cls_name=attr_type, position=node.position) else: # cls cross ref might already be set in case of multiple assignment # to the same attribute. If types are not the same we shall use # OBJECT as generic type. if cls_attr.cls.cls_name != attr_type: cls_attr.cls.cls_name = 'OBJECT' if self.debug: self.dprint("Created attribute {}:{}[cls={}, cont={}, " "ref={}, mult={}, pos={}]".format( cls.__name__, attr_name, cls_attr.cls.cls_name, cls_attr.cont, cls_attr.ref, cls_attr.mult, cls_attr.position)) assignment_rule._attr_name = attr_name assignment_rule._exp_str = attr_name # For nice error reporting return assignment_rule
def package(): # match - package ('")PACKAGE('")(;) return RegExMatch(r'package\s?[\'\"]?.*[\'\"]?\;?'), OneOrMore(endLine)
def sequence(): return OneOrMore(expression) def conditional(): return "(", "if", expression, expression, Optional(expression), ")"
def pkgImports(): # match - import ['"]PACKAGE['"](;) return RegExMatch(r'import\s?[\'\"].*[\'\"]\;?'), OneOrMore(endLine)
def else_clause(): return "(", "else", OneOrMore(expression), ")" def or_(): return "(", "or", ZeroOrMore(expression), ")"
def messageDef(): return ('message', defHeader, OneOrMore(defField), '}')
def template_with_last(): return ("(", OneOrMore(template_element), ".", template, ")") def template_element(): return [template, (template, ellipsis)]
def metalstar(): return Optional(noprefixstar), OneOrMore(metalsection)
def calc(): return OneOrMore(expression), EOF def test_pp_construction():
def scrange(): return [(numeric, '-', Optional(numeric)), (numeric, '+'), (numeric, Optional(OneOrMore(' '), '-', OneOrMore(' '), numeric))]
def enumDef(): return (defHeader, OneOrMore(enumField), OneOrMore(endLine))
def integer(): return (Optional("-"), OneOrMore(nums))
def typeDefs(): return (OneOrMore( UnorderedGroup(ZeroOrMore(recordDef), ZeroOrMore(enumDef), ZeroOrMore(arrayOfDef))))
def _list(): return [("(", ZeroOrMore(datum), ")"), ("(", OneOrMore(datum), ".", datum, ")")] #, abbreviation] def abbreviation(): return abbrev_prefix, datum
def nclasses(): return OneOrMore(nclass) def nclass(): return Optional(ABSTRACT),CLASS, class_name,Optional(":", class_name), ZeroOrMore(attributes)
def parameters_with_last(): return "(", OneOrMore(variable), ".", variable, ")" def body(): return ZeroOrMore(definition), sequence
def assignment_SA(parser, node, children): """ Create parser rule for addition and register attribute types on metaclass. """ attr_name = children[0] op = children[1] rhs_rule, modifiers = children[2] cls = parser._current_cls target_cls = None if parser.debug: parser.dprint("Processing assignment {}{}...".format(attr_name, op)) if parser.debug: parser.dprint("Creating attribute {}:{}".format(cls.__name__, attr_name)) parser.dprint("Assignment operation = {}".format(op)) if attr_name in cls._tx_attrs: # If attribute already exists in the metamodel it is # multiple assignment to the same attribute. # Cannot use operator ?= on multiple assignments if op == '?=': line, col = parser.pos_to_linecol(node.position) raise TextXSemanticError( 'Cannot use "?=" operator on multiple' ' assignments for attribute "{}" at {}' .format(attr_name, (line, col)), line, col) cls_attr = cls._tx_attrs[attr_name] # Must be a many multiplicity. # OneOrMore is "stronger" constraint. if cls_attr.mult is not MULT_ONEORMORE: cls_attr.mult = MULT_ZEROORMORE else: cls_attr = parser.metamodel.new_cls_attr(cls, name=attr_name, position=node.position) # Keep track of metaclass references and containments if type(rhs_rule) is tuple and rhs_rule[0] == "obj_ref": cls_attr.cont = False cls_attr.ref = True # Override rhs by its PEG rule for further processing rhs_rule = rhs_rule[1] # Target class is not the same as target rule target_cls = rhs_rule.cls base_rule_name = rhs_rule.rule_name if op == '+=': assignment_rule = OneOrMore( nodes=[rhs_rule], rule_name='__asgn_oneormore', root=True) cls_attr.mult = MULT_ONEORMORE elif op == '*=': assignment_rule = ZeroOrMore( nodes=[rhs_rule], rule_name='__asgn_zeroormore', root=True) if cls_attr.mult is not MULT_ONEORMORE: cls_attr.mult = MULT_ZEROORMORE elif op == '?=': assignment_rule = Optional( nodes=[rhs_rule], rule_name='__asgn_optional', root=True) cls_attr.mult = MULT_OPTIONAL base_rule_name = 'BOOL' # ?= assigment should have default value of False. # so we shall mark it as such. cls_attr.bool_assignment = True else: assignment_rule = Sequence( nodes=[rhs_rule], rule_name='__asgn_plain', root=True) # Modifiers if modifiers: modifiers, position = modifiers # Sanity check. Modifiers do not make # sense for ?= operator at the moment. if op == '?=': line, col = parser.pos_to_linecol(position) raise TextXSyntaxError( 'Modifiers are not allowed for "?=" operator at {}' .format(text((line, col))), line, col) # Separator modifier if 'sep' in modifiers: sep = modifiers['sep'] assignment_rule = Sequence( nodes=[rhs_rule, ZeroOrMore(nodes=[Sequence(nodes=[sep, rhs_rule])])], rule_name='__asgn_list', root=True) if op == "*=": assignment_rule = Optional(nodes=[rhs_rule]) assignment_rule = Optional(nodes=[Sequence( nodes=[rhs_rule, ZeroOrMore(nodes=[ Sequence(nodes=[sep, rhs_rule])])])], rule_name='__asgn_list', root=True) # End of line termination modifier if 'eolterm' in modifiers: assignment_rule.eolterm = True if target_cls: attr_type = target_cls else: # Use STRING as default attr class attr_type = base_rule_name if base_rule_name else 'STRING' cls_attr.cls = ClassCrossRef(cls_name=attr_type, position=node.position) if parser.debug: parser.dprint("Created attribute {}:{}[cls={}, cont={}, " "ref={}, mult={}, pos={}]" .format(cls.__name__, attr_name, cls_attr.cls.cls_name, cls_attr.cont, cls_attr.ref, cls_attr.mult, cls_attr.position)) assignment_rule._attr_name = attr_name assignment_rule._exp_str = attr_name # For nice error reporting return assignment_rule
def sequence(): return OneOrMore(repeatable_expr)