示例#1
0
 internal ParseResult(Program program)
 {
     Name      = program.Name;
     Program   = new ASTConcr <Program>(program, false);
     Flags     = new ImmutableCollection <Flag>(flags);
     Succeeded = true;
 }
示例#2
0
 /// <summary>
 /// This constructor is for parsing data terms and rules.
 /// </summary>
 internal ParseResult()
 {
     Name      = new API.ProgramName("dummy.4ml");
     Program   = new ASTConcr <Program>(new Program(Name), false);
     Flags     = new ImmutableCollection <Flag>(flags);
     Succeeded = true;
 }
示例#3
0
 /// <summary>
 /// Returns a task that builds an output model. Returns null if there is no output model named outModelName.
 /// If aliasPrefix is null, then no aliasing.
 /// TODO: Support cancellation
 /// </summary>
 public Task <AST <Program> > GetOutputModel(
     string outModelName,
     ProgramName outProgName,
     string aliasPrefix,
     CancellationToken cancel = default(CancellationToken))
 {
     return(GetOutputModel(outModelName, outProgName, (_ => aliasPrefix), cancel));
 }
示例#4
0
文件: Flag.cs 项目: thisiscam/formula
 public Flag(SeverityKind severity, Span span, string message, int code, ProgramName progName = null)
 {
     Severity    = severity;
     Span        = span;
     Message     = string.IsNullOrWhiteSpace(message) ? "" : message;
     Code        = code;
     Node        = null;
     ProgramName = progName;
 }
示例#5
0
文件: Flag.cs 项目: thisiscam/formula
 public Flag(SeverityKind severity, Node node, string message, int code, ProgramName progName = null)
 {
     Contract.Requires(node != null);
     Severity    = severity;
     Span        = node.Span;
     Message     = string.IsNullOrWhiteSpace(message) ? "" : message;
     Code        = code;
     Node        = node;
     ProgramName = progName;
 }
示例#6
0
文件: Span.cs 项目: thisiscam/formula
 public Span(int startLine, int startCol, int endLine, int endCol, ProgramName program)
 {
     if (program == null)
     {
         throw new ArgumentNullException("program");
     }
     this.startLine = startLine;
     this.startCol  = startCol;
     this.endLine   = endLine;
     this.endCol    = endCol;
     this.program   = program;
 }
示例#7
0
        public ProgramName(string uriString, ProgramName relativeToProgram)
        {
            Contract.Requires(uriString != null && relativeToProgram != null);
            workingUri = new Uri(string.Format("{0}/", Environment.CurrentDirectory), UriKind.Absolute);
            Uri        = new Uri(relativeToProgram.Uri, uriString);

            if (!Uri.AbsoluteUri.StartsWith(fileScheme.AbsoluteUri) &&
                !Uri.AbsoluteUri.StartsWith(envScheme.AbsoluteUri))
            {
                // Why ?
                throw new Exception("Invalid program name scheme; must be file or env.");
            }
        }
示例#8
0
        internal bool ParseText(ProgramName name, string programText, Span location, System.Threading.CancellationToken ctok, out ParseResult pr)
        {
            parseResult = new ParseResult(new Program(name));
            pr          = parseResult;
            bool result;

            try
            {
                var str = new System.IO.MemoryStream(System.Text.Encoding.ASCII.GetBytes(programText));
                ((Scanner)Scanner).SetSource(str);
                ((Scanner)Scanner).ParseResult = parseResult;
                ResetState();

                result = Parse(ctok);
                str.Close();
            }
            catch (Exception e)
            {
                var badFile = new Flag(
                    SeverityKind.Error,
                    default(Span),
                    Constants.BadFile.ToString(e.Message),
                    Constants.BadFile.Code,
                    parseResult.Program.Node.Name);
                parseResult.AddFlag(badFile);
                parseResult.Program.Node.GetNodeHash();
                return(false);
            }

            if (ctok.IsCancellationRequested)
            {
                var badFile = new Flag(
                    SeverityKind.Error,
                    default(Span),
                    Constants.OpCancelled.ToString(string.Format("Cancelled parsing of {0}", name.ToString(envParams))),
                    Constants.OpCancelled.Code,
                    parseResult.Program.Node.Name);
                parseResult.AddFlag(badFile);
                parseResult.Program.Node.GetNodeHash();
                return(false);
            }

            parseResult.Program.Node.GetNodeHash();
            return(result);
        }
示例#9
0
        /// <summary>
        /// Returns a task that builds an output model. Returns null if there is no output model named outModelName.
        /// If aliasPrefix is null, then no aliasing.
        /// TODO: Support cancellation
        /// </summary>
        public Task <AST <Program> > GetOutputModel(
            string outModelName,
            ProgramName outProgName,
            string aliasPrefix,
            CancellationToken cancel = default(CancellationToken))
        {
            Contract.Requires(!string.IsNullOrWhiteSpace(outModelName));
            Contract.Requires(outProgName != null);

            Set <Term> facts;

            if (!modelOutputs.TryFindValue(outModelName, out facts))
            {
                return(null);
            }

            aliasPrefix = aliasPrefix == null ? null : aliasPrefix.Trim();
            return(Task.Factory.StartNew <AST <Program> >(() =>
            {
                var bldr = new Builder();
                var modelRef = MkModelDecl(outModelName, bldr);
                var removeRenaming = applyTarget.Source.AST.Node.NodeKind == NodeKind.Transform;
                var aliases = new Map <Term, string>(Term.Compare);
                foreach (var t in facts)
                {
                    BuildFactBody(facts, t, bldr, modelRef, aliasPrefix, aliases, removeRenaming);
                }

                int count;
                bldr.GetStackCount(out count);
                Contract.Assert(count == 0);
                bldr.Load(modelRef);
                bldr.Close();

                ImmutableArray <AST <Node> > asts;
                bldr.GetASTs(out asts);

                var prog = Factory.Instance.MkProgram(outProgName);
                return Factory.Instance.AddModule(prog, asts[0]);
            }));
        }
