Ejemplo n.º 1
0
        internal static ParsingResult TryParse(string input, out Interval interval)
        {
            interval = null;

            if (string.IsNullOrWhiteSpace(input))
            {
                return(ParsingResult.EmptyInputString);
            }

            var match = ParsingUtilities.Match(input, Patterns);

            if (match == null)
            {
                return(ParsingResult.NotMatched);
            }

            int halfSteps;

            if (!ParsingUtilities.ParseInt(match, HalfStepsGroupName, 0, out halfSteps) ||
                !IntervalUtilities.IsIntervalValid(halfSteps))
            {
                return(ParsingResult.Error(HalfStepsNumberIsOutOfRange));
            }

            interval = Interval.FromHalfSteps(halfSteps);
            return(ParsingResult.Parsed);
        }
Ejemplo n.º 2
0
        internal static ParsingResult TryParse(string input, out MidiTimeSpan timeSpan)
        {
            timeSpan = null;

            if (string.IsNullOrWhiteSpace(input))
            {
                return(ParsingResult.EmptyInputString);
            }

            var match = ParsingUtilities.Match(input, Patterns);

            if (match == null)
            {
                return(ParsingResult.NotMatched);
            }

            long midiTimeSpan;

            if (!ParsingUtilities.ParseNonnegativeLong(match, TimeSpanGroupName, 0, out midiTimeSpan))
            {
                return(ParsingResult.Error(OutOfRange));
            }

            timeSpan = new MidiTimeSpan(midiTimeSpan);
            return(ParsingResult.Parsed);
        }
        internal static ParsingResult TryParse(string input, out BarBeatFractionTimeSpan timeSpan)
        {
            timeSpan = null;

            if (string.IsNullOrWhiteSpace(input))
            {
                return(ParsingResult.EmptyInputString);
            }

            var match = ParsingUtilities.Match(input, Patterns);

            if (match == null)
            {
                return(ParsingResult.NotMatched);
            }

            long bars;

            if (!ParsingUtilities.ParseNonnegativeLong(match, BarsGroupName, 0, out bars))
            {
                return(ParsingResult.Error(BarsIsOutOfRange));
            }

            double beats;

            if (!ParsingUtilities.ParseNonnegativeDouble(match, BeatsGroupName, 0, out beats))
            {
                return(ParsingResult.Error(BeatsIsOutOfRange));
            }

            timeSpan = new BarBeatFractionTimeSpan(bars, beats);
            return(ParsingResult.Parsed);
        }
Ejemplo n.º 4
0
        internal static ParsingResult TryParse(string input, out BeatTimeSpan timeSpan)
        {
            timeSpan = null;

            if (string.IsNullOrWhiteSpace(input))
            {
                return(ParsingResult.EmptyInputString);
            }

            var match = ParsingUtilities.Match(input, Patterns);

            if (match == null)
            {
                return(ParsingResult.NotMatched);
            }

            long beats;

            if (!ParsingUtilities.ParseNonnegativeLong(match, BeatsGroupName, 0, out beats))
            {
                return(ParsingResult.Error(BeatsIsOutOfRange));
            }

            long ticks;

            if (!ParsingUtilities.ParseNonnegativeLong(match, TicksGroupName, 0, out ticks))
            {
                return(ParsingResult.Error(TicksIsOutOfRange));
            }

            timeSpan = new BeatTimeSpan(beats, ticks);
            return(ParsingResult.Parsed);
        }
        internal static ParsingResult TryParse(string input, out Octave octave)
        {
            octave = null;

            if (string.IsNullOrWhiteSpace(input))
            {
                return(ParsingResult.EmptyInputString);
            }

            var match = ParsingUtilities.Match(input, Patterns);

            if (match == null)
            {
                return(ParsingResult.NotMatched);
            }

            int octaveNumber;

            if (!ParsingUtilities.ParseInt(match, OctaveNumberGroupName, Octave.Middle.Number, out octaveNumber) ||
                octaveNumber < Octave.MinOctaveNumber ||
                octaveNumber > Octave.MaxOctaveNumber)
            {
                return(ParsingResult.Error(OctaveIsOutOfRange));
            }

            octave = Octave.Get(octaveNumber);
            return(ParsingResult.Parsed);
        }
Ejemplo n.º 6
0
        public override void Visit(KeywordExpression expression)
        {
            if (expression.Name.Text == keyword)
            {
                var list = new List <Statement>(containerStack);
                list.Reverse();
                if (ParentNode is ExpressionStatement)
                {
                    list.Add(ParentNode as ExpressionStatement);
                }
                else
                {
                    parserResult.Error("{0} keyword detected, but outside of an ExpressionStatement. It is impossible to unroll the loop", expression.Span, keyword);
                }

                scopeList.Add(list);
            }
        }
