public void TestBuild()
        {
            GlobalIndex   globalIndex = new GlobalIndex();
            Document      document    = new StringDocument("I love sewing; my sewing machine is a Pfaff and I love it.");
            ISet <string> terms       = new HashSet <string>();

            terms.Add("machine");
            terms.Add("sewing machine");
            terms.Add("sewing");

            globalIndex.IndexDocWithCanonicalTerms(document, terms);
            IDictionary <string, ISet <string> > termVariants = new Dictionary <string, ISet <string> >();
            ISet <string> machineVariants = new HashSet <string>();

            machineVariants.Add("machine");
            termVariants.Add("machine", machineVariants);
            ISet <string> sewingMachineVariants = new HashSet <string>();

            sewingMachineVariants.Add("sewing machine");
            termVariants.Add("sewing machine", sewingMachineVariants);
            ISet <string> sewingVariants = new HashSet <string>();

            sewingVariants.Add("sewing");
            termVariants.Add("sewing", sewingVariants);
            globalIndex.IndexTermWithVariant(termVariants);

            FeatureCorpusTermFrequencyBuilder featureCorpusTermFrequencyBuilder = new FeatureCorpusTermFrequencyBuilder();
            FeatureCorpusTermFrequency        featureCorpusTermFrequency        = featureCorpusTermFrequencyBuilder.Build(globalIndex);

            Assert.AreEqual(13, featureCorpusTermFrequency.GetTotalCorpusTermFrequency());
            Assert.AreEqual(1, featureCorpusTermFrequency.GetTermFrequency("machine"));

            Assert.AreEqual(2, featureCorpusTermFrequency.GetTermFrequency("sewing"));
        }
Example #2
0
        private static void _error <T>(ErrorToken <T> token, string source)
        {
            var col = token.ErrorResult.Remainder.Column;
            var lin = token.ErrorResult.Remainder.Line;
            var exp = token.ErrorResult.Expectations.First();
            var rem = token.ErrorResult.Remainder.Current;

            var nestedLine  = source.Split('\n')[lin - 1];
            var f**k        = getFromMiddle(nestedLine, col, nestedLine.Length - col, true);
            var startOffset = source.IndexOf(nestedLine, StringComparison.InvariantCultureIgnoreCase);
            var nameOffset  = (startOffset + col - 1);

            var doc2            = new StringDocument("", source);
            var highlightRegion = new SourceRegion(new SourceSpan(doc2, startOffset, nestedLine.Length));

            var focusRegion = new SourceRegion(
                new SourceSpan(doc2, nameOffset, f**k.Length));
            var title   = $"{token.ErrorResult.getWarningCode().To<string>().Pastel(Color.Orange)}";
            var message = $"character '{exp}' expected".Pastel(Color.Orange);

            string Render(MarkupNode node, params NodeRenderer[] extraRenderers)
Example #3
0
            /// <summary>
            /// Creates a synthetic token.
            /// </summary>
            /// <param name="value">The token's value.</param>
            /// <param name="kind">The type of the token to synthesize.</param>
            /// <returns>A synthetic token.</returns>
            public static Token Synthesize(object value, TokenKind kind = TokenKind.Synthetic)
            {
                var doc = new StringDocument("<synthetic>", value.ToString());

                return(new Token(kind, new SourceSpan(doc, 0, doc.Length), value));
            }
Example #4
0
        public static int Main(string[] args)
        {
            // Acquire a log.
            var rawLog = TerminalLog.Acquire();
            var log    = new TransformLog(
                rawLog,
                new Func <LogEntry, LogEntry>[]
            {
                MakeDiagnostic
            });

            // Parse command-line options.
            var parser = new GnuOptionSetParser(
                Options.All, Options.Input);

            var recLog        = new RecordingLog(log);
            var parsedOptions = parser.Parse(args, recLog);

            if (recLog.Contains(Severity.Error))
            {
                // Stop the program if the command-line arguments
                // are half baked.
                return(1);
            }

            if (parsedOptions.GetValue <bool>(Options.Help))
            {
                // Wrap the help message into a log entry and send it to the log.
                rawLog.Log(
                    new LogEntry(
                        Severity.Info,
                        new HelpMessage(
                            "fbfc is a compiler that turns Brainfuck code into CIL assemblies.",
                            "fbfc path [options...]",
                            Options.All)));
                return(0);
            }

            var inputPath = parsedOptions.GetValue <string>(Options.Input);

            if (string.IsNullOrEmpty(inputPath))
            {
                log.Log(
                    new LogEntry(
                        Severity.Error,
                        "nothing to compile",
                        "no input file"));
                return(1);
            }

            var outputPath = parsedOptions.GetValue <string>(Options.Output);

            if (string.IsNullOrEmpty(outputPath))
            {
                outputPath = Path.GetFileNameWithoutExtension(inputPath) + ".exe";
            }

            // Read the Brainfuck source code from disk.
            SourceDocument source;

            try
            {
                source = new StringDocument(inputPath, File.ReadAllText(inputPath));
            }
            catch (Exception)
            {
                log.Log(
                    new LogEntry(
                        Severity.Error,
                        "invalid source path",
                        Quotation.QuoteEvenInBold(
                            "cannot read Brainfuck source code at ",
                            inputPath,
                            ".")));
                return(1);
            }

            var asmName  = Path.GetFileNameWithoutExtension(outputPath);
            var cecilAsm = Mono.Cecil.AssemblyDefinition.CreateAssembly(
                new Mono.Cecil.AssemblyNameDefinition(asmName, new Version(1, 0, 0, 0)),
                asmName,
                Mono.Cecil.ModuleKind.Console);

            var flameAsm = ClrAssembly.Wrap(cecilAsm);

            var typeEnv  = flameAsm.Resolver.TypeEnvironment;
            var compiler = new Compiler(
                flameAsm,
                Dependencies.Resolve(
                    typeEnv,
                    new ReadOnlyTypeResolver(typeEnv.Object.Parent.Assembly),
                    log),
                log,
                parsedOptions);

            compiler.Compile(source);

            cecilAsm.Write(outputPath);

            return(0);
        }