Example #1
0
 def __default__(self, data, children, meta):
     return Tree(data, children, meta)
def format_comma_separated_list(a_list: List[Node],
                                expression_context: ExpressionContext,
                                context: Context) -> FormattedLines:
    elements = [node for node in a_list if not is_any_comma(node)]
    child_context = context.create_child_context(
        expression_context.prefix_line)
    fake_expression = Tree("fake", a_list)
    multiline_mode_forced = is_expression_forcing_multiple_lines(
        fake_expression)
    if not multiline_mode_forced or "preload" in expression_context.prefix_string:
        strings_to_join = list(map(standalone_expression_to_str, elements))
        single_line_expression = "{}{}{}".format(
            expression_context.prefix_string,
            ", ".join(strings_to_join),
            expression_context.suffix_string,
        )
        single_line_length = len(single_line_expression) + context.indent
        if (single_line_length <= context.max_line_length
                or "preload" in expression_context.prefix_string):
            return [(
                expression_context.prefix_line,
                "{}{}".format(context.indent_string, single_line_expression),
            )]
        indented_single_line_expression = ", ".join(strings_to_join)
        if (len(indented_single_line_expression) + child_context.indent <=
                context.max_line_length):
            return [
                (
                    expression_context.prefix_line,
                    "{}{}".format(context.indent_string,
                                  expression_context.prefix_string),
                ),
                (
                    a_list[-1].end_line,
                    "{}{}".format(child_context.indent_string,
                                  indented_single_line_expression),
                ),
                (
                    a_list[-1].end_line,
                    "{}{}".format(context.indent_string,
                                  expression_context.suffix_string),
                ),
            ]
    formatted_lines = [(
        expression_context.prefix_line,
        "{}{}".format(context.indent_string, expression_context.prefix_string),
    )]  # type: FormattedLines
    trailing_comma_present = is_trailing_comma(a_list[-1])
    for i, element in enumerate(elements):
        suffix = "," if i != len(elements) - 1 or trailing_comma_present else ""
        child_expression_context = ExpressionContext("", element.line, suffix)
        lines, _ = _format_standalone_expression(element,
                                                 child_expression_context,
                                                 child_context)
        formatted_lines += lines
    formatted_lines.append((
        expression_context.suffix_line
        if expression_context.suffix_line is not None else a_list[-1].end_line,
        "{}{}".format(context.indent_string, expression_context.suffix_string),
    ))
    return formatted_lines
Example #3
0
def decay(transformed):
    return Tree("decay", list(transformed.find_data("decay")))
Example #4
0
 def color(self, args):
     return Tree('color', [args[1]])
Example #5
0
 def nhpr(self, args):
     return Tree('hpr', [args[1]])
Example #6
0
 def code(self, args):
     return Tree('code', [args[1].value])
Example #7
0
 def width(self, args):
     return Tree('width', [args[1].value])
Example #8
0
 def buildingtype(self, args):
     return Tree('building_type', [args[1].value])
Example #9
0
    def text(self, args):
        kw1, kw2, letter = args

        return Tree('text', [letter])
Example #10
0
    def test_veryComplexMeasure(self):
        very_Complex_Input = Tree('measure', [
            Tree('instrumentation', [
                Token('INSTRUMENT', 'acousticgrandpiano'),
                Tree('noteitem', [
                    Tree('note', [
                        Tree('division', [
                            Tree('number', [Token('__ANON_0', '1')]),
                            Tree('number', [Token('__ANON_0', '2')])
                        ]),
                        Tree('notename', [
                            Token('__ANON_0', 'C'),
                            Tree('number', [Token('__ANON_0', '4')])
                        ])
                    ])
                ]),
                Tree('noteitem', [
                    Tree('note', [
                        Tree('division', [
                            Tree('number', [Token('__ANON_0', '1')]),
                            Tree('number', [Token('__ANON_0', '4')])
                        ]),
                        Token('REST', '--')
                    ])
                ]),
                Tree('noteitem', [
                    Tree('note', [
                        Tree('division', [
                            Tree('number', [Token('__ANON_0', '1')]),
                            Tree('number', [Token('__ANON_0', '4')])
                        ]),
                        Tree('chord', [
                            Tree('notename', [
                                Token('__ANON_0', 'C'),
                                Tree('number', [Token('__ANON_0', '4')])
                            ]),
                            Tree('notename', [
                                Token('__ANON_0', 'E'),
                                Tree('number', [Token('__ANON_0', '4')])
                            ]),
                            Tree('notename', [
                                Token('__ANON_0', 'G'),
                                Tree('number', [Token('__ANON_0', '4')])
                            ])
                        ])
                    ])
                ])
            ])
        ])

        expected = [{
            'type': 'measure',
            'start': True
        }, {
            'type': 'instrument',
            'name': 'acousticgrandpiano'
        }, {
            'type': 'note',
            'note_name': 'C4',
            'length_num': 1,
            'length_denom': 2
        }, {
            'type': 'rest',
            'length_num': 1,
            'length_denom': 4
        }, {
            'type': 'chord',
            'notes': ['C4', 'E4', 'G4'],
            'length_num': 1,
            'length_denom': 4
        }, {
            'type': 'measure',
            'start': False
        }]

        signal = self.semantic.measure_to_signal(very_Complex_Input)
        self.assertEqual(expected, signal,
                         "Very complex measure signal not valid")
Example #11
0
 def title(self, args):
     return Tree('title', [args[1].value])