Ejemplo n.º 7
0
        internal static ParsingResult TryParse(string input, out MetricTimeSpan timeSpan)
        {
            timeSpan = null;

            if (string.IsNullOrWhiteSpace(input))
            {
                return(ParsingResult.EmptyInputString);
            }

            var match = ParsingUtilities.Match(input, Patterns);

            if (match == null)
            {
                return(ParsingResult.NotMatched);
            }

            int hours;

            if (!ParsingUtilities.ParseNonnegativeInt(match, HoursGroupName, 0, out hours))
            {
                return(ParsingResult.Error(HoursIsOutOfRange));
            }

            int minutes;

            if (!ParsingUtilities.ParseNonnegativeInt(match, MinutesGroupName, 0, out minutes))
            {
                return(ParsingResult.Error(MinutesIsOutOfRange));
            }

            int seconds;

            if (!ParsingUtilities.ParseNonnegativeInt(match, SecondsGroupName, 0, out seconds))
            {
                return(ParsingResult.Error(SecondsIsOutOfRange));
            }

            int milliseconds;

            if (!ParsingUtilities.ParseNonnegativeInt(match, MillisecondsGroupName, 0, out milliseconds))
            {
                return(ParsingResult.Error(MillisecondsIsOutOfRange));
            }

            timeSpan = new MetricTimeSpan(hours, minutes, seconds, milliseconds);
            return(ParsingResult.Parsed);
        }
Ejemplo n.º 8
0
        internal static ParsingResult TryParse(string input, byte minValue, byte maxValue, out byte result)
        {
            result = default(byte);

            if (string.IsNullOrWhiteSpace(input))
            {
                return(ParsingResult.EmptyInputString);
            }

            byte tmpResult;

            if (!byte.TryParse(input.Trim(), out tmpResult) || tmpResult < minValue || tmpResult > maxValue)
            {
                return(ParsingResult.Error("Number is invalid or is out of valid range."));
            }

            result = tmpResult;
            return(ParsingResult.Parsed);
        }
Ejemplo n.º 9
0
        internal static ParsingResult TryParse(string input, out Interval interval)
        {
            interval = null;

            if (string.IsNullOrWhiteSpace(input))
            {
                return(ParsingResult.EmptyInputString);
            }

            var match = ParsingUtilities.Match(input, Patterns, ignoreCase: false);

            if (match == null)
            {
                return(ParsingResult.NotMatched);
            }

            var intervalQualityGroup = match.Groups[IntervalQualityGroupName];

            if (!intervalQualityGroup.Success)
            {
                int halfSteps;
                if (!ParsingUtilities.ParseInt(match, HalfStepsGroupName, 0, out halfSteps) ||
                    !IntervalUtilities.IsIntervalValid(halfSteps))
                {
                    return(ParsingResult.Error(HalfStepsNumberIsOutOfRange));
                }

                interval = Interval.FromHalfSteps(halfSteps);
                return(ParsingResult.Parsed);
            }

            var intervalQuality = IntervalQualitiesByLetters[intervalQualityGroup.Value];

            int intervalNumber;

            if (!ParsingUtilities.ParseInt(match, IntervalNumberGroupName, 0, out intervalNumber) || intervalNumber < 1)
            {
                return(ParsingResult.Error(IntervalNumberIsOutOfRange));
            }

            interval = Interval.Get(intervalQuality, intervalNumber);
            return(ParsingResult.Parsed);
        }
Ejemplo n.º 10
0
        internal static ParsingResult TryParse(string input, out Note note)
        {
            note = null;

            if (string.IsNullOrWhiteSpace(input))
            {
                return(ParsingResult.EmptyInputString);
            }

            var match = ParsingUtilities.Match(input, Patterns);

            if (match == null)
            {
                return(ParsingResult.NotMatched);
            }

            var noteNameGroup = match.Groups[NoteNameGroupName];

            NoteName noteName;
            var      noteNameParsingResult = NoteNameParser.TryParse(noteNameGroup.Value, out noteName);

            if (noteNameParsingResult.Status != ParsingStatus.Parsed)
            {
                return(noteNameParsingResult);
            }

            int octaveNumber;

            if (!ParsingUtilities.ParseInt(match, OctaveGroupName, Octave.Middle.Number, out octaveNumber))
            {
                return(ParsingResult.Error(OctaveIsOutOfRange));
            }

            if (!NoteUtilities.IsNoteValid(noteName, octaveNumber))
            {
                return(ParsingResult.Error(NoteIsOutOfRange));
            }

            note = Note.Get(noteName, octaveNumber);
            return(ParsingResult.Parsed);
        }
