internal static bool LineTryMatch(this Lines lines, int lineNo, Regex regex, out Match match, bool consume)
        {
            if (!lines.Any())
            {
                match = null;
                return(false);
            }

            if (lines.Count() < lineNo)
            {
                match = null;
                return(false);
            }

            var line = lines[lineNo];

            match = regex.Match(line);

            if (match.Success && consume)
            {
                lines.Consume(lineNo, match.Index, match.Length);
            }

            return(match.Success);
        }
        internal bool ParseMultiLineTextConstant(Lines lines)
        {
            Match match = null;

            if (!lines.FirstLineTryMatch(Patterns.MultiLineTextConst, out match))
            {
                return(false);
            }

            var variableName  = match.Groups[1].Value;
            var variableID    = match.Groups[2].Value.ToInteger();
            var stringBuilder = new StringBuilder();

            while (lines.FirstLineTryMatch(Patterns.MultiLineTextConstValue, out match))
            {
                var languageCode  = match.Groups[1].Value;
                var languageValue = match.Groups[2].Value;
                var separator     = match.Groups[3].Value;
                if (String.IsNullOrEmpty(separator)) //TextConst value is multiline
                {
                    while (!lines.FirstLineTryMatch(Patterns.EndMultiLineTextConstValue, true))
                    {
                        var valuePart = lines.First();
                        lines.Consume(0, 0, valuePart.Length);
                        languageValue += valuePart + "\n";
                    }
                }
                stringBuilder.AppendFormat("{0}={1};", languageCode, languageValue);
            }

            Listener.OnVariable(variableID, variableName, VariableType.TextConst, "", null, null, stringBuilder.ToString(), false, null, false, false, null, false, false);

            return(true);
        }
        internal static Match LineMustMatch(this Lines lines, int lineNo, Regex regex)
        {
            var line  = lines[lineNo];
            var match = regex.Match(line);

            if (!match.Success)
            {
                Exceptions.ThrowException(Exceptions.TextDoesNotMatchPattern, line, regex);
            }

            lines.Consume(lineNo, match.Index, match.Length);

            return(match);
        }
        internal bool ParseVariable(Lines lines)
        {
            var   variablePattern = CodeStyle.NoVariableIds ? Patterns.VariableNoId : Patterns.Variable;
            Match match           = null;

            lines.FirstLineTryMatch(Patterns.BlankLine);
            if (!lines.FirstLineTryMatch(Patterns.MultiLineTextConst, out match))
            {
                if (!lines.FirstLineTryMatch(variablePattern, out match))
                {
                    return(false);
                }
            }

            var variableName = match.Groups[1].Value.TrimEnd();
            var variableID   = match.Groups[2].Value.ToInteger();
            var variableType = match.Groups[3].Value;

            if (String.IsNullOrEmpty(variableType))
            {
                variableType = "TextConst";
            }
            var variableSuppressDispose   = ParseSuppressDispose(ref variableType);
            var variableRunOnClient       = ParseRunOnClient(ref variableType);
            var variableWithEvents        = ParseWithEvents(ref variableType);
            var variableSecurityFiltering = ParseSecurityFiltering(ref variableType);
            var variableInDataSet         = ParseInDataSet(ref variableType);
            var variableDimensions        = ParseDimensions(ref variableType);
            var variableTemporary         = ParseTemporary(ref variableType);
            var variableConstValue        = ParseTextConstant(ref variableType); //.ToConstValue();
            var variableSubType           = ParseVariableSubType(ref variableType);
            var variableLength            = ParseVariableLength(ref variableType);
            var variableOptionString      = ParseOptionString(ref variableType);

            if (variableType == "TextConst")
            {
                var stringBuilder = new StringBuilder();
                while (lines.FirstLineTryMatch(Patterns.MultiLineTextConstValue, out match))
                {
                    var languageCode  = match.Groups[1].Value;
                    var languageValue = match.Groups[2].Value;
                    var separator     = match.Groups[3].Value;
                    if (String.IsNullOrEmpty(separator)) //TextConst value is multiline
                    {
                        while (!lines.FirstLineTryMatch(Patterns.EndMultiLineTextConstValue, true))
                        {
                            var valuePart = lines.First();
                            lines.Consume(0, 0, valuePart.Length);
                            languageValue += valuePart + "\n";
                        }
                    }
                    stringBuilder.AppendFormat("{0}={1};", languageCode, languageValue);
                }
                variableConstValue = stringBuilder.ToString();
                if (String.IsNullOrEmpty(variableConstValue))
                {
                    lines.FirstLineTryMatch(Patterns.EndOfCodeLine, true);
                }
            }

            Listener.OnVariable(
                variableID,
                variableName,
                variableType.ToEnum <VariableType>(),
                variableSubType,
                variableLength,
                variableOptionString,
                variableConstValue,
                variableTemporary,
                variableDimensions,
                variableRunOnClient,
                variableWithEvents,
                variableSecurityFiltering,
                variableInDataSet,
                variableSuppressDispose);

            return(true);
        }