Example #1
0
def initial():               return OneOrMore([nclasses,enumeration]),database_config, EOF
def nclasses():              return OneOrMore(nclass)
Example #2
0
def attributes():            return "[", OneOrMore(attribute), Optional(attribute_label),"]"
def attribute():             return attribute_key, "=", attribute_value
Example #3
0
 def recordDef():
     return (defHeader, OneOrMore(defField), Optional('}'), Optional(')'),
             Optional(']'))
Example #4
0
 def arrayOfDef():
     return (
         ZeroOrMore(RegExMatch(r'[\w]+')),  # name
         OneOrMore(RegExMatch(r'.*\]')),  # data up to comment
         ZeroOrMore(RegExMatch(r';[\s*\w]+')))
Example #5
0
def pattern_with_last(): return ("(", OneOrMore(pattern), ".", pattern, ")")
def pattern_datum(): return [string, character, boolean, number]
Example #6
0
def command_or_definition(): return [expression,
        definition,
        syntax_definition,
        ("(", "begin", OneOrMore(command_or_definition), ")")]
def definition(): return [ ("(", "define", variable, expression, ")"),
Example #7
0
 def syntax():
     # match - syntax = ['"]SYNTAX['"](;)
     return RegExMatch(r'syntax\s?=\s?[\'\"].*[\'\"]\;?'), OneOrMore(
         endLine)
Example #8
0
def cond(): return "(", "cond", OneOrMore([cond_clause, cond_call]), Optional(else_clause), ")"
def cond_clause(): return "(", expression, ZeroOrMore(expression), ")"
Example #9
0
def citeString():
    return (keywords, [(Optional(date), OneOrMore(cite)),
                       partialCite], oComma, EOF)
Example #10
0
 def oneofDef():
     return ('oneof', defHeader, OneOrMore(defField), '}')
Example #11
0
def intraCite():
    return OneOrMore(
        (supraSection, Optional(section), oComma, Optional(inPara)))
Example #12
0
File: day3.py Project: hank/AOC2018
def claims(): return OneOrMore(claim), EOF

class ClaimVisitor(PTNodeVisitor):
Example #13
0
def repeat_modifiers():
    return '[', OneOrMore([simple_match, 'eolterm']), ']'
Example #14
0
    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
Example #15
0
 def package():
     # match - package ('")PACKAGE('")(;)
     return RegExMatch(r'package\s?[\'\"]?.*[\'\"]?\;?'), OneOrMore(endLine)
Example #16
0
def sequence(): return OneOrMore(expression)

def conditional(): return "(", "if", expression, expression, Optional(expression), ")"
Example #17
0
 def pkgImports():
     # match - import ['"]PACKAGE['"](;)
     return RegExMatch(r'import\s?[\'\"].*[\'\"]\;?'), OneOrMore(endLine)
Example #18
0
def else_clause(): return "(", "else", OneOrMore(expression), ")"
def or_(): return "(", "or", ZeroOrMore(expression), ")"
Example #19
0
 def messageDef():
     return ('message', defHeader, OneOrMore(defField), '}')
Example #20
0
def template_with_last(): return ("(", OneOrMore(template_element), ".", template, ")")
def template_element(): return [template, (template, ellipsis)]
Example #21
0
def metalstar():
    return Optional(noprefixstar), OneOrMore(metalsection)
def calc():       return OneOrMore(expression), EOF


def test_pp_construction():
Example #23
0
def scrange():
    return [(numeric, '-', Optional(numeric)), (numeric, '+'),
            (numeric, Optional(OneOrMore(' '), '-', OneOrMore(' '), numeric))]
Example #24
0
 def enumDef():
     return (defHeader, OneOrMore(enumField), OneOrMore(endLine))
Example #25
0
def integer():
    return (Optional("-"), OneOrMore(nums))
Example #26
0
 def typeDefs():
     return (OneOrMore(
         UnorderedGroup(ZeroOrMore(recordDef), ZeroOrMore(enumDef),
                        ZeroOrMore(arrayOfDef))))
Example #27
0
def _list(): return  [("(", ZeroOrMore(datum), ")"), ("(", OneOrMore(datum), ".", datum, ")")] #, abbreviation]
def abbreviation(): return abbrev_prefix, datum
Example #28
0
def nclasses():              return OneOrMore(nclass)
def nclass():                return  Optional(ABSTRACT),CLASS, class_name,Optional(":", class_name), ZeroOrMore(attributes)
Example #29
0
def parameters_with_last(): return "(", OneOrMore(variable), ".", variable, ")"
def body(): return ZeroOrMore(definition), sequence
Example #30
0
File: textx.py Project: zeph/textX
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
Example #31
0
def sequence():
    return OneOrMore(repeatable_expr)