Ejemplo n.º 11
0
        internal static ParsingResult TryParse(string input, out Chord chord)
        {
            chord = null;

            if (string.IsNullOrWhiteSpace(input))
                return ParsingResult.EmptyInputString;

            var match = ParsingUtilities.Match(input, Patterns, ignoreCase: false);
            if (match == null)
                return ParsingResult.NotMatched;

            var rootNoteNameGroup = match.Groups[RootNoteNameGroupName];

            NoteName rootNoteName;
            var rootNoteNameParsingResult = NoteNameParser.TryParse(rootNoteNameGroup.Value, out rootNoteName);
            if (rootNoteNameParsingResult.Status != ParsingStatus.Parsed)
                return rootNoteNameParsingResult;

            //

            NoteName? bassNoteName = null;
            var bassNoteNameGroup = match.Groups[BassNoteNameGroupName];
            if (bassNoteNameGroup.Success)
            {
                NoteName actualBassNoteName;
                var bassNoteNameParsingResult = NoteNameParser.TryParse(bassNoteNameGroup.Value, out actualBassNoteName);
                if (bassNoteNameParsingResult.Status != ParsingStatus.Parsed)
                    return bassNoteNameParsingResult;

                bassNoteName = actualBassNoteName;
            }

            var notesNames = ChordsNamesTable.GetChordNotesNames(rootNoteName, match.Groups[ChordCharacteristicsGroupName].Value, bassNoteName);
            if (!notesNames.Any())
                return ParsingResult.Error(ChordCharacteristicIsUnknown);
            
            chord = new Chord(notesNames);
            return ParsingResult.Parsed;
        }
Ejemplo n.º 12
0
        internal static ParsingResult TryParse(string input, out NoteName noteName)
        {
            noteName = default(NoteName);

            if (string.IsNullOrWhiteSpace(input))
            {
                return(ParsingResult.EmptyInputString);
            }

            var match = ParsingUtilities.Match(input, Patterns);

            if (match == null)
            {
                return(ParsingResult.NotMatched);
            }

            var noteNameGroup  = match.Groups[NoteNameGroupName];
            var noteNameString = noteNameGroup.Value;

            var accidentalGroup = match.Groups[AccidentalGroupName];

            if (accidentalGroup.Success)
            {
                var accidental = accidentalGroup.Value;
                accidental      = accidental.Replace(Note.SharpShortString, Note.SharpLongString);
                accidental      = char.ToUpper(accidental[0]) + accidental.Substring(1);
                noteNameString += accidental;
            }

            if (!Enum.TryParse(noteNameString, out noteName))
            {
                return(ParsingResult.Error(NoteNameIsInvalid));
            }

            return(ParsingResult.Parsed);
        }
Ejemplo n.º 13
0
        internal static ParsingResult TryParse(string input, out MusicalTimeSpan timeSpan)
        {
            timeSpan = null;

            if (string.IsNullOrWhiteSpace(input))
            {
                return(ParsingResult.EmptyInputString);
            }

            var match = ParsingUtilities.Match(input, Patterns);

            if (match == null)
            {
                return(ParsingResult.NotMatched);
            }

            // Fraction

            long numerator;

            if (!ParsingUtilities.ParseNonnegativeLong(match, NumeratorGroupName, 1, out numerator))
            {
                return(ParsingResult.Error(NumeratorIsOutOfRange));
            }

            long denominator;

            if (!ParsingUtilities.ParseNonnegativeLong(match, DenominatorGroupName, 1, out denominator))
            {
                return(ParsingResult.Error(DenominatorIsOutOfRange));
            }

            var fractionMnemonicGroup = match.Groups[FractionMnemonicGroupName];

            if (fractionMnemonicGroup.Success)
            {
                var fraction = Fractions[fractionMnemonicGroup.Value];
                numerator   = fraction.Item1;
                denominator = fraction.Item2;
            }

            // Tuplet

            int tupletNotesCount;

            if (!ParsingUtilities.ParseNonnegativeInt(match, TupletNotesCountGroupName, 1, out tupletNotesCount))
            {
                return(ParsingResult.Error(TupletNotesCountIsOutOfRange));
            }

            int tupletSpaceSize;

            if (!ParsingUtilities.ParseNonnegativeInt(match, TupletSpaceSizeGroupName, 1, out tupletSpaceSize))
            {
                return(ParsingResult.Error(TupletSpaceSizeIsOutOfRange));
            }

            var tupletMnemonicGroup = match.Groups[TupletMnemonicGroupName];

            if (tupletMnemonicGroup.Success)
            {
                var tuplet = Tuplets[tupletMnemonicGroup.Value];
                tupletNotesCount = tuplet.Item1;
                tupletSpaceSize  = tuplet.Item2;
            }

            // Dots

            var dotsGroup = match.Groups[DotsGroupName];
            var dots      = dotsGroup.Success
                ? dotsGroup.Value.Length
                : 0;

            //

            timeSpan = new MusicalTimeSpan(numerator, denominator).Dotted(dots).Tuplet(tupletNotesCount, tupletSpaceSize);
            return(ParsingResult.Parsed);
        }
