public static DataLine Parse(POFile file, IO.TextLine source, Line previous) { Match match; bool isContinuation; DataLine.Kinds?kind; string text; int? index; string sourceValue = source.Value; if ((match = DataLine.RxContinuation.Match(sourceValue)).Success) { if (previous == null) { throw new Exception("Data continuation at block start."); } if (previous.Type != Line.Types.Data) { throw new Exception("Data continuation after a non-data line."); } DataLine prevData = (DataLine)previous; isContinuation = true; kind = prevData._kind; text = match.Groups["text"].Value; Line.IIndexedLine idx = prevData as Line.IIndexedLine; index = (idx != null) ? idx.Index : (int?)null; } else { isContinuation = false; kind = null; text = null; index = null; foreach (KeyValuePair <DataLine.Kinds, Regex> kv in DataLine.RxFindKind) { match = kv.Value.Match(sourceValue); if (!match.Success) { continue; } kind = kv.Key; text = match.Groups["text"].Value; index = (match.Groups["index"].Value.Length > 0) ? int.Parse(match.Groups["index"].Value) : (int?)null; break; } } if (!kind.HasValue) { throw new Exception("Unknown line kind."); } if (index.HasValue) { return(new IndexedDataLine(file, source, isContinuation, kind.Value, text, index.Value)); } else { return(new DataLine(file, source, isContinuation, kind.Value, text)); } }
public static CommentLine Parse(POFile file, IO.TextLine source, Line previous) { Match match; bool isContinuation; CommentLine.Kinds?kind; string text; int?index; string sourceValue = source.Value; if ((match = CommentLine.RxContinuation_PreviousUntraslated.Match(sourceValue)).Success) { if (previous == null) { throw new Exception("Comment of kind 'previous-untranslated' continuation at block start."); } if (previous.Type != Line.Types.Comment) { throw new Exception("Comment of kind 'previous-untranslated' continuation after a non-comment line."); } CommentLine prevComment = (CommentLine)previous; switch (prevComment._kind) { case CommentLine.Kinds.PreviousUntraslated_Context: case CommentLine.Kinds.PreviousUntraslated_ID: case CommentLine.Kinds.PreviousUntraslated_IDPlural: break; default: throw new Exception(string.Format("Comment of kind 'previous-untranslated' continuation after a comment of kind '{0}'.", prevComment._kind)); } isContinuation = true; kind = prevComment._kind; text = match.Groups["text"].Value; Line.IIndexedLine idx = prevComment as Line.IIndexedLine; index = (idx != null) ? idx.Index : (int?)null; } else if ((match = CommentLine.RxContinuation_Removed.Match(sourceValue)).Success) { if (previous == null) { throw new Exception("Comment of kind 'removed' continuation at block start."); } if (previous.Type != Line.Types.Comment) { throw new Exception("Comment of kind 'removed' continuation after a non-comment line."); } CommentLine prevComment = (CommentLine)previous; switch (prevComment._kind) { case CommentLine.Kinds.Removed_Context: case CommentLine.Kinds.Removed_ID: case CommentLine.Kinds.Removed_IDPlural: case CommentLine.Kinds.Removed_Translated: case CommentLine.Kinds.Removed_TranslatedIndexed: break; default: throw new Exception(string.Format("Comment of kind 'removed' continuation after a comment of kind '{0}'.", prevComment._kind)); } isContinuation = true; kind = prevComment._kind; text = match.Groups["text"].Value; Line.IIndexedLine idx = prevComment as Line.IIndexedLine; index = (idx != null) ? idx.Index : (int?)null; } else { isContinuation = false; kind = null; text = null; index = null; foreach (KeyValuePair <CommentLine.Kinds, Regex> kv in CommentLine.RxFindKind) { match = kv.Value.Match(sourceValue); if (!match.Success) { continue; } kind = kv.Key; text = match.Groups["text"].Value; index = (match.Groups["index"].Value.Length > 0) ? int.Parse(match.Groups["index"].Value) : (int?)null; break; } } if (index.HasValue) { return(new IndexedCommentLine(file, source, isContinuation, kind.Value, text, index.Value)); } else { return(new CommentLine(file, source, isContinuation, kind.Value, text)); } }