summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibJS/SourceCode.cpp
blob: 26bc90e360a118b4a1a299054627a6752b2bcbc7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/*
 * Copyright (c) 2022, Andreas Kling <kling@serenityos.org>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#include <AK/BinarySearch.h>
#include <AK/Utf8View.h>
#include <LibJS/SourceCode.h>
#include <LibJS/SourceRange.h>
#include <LibJS/Token.h>

namespace JS {

NonnullRefPtr<SourceCode> SourceCode::create(DeprecatedString filename, DeprecatedString code)
{
    return adopt_ref(*new SourceCode(move(filename), move(code)));
}

SourceCode::SourceCode(DeprecatedString filename, DeprecatedString code)
    : m_filename(move(filename))
    , m_code(move(code))
{
}

DeprecatedString const& SourceCode::filename() const
{
    return m_filename;
}

DeprecatedString const& SourceCode::code() const
{
    return m_code;
}

void SourceCode::compute_line_break_offsets() const
{
    m_line_break_offsets = Vector<size_t> {};

    if (m_code.is_empty())
        return;

    bool previous_code_point_was_carriage_return = false;
    Utf8View view(m_code.view());
    for (auto it = view.begin(); it != view.end(); ++it) {
        u32 code_point = *it;
        bool is_line_terminator = code_point == '\r' || (code_point == '\n' && !previous_code_point_was_carriage_return) || code_point == LINE_SEPARATOR || code_point == PARAGRAPH_SEPARATOR;
        previous_code_point_was_carriage_return = code_point == '\r';

        if (is_line_terminator)
            m_line_break_offsets->append(view.byte_offset_of(it));
    }
}

SourceRange SourceCode::range_from_offsets(u32 start_offset, u32 end_offset) const
{
    // If the underlying code is an empty string, the range is 1,1 - 1,1 no matter what.
    if (m_code.is_empty())
        return { *this, { .line = 1, .column = 1, .offset = 0 }, { .line = 1, .column = 1, .offset = 0 } };

    if (!m_line_break_offsets.has_value())
        compute_line_break_offsets();

    size_t line = 1;
    size_t nearest_line_break_index = 0;
    size_t nearest_preceding_line_break_offset = 0;

    if (!m_line_break_offsets->is_empty()) {
        binary_search(*m_line_break_offsets, start_offset, &nearest_line_break_index);
        line = 1 + nearest_line_break_index;
        nearest_preceding_line_break_offset = (*m_line_break_offsets)[nearest_line_break_index];
    }

    Optional<Position> start;
    Optional<Position> end;

    size_t column = 1;

    bool previous_code_point_was_carriage_return = false;

    Utf8View view(m_code.view());
    for (auto it = view.iterator_at_byte_offset_without_validation(nearest_preceding_line_break_offset); it != view.end(); ++it) {

        // If we're on or after the start offset, this is the start position.
        if (!start.has_value() && view.byte_offset_of(it) >= start_offset) {
            start = Position {
                .line = line,
                .column = column,
                .offset = start_offset,
            };
        }

        // If we're on or after the end offset, this is the end position.
        if (!end.has_value() && view.byte_offset_of(it) >= end_offset) {
            end = Position {
                .line = line,
                .column = column,
                .offset = end_offset,
            };
            break;
        }

        u32 code_point = *it;

        bool is_line_terminator = code_point == '\r' || (code_point == '\n' && !previous_code_point_was_carriage_return) || code_point == LINE_SEPARATOR || code_point == PARAGRAPH_SEPARATOR;
        previous_code_point_was_carriage_return = code_point == '\r';

        if (is_line_terminator) {
            ++line;
            column = 1;
            continue;
        }
        ++column;
    }

    // If we didn't find both a start and end position, just return 1,1-1,1.
    // FIXME: This is a hack. Find a way to return the nicest possible values here.
    if (!start.has_value() || !end.has_value())
        return SourceRange { *this, { .line = 1, .column = 1 }, { .line = 1, .column = 1 } };

    return SourceRange { *this, *start, *end };
}

}