Ejemplo n.º 14
0
        internal static ParsingResult TryParseChordName(Match match, NoteName rootNoteName, out Chord chord)
        {
            chord = null;

            //

            Quality?quality = null;

            var qualityGroup = match.Groups[ChordQualityGroupName];

            if (qualityGroup.Success)
            {
                quality = GroupsQualities.FirstOrDefault(gq => match.Groups[gq.Key].Success).Value;
            }

            //

            var             extensionIntervalNumber  = -1;
            IntervalQuality?extensionIntervalQuality = null;

            var extensionGroup = match.Groups[ExtensionGroupName];

            if (extensionGroup.Success && !string.IsNullOrWhiteSpace(extensionGroup.Value))
            {
                if (match.Groups[ExtensionQualityGroupName].Success)
                {
                    extensionIntervalQuality = GroupsExtensionQualities.FirstOrDefault(gq => match.Groups[gq.Key].Success).Value;
                }

                if (!ParsingUtilities.ParseInt(match, ExtensionNumberGroupName, -1, out extensionIntervalNumber) ||
                    extensionIntervalNumber < 5)
                {
                    return(ParsingResult.Error(ExtensionNumberIsOutOfRange));
                }
            }

            if (quality == Quality.HalfDiminished || quality == Quality.Dominant)
            {
                if (extensionIntervalNumber >= 0 && extensionIntervalNumber != 7)
                {
                    return(ParsingResult.Error(HalfDiminishedOrDominantIsNotSeventh));
                }

                if (extensionIntervalNumber < 0)
                {
                    extensionIntervalNumber = 7;
                }
            }

            var extensionNotes   = new List <NoteName>();
            var extensionNumbers = new List <int>();

            if (extensionIntervalNumber >= 0)
            {
                var extensionNotesDictionary = GetExtensionNotes(quality, rootNoteName, extensionIntervalNumber, extensionIntervalQuality);
                extensionNotes.AddRange(extensionNotesDictionary.Keys);
                extensionNumbers.AddRange(extensionNotesDictionary.Values);
            }

            if (quality == Quality.HalfDiminished)
            {
                quality = Quality.Diminished;
            }
            else if (quality == Quality.Dominant)
            {
                quality = Quality.Major;
            }

            if (quality == null)
            {
                quality = Quality.Major;
            }

            //

            var notes = new List <NoteName>(Chord.GetByTriad(rootNoteName, ChordQualities[quality.Value]).NotesNames);

            notes.AddRange(extensionNotes);
            extensionNumbers.InsertRange(0, new[] { 1, 3, 5 });
            if (extensionIntervalNumber == 5)
            {
                notes.Clear();
                notes.AddRange(new[] { rootNoteName, extensionNotes.First() });

                extensionNumbers.Clear();
                extensionNumbers.AddRange(new[] { 1, 5 });
            }

            //

            var alteredToneGroup = match.Groups[AlteredToneGroupName];

            if (alteredToneGroup.Success)
            {
                int alteredToneNumber;
                if (!ParsingUtilities.ParseInt(match, AlteredToneNumberGroupName, -1, out alteredToneNumber))
                {
                    return(ParsingResult.Error(AlteredToneNumberIsOutOfRange));
                }

                var transposeBy = 0;

                var accidental = match.Groups[AlteredToneAccidentalGroupName].Value;
                switch (accidental)
                {
                case "#":
                case "+":
                    transposeBy = 1;
                    break;

                case "b":
                case "-":
                    transposeBy = -1;
                    break;
                }

                var maxExtensionNumber = extensionNumbers.Max();
                if (maxExtensionNumber < alteredToneNumber)
                {
                    var extensionNotesDictionary = GetExtensionNotes(quality, rootNoteName, alteredToneNumber, null)
                                                   .Where(kv => kv.Value > maxExtensionNumber);

                    notes.AddRange(extensionNotesDictionary.Select(kv => kv.Key));
                    extensionNumbers.AddRange(extensionNotesDictionary.Select(kv => kv.Value));
                }

                var index = extensionNumbers.IndexOf(alteredToneNumber);
                if (index >= 0)
                {
                    notes[index] = notes[index].Transpose(Interval.FromHalfSteps(transposeBy));
                }
            }

            //

            var suspendedGroup = match.Groups[SuspendedGroupName];

            if (suspendedGroup.Success)
            {
                int suspensionNumber;
                if (!ParsingUtilities.ParseInt(match, SuspendedNumberGroupName, -1, out suspensionNumber) ||
                    suspensionNumber != 2 && suspensionNumber != 4)
                {
                    return(ParsingResult.Error(SuspensionNumberIsOutOfRange));
                }

                var interval = suspensionNumber == 2
                    ? Interval.Get(IntervalQuality.Major, 2)
                    : Interval.Get(IntervalQuality.Perfect, 4);
                notes[1] = rootNoteName.Transpose(interval);
            }

            //

            var addedToneGroup = match.Groups[AddedToneGroupName];

            if (addedToneGroup.Success)
            {
                int addedToneNumber;
                if (!ParsingUtilities.ParseInt(match, AddedToneNumberGroupName, -1, out addedToneNumber))
                {
                    return(ParsingResult.Error(AddedToneNumberIsOutOfRange));
                }

                var interval = Interval.IsPerfect(addedToneNumber)
                    ? Interval.Get(IntervalQuality.Perfect, addedToneNumber)
                    : Interval.Get(IntervalQuality.Major, addedToneNumber);
                notes.Add(rootNoteName.Transpose(interval));
            }

            //

            var bassNoteNameGroup = match.Groups[BassNoteNameGroupName];

            if (bassNoteNameGroup.Success)
            {
                NoteName bassNoteName;
                var      bassNoteNameParsingResult = NoteNameParser.TryParse(bassNoteNameGroup.Value, out bassNoteName);
                if (bassNoteNameParsingResult.Status != ParsingStatus.Parsed)
                {
                    return(bassNoteNameParsingResult);
                }

                notes.Insert(0, bassNoteName);
            }

            //

            chord = new Chord(notes);
            return(ParsingResult.Parsed);
        }
