Differences From Artifact [3ec57c3fbe75652c]:
- Executable file
parse.py
- 2011-02-10 22:34:39 - part of checkin [702d933446] on branch master - Added lexical support for parens instead of quotes for chords, cleaned up the yacc parser, added lex tokens for syncopation (user: brian@linux-85dd.site) [annotate]
To Artifact [c06717e3357dd7f7]:
- Executable file
parse.py
- 2011-06-12 20:32:21 - part of checkin [87435601e4] on branch master - Made some changes to the parser. Don't remember what. (user: brian) [annotate]
- 2011-06-12 20:32:59 - part of checkin [60bf1cfb19] on branch tree - Made some changes to the parser. Don't remember what. (user: brian) [annotate]
13 class Chord(): 13 class Chord():
14 def __init__(self, value, duration=.5, chord_type="major", octave=5): 14 def __init__(self, value, duration=.5, chord_type="major", octave=5):
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 self.octave = octave 18 self.octave = octave
19 def __repr__(self): 19 def __repr__(self):
20 return "Chord %s %s %s" % (self.value, self.duration, self.chord_type, s | 20 return "Chord %s %d %s %s" % (self.value, self.duration, self.chord_type
21 21
22 class Rest(): 22 class Rest():
23 def __init__(self, duration=.25): 23 def __init__(self, duration=.25):
24 self.duration = duration 24 self.duration = duration
25 def __repr__(self): 25 def __repr__(self):
26 return "Rest node %s" % self.duration 26 return "Rest node %s" % self.duration
27 27
................................................................................................................................................................................
33 "BASENOTE", 33 "BASENOTE",
34 "ACCIDENTAL", 34 "ACCIDENTAL",
35 "REST", 35 "REST",
36 "OCTAVE", 36 "OCTAVE",
37 "CHORD_TYPE", 37 "CHORD_TYPE",
38 "PAREN", 38 "PAREN",
39 "SYNCOPATE", 39 "SYNCOPATE",
> 40 "NODE",
40 ) 41 )
41 42
42 t_ignore = " |" 43 t_ignore = " |"
43 44
44 #t_BASENOTE = r"[A-Ga-g]" <
45 t_BASENOTE = r"I+V?|VI*|i+v?|vi*" 45 t_BASENOTE = r"I+V?|VI*|i+v?|vi*"
46 t_ACCIDENTAL = r"\^{1,2}|_{1,2}|=" 46 t_ACCIDENTAL = r"\^{1,2}|_{1,2}|="
47 t_REST = r"z" 47 t_REST = r"z"
48 t_OCTAVE = r"'+|,+" 48 t_OCTAVE = r"'+|,+"
49 t_CHORD_TYPE = r"m|7|m7|0|o|\+|mb5|sus|sus4|maj7|mmaj7|7sus4|dim|dim7|7b5|m7 | 49 t_CHORD_TYPE = r"m|7|m7|0|o|mb5|sus|sus4|maj7|mmaj7|7sus4|dim|dim7|7b5|m7b5|
50 t_PAREN = "\(|\)" 50 t_PAREN = "\(|\)"
51 t_SYNCOPATE = "\+|-" 51 t_SYNCOPATE = "\+|-"
> 52 # t_NODE = "\w*(?!(([Vv][Ii]{0,3})|([Ii][Vv]?)))\w+"
> 53 t_NODE = "\S+"
52 54
53 def t_NOTE_LENGTH(t): 55 def t_NOTE_LENGTH(t):
54 r"/?\d+" 56 r"/?\d+"
55 multiplier = float(t.value.strip("/")) 57 multiplier = float(t.value.strip("/"))
56 if t.value.startswith("/"): 58 if t.value.startswith("/"):
57 multiplier = 1/multiplier 59 multiplier = 1/multiplier
58 t.value = multiplier 60 t.value = multiplier
................................................................................................................................................................................
67 69
68 # Parse (yacc) 70 # Parse (yacc)
69 71
70 def p_note_list(p): 72 def p_note_list(p):
71 '''score : score note 73 '''score : score note
72 | score chord 74 | score chord
73 | score rest 75 | score rest
> 76 | score node
74 ''' 77 '''
75 p[0] = p[1] + [p[2]] 78 p[0] = p[1] + [p[2]]
76 79
77 def p_score(p): 80 def p_score(p):
78 '''score : note 81 '''score : note
79 | chord 82 | chord
80 | rest 83 | rest
81 ''' 84 '''
82 p[0] = [p[1]] 85 p[0] = [p[1]]
83 86
> 87
> 88 def p_node(p):
> 89 '''node : NODE
> 90 '''
> 91 p[0] = p[1]
> 92
84 93
85 def p_chord_length(p): 94 def p_chord_length(p):
86 ''' chord : chord NOTE_LENGTH 95 ''' chord : chord NOTE_LENGTH
87 ''' 96 '''
88 new_note = p[1] 97 new_note = p[1]
89 new_note.duration = p[2] 98 new_note.duration = p[2]
90 p[0] = new_note 99 p[0] = new_note
................................................................................................................................................................................
101 def p_chord(p): 110 def p_chord(p):
102 '''chord : PAREN note PAREN 111 '''chord : PAREN note PAREN
103 | PAREN note CHORD_TYPE PAREN 112 | PAREN note CHORD_TYPE PAREN
104 ''' 113 '''
105 pitch = p[2].value 114 pitch = p[2].value
106 pitch = pitch.upper() 115 pitch = pitch.upper()
107 p[0] = Chord(value=pitch, octave=default_octave) 116 p[0] = Chord(value=pitch, octave=default_octave)
108 if len(p) > 3: | 117 if len(p) > 4:
109 p[0].chord_type = p[3] 118 p[0].chord_type = p[3]
110 119
111 120
112 def p_note_syncopate(p): 121 def p_note_syncopate(p):
113 ''' note : note SYNCOPATE 122 ''' note : note SYNCOPATE
114 ''' 123 '''
115 note.syncopate = p[2] | 124 p[1].syncopate = p[2]
> 125 p[0] = p[1]
116 126
117 127
118 def p_accidental(p): 128 def p_accidental(p):
119 '''note : ACCIDENTAL note 129 '''note : ACCIDENTAL note
120 ''' 130 '''
121 p[2].accidental = p[1] 131 p[2].accidental = p[1]
122 p[0] = p[2] 132 p[0] = p[2]