示例#10
0
        /// <summary>
        /// Returns a task that builds a solution model. Returns null if there is no such solution.
        /// TODO: Support cancellation
        /// </summary>
        public Task <AST <Program> > GetOutputModel(
            int solNum,
            string outModelName,
            ProgramName outProgName,
            string aliasPrefix,
            CancellationToken cancel = default(CancellationToken))
        {
            Contract.Requires(!string.IsNullOrWhiteSpace(outModelName));
            Contract.Requires(outProgName != null && !string.IsNullOrWhiteSpace(aliasPrefix));

            if ((solNum < NumSolutions) != true)
            {
                return(null);
            }

            var facts = MkSolutionTerms(solNum, new TermIndex(partialModel.Index.SymbolTable));

            aliasPrefix = aliasPrefix.Trim();
            return(Task.Factory.StartNew <AST <Program> >(() =>
            {
                var bldr = new Builder();
                var modelRef = MkModelDecl(outModelName, bldr);
                var aliases = new Map <Term, string>(Term.Compare);
                foreach (var t in facts)
                {
                    BuildFactBody(facts, t, bldr, modelRef, aliasPrefix, aliases, false);
                }

                int count;
                bldr.GetStackCount(out count);
                Contract.Assert(count == 0);
                bldr.Load(modelRef);
                bldr.Close();

                ImmutableArray <AST <Node> > asts;
                bldr.GetASTs(out asts);

                var prog = Factory.Instance.MkProgram(outProgName);
                return Factory.Instance.AddModule(prog, asts[0]);
            }));
        }
示例#11
0
 public static int Compare(ProgramName n1, ProgramName n2)
 {
     Contract.Requires(n1 != null && n2 != null);
     return(n1 == n2 ? 0 :
            Uri.Compare(n1.Uri, n2.Uri, UriComponents.AbsoluteUri, UriFormat.SafeUnescaped, StringComparison.OrdinalIgnoreCase));
 }
示例#12
0
 public bool TryGetStatus(ProgramName name, out InstallStatus status)
 {
     Contract.Requires(name != null);
     return(touched.TryFindValue(name, out status));
 }
示例#13
0
        internal bool ParseFile(ProgramName name, string referrer, Span location, System.Threading.CancellationToken ctok, out ParseResult pr)
        {
            parseResult = new ParseResult(new Program(name));
            pr          = parseResult;
            bool result;

            try
            {
                var fi = new System.IO.FileInfo(name.Uri.AbsolutePath);
                if (!fi.Exists)
                {
                    var badFile = new Flag(
                        SeverityKind.Error,
                        default(Span),
                        referrer == null ?
                        Constants.BadFile.ToString(string.Format("The file {0} does not exist", name.ToString(envParams))) :
                        Constants.BadFile.ToString(string.Format("The file {0} referred to in {1} ({2}, {3}) does not exist", name.ToString(envParams), referrer, location.StartLine, location.StartCol)),
                        Constants.BadFile.Code,
                        parseResult.Program.Node.Name);
                    parseResult.AddFlag(badFile);
                    parseResult.Program.Node.GetNodeHash();
                    return(false);
                }

                var str = new System.IO.FileStream(name.Uri.AbsolutePath, System.IO.FileMode.Open, System.IO.FileAccess.Read);
                ((Scanner)Scanner).SetSource(str);
                ((Scanner)Scanner).ParseResult = parseResult;
                ResetState();

                result = Parse(ctok);
                str.Close();
            }
            catch (Exception e)
            {
                var badFile = new Flag(
                    SeverityKind.Error,
                    default(Span),
                    referrer == null ?
                    Constants.BadFile.ToString(e.Message) :
                    Constants.BadFile.ToString(string.Format("{0} referred to in {1} ({2}, {3})", e.Message, referrer, location.StartLine, location.StartCol)),
                    Constants.BadFile.Code,
                    parseResult.Program.Node.Name);
                parseResult.AddFlag(badFile);
                parseResult.Program.Node.GetNodeHash();
                return(false);
            }

            if (ctok.IsCancellationRequested)
            {
                var badFile = new Flag(
                    SeverityKind.Error,
                    default(Span),
                    referrer == null ?
                    Constants.OpCancelled.ToString(string.Format("Cancelled parsing of {0}", name.ToString(envParams))) :
                    Constants.OpCancelled.ToString(string.Format("Cancelled parsing of {0} referred to in {1} ({2}, {3})", name.ToString(envParams), referrer, location.StartLine, location.StartCol)),
                    Constants.OpCancelled.Code,
                    parseResult.Program.Node.Name);
                parseResult.AddFlag(badFile);
                parseResult.Program.Node.GetNodeHash();
                return(false);
            }

            parseResult.Program.Node.GetNodeHash();
            return(result);
        }