Ejemplo n.º 15
0
        internal static ParsingResult TryParse(string input, out Scale scale)
        {
            scale = null;

            if (string.IsNullOrWhiteSpace(input))
            {
                return(ParsingResult.EmptyInputString);
            }

            var match = ParsingUtilities.Match(input, Patterns);

            if (match == null)
            {
                return(ParsingResult.NotMatched);
            }

            var rootNoteNameGroup = match.Groups[RootNoteNameGroupName];

            NoteName rootNoteName;
            var      rootNoteNameParsingResult = NoteNameParser.TryParse(rootNoteNameGroup.Value, out rootNoteName);

            if (rootNoteNameParsingResult.Status != ParsingStatus.Parsed)
            {
                return(rootNoteNameParsingResult);
            }

            //

            IEnumerable <Interval> intervals = null;

            var intervalGroup = match.Groups[IntervalGroupName];

            if (intervalGroup.Success)
            {
                var intervalsParsingResults = intervalGroup
                                              .Captures
                                              .OfType <Capture>()
                                              .Select(c =>
                {
                    Interval interval;
                    var parsingResult = IntervalParser.TryParse(c.Value, out interval);

                    return(new
                    {
                        Interval = interval,
                        ParsingResult = parsingResult
                    });
                })
                                              .ToArray();

                var notParsedResult =
                    intervalsParsingResults.FirstOrDefault(r => r.ParsingResult.Status != ParsingStatus.Parsed);
                if (notParsedResult != null)
                {
                    return(notParsedResult.ParsingResult);
                }

                intervals = intervalsParsingResults.Select(r => r.Interval).ToArray();
            }
            else
            {
                var intervalsMnemonicGroup = match.Groups[IntervalsMnemonicGroupName];
                var intervalsName          = intervalsMnemonicGroup.Value;

                intervals = ScaleIntervals.GetByName(intervalsName);
            }

            if (intervals == null)
            {
                return(ParsingResult.Error(ScaleIsUnknown));
            }

            //

            scale = new Scale(intervals, rootNoteName);
            return(ParsingResult.Parsed);
        }