string GenerateTranslationFile(string languageName, string direction, string translatedText)
        {
            var mapQueue  = new Queue <LineMap>(maps);
            var lineQueue = new Queue <string>(translatedText.Split(new[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries));

            var translationDoc = XDocument.Load("Translation.xaml");

            foreach (var node in translationDoc.Descendants())
            {
                if (node.Name.LocalName.Equals("FlowDirection"))
                {
                    node.Value = direction;
                }
                else if (IsStringNode(node))
                {
                    LineMap map = mapQueue.Dequeue();
                    node.Value = lineQueue.Dequeue();
                    node.Value = node.Value.Replace(newLineSymbol, "\r\n");
                    foreach (string word in map.Stopped)
                    {
                        node.Value = ReplaceFirstOccurrance(node.Value, stopWordSymbol, word);
                    }
                }
            }

            string fileName = "Translation." + languageName + ".xaml";

            translationDoc.Save(fileName);
            return(fileName);
        }
Exemplo n.º 2
0
 /// <nodoc />
 public DeserializationContext(FileModuleLiteral currentFile, BuildXLReader reader, PathTable pathTable, LineMap lineMap)
 {
     LineMap     = lineMap;
     CurrentFile = currentFile;
     Reader      = reader;
     PathTable   = pathTable;
 }
        public CompilationUnitNode BuildCodeModel(IStreamSource source)
        {
            _hasErrors = false;

            string filePath = source.FullName;

#if DEBUG
            if (_options.InternalTestMode)
            {
                // This ensures in file paths are just file names in test output.
                filePath = Path.GetFileName(filePath);
            }
#endif // DEBUG
            char[] buffer = GetBuffer(source);
            if (buffer == null)
            {
                _errorHandler.ReportError("Unable to read from file " + filePath, filePath);
                return(null);
            }

            IDictionary definesTable = new Hashtable();
            if ((_options.Defines != null) && (_options.Defines.Count != 0))
            {
                foreach (string s in _options.Defines)
                {
                    definesTable[s] = null;
                }
            }

            NameTable nameTable = new NameTable();
            LineMap   lineMap   = new LineMap(filePath);

            FileLexer lexer = new FileLexer(nameTable, filePath);
            lexer.OnError += new FileErrorEventHandler(OnError);
            Token[] tokens = lexer.Lex(buffer, definesTable, lineMap, /* includeComments */ false);

            if (_hasErrors == false)
            {
                FileParser parser = new FileParser(nameTable, filePath);
                parser.OnError += new FileErrorEventHandler(OnError);

                CompilationUnitNode compilationUnit = parser.Parse(tokens, lineMap);
                foreach (ParseNode node in compilationUnit.Members)
                {
                    NamespaceNode namespaceNode = node as NamespaceNode;
                    if (namespaceNode != null)
                    {
                        namespaceNode.IncludeCompilationUnitUsingClauses();
                    }
                }

                if (_hasErrors == false)
                {
                    return(compilationUnit);
                }
            }

            return(null);
        }
Exemplo n.º 4
0
        public void ImportTXT(string txtfile, string map, bool auto, int width, bool skip, Encoding encoding)
        {
            int     Width = (int)Math.Round((double)width / 0.9375);
            LineMap MAP   = new LineMap(map);

            if (MAP.CanGetText | MAP.CanGetName)
            {
                using (StreamReader SR = new StreamReader(File.OpenRead(txtfile), encoding))
                {
                    while (SR.EndOfStream == false)
                    {
                        string[] linespl = Regex.Split(SR.ReadLine(), "\t");

                        if (MAP.CanGetText)
                        {
                            if (OpenFileName.Equals(linespl[MAP[LineMap.Type.FileName]], StringComparison.OrdinalIgnoreCase))
                            {
                                string NewText = linespl[MAP[LineMap.Type.NewText]];

                                if (!(NewText == "" & skip))
                                {
                                    if (auto)
                                    {
                                        NewText = NewText.SplitByWidth(NewCharList, Width);
                                    }
                                    else
                                    {
                                        NewText = NewText.Replace("\\n", "\n");
                                    }

                                    if (MAP[LineMap.Type.MSGindex] >= 0)
                                    {
                                        ImportText(Convert.ToInt32(linespl[MAP[LineMap.Type.MSGindex]]), Convert.ToInt32(linespl[MAP[LineMap.Type.StringIndex]]), NewText);
                                        if (MAP.CanGetNameMSG)
                                        {
                                            ImportNameByMSG(Convert.ToInt32(linespl[MAP[LineMap.Type.MSGindex]]), linespl[MAP[LineMap.Type.NewName]]);
                                        }
                                    }
                                    else
                                    {
                                        ImportText(linespl[MAP[LineMap.Type.MSGname]], Convert.ToInt32(linespl[MAP[LineMap.Type.StringIndex]]), NewText);
                                        if (MAP.CanGetNameMSG)
                                        {
                                            ImportNameByMSG(linespl[MAP[LineMap.Type.MSGname]], linespl[MAP[LineMap.Type.NewName]]);
                                        }
                                    }
                                }
                            }
                        }
                        else if (MAP.CanGetName)
                        {
                            ImportNameByName(linespl[MAP[LineMap.Type.OldName]], linespl[MAP[LineMap.Type.NewName]]);
                        }
                    }
                }
            }
        }
Exemplo n.º 5
0
 /// <nodoc />
 internal FileModuleLiteral(
     BuildXLReader reader,
     PathTable pathTable,
     GlobalModuleLiteral outerScope,
     ModuleRegistry moduleRegistry,
     LineMap lineMap)
     : this(reader, pathTable, reader.ReadAbsolutePath(), ReadPackage(reader, pathTable), outerScope, moduleRegistry, lineMap)
 {
 }
Exemplo n.º 6
0
        public bool ExportTXT(string output, string map, bool removesplit)
        {
            var temp = new LineMap(map).GetList();

            List <string> list = new List <string>();

            foreach (var a in msg)
            {
                foreach (var b in a.Strings)
                {
                    string returned = "";

                    foreach (var type in temp)
                    {
                        if (type == LineMap.Type.FileName)
                        {
                            returned += OpenFileName + "\t";
                        }
                        else if (type == LineMap.Type.MSGindex)
                        {
                            returned += a.Index + "\t";
                        }
                        else if (type == LineMap.Type.MSGname)
                        {
                            returned += a.Name + "\t";
                        }
                        else if (type == LineMap.Type.StringIndex)
                        {
                            returned += b.Index + "\t";
                        }
                        else if (type == LineMap.Type.OldText)
                        {
                            returned += removesplit ? b.OldString.GetString(OldCharList, false).Replace("\n", " ") + "\t" : b.OldString.GetString(OldCharList, false).Replace("\n", "\\n") + "\t";
                        }
                        else if (type == LineMap.Type.NewText)
                        {
                            returned += removesplit ? b.NewString.Replace("\n", " ") + "\t" : b.NewString.Replace("\n", "\\n") + "\t";
                        }
                        else if (type == LineMap.Type.OldName)
                        {
                            var name = names.FirstOrDefault(x => x.Index == a.CharacterIndex);
                            returned += name == null ? " \t" : name.OldName.GetTextBaseList().GetString(OldCharList, false).Replace("\n", " ") + "\t";
                        }
                        else if (type == LineMap.Type.NewName)
                        {
                            var name = names.FirstOrDefault(x => x.Index == a.CharacterIndex);
                            returned += name == null ? " \t" : name.NewName.Replace("\n", " ") + "\t";
                        }
                    }

                    list.Add(returned);
                }
            }

            File.AppendAllLines(output, list);
            return(true);
        }
Exemplo n.º 7
0
        public CompilationUnitNode BuildCodeModel(IStreamSource source)
        {
            hasErrors = false;

            string filePath = source.FullName;

            char[] buffer = GetBuffer(source);

            if (buffer == null)
            {
                errorHandler.ReportInputError(filePath);

                return(null);
            }

            IDictionary definesTable = new Hashtable();

            if (options.Defines != null && options.Defines.Count != 0)
            {
                foreach (string s in options.Defines)
                {
                    definesTable[s] = null;
                }
            }

            NameTable nameTable = new NameTable();
            LineMap   lineMap   = new LineMap(filePath);

            FileLexer lexer = new FileLexer(nameTable, filePath);

            lexer.OnError += OnError;
            Token[] tokens = lexer.Lex(buffer, definesTable, lineMap, /* includeComments */ false);

            if (hasErrors == false)
            {
                FileParser parser = new FileParser(nameTable, filePath);
                parser.OnError += OnError;

                CompilationUnitNode compilationUnit = parser.Parse(tokens, lineMap);

                foreach (ParseNode node in compilationUnit.Members)
                {
                    if (node is NamespaceNode namespaceNode)
                    {
                        namespaceNode.IncludeCompilationUnitUsingClauses();
                    }
                }

                if (hasErrors == false)
                {
                    return(compilationUnit);
                }
            }

            return(null);
        }
Exemplo n.º 8
0
 public TagLine(List <object> list, long Time, bool type, int TagNum, DataGrid grid, int Indx, DataTable data, LineMap Mp)
 {
     InitializeComponent();
     Getgrid   = grid;
     TagN      = TagNum;
     GrIndex   = Indx;
     GetData   = data;
     lineMapMs = Mp;
     TimeMp    = Time;
     LoadLineTag(list, Time, type, TagNum);
 }
Exemplo n.º 9
0
        /// <nodoc/>
        internal FileModuleLiteral(ModuleLiteralId id, QualifierValue qualifier, GlobalModuleLiteral outerScope, Package package, LineMap lineMap)
            : base(id, qualifier, outerScope, location: default(LineInfo))
        {
            Contract.Requires(id.Path.IsValid);
            Contract.Requires(package != null);
            Contract.Requires(lineMap != null);

            Package = package;
            m_partialSymbolsCache = Lazy.Create(() => new ConcurrentDictionary <FullSymbol, ModuleBinding>());
            LineMap = lineMap;
        }
Exemplo n.º 10
0
        /// <nodoc />
        public static Range ToRange(this LineAndColumn lineAndColumn, LineMap lineMap, int?length)
        {
            var start = lineAndColumn.ToPosition();

            // Compute the absolute end of the range by getting the absolute start and offseting that with the length
            var absoluteStart = lineMap.Map[lineAndColumn.Line - 1] + lineAndColumn.Character - 1;
            var absoluteEnd   = absoluteStart + length ?? 0;

            var end = LineInfo.FromLineMap(lineMap, absoluteEnd).ToPosition();

            return(new Range {
                Start = start, End = end
            });
        }
Exemplo n.º 11
0
        internal static FileModuleLiteral Read(
            BuildXLReader reader,
            PathTable pathTable,
            GlobalModuleLiteral outerScope,
            ModuleRegistry moduleRegistry)
        {
            var kind = (SyntaxKind)reader.ReadInt32Compact();

            var lineMap = LineMap.Read(reader);

            switch (kind)
            {
            case SyntaxKind.FileModuleLiteral:
                return(new FileModuleLiteral(reader, pathTable, outerScope, moduleRegistry, lineMap));

            default:
                string message = I($"The file module literal {kind} is not deserializable yet.");
                throw new InvalidOperationException(message);
            }
        }
Exemplo n.º 12
0
        /// <inheritdoc/>
        /// <exception cref="ArgumentOutOfRangeException">
        /// line
        /// or
        /// column
        /// </exception>
        public Place TranslateSourcePositionToConsole(int line, int column)
        {
            if (line <= 1)
            {
                throw new ArgumentOutOfRangeException(nameof(line));
            }
            if (column <= 1)
            {
                throw new ArgumentOutOfRangeException(nameof(column));
            }

            if (!LineMap.TryGetValue(line, out var newLine))
            {
                return(Place.Empty);
            }

            var newColumn = (column % ConsoleBufferWidth) - 1;

            return(new Place(newLine, newColumn));
        }
Exemplo n.º 13
0
        /// <inheritdoc/>
        public override void Serialize(BuildXLWriter writer)
        {
            // The kind for the base class (FileModuleLiteral) is sealed and thera are consumers
            // that rely on it for any subclass of FileModuleLiteral. So serializing the most specific kind
            // explicitly so deserialization knows what's coming
            writer.WriteCompact((int)SyntaxKind.FileModuleLiteral);

            LineMap.Write(writer);

            writer.Write(Path);

            WritePackage(Package, writer);

            // Don't need to save qualifier, because it only valid for instantiated modules, and this module should be uninstantiated.
            writer.WriteCompact(m_resolvedEntries.Count);
            foreach (var kvp in m_resolvedEntries)
            {
                WriteFilePosition(kvp.Key, writer);
                kvp.Value.Serialize(writer);
            }
        }
        void LoadTranslationFile()
        {
            var lines = (from node in XDocument.Load("Translation.xaml").Descendants()
                         where IsStringNode(node)
                         select node.Value).ToList();

            foreach (string line in lines)
            {
                var cleanLine = line.Replace("_", "").Replace("\r\n", newLineSymbol);
                var map       = new LineMap();
                foreach (string word in SplitWords(line))
                {
                    if (stopWords.Any(s => s.Equals(word, StringComparison.InvariantCultureIgnoreCase)))
                    {
                        map.Stopped.Add(word);
                        cleanLine = ReplaceFirstOccurrance(cleanLine, word, stopWordSymbol);
                    }
                }
                map.Line = cleanLine.ToString();
                maps.Add(map);
            }
        }
Exemplo n.º 15
0
        private void CreateSourceMapData()
        {
            var offset      = 0;
            var sourceLines = Source.Split('\r', '\n');
            var start       = sourceLines.Length - ConsoleBufferHeight;
            var lastLine    = sourceLines.Length;

            for (int i = start; i < lastLine; i++)
            {
                var line = sourceLines[i];
                LineMap.Add(i + 1, i + offset);
                if (line.Length > ConsoleBufferWidth)
                {
                    offset += line.Length / ConsoleBufferWidth;
                    //offset += (int)Math.Truncate((double)(line.Length / ConsoleBufferWidth));
                    if (line.Length % ConsoleBufferWidth == 0)
                    {
                        offset--;
                    }
                }
            }
        }
Exemplo n.º 16
0
        /// <summary>
        /// Constructs an uninstantiated file module denoted by a path.
        /// </summary>
        /// <remarks>
        /// This factory should only be used during parsing.
        /// </remarks>
        public static FileModuleLiteral CreateFileModule(AbsolutePath path, GlobalModuleLiteral globalScope, Package package, ModuleRegistry moduleRegistry, LineMap lineMap)
        {
            Contract.Requires(path.IsValid);
            Contract.Requires(globalScope != null);
            Contract.Requires(package != null);
            Contract.Requires(lineMap != null);

            return(CreateInstantiatedFileModule(path, QualifierValue.Unqualified, globalScope, package, moduleRegistry, lineMap: lineMap));
        }
Exemplo n.º 17
0
        /// <nodoc/>
        internal FileModuleLiteral(AbsolutePath path, QualifierValue qualifier, GlobalModuleLiteral outerScope, Package package, ModuleRegistry moduleRegistry, LineMap lineMap)
            : this(ModuleLiteralId.Create(path), qualifier, outerScope, package, lineMap)
        {
            Contract.Requires(path.IsValid);
            Contract.Requires(lineMap != null);

            m_moduleRegistry = moduleRegistry;
        }
Exemplo n.º 18
0
        public static bool TryInjectToElements(string source, XElement root)
        {
            var actions = new List <Action>();
            var lineMap = new LineMap(source);

            var success = true;

            foreach (var node in root.DescendantNodesAndSelf().OfType <XElement>())
            {
                foreach (var attr in node.Attributes())
                {
                    int offset;
                    if (lineMap.TryGetOffset(attr, out offset))
                    {
                        success = false;
                        break;
                    }

                    // Line info now at beginning of attribute
                    // Assertion below to check that assumption
                    // Search backwards until match.
                    var end = offset;
                    while (offset > 0 && char.IsWhiteSpace(source[offset - 1]))
                    {
                        offset--;
                    }

                    var len = end - offset;
                    if (len == 0)
                    {
                        throw new InvalidOperationException(
                                  "Assumption failed: should always be at least one whitespace char before attribute!");
                    }

                    // If it's just one whitespace character that will be implicit
                    if (len == 1 && source[offset] == ' ')
                    {
                        continue;
                    }

                    actions.Add(
                        () =>
                    {
                        attr.RemoveAnnotations <InternalTagWhitespace>();
                        attr.AddAnnotation(new InternalTagWhitespace(source.Substring(offset, len)));
                    });
                }

                if (!success)
                {
                    break;
                }

                // TODO add any remaining whitespace annotation not preceding attribute.
                int elementStartOffset;
                if (!lineMap.TryGetOffset(node, out elementStartOffset))
                {
                    var match = attributeRegex.Match(source, elementStartOffset);
                    if (!match.Success)
                    {
                        success = false;
                        break;
                    }
                    var endws = match.Groups["endws"];
                    if (endws.Length > 0)
                    {
                        node.RemoveAnnotations <InternalTagWhitespace>();
                        node.AddAnnotation(new InternalTagWhitespace(endws.Value));
                    }
                }
            }
            foreach (var action in actions)
            {
                action();
            }
            return(true);
        }
Exemplo n.º 19
0
        /// <nodoc />
        private FileModuleLiteral(BuildXLReader reader, PathTable pathTable, AbsolutePath path, Package package, GlobalModuleLiteral outerScope, ModuleRegistry moduleRegistry, LineMap lineMap)
            : this(path, QualifierValue.Unqualified, outerScope, package, moduleRegistry, lineMap)
        {
            var context = new DeserializationContext(this, reader, pathTable, lineMap);

            int resolveEntries = reader.ReadInt32Compact();

            for (int i = 0; i < resolveEntries; i++)
            {
                FilePosition location = ReadFilePosition(reader);

                var resolvedEntry = ResolvedEntry.ReadResolvedEntry(context);
                AddResolvedEntry(location, resolvedEntry);

                if (resolvedEntry.SymbolName.IsValid)
                {
                    AddResolvedEntry(resolvedEntry.SymbolName, resolvedEntry);
                }
            }
        }
Exemplo n.º 20
0
 internal FileLexerErrorEventArgs(ErrorEventArgs e, LineMap lineMap)
     : this(e.Error, lineMap.Map(e.Position), e.Args)
 {
 }
Exemplo n.º 21
0
        public static string GetErrorBaseline(IEnumerable <TestFile> inputFiles, List <Diagnostic> diagnostics)
        {
            diagnostics.Sort(DiagnosticUtilities.DiagnosticComparer.Instance);

            List <string> outputLines = new List <string>();

            // Count up all errors that were found in files other than lib.d.ts so we don't miss any
            int totalErrorsReportedInNonLibraryFiles = 0;

            // Report global errors
            var globalErrors = diagnostics.Where(err => err.File == null);

            foreach (var g in globalErrors)
            {
                OutputErrorText(g, outputLines, ref totalErrorsReportedInNonLibraryFiles);
            }

            // 'merge' the lines of each input file with any errors associated with it
            foreach (var inputFile in inputFiles.Where(f => !string.IsNullOrEmpty(f.Content)))
            {
                // Filter down to the errors in the file
                IEnumerable <Diagnostic> fileErrors = diagnostics.Where(e =>
                                                                        (e.File != null) && (e.File.FileName == inputFile.UnitName));

                // Header
                outputLines.Add("==== " + inputFile.UnitName + " (" + fileErrors.Count() + " errors) ====");

                // Make sure we emit something for every error
                int markedErrorCount = 0;

                var lineStarts = LineMap.ComputeLineStarts(inputFile.Content);
                var lines      = inputFile.Content.Split(new char[] { '\n' });
                if (lines.Length == 1)
                {
                    lines = lines[0].Split(new char[] { '\r' });
                }

                for (int lineIndex = 0; lineIndex < lines.Length; lineIndex++)
                {
                    string line = lines[lineIndex];

                    if (line.Length > 0 && line[line.Length - 1] == '\r')
                    {
                        line = line.Substring(0, line.Length - 1);
                    }

                    int thisLineStart = lineStarts[lineIndex];

                    // On the last line of the file, fake the next line start number so that we handle errors on the last character of the file correctly
                    int nextLineStart = (lineIndex == lines.Length - 1) ?
                                        inputFile.Content.Length :
                                        lineStarts[lineIndex + 1];

                    // Emit this line from the original file
                    outputLines.Add("    " + line);

                    foreach (Diagnostic err in fileErrors)
                    {
                        // Does any error start or continue on to this line? Emit squiggles
                        int end = err.TextSpanEnd;

                        if ((end >= thisLineStart) &&
                            ((err.Start < nextLineStart) || (lineIndex == lines.Length - 1)))
                        {
                            // How many characters from the start of this line the error starts at (could be positive or negative)
                            int relativeOffset = err.Start.Value - thisLineStart;

                            // How many characters of the error are on this line (might be longer than this line in reality)
                            int length = (end - err.Start.Value) - Math.Max(0, thisLineStart - err.Start.Value);

                            // Calculate the start of the squiggle
                            int squiggleStart = Math.Max(0, relativeOffset);

                            StringBuilder builder = new StringBuilder();

                            // outputLines.push("    " + line.substr(0, squiggleStart).replace(/[^\s]/g, " ") + new Array(Math.min(length, line.length - squiggleStart) + 1).join("~"));
                            builder.Append("    ");
                            builder.Append(new string(' ', squiggleStart));
                            builder.Append(new string('~', Math.Min(length, line.Length - squiggleStart)));

                            outputLines.Add(builder.ToString());

                            // If the error ended here, or we're at the end of the file, emit its message
                            if ((lineIndex == lines.Length - 1) ||
                                (nextLineStart > end))
                            {
                                OutputErrorText(err, outputLines, ref totalErrorsReportedInNonLibraryFiles);
                                markedErrorCount++;
                            }
                        }
                    }
                }

                Assert.Equal(markedErrorCount, fileErrors.Count());
            }

            // TODO: Do we need to implement IsBuiltFile?
            var numLibraryDiagnostics = diagnostics.Where(diagnostic =>
                                                          diagnostic.File != null &&
                                                          IsLibraryFile(diagnostic.File.FileName)).Count();

            var numTest262HarnessDiagnostics = diagnostics.Where(diagnostic =>
                                                                 diagnostic.File != null &&
                                                                 diagnostic.File.FileName.Contains("test262-harness")).Count();

            // Verify we didn't miss any errors in total
            Assert.Equal(
                totalErrorsReportedInNonLibraryFiles + numLibraryDiagnostics + numTest262HarnessDiagnostics,
                diagnostics.Count);

            StringBuilder result = new StringBuilder();

            result
            .Append(MinimalDiagnosticsToString(diagnostics))
            .AppendLine()
            .AppendLine()
            .Append(string.Join("\r\n", outputLines));

            return(result.ToString());
        }
Exemplo n.º 22
0
 public static int GetLine(this Exception ex)
 {
     return(LineMap.InfoFrom(ex).Line);
 }
Exemplo n.º 23
0
 /// <summary>
 /// Constructs an instantiated file module denoted by a path.
 /// </summary>
 /// <remarks>
 /// The outer scope is typically the global module.
 /// </remarks>
 protected static FileModuleLiteral CreateInstantiatedFileModule(AbsolutePath path, QualifierValue qualifier, GlobalModuleLiteral globalScope, Package package, ModuleRegistry moduleRegistry, LineMap lineMap)
 {
     return(new FileModuleLiteral(path, qualifier, globalScope, package, moduleRegistry, lineMap));
 }
Exemplo n.º 24
0
 public static string GetSourceFile(this Exception ex)
 {
     return(LineMap.InfoFrom(ex).SourceFile);
 }
Exemplo n.º 25
0
        /// <summary>
        /// Constructs an uninstantiated file module denoted by a path.
        /// </summary>
        /// <remarks>
        /// This factory should only be used during parsing.
        /// </remarks>
        public static FileModuleLiteral CreateFileModule(AbsolutePath path, IModuleRegistry moduleRegistry, Package package, LineMap lineMap)
        {
            Contract.Requires(path.IsValid);
            Contract.Requires(moduleRegistry != null);
            Contract.Requires(package != null);
            Contract.Requires(lineMap != null);

            return(CreateInstantiatedFileModule(path, QualifierValue.Unqualified, ((ModuleRegistry)moduleRegistry).GlobalLiteral, package, (ModuleRegistry)moduleRegistry, lineMap: lineMap));
        }