Files
lib_ruby_parser
bytes
containers
error
lexer
loc
nodes
node_enum
types
alias
and
and_asgn
arg
args
array
array_pattern
array_pattern_with_tail
back_ref
begin
block
block_pass
blockarg
break_
c_send
case
case_match
casgn
cbase
class
complex
const_
const_pattern
cvar
cvasgn
def
defined
defs
dstr
dsym
e_flip_flop
empty_else
encoding
ensure
erange
false_
file
find_pattern
float
for_
forward_arg
forwarded_args
gvar
gvasgn
hash
hash_pattern
heredoc
i_flip_flop
if_
if_guard
if_mod
if_ternary
in_pattern
index
index_asgn
int
irange
ivar
ivasgn
kw_begin
kwarg
kwargs
kwnilarg
kwoptarg
kwrestarg
kwsplat
lambda
line
lvar
lvasgn
masgn
match_alt
match_as
match_current_line
match_nil_pattern
match_pattern
match_pattern_p
match_rest
match_var
match_with_lvasgn
mlhs
module
next
nil
nth_ref
numblock
op_asgn
optarg
or
or_asgn
pair
pin
postexe
preexe
procarg0
rational
redo
reg_opt
regexp
rescue
rescue_body
restarg
retry
return_
s_class
self_
send
shadowarg
splat
str_
super_
sym
true_
undef
unless_guard
until
until_post
when
while_
while_post
x_heredoc
xstr
yield_
z_super
parser
parser_options
parser_result
reserved_words
source
token
traverse
 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
crate::use_native_or_external!(Maybe);
crate::use_native_or_external!(StringPtr);
crate::use_native_or_external!(List);
crate::use_native_or_external!(SharedByteList);

use crate::source::Decoder;
use crate::source::SourceLine;
use crate::source::{decode_input, DecodedInput, InputError};

/// Representation of the source code.
#[derive(Debug, Default)]
#[repr(C)]
pub struct Input {
    pub(crate) decoded: DecodedInput,
    decoder: Maybe<Decoder>,
}

impl Input {
    /// Constructs a new input
    pub fn new<Name>(name: Name, decoder: Maybe<Decoder>) -> Self
    where
        Name: Into<StringPtr>,
    {
        Self {
            decoded: DecodedInput::named(name),
            decoder,
        }
    }

    /// Populates `Input` with a given byte array
    pub fn update_bytes(&mut self, bytes: List<u8>) {
        self.decoded.update_bytes(bytes)
    }

    pub(crate) fn byte_at(&self, idx: usize) -> Option<u8> {
        self.decoded.bytes().get(idx).copied()
    }

    pub(crate) fn unchecked_byte_at(&self, idx: usize) -> u8 {
        self.decoded.bytes()[idx]
    }

    pub(crate) fn substr_at(&self, start: usize, end: usize) -> Option<&[u8]> {
        self.decoded.substr_at(start, end)
    }

    /// Returns (line, col) pair for a given byte offset.
    ///
    /// Returns None if given offset is out of range.
    pub fn line_col_for_pos(&self, pos: usize) -> Option<(usize, usize)> {
        self.decoded.line_col_for_pos(pos)
    }

    pub(crate) fn len(&self) -> usize {
        self.decoded.len()
    }

    // pub(crate) fn is_empty(&self) -> bool {
    //     self.decoded.bytes.is_empty()
    // }

    pub(crate) fn line_at(&self, idx: usize) -> &SourceLine {
        self.decoded.line_at(idx)
    }

    pub(crate) fn lines_count(&self) -> usize {
        self.decoded.lines().len()
    }

    pub(crate) fn set_encoding(&mut self, encoding: &str) -> Result<(), InputError> {
        let new_input = decode_input(
            self.decoded.take_bytes(),
            StringPtr::from(encoding),
            &mut self.decoder,
        )
        .into_result()?;
        self.update_bytes(new_input);
        Ok(())
    }

    /// Returns raw bytes after decoding
    pub fn as_shared_bytes(&self) -> SharedByteList {
        self.decoded.as_shared_bytes()
    }

    /// Converts itself into owned vector of bytes
    pub fn into_bytes(self) -> List<u8> {
        self.decoded.into_bytes()
    }
}