示例#1
0
文件: Expr.cs 项目: taiab/nledger
        public static Value SourceCommand(CallScope args)
        {
            string     pathname;
            TextReader reader;

            if (args.Has(0))
            {
                pathname = args.Get <string>(0);
                reader   = FileSystem.GetStreamReader(pathname);
            }
            else
            {
                pathname = "<stdin>";
                reader   = VirtualConsole.Input;
            }

            SymbolScope filelocals = new SymbolScope(args);
            int         lineNum    = 0;
            string      line;
            int         pos    = 0;
            int         endpos = 0;

            while ((line = reader.ReadLine()) != null)
            {
                pos    = endpos;
                endpos = line.Length + Environment.NewLine.Length;
                lineNum++;

                line = line.TrimStart();
                if (!String.IsNullOrEmpty(line) && !line.StartsWith(";"))
                {
                    try
                    {
                        new Expr(line).Calc(filelocals);
                    }
                    catch
                    {
                        ErrorContext.Current.AddErrorContext(String.Format("While parsing value expression on line {0}:", lineNum));
                        ErrorContext.Current.AddErrorContext(ErrorContext.SourceContext(pathname, pos, endpos, "> "));
                    }
                }
            }

            return(Value.True);
        }
示例#2
0
        public bool AddXact(Xact xact)
        {
            if (xact == null)
            {
                throw new ArgumentNullException("xact");
            }

            xact.Journal = this;

            if (!xact.FinalizeXact())
            {
                xact.Journal = null;
                return(false);
            }

            ExtendXact(xact);
            CheckAllMetadata(xact);

            foreach (Post post in xact.Posts)
            {
                Post.ExtendPost(post, this);
                CheckAllMetadata(post);
            }

            // If a transaction with this UUID has already been seen, simply do
            // not add this one to the journal.  However, all automated checks
            // will have been performed by extend_xact, so asserts can still be
            // applied to it.
            Value refVal = xact.GetTag("UUID");

            if (!Value.IsNullOrEmpty(refVal))
            {
                string uuid = refVal.AsString;
                if (ChecksumMapping.ContainsKey(uuid))
                {
                    // This UUID has been seen before; apply any postings which the
                    // earlier version may have deferred.
                    foreach (Post post in xact.Posts)
                    {
                        Account            acct          = post.Account;
                        IEnumerable <Post> deferredPosts = acct.GetDeferredPosts(uuid);
                        if (deferredPosts != null)
                        {
                            foreach (Post rpost in deferredPosts)
                            {
                                if (acct == rpost.Account)
                                {
                                    acct.AddPost(rpost);
                                }
                            }
                            acct.DeleteDeferredPosts(uuid);
                        }
                    }

                    Xact other = ChecksumMapping[uuid];

                    // Copy the two lists of postings (which should be relatively
                    // short), and make sure that the intersection is the empty set
                    // (i.e., that they are the same list).
                    IEnumerable <Post> thisPosts  = xact.Posts.OrderBy(p => p.Account.FullName);
                    IEnumerable <Post> otherPosts = other.Posts.OrderBy(p => p.Account.FullName);
                    bool match = !thisPosts.Except(otherPosts, EquivalentPostingComparer.Current).Any();
                    if (!match || thisPosts.Count() != otherPosts.Count())
                    {
                        ErrorContext.Current.AddErrorContext("While comparing this previously seen transaction:");
                        ErrorContext.Current.AddErrorContext(ErrorContext.SourceContext(other.Pos.PathName, other.Pos.BegPos, other.Pos.EndPos, "> "));
                        ErrorContext.Current.AddErrorContext("to this later transaction:");
                        ErrorContext.Current.AddErrorContext(ErrorContext.SourceContext(xact.Pos.PathName, xact.Pos.BegPos, xact.Pos.EndPos, "> "));
                        throw new RuntimeError(RuntimeError.ErrorMessageTransactionsWithTheSameUUIDmustHaveEquivalentPostings);
                    }

                    xact.Journal = null;
                    return(false);
                }
                else
                {
                    ChecksumMapping.Add(uuid, xact);
                }
            }

            Xacts.Add(xact);
            return(true);
        }
示例#3
0
文件: Item.cs 项目: wittyansh/nledger
 public static string PrintItem(Item item, string prefix = null)
 {
     return(ErrorContext.SourceContext(item.Pos.PathName, item.Pos.BegPos, item.Pos.EndPos, prefix ?? String.Empty));
 }