コード例 #1
0
        /**
         * Returns the original source, line, and column information for the generated
         * source's line and column positions provided. The only argument is an object
         * with the following properties:
         *
         *   - line: The line number in the generated source.
         *   - column: The column number in the generated source.
         *
         * and an object is returned with the following properties:
         *
         *   - source: The original source file, or null.
         *   - line: The line number in the original source, or null.
         *   - column: The column number in the original source, or null.
         *   - name: The original identifier, or null.
         */
        public override OriginalPosition OriginalPositionFor(int line, int column, EBias?bias = null)

        {
            var needle = new MappingItemIndexed
            {
                GeneratedLine   = line,
                GeneratedColumn = column
            };

            // Find the section containing the generated position we're trying to map
            // to an original position.
            var sectionIndex = BinarySearch.Search(
                needle,
                _sections,
                (n, s) =>
            {
                var cmp = n.GeneratedLine - s.GeneratedOffset.GeneratedLine;
                if (cmp == 0)
                {
                    return(cmp);
                }

                return(n.GeneratedColumn - s.GeneratedOffset.GeneratedColumn);
            },
                EBias.GREATEST_LOWER_BOUND);
            var section = _sections[sectionIndex];

            if (section == null)
            {
                return(new OriginalPosition
                {
                    Source = null,
                    Line = null,
                    Column = null,
                    Name = null
                });
            }

            return(section.Consumer.OriginalPositionFor(
                       needle.GeneratedLine - (section.GeneratedOffset.GeneratedLine - 1),
                       needle.GeneratedColumn - (section.GeneratedOffset.GeneratedLine == needle.GeneratedLine
                                        ? section.GeneratedOffset.GeneratedColumn - 1
                                        : 0),
                       bias));
        }
コード例 #2
0
        /**
         * Find the mapping that best matches the hypothetical "needle" mapping that
         * we are searching for in the given "haystack" of mappings.
         */

        protected int FindMapping(
            MappingItemIndexed needle,
            List <MappingItemIndexed> aMappings,
            Func <MappingItemIndexed, int?> line,
            Func <MappingItemIndexed, int?> column,
            Func <MappingItemIndexed, MappingItemIndexed, int> aComparator,
            EBias aBias)
        {
            // To return the position we are searching for, we must first find the
            // mapping for the given position and then return the opposite position it
            // points to. Because the mappings are sorted, we can use binary search to
            // find the best mapping.
            if (line(needle) <= 0)
            {
                throw new ArgumentException($"Line must be greater than or equal to 1, got {line(needle)}");
            }
            if (column(needle) < 0)
            {
                throw new ArgumentException($"Column must be greater than or equal to 0, got {column(needle)}");
            }

            return(BinarySearch.Search <MappingItemIndexed>(needle, aMappings, aComparator, aBias));
        }