Differences From Artifact [00dc77f47fceb1bf]:
- Executable file
parse.py
- 2010-11-17 06:27:03 - part of checkin [db1df2f460] on branch tld - TLD now accepts ordering of movements. Also, fixed bug that caused all movements and instruments to play simultaneously. (user: spiffytech@gmail.com) [annotate]
To Artifact [3a88b8ab433d4b10]:
- Executable file
parse.py
- 2010-11-17 07:30:34 - part of checkin [aa88358397] on branch tld - Added support for rest notes (user: spiffytech@gmail.com) [annotate]
14 def __init__(self, value, duration=.5, chord_type="major"): 14 def __init__(self, value, duration=.5, chord_type="major"):
15 self.value = value 15 self.value = value
16 self.duration = duration 16 self.duration = duration
17 self.chord_type = chord_type 17 self.chord_type = chord_type
18 def __repr__(self): 18 def __repr__(self):
19 return "Chord %s %s %s" % (self.value, self.duration, self.chord_type) 19 return "Chord %s %s %s" % (self.value, self.duration, self.chord_type)
20 20
> 21
> 22 class Rest():
> 23 def __init__(self, duration=.25):
> 24 self.duration = duration
> 25 def __repr__(self):
> 26 return "Rest node %s" % self.duration
> 27
21 28
22 def parse(score): 29 def parse(score):
23 # Tokenize (lex) 30 # Tokenize (lex)
24 tokens = ( 31 tokens = (
25 "NOTE_LENGTH", 32 "NOTE_LENGTH",
26 "BASENOTE", 33 "BASENOTE",
27 "ACCIDENTAL", 34 "ACCIDENTAL",
................................................................................................................................................................................
64 71
65 72
66 # Parse (yacc) 73 # Parse (yacc)
67 74
68 75
69 def p_pitch_list(p): 76 def p_pitch_list(p):
70 '''score : score note 77 '''score : score note
71 score : score chord | 78 | score chord
> 79 | score rest
72 ''' 80 '''
73 p[0] = p[1] + [p[2]] 81 p[0] = p[1] + [p[2]]
74 82
75 def p_score(p): 83 def p_score(p):
76 '''score : note 84 '''score : note
77 score : chord | 85 | chord
> 86 | rest
78 ''' 87 '''
79 p[0] = [p[1]] 88 p[0] = [p[1]]
80 89
81 90
82 def p_note(p): 91 def p_note(p):
83 '''note : pitch 92 '''note : pitch
84 ''' 93 '''
................................................................................................................................................................................
127 p[0] = p[1] 136 p[0] = p[1]
128 137
129 def p_pitch(p): 138 def p_pitch(p):
130 '''pitch : BASENOTE 139 '''pitch : BASENOTE
131 ''' 140 '''
132 p[0] = Note(p[1]) 141 p[0] = Note(p[1])
133 142
> 143 def p_rest(p):
> 144 ''' rest : REST
> 145 | REST NOTE_LENGTH
> 146 '''
> 147 p[0] = Rest()
> 148 if len(p) > 2:
> 149 p[0].duration = p[2]
> 150
134 def p_error(p): 151 def p_error(p):
135 print "Syntax error at '%s' of element type %s" % (p.value, p.type) 152 print "Syntax error at '%s' of element type %s" % (p.value, p.type)
136 153
137 yacc.yacc() 154 yacc.yacc()
138 155
139 #print yacc.parse("GFG B'AB,, | g/2fg gab | GFG BAB | d2A AFD") 156 #print yacc.parse("GFG B'AB,, | g/2fg gab | GFG BAB | d2A AFD")
140 return yacc.parse(score) 157 return yacc.parse(score)