Example #12
0
 def test_inlineDynamicSigGen(self):
     input_dynamic = Tree('inlinedynamic', [Token('__ANON_0', 'mp')])
     expected = [{'type': 'dynamic', 'volume': 'mp'}]
     signal = self.semantic.inlinedynamic_to_signal(input_dynamic)
     self.assertEqual(expected, signal, "Inline dynamic signal not valid.")
Example #13
0
 def test_instrumentationSigGen(self):
     input_instrumentation = Tree('instrumentation',
                                  [Token('INSTRUMENT', 'trumpet')])
     expected = [{'type': 'instrument', 'name': 'trumpet'}]
     signal = self.semantic.instrumentation_to_signal(input_instrumentation)
     self.assertEqual(expected, signal, "Instrumentation signal not valid")
Example #14
0
 def setUp(self):
     self.help = TestHelp()
     self.semantic = Semantic(Tree('start', []))
Example #15
0
 def count(self, args):
     return Tree('count', [int(args[1].value)])
Example #16
0
 def stomp(self, args):
     return Tree('stomp', [args[1].value])
Example #17
0
 def texture(self, args):
     return Tree('texture', [args[1].value])
Example #18
0
 def stumble(self, args):
     return Tree('stumble', [args[1].value])
Example #19
0
 def vis(self, args):
     return Tree('vis',
                 [int(token.value.split(':')[0]) for token in args[1:]])
Example #20
0
 def kern(self, args):
     return Tree('kern', [args[1].value])
Example #21
0
 def height(self, args):
     return Tree('height', [args[1].value])
Example #22
0
 def wiggle(self, args):
     return Tree('wiggle', [args[1].value])
Example #23
0
 def pos(self, args):
     return Tree('pos', [args[1]])
Example #24
0
 def anim(self, args):
     return Tree('anim', [args[1].value])
Example #25
0
def test_Lark_DecayModelParamValueReplacement_Visitor_list():
    t = Tree(
        "decay",
        [
            Tree("particle", [Token("LABEL", "B0sig")]),
            Tree(
                "decayline",
                [
                    Tree("value", [Token("SIGNED_NUMBER", "1.000")]),
                    Tree("particle", [Token("LABEL", "MyFirstD*-")]),
                    Tree("particle", [Token("LABEL", "MySecondD*+")]),
                    Tree(
                        "model",
                        [
                            Token("MODEL_NAME", "SVV_HELAMP"),
                            Tree(
                                "model_options",
                                [
                                    Tree("value",
                                         [Token("SIGNED_NUMBER", "0.0")]),
                                    Tree("value",
                                         [Token("SIGNED_NUMBER", "0.0")]),
                                    Tree("value",
                                         [Token("SIGNED_NUMBER", "0.0")]),
                                    Tree("value",
                                         [Token("SIGNED_NUMBER", "0.0")]),
                                    Tree("value",
                                         [Token("SIGNED_NUMBER", "1.0")]),
                                    Tree("value",
                                         [Token("SIGNED_NUMBER", "0.0")]),
                                ],
                            ),
                        ],
                    ),
                ],
            ),
        ],
    )

    DecayModelParamValueReplacement().visit(t)

    # The visitor should do nothing in this case
    tree_decayline = list(t.find_data("decayline"))[0]
    assert get_model_name(tree_decayline) == "SVV_HELAMP"
    assert get_model_parameters(tree_decayline) == [
        0.0, 0.0, 0.0, 0.0, 1.0, 0.0
    ]
Example #26
0
 def cell_id(self, args):
     return Tree('cell_id', [int(args[1].value)])
Example #27
0
 def roll_one(self, tree):
     return Tree('roll_n', [1, tree.children[0]])
Example #28
0
 def scale(self, args):
     return Tree('scale', [args[1]])
Example #29
0
    def date_qualifier(self, with_exp, field, check_type, date_info):
        """
        Take a date check type and date information parsed
        and determine the actual date to check against
        """

        op = "="

        year = date_info[0][0]
        month = date_info[1][0]
        day = date_info[2][0]

        year_found = date_info[0][1]
        month_found = date_info[1][1]
        day_found = date_info[2][1]

        assert (year_found)
        if check_type.type == "DATE_EXACT" and (not day_found
                                                or not month_found):
            raise MissingExactDateSException(
                "Exact date check requires month, day, and year all be specified"
            )

        elif check_type.type == "DATE_BEFORE":
            op = "<"

            # If no month parsed, default to january
            if not month_found:
                month = 1

            # If no day parsed, default to the first
            if not day_found:
                day = 1

        elif check_type.type == "DATE_AFTER":
            op = ">"

            # If no month parsed, default to December
            if not month_found:
                month = 12

            # If no day parsed, default to last day of month
            if not day_found:
                day = monthrange(year, month)[1]

        target_date = None
        try:
            target_date = date(year, month, day)
        except ValueError as e:
            raise InvalidDateSException("Unable to create date type from " +
                                        str(month) + "/" + str(day) + "/" +
                                        str(year) + ": " + e.args[0]) from e

        if with_exp.lower() == "without":
            if check_type.type == "DATE_AFTER":
                op = "<"
                target_date = target_date + timedelta(hours=24)
            elif check_type.type == "DATE_BEFORE":
                op = ">"
                target_date = target_date - timedelta(hours=24)

        return Tree("date_qualifier", [field, op, target_date])
Example #30
0
File: hedy.py Project: lpmi-13/hedy
 def text(self, args):
     return Tree('text', [''.join([str(c) for c in args])])