-
Notifications
You must be signed in to change notification settings - Fork 0
/
Note.js
92 lines (87 loc) · 2.37 KB
/
Note.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
class NoteSyntax {
static ArrayToRegExp(array, multi = true) {
let charset = '', quantifier = ''
if (array.length > 0) {
if (multi) quantifier = '*'
charset = '[' + array.join('') + ']'
}
return charset + quantifier
}
constructor(chords, degrees) {
const normal = degrees.includes('1')
this.exist = degrees.length > 0
this.degree = NoteSyntax.ArrayToRegExp(degrees, false)
this.chord = normal ? NoteSyntax.ArrayToRegExp(chords, true) : ''
const pitOp = normal ? "[#b',]*" : ''
const durOp = '[._=-]*'
const volOp = '[>:]*'
const epilog = '[`]*'
const inner = `(?:${pitOp}${this.chord}${volOp})`
const outer = `(?:${durOp}${epilog})`
this.deg = `(${this.degree})`
this.in = `(${pitOp})(${this.chord})(${volOp})`
this.out = `(${durOp})(${epilog})`
this.sqr = `\\[((?:${this.degree}${inner})+)\\]`
this.Patt = `(?:(?:\\[(?:${this.degree}${inner})+\\]|${this.degree})${inner}${outer})`
}
pattern() {
return this.Patt
}
pitch() {
return this.deg + this.in
}
context() {
const deg = this.deg
const _in = this.in
const out = this.out
const sqr = this.sqr
return this.exist ? [
{
patt: new RegExp('^' + deg + _in + out),
token(match) {
return {
Type: 'Note',
Pitch: [
{
Pitch: match[1],
PitOp: match[2],
Chord: match[3],
VolOp: match[4]
}
],
PitOp: '',
Chord: '',
VolOp: '',
DurOp: match[5],
Stac: match[6].length
}
}
},
{
patt: new RegExp('^' + sqr + _in + out),
token(match) {
const inner = new RegExp(deg + _in)
const match1 = match[1].match(new RegExp(inner, 'g'))
return {
Type: 'Note',
Pitch: match1.map(str => {
const match = inner.exec(str)
return {
Pitch: match[1],
PitOp: match[2],
Chord: match[3],
VolOp: match[4]
}
}),
PitOp: match[2],
Chord: match[3],
VolOp: match[4],
DurOp: match[5],
Stac: match[6].length
}
}
}
] : []
}
}
module.exports = NoteSyntax