spiffyscore

Check-in [18dba95a2e]
Login
Overview
Comment:Changed parser to include nodes
Timelines: family | ancestors | descendants | both | feature/abc
Files: files | file ages | folders
SHA1: 18dba95a2ee0cf3b0795ff7e88437a1a3104a7b1
User & Date: brian on 2011-09-13 20:02:12
Other Links: branch diff | manifest | tags
Context
2011-09-13
20:08
Added a name parameter to the Tree class that will prove helpful in generating the score check-in: 029fd48614 user: brian tags: feature/abc
20:02
Changed parser to include nodes check-in: 18dba95a2e user: brian tags: feature/abc
19:47
Replaced roman numerals with note literals check-in: 6a80123223 user: brian tags: feature/abc
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Modified parse.py from [67fb9f1f54] to [751d12bb6a].

     1      1   #!/usr/bin/env python
            2  +
            3  +import tree
     2      4   
     3      5   from ply import lex, yacc
     4      6   class Note():
     5      7       def __init__(self, value, duration=.25, octave=8):
     6      8           self.value = value
     7      9           self.duration = duration
     8     10           self.octave = octave
................................................................................
    33     35           "NOTE_LENGTH",
    34     36           "BASENOTE",
    35     37           "ACCIDENTAL",
    36     38           "REST",
    37     39           "OCTAVE",
    38     40           "CHORD_TYPE",
    39     41           "QUOTE",
           42  +        "NODE",
    40     43       )
    41     44   
    42     45       t_ignore = " |"
    43     46   
    44     47       #t_BASENOTE = r"[A-Ga-g]"
    45     48   #    t_BASENOTE = r"I+V?|VI*|i+v?|vi*"
    46     49       t_BASENOTE = r"[A-Ga-g]"
    47     50       t_ACCIDENTAL = r"\^{1,2}|_{1,2}|="
    48     51       t_REST = r"z"
    49     52       t_OCTAVE = r"'+|,+"
    50     53       t_CHORD_TYPE = r"m|7|m7|0|o|\+|mb5|sus|sus4|maj7|mmaj7|7sus4|dim|dim7|7b5|m7b5|6|b6|m6|mb6|46|maj9|9|add9|7b9|m9"
    51     54       t_QUOTE = '"'
           55  +    t_NODE = r"([a-zA-Z0-9_-]+)"
    52     56   
    53     57       def t_NOTE_LENGTH(t):
    54     58           r"/?\d+"
    55     59           multiplier = float(t.value.strip("/"))
    56     60           if t.value.startswith("/"):
    57     61               multiplier = 1/multiplier
    58     62           t.value = multiplier
................................................................................
    75     79       # Parse (yacc)
    76     80   
    77     81   
    78     82       def p_pitch_list(p):
    79     83           '''score : score note
    80     84               | score chord
    81     85               | score rest
           86  +            | score node
    82     87           '''
    83     88           p[0] = p[1] + [p[2]]
    84     89   
    85     90       def p_score(p):
    86     91           '''score : note
    87     92               | chord
    88     93               | rest
           94  +            | node
    89     95           '''
    90     96           p[0] = [p[1]]
    91     97   
    92     98   
    93     99       def p_note(p):
    94    100           '''note : pitch
    95    101           '''
................................................................................
   144    150       def p_rest(p):
   145    151           ''' rest : REST
   146    152                   | REST NOTE_LENGTH
   147    153           '''
   148    154           p[0] = Rest()
   149    155           if len(p) > 2:
   150    156               p[0].duration = p[2]
          157  +
          158  +    def node(p):
          159  +        '''node: NODE
          160  +        '''
          161  +        p[0] = tree.tree(node.strip("(").strip(")"))
          162  +
   151    163   
   152    164       def p_error(p):
   153    165           raise Exception("Syntax error at '%s' of element type %s" % (p.value, p.type))
   154    166           
   155    167       yacc.yacc()
   156    168   
   157    169       #print yacc.parse("GFG B'AB,, | g/2fg gab | GFG BAB | d2A AFD")
   158    170       return yacc.parse(score)

Added tree.py version [7aba067c30].

            1  +import pdb
            2  +
            3  +class Tree():
            4  +    def __init__(self):
            5  +        self.nodes = []
            6  +
            7  +    def traverse_depth_first(self):
            8  +        all_nodes = []
            9  +        for node in self.nodes:
           10  +            if isinstance(node, Node):
           11  +                all_nodes.append(node)
           12  +            elif isinstance(node, Tree):
           13  +#                pdb.set_trace()
           14  +                all_nodes.extend(node.traverse_depth_first())
           15  +        return all_nodes
           16  +
           17  +    def __str__(self):
           18  +        return str(self.__unicode__())
           19  +    def __unicode__(self):
           20  +        return self.nodes
           21  +
           22  +
           23  +class Node():
           24  +    def __init__(self, parent, data):
           25  +        self.parent = parent
           26  +        self.data = data
           27  +
           28  +    def __str__(self):
           29  +        return str(self.__unicode__())
           30  +    def __unicode__(self):
           31  +        return self.data