Skip to main content

ruby_prism/
lib.rs

1//! # ruby-prism
2//!
3//! Rustified version of Ruby's prism parser.
4//!
5#![warn(clippy::all, clippy::nursery, clippy::pedantic, future_incompatible, missing_docs, nonstandard_style, rust_2018_idioms, trivial_casts, trivial_numeric_casts, unreachable_pub, unused_qualifications)]
6
7// Most of the code in this file is generated, so sometimes it generates code
8// that doesn't follow the clippy rules. We don't want to see those warnings.
9#[allow(clippy::too_many_lines, clippy::use_self)]
10mod bindings {
11    use std::ptr::NonNull;
12
13    // In `build.rs`, we generate bindings based on the config.yml file. Here is
14    // where we pull in those bindings and make them part of our library.
15    include!(concat!(env!("OUT_DIR"), "/bindings.rs"));
16}
17
18mod node;
19mod node_ext;
20mod parse_result;
21mod string_query;
22
23use std::ffi::CString;
24use std::ptr::NonNull;
25
26pub use self::bindings::*;
27pub use self::node::{ConstantId, ConstantList, ConstantListIter, Integer, NodeList, NodeListIter};
28pub use self::node_ext::{ConstantPathError, FullName};
29pub use self::parse_result::{Comment, CommentType, Comments, Diagnostic, Diagnostics, Location, MagicComment, MagicComments, NodeComments, ParseResult};
30pub use self::string_query::{InvalidEncodingError, StringQuery};
31
32use ruby_prism_sys::{
33    pm_arena_new, pm_options_attach_comments_set, pm_options_command_line_set, pm_options_encoding_locked_set, pm_options_encoding_set, pm_options_filepath_set, pm_options_free, pm_options_frozen_string_literal_set, pm_options_line_set, pm_options_main_script_set, pm_options_new,
34    pm_options_partial_script_set, pm_options_scope_forwarding_set, pm_options_scope_init, pm_options_scope_local_mut, pm_options_scope_mut, pm_options_scopes_init, pm_options_t, pm_options_version_set, pm_parse, pm_parser_new, pm_string_init,
35};
36
37/// The version of Ruby syntax to parse with.
38#[derive(Debug, Clone, Copy, PartialEq, Eq)]
39pub enum Version {
40    /// Use the latest version of prism.
41    Latest,
42    /// The vendored version of prism in `CRuby` 3.3.x.
43    CRuby3_3,
44    /// The vendored version of prism in `CRuby` 3.4.x.
45    CRuby3_4,
46    /// The vendored version of prism in `CRuby` 4.0.x, formerly known as 3.5.x.
47    #[deprecated(note = "Use Version::CRuby4_0 instead", since = "2.1.0")]
48    CRuby3_5,
49    /// The vendored version of prism in `CRuby` 4.0.x.
50    CRuby4_0,
51    /// The vendored version of prism in `CRuby` 4.1.x.
52    CRuby4_1,
53}
54
55impl Version {
56    /// Returns the version string that prism's C functions take for this
57    /// version, or `None` for `Latest`, which they take as `NULL`.
58    const fn as_c_str(self) -> Option<&'static std::ffi::CStr> {
59        match self {
60            Self::Latest => None,
61            Self::CRuby3_3 => Some(c"3.3"),
62            Self::CRuby3_4 => Some(c"3.4"),
63            #[allow(deprecated)]
64            Self::CRuby3_5 | Self::CRuby4_0 => Some(c"4.0"),
65            Self::CRuby4_1 => Some(c"4.1"),
66        }
67    }
68
69    /// Returns a pointer to the version string that prism's C functions take
70    /// for this version, which is `NULL` for `Latest`.
71    fn as_ptr(self) -> *const std::ffi::c_char {
72        self.as_c_str().map_or(std::ptr::null(), std::ffi::CStr::as_ptr)
73    }
74
75    /// Calls `pm_options_version_set` with the appropriate version string.
76    /// `Latest` needs no call, since the parser uses the latest version when
77    /// none is set.
78    unsafe fn set_on(self, opts: *mut pm_options_t) {
79        if let Some(version) = self.as_c_str() {
80            pm_options_version_set(opts, version.as_ptr(), version.to_bytes().len());
81        }
82    }
83}
84
85/// A command line option that affects parsing behavior.
86#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
87pub enum CommandLineFlag {
88    /// `-a`: splits the input line `$_` into `$F`.
89    A,
90    /// `-e`: specifies a script to be executed.
91    E,
92    /// `-l`: chomps the input line by default.
93    L,
94    /// `-n`: wraps the script in a `while gets` loop.
95    N,
96    /// `-p`: prints the value of `$_` at the end of each loop.
97    P,
98    /// `-x`: searches the input file for a shebang.
99    X,
100}
101
102impl From<CommandLineFlag> for u8 {
103    fn from(flag: CommandLineFlag) -> Self {
104        match flag {
105            CommandLineFlag::A => ruby_prism_sys::PM_OPTIONS_COMMAND_LINE_A,
106            CommandLineFlag::E => ruby_prism_sys::PM_OPTIONS_COMMAND_LINE_E,
107            CommandLineFlag::L => ruby_prism_sys::PM_OPTIONS_COMMAND_LINE_L,
108            CommandLineFlag::N => ruby_prism_sys::PM_OPTIONS_COMMAND_LINE_N,
109            CommandLineFlag::P => ruby_prism_sys::PM_OPTIONS_COMMAND_LINE_P,
110            CommandLineFlag::X => ruby_prism_sys::PM_OPTIONS_COMMAND_LINE_X,
111        }
112    }
113}
114
115/// A forwarding parameter for a scope.
116#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
117pub enum ScopeForwardingFlag {
118    /// Forwarding with the `*` parameter.
119    Positionals,
120    /// Forwarding with the `**` parameter.
121    Keywords,
122    /// Forwarding with the `&` parameter.
123    Block,
124    /// Forwarding with the `...` parameter.
125    All,
126}
127
128impl From<ScopeForwardingFlag> for u8 {
129    fn from(flag: ScopeForwardingFlag) -> Self {
130        match flag {
131            ScopeForwardingFlag::Positionals => ruby_prism_sys::PM_OPTIONS_SCOPE_FORWARDING_POSITIONALS,
132            ScopeForwardingFlag::Keywords => ruby_prism_sys::PM_OPTIONS_SCOPE_FORWARDING_KEYWORDS,
133            ScopeForwardingFlag::Block => ruby_prism_sys::PM_OPTIONS_SCOPE_FORWARDING_BLOCK,
134            ScopeForwardingFlag::All => ruby_prism_sys::PM_OPTIONS_SCOPE_FORWARDING_ALL,
135        }
136    }
137}
138
139/// A scope of locals surrounding the code that is being parsed.
140#[derive(Debug, Clone, Default)]
141pub struct Scope {
142    locals: Vec<Vec<u8>>,
143    forwarding: Vec<ScopeForwardingFlag>,
144}
145
146impl Scope {
147    /// Sets the local variable names in this scope.
148    #[must_use]
149    pub fn locals(mut self, locals: Vec<Vec<u8>>) -> Self {
150        self.locals = locals;
151        self
152    }
153
154    /// Sets the forwarding parameters in this scope.
155    #[must_use]
156    pub fn forwarding(mut self, forwarding: Vec<ScopeForwardingFlag>) -> Self {
157        self.forwarding = forwarding;
158        self
159    }
160}
161
162/// Options that can be passed to the parser.
163#[allow(clippy::struct_excessive_bools)]
164#[derive(Debug, Clone, Default)]
165pub struct Options {
166    filepath: Option<String>,
167    line: Option<i32>,
168    encoding: Option<String>,
169    encoding_locked: bool,
170    frozen_string_literal: Option<bool>,
171    command_line: Vec<CommandLineFlag>,
172    version: Option<Version>,
173    main_script: bool,
174    partial_script: bool,
175    attach_comments: bool,
176    scopes: Vec<Scope>,
177}
178
179impl Options {
180    /// Sets the filepath option.
181    #[must_use]
182    pub fn filepath(mut self, filepath: &str) -> Self {
183        self.filepath = Some(filepath.to_string());
184        self
185    }
186
187    /// Sets the line option.
188    #[must_use]
189    pub const fn line(mut self, line: i32) -> Self {
190        self.line = Some(line);
191        self
192    }
193
194    /// Sets the encoding option.
195    #[must_use]
196    pub fn encoding(mut self, encoding: &str) -> Self {
197        self.encoding = Some(encoding.to_string());
198        self
199    }
200
201    /// Sets the encoding locked option.
202    #[must_use]
203    pub const fn encoding_locked(mut self, locked: bool) -> Self {
204        self.encoding_locked = locked;
205        self
206    }
207
208    /// Sets the frozen string literal option. `Some(true)` freezes string
209    /// literals, `Some(false)` keeps them mutable, and `None` leaves the
210    /// option unset.
211    #[must_use]
212    pub const fn frozen_string_literal(mut self, frozen: Option<bool>) -> Self {
213        self.frozen_string_literal = frozen;
214        self
215    }
216
217    /// Sets the command line flags.
218    #[must_use]
219    pub fn command_line(mut self, command_line: Vec<CommandLineFlag>) -> Self {
220        self.command_line = command_line;
221        self
222    }
223
224    /// Sets the version option.
225    #[must_use]
226    pub const fn version(mut self, version: Version) -> Self {
227        self.version = Some(version);
228        self
229    }
230
231    /// Sets the main script option.
232    #[must_use]
233    pub const fn main_script(mut self, main_script: bool) -> Self {
234        self.main_script = main_script;
235        self
236    }
237
238    /// Sets the partial script option.
239    #[must_use]
240    pub const fn partial_script(mut self, partial_script: bool) -> Self {
241        self.partial_script = partial_script;
242        self
243    }
244
245    /// Sets the attach comments option. When enabled, comments are attached to
246    /// the nodes they belong to and can be read with [`Node::comments`].
247    #[must_use]
248    pub const fn attach_comments(mut self, attach_comments: bool) -> Self {
249        self.attach_comments = attach_comments;
250        self
251    }
252
253    /// Adds a scope to the options.
254    #[must_use]
255    pub fn scope(mut self, scope: Scope) -> Self {
256        self.scopes.push(scope);
257        self
258    }
259
260    /// Sets the scopes, replacing any previously added scopes.
261    #[must_use]
262    pub fn scopes(mut self, scopes: Vec<Scope>) -> Self {
263        self.scopes = scopes;
264        self
265    }
266
267    /// Builds the C-level parse options from these options. The returned
268    /// `ParseOptions` must outlive any `ParseResult` created from it.
269    ///
270    /// # Panics
271    ///
272    /// Panics if `filepath` or `encoding` contain interior null bytes.
273    #[must_use]
274    pub fn build(self) -> ParseOptions {
275        let opts = unsafe { pm_options_new() };
276
277        let c_filepath = self.filepath.map(|filepath| {
278            let cstring = CString::new(filepath).unwrap();
279            unsafe { pm_options_filepath_set(opts, cstring.as_ptr()) };
280            cstring
281        });
282
283        if let Some(line) = self.line {
284            unsafe { pm_options_line_set(opts, line) };
285        }
286
287        let c_encoding = self.encoding.map(|encoding| {
288            let cstring = CString::new(encoding).unwrap();
289            unsafe { pm_options_encoding_set(opts, cstring.as_ptr()) };
290            cstring
291        });
292
293        if self.encoding_locked {
294            unsafe { pm_options_encoding_locked_set(opts, true) };
295        }
296
297        if let Some(frozen) = self.frozen_string_literal {
298            unsafe { pm_options_frozen_string_literal_set(opts, frozen) };
299        }
300
301        let command_line = self.command_line.iter().fold(0u8, |acc, &flag| acc | u8::from(flag));
302        if command_line != 0 {
303            unsafe { pm_options_command_line_set(opts, command_line) };
304        }
305
306        if let Some(version) = self.version {
307            unsafe { version.set_on(opts) };
308        }
309
310        if self.main_script {
311            unsafe { pm_options_main_script_set(opts, true) };
312        }
313
314        if self.partial_script {
315            unsafe { pm_options_partial_script_set(opts, true) };
316        }
317
318        if self.attach_comments {
319            unsafe { pm_options_attach_comments_set(opts, true) };
320        }
321
322        if !self.scopes.is_empty() {
323            unsafe { pm_options_scopes_init(opts, self.scopes.len()) };
324
325            for (scope_index, scope) in self.scopes.iter().enumerate() {
326                let pm_scope = unsafe { pm_options_scope_mut(opts, scope_index) };
327                unsafe { pm_options_scope_init(pm_scope, scope.locals.len()) };
328
329                for (local_index, local) in scope.locals.iter().enumerate() {
330                    let pm_local = unsafe { pm_options_scope_local_mut(pm_scope, local_index) };
331                    unsafe { pm_string_init(pm_local, local.as_ptr(), local.len()) };
332                }
333
334                let forwarding = scope.forwarding.iter().fold(0u8, |acc, &flag| acc | u8::from(flag));
335                if forwarding != 0 {
336                    unsafe { pm_options_scope_forwarding_set(pm_scope, forwarding) };
337                }
338            }
339        }
340
341        ParseOptions {
342            options: opts,
343            _filepath: c_filepath,
344            _encoding: c_encoding,
345            _scopes: self.scopes,
346        }
347    }
348}
349
350/// The C-level parse options. Created from [`Options::build`]. Must outlive
351/// any [`ParseResult`] created with [`parse_with_options`].
352pub struct ParseOptions {
353    options: *mut pm_options_t,
354    // These CStrings back the pm_string_t values inside `options`, which only
355    // view their bytes.
356    // They must not be dropped before `options` is freed.
357    _filepath: Option<CString>,
358    _encoding: Option<CString>,
359    // The scopes data that pm_string_t values point into.
360    _scopes: Vec<Scope>,
361}
362
363impl Drop for ParseOptions {
364    fn drop(&mut self) {
365        unsafe { pm_options_free(self.options) };
366    }
367}
368
369/// Initializes a parser, parses the source, and returns the result.
370///
371/// # Safety
372///
373/// `options` must be a valid pointer to a `pm_options_t` or null.
374unsafe fn parse_impl(source: &[u8], options: *const pm_options_t) -> ParseResult<'_> {
375    let arena = pm_arena_new();
376    let parser = pm_parser_new(arena, source.as_ptr(), source.len(), options);
377    let node = NonNull::new_unchecked(pm_parse(parser));
378    ParseResult::new(source, arena, parser, node)
379}
380
381/// Parses the given source string and returns a parse result.
382///
383/// # Panics
384///
385/// Panics if the parser fails to initialize.
386///
387#[must_use]
388pub fn parse(source: &[u8]) -> ParseResult<'_> {
389    unsafe { parse_impl(source, std::ptr::null()) }
390}
391
392/// Parses the given source string with the given options and returns a parse
393/// result. The `options` must outlive the returned `ParseResult`.
394///
395/// # Panics
396///
397/// Panics if the parser fails to initialize.
398///
399#[must_use]
400pub fn parse_with_options<'a>(source: &'a [u8], options: &'a ParseOptions) -> ParseResult<'a> {
401    unsafe { parse_impl(source, options.options) }
402}
403
404#[cfg(test)]
405mod tests {
406    use super::{parse, parse_with_options, Options};
407
408    #[test]
409    fn attach_comments_test() {
410        let source = "foo # comment 1\nbar # comment 2\n";
411
412        let options = Options::default().attach_comments(true).build();
413        let result = parse_with_options(source.as_ref(), &options);
414        let statements = result.node().as_program_node().unwrap().statements();
415        let body: Vec<_> = statements.body().iter().collect();
416
417        assert_eq!(body.len(), 2);
418        assert_eq!(body[0].comments().len(), 1);
419
420        let comments: Vec<_> = body[0].comments().collect();
421        assert_eq!(comments.len(), 1);
422        assert_eq!(comments[0].text(), b"# comment 1");
423
424        let comments: Vec<_> = body[1].comments().collect();
425        assert_eq!(comments.len(), 1);
426        assert_eq!(comments[0].text(), b"# comment 2");
427
428        // Without the option, no comments are attached to the nodes.
429        let result = parse(source.as_ref());
430        let statements = result.node().as_program_node().unwrap().statements();
431        let body: Vec<_> = statements.body().iter().collect();
432        assert_eq!(body[0].comments().len(), 0);
433        assert_eq!(body[1].comments().len(), 0);
434    }
435
436    #[test]
437    fn comments_test() {
438        let source = "# comment 1\n# comment 2\n# comment 3\n";
439        let result = parse(source.as_ref());
440
441        for comment in result.comments() {
442            assert_eq!(super::CommentType::InlineComment, comment.type_());
443            let text = std::str::from_utf8(comment.text()).unwrap();
444            assert!(text.starts_with("# comment"));
445        }
446    }
447
448    #[test]
449    fn line_offsets_test() {
450        let source = "";
451        let result = parse(source.as_ref());
452
453        let expected: [u32; 1] = [0];
454        assert_eq!(expected, result.line_offsets());
455
456        let source = "1 + 1";
457        let result = parse(source.as_ref());
458
459        let expected: [u32; 1] = [0];
460        assert_eq!(expected, result.line_offsets());
461
462        let source = "begin\n1 + 1\n2 + 2\nend";
463        let result = parse(source.as_ref());
464
465        let expected: [u32; 4] = [0, 6, 12, 18];
466        assert_eq!(expected, result.line_offsets());
467    }
468
469    #[test]
470    fn magic_comments_test() {
471        use crate::MagicComment;
472
473        let source = "# typed: ignore\n# typed:true\n#typed: strict\n";
474        let result = parse(source.as_ref());
475
476        let comments: Vec<MagicComment<'_>> = result.magic_comments().collect();
477        assert_eq!(3, comments.len());
478
479        assert_eq!(b"typed", comments[0].key());
480        assert_eq!(b"ignore", comments[0].value());
481
482        assert_eq!(b"typed", comments[1].key());
483        assert_eq!(b"true", comments[1].value());
484
485        assert_eq!(b"typed", comments[2].key());
486        assert_eq!(b"strict", comments[2].value());
487    }
488
489    #[test]
490    fn data_loc_test() {
491        let source = "1";
492        let result = parse(source.as_ref());
493        let data_loc = result.data_loc();
494        assert!(data_loc.is_none());
495
496        let source = "__END__\nabc\n";
497        let result = parse(source.as_ref());
498        let data_loc = result.data_loc().unwrap();
499        let slice = std::str::from_utf8(result.as_slice(&data_loc)).unwrap();
500        assert_eq!(slice, "__END__\nabc\n");
501
502        let source = "1\n2\n3\n__END__\nabc\ndef\n";
503        let result = parse(source.as_ref());
504        let data_loc = result.data_loc().unwrap();
505        let slice = std::str::from_utf8(result.as_slice(&data_loc)).unwrap();
506        assert_eq!(slice, "__END__\nabc\ndef\n");
507    }
508
509    #[test]
510    fn location_slice_test() {
511        let source = "111 + 222 + 333";
512        let result = parse(source.as_ref());
513
514        let node = result.node();
515        let node = node.as_program_node().unwrap().statements().body().iter().next().unwrap();
516        let node = node.as_call_node().unwrap().receiver().unwrap();
517        let plus = node.as_call_node().unwrap();
518        let node = plus.arguments().unwrap().arguments().iter().next().unwrap();
519
520        let location = node.as_integer_node().unwrap().location();
521        let slice = std::str::from_utf8(result.as_slice(&location)).unwrap();
522
523        assert_eq!(slice, "222");
524        assert_eq!(6, location.start);
525        assert_eq!(location.start, location.start());
526        assert_eq!(9, location.end());
527
528        let recv_loc = plus.receiver().unwrap().location();
529        assert_eq!(recv_loc.as_slice(), b"111");
530        assert_eq!(0, recv_loc.start);
531        assert_eq!(3, recv_loc.end());
532
533        let joined = recv_loc.join(&location).unwrap();
534        assert_eq!(joined.as_slice(), b"111 + 222");
535
536        let not_joined = location.join(&recv_loc);
537        assert!(not_joined.is_none());
538
539        {
540            let result = parse(source.as_ref());
541            let node = result.node();
542            let node = node.as_program_node().unwrap().statements().body().iter().next().unwrap();
543            let node = node.as_call_node().unwrap().receiver().unwrap();
544            let plus = node.as_call_node().unwrap();
545            let node = plus.arguments().unwrap().arguments().iter().next().unwrap();
546
547            let location = node.as_integer_node().unwrap().location();
548            let not_joined = recv_loc.join(&location);
549            assert!(not_joined.is_none());
550
551            let not_joined = location.join(&recv_loc);
552            assert!(not_joined.is_none());
553        }
554
555        let location = node.location();
556        let slice = std::str::from_utf8(result.as_slice(&location)).unwrap();
557
558        assert_eq!(slice, "222");
559
560        let slice = std::str::from_utf8(location.as_slice()).unwrap();
561
562        assert_eq!(slice, "222");
563    }
564
565    #[test]
566    fn location_line_column_test() {
567        let source = "first\nsecond\nthird";
568        let result = parse(source.as_ref());
569
570        let node = result.node();
571        let program = node.as_program_node().unwrap();
572        let statements = program.statements().body();
573        let mut iter = statements.iter();
574
575        let _first = iter.next().unwrap();
576        let second = iter.next().unwrap();
577        let third = iter.next().unwrap();
578
579        let second_loc = second.location();
580        assert_eq!(second_loc.start_line(), 2);
581        assert_eq!(second_loc.end_line(), 2);
582        assert_eq!(second_loc.start_column(), 0);
583        assert_eq!(second_loc.end_column(), 6);
584
585        let third_loc = third.location();
586        assert_eq!(third_loc.start_line(), 3);
587        assert_eq!(third_loc.end_line(), 3);
588        assert_eq!(third_loc.start_column(), 0);
589        assert_eq!(third_loc.end_column(), 5);
590    }
591
592    #[test]
593    fn location_chop_test() {
594        let result = parse(b"foo");
595        let mut location = result.node().as_program_node().unwrap().location();
596
597        assert_eq!(location.chop().as_slice(), b"fo");
598        assert_eq!(location.chop().chop().chop().as_slice(), b"");
599
600        // Check that we don't go negative.
601        for _ in 0..10 {
602            location = location.chop();
603        }
604        assert_eq!(location.as_slice(), b"");
605    }
606
607    #[test]
608    fn visitor_test() {
609        use super::{visit_interpolated_regular_expression_node, visit_regular_expression_node, InterpolatedRegularExpressionNode, RegularExpressionNode, Visit};
610
611        struct RegularExpressionVisitor {
612            count: usize,
613        }
614
615        impl Visit<'_> for RegularExpressionVisitor {
616            fn visit_interpolated_regular_expression_node(&mut self, node: &InterpolatedRegularExpressionNode<'_>) {
617                self.count += 1;
618                visit_interpolated_regular_expression_node(self, node);
619            }
620
621            fn visit_regular_expression_node(&mut self, node: &RegularExpressionNode<'_>) {
622                self.count += 1;
623                visit_regular_expression_node(self, node);
624            }
625        }
626
627        let source = "# comment 1\n# comment 2\nmodule Foo; class Bar; /abc #{/def/}/; end; end";
628        let result = parse(source.as_ref());
629
630        let mut visitor = RegularExpressionVisitor { count: 0 };
631        visitor.visit(&result.node());
632
633        assert_eq!(visitor.count, 2);
634    }
635
636    #[test]
637    fn node_upcast_test() {
638        use super::Node;
639
640        let source = "module Foo; end";
641        let result = parse(source.as_ref());
642
643        let node = result.node();
644        let upcast_node = node.as_program_node().unwrap().as_node();
645        assert!(matches!(upcast_node, Node::ProgramNode { .. }));
646
647        let node = node.as_program_node().unwrap().statements().body().iter().next().unwrap();
648        let upcast_node = node.as_module_node().unwrap().as_node();
649        assert!(matches!(upcast_node, Node::ModuleNode { .. }));
650    }
651
652    #[test]
653    fn constant_id_test() {
654        let source = "module Foo; x = 1; y = 2; end";
655        let result = parse(source.as_ref());
656
657        let node = result.node();
658        assert_eq!(node.as_program_node().unwrap().statements().body().len(), 1);
659        assert!(!node.as_program_node().unwrap().statements().body().is_empty());
660        let module = node.as_program_node().and_then(|pn| pn.statements().body().first()).unwrap();
661        let module = module.as_module_node().unwrap();
662
663        assert_eq!(module.locals().len(), 2);
664        assert!(!module.locals().is_empty());
665
666        assert_eq!(module.locals().first().unwrap().as_slice(), b"x");
667        assert_eq!(module.locals().last().unwrap().as_slice(), b"y");
668
669        let source = "module Foo; end";
670        let result = parse(source.as_ref());
671
672        let node = result.node();
673        assert_eq!(node.as_program_node().unwrap().statements().body().len(), 1);
674        assert!(!node.as_program_node().unwrap().statements().body().is_empty());
675        let module = node.as_program_node().and_then(|pn| pn.statements().body().first()).unwrap();
676        let module = module.as_module_node().unwrap();
677
678        assert_eq!(module.locals().len(), 0);
679        assert!(module.locals().is_empty());
680    }
681
682    #[test]
683    fn optional_loc_test() {
684        let source = r"
685module Example
686  x = call_func(3, 4)
687  y = x.call_func 5, 6
688end
689";
690        let result = parse(source.as_ref());
691
692        let node = result.node();
693        let module = node.as_program_node().unwrap().statements().body().iter().next().unwrap();
694        let module = module.as_module_node().unwrap();
695        let body = module.body();
696        let writes = body.iter().next().unwrap().as_statements_node().unwrap().body().iter().collect::<Vec<_>>();
697        assert_eq!(writes.len(), 2);
698
699        let asgn = &writes[0];
700        let call = asgn.as_local_variable_write_node().unwrap().value();
701        let call = call.as_call_node().unwrap();
702
703        let call_operator_loc = call.call_operator_loc();
704        assert!(call_operator_loc.is_none());
705        let closing_loc = call.closing_loc();
706        assert!(closing_loc.is_some());
707
708        let asgn = &writes[1];
709        let call = asgn.as_local_variable_write_node().unwrap().value();
710        let call = call.as_call_node().unwrap();
711
712        let call_operator_loc = call.call_operator_loc();
713        assert!(call_operator_loc.is_some());
714        let closing_loc = call.closing_loc();
715        assert!(closing_loc.is_none());
716    }
717
718    #[test]
719    fn frozen_strings_test() {
720        let source = r#"
721# frozen_string_literal: true
722"foo"
723"#;
724        let result = parse(source.as_ref());
725        assert!(result.frozen_string_literals());
726
727        let source = "3";
728        let result = parse(source.as_ref());
729        assert!(!result.frozen_string_literals());
730    }
731
732    #[test]
733    fn string_flags_test() {
734        let source = r#"
735# frozen_string_literal: true
736"foo"
737"#;
738        let result = parse(source.as_ref());
739
740        let node = result.node();
741        let string = node.as_program_node().unwrap().statements().body().iter().next().unwrap();
742        let string = string.as_string_node().unwrap();
743        assert!(string.is_frozen());
744
745        let source = r#"
746"foo"
747"#;
748        let result = parse(source.as_ref());
749
750        let node = result.node();
751        let string = node.as_program_node().unwrap().statements().body().iter().next().unwrap();
752        let string = string.as_string_node().unwrap();
753        assert!(!string.is_frozen());
754    }
755
756    #[test]
757    fn call_flags_test() {
758        let source = r"
759x
760";
761        let result = parse(source.as_ref());
762
763        let node = result.node();
764        let call = node.as_program_node().unwrap().statements().body().iter().next().unwrap();
765        let call = call.as_call_node().unwrap();
766        assert!(call.is_variable_call());
767
768        let source = r"
769x&.foo
770";
771        let result = parse(source.as_ref());
772
773        let node = result.node();
774        let call = node.as_program_node().unwrap().statements().body().iter().next().unwrap();
775        let call = call.as_call_node().unwrap();
776        assert!(call.is_safe_navigation());
777    }
778
779    #[test]
780    fn integer_flags_test() {
781        let source = r"
7820b1
783";
784        let result = parse(source.as_ref());
785
786        let node = result.node();
787        let i = node.as_program_node().unwrap().statements().body().iter().next().unwrap();
788        let i = i.as_integer_node().unwrap();
789        assert!(i.is_binary());
790        assert!(!i.is_decimal());
791        assert!(!i.is_octal());
792        assert!(!i.is_hexadecimal());
793
794        let source = r"
7951
796";
797        let result = parse(source.as_ref());
798
799        let node = result.node();
800        let i = node.as_program_node().unwrap().statements().body().iter().next().unwrap();
801        let i = i.as_integer_node().unwrap();
802        assert!(!i.is_binary());
803        assert!(i.is_decimal());
804        assert!(!i.is_octal());
805        assert!(!i.is_hexadecimal());
806
807        let source = r"
8080o1
809";
810        let result = parse(source.as_ref());
811
812        let node = result.node();
813        let i = node.as_program_node().unwrap().statements().body().iter().next().unwrap();
814        let i = i.as_integer_node().unwrap();
815        assert!(!i.is_binary());
816        assert!(!i.is_decimal());
817        assert!(i.is_octal());
818        assert!(!i.is_hexadecimal());
819
820        let source = r"
8210x1
822";
823        let result = parse(source.as_ref());
824
825        let node = result.node();
826        let i = node.as_program_node().unwrap().statements().body().iter().next().unwrap();
827        let i = i.as_integer_node().unwrap();
828        assert!(!i.is_binary());
829        assert!(!i.is_decimal());
830        assert!(!i.is_octal());
831        assert!(i.is_hexadecimal());
832    }
833
834    #[test]
835    fn range_flags_test() {
836        let source = r"
8370..1
838";
839        let result = parse(source.as_ref());
840
841        let node = result.node();
842        let range = node.as_program_node().unwrap().statements().body().iter().next().unwrap();
843        let range = range.as_range_node().unwrap();
844        assert!(!range.is_exclude_end());
845
846        let source = r"
8470...1
848";
849        let result = parse(source.as_ref());
850
851        let node = result.node();
852        let range = node.as_program_node().unwrap().statements().body().iter().next().unwrap();
853        let range = range.as_range_node().unwrap();
854        assert!(range.is_exclude_end());
855    }
856
857    #[allow(clippy::too_many_lines, clippy::cognitive_complexity)]
858    #[test]
859    fn regex_flags_test() {
860        let source = r"
861/a/i
862";
863        let result = parse(source.as_ref());
864
865        let node = result.node();
866        let regex = node.as_program_node().unwrap().statements().body().iter().next().unwrap();
867        let regex = regex.as_regular_expression_node().unwrap();
868        assert!(regex.is_ignore_case());
869        assert!(!regex.is_extended());
870        assert!(!regex.is_multi_line());
871        assert!(!regex.is_euc_jp());
872        assert!(!regex.is_ascii_8bit());
873        assert!(!regex.is_windows_31j());
874        assert!(!regex.is_utf_8());
875        assert!(!regex.is_once());
876
877        let source = r"
878/a/x
879";
880        let result = parse(source.as_ref());
881
882        let node = result.node();
883        let regex = node.as_program_node().unwrap().statements().body().iter().next().unwrap();
884        let regex = regex.as_regular_expression_node().unwrap();
885        assert!(!regex.is_ignore_case());
886        assert!(regex.is_extended());
887        assert!(!regex.is_multi_line());
888        assert!(!regex.is_euc_jp());
889        assert!(!regex.is_ascii_8bit());
890        assert!(!regex.is_windows_31j());
891        assert!(!regex.is_utf_8());
892        assert!(!regex.is_once());
893
894        let source = r"
895/a/m
896";
897        let result = parse(source.as_ref());
898
899        let node = result.node();
900        let regex = node.as_program_node().unwrap().statements().body().iter().next().unwrap();
901        let regex = regex.as_regular_expression_node().unwrap();
902        assert!(!regex.is_ignore_case());
903        assert!(!regex.is_extended());
904        assert!(regex.is_multi_line());
905        assert!(!regex.is_euc_jp());
906        assert!(!regex.is_ascii_8bit());
907        assert!(!regex.is_windows_31j());
908        assert!(!regex.is_utf_8());
909        assert!(!regex.is_once());
910
911        let source = r"
912/a/e
913";
914        let result = parse(source.as_ref());
915
916        let node = result.node();
917        let regex = node.as_program_node().unwrap().statements().body().iter().next().unwrap();
918        let regex = regex.as_regular_expression_node().unwrap();
919        assert!(!regex.is_ignore_case());
920        assert!(!regex.is_extended());
921        assert!(!regex.is_multi_line());
922        assert!(regex.is_euc_jp());
923        assert!(!regex.is_ascii_8bit());
924        assert!(!regex.is_windows_31j());
925        assert!(!regex.is_utf_8());
926        assert!(!regex.is_once());
927
928        let source = r"
929/a/n
930";
931        let result = parse(source.as_ref());
932
933        let node = result.node();
934        let regex = node.as_program_node().unwrap().statements().body().iter().next().unwrap();
935        let regex = regex.as_regular_expression_node().unwrap();
936        assert!(!regex.is_ignore_case());
937        assert!(!regex.is_extended());
938        assert!(!regex.is_multi_line());
939        assert!(!regex.is_euc_jp());
940        assert!(regex.is_ascii_8bit());
941        assert!(!regex.is_windows_31j());
942        assert!(!regex.is_utf_8());
943        assert!(!regex.is_once());
944
945        let source = r"
946/a/s
947";
948        let result = parse(source.as_ref());
949
950        let node = result.node();
951        let regex = node.as_program_node().unwrap().statements().body().iter().next().unwrap();
952        let regex = regex.as_regular_expression_node().unwrap();
953        assert!(!regex.is_ignore_case());
954        assert!(!regex.is_extended());
955        assert!(!regex.is_multi_line());
956        assert!(!regex.is_euc_jp());
957        assert!(!regex.is_ascii_8bit());
958        assert!(regex.is_windows_31j());
959        assert!(!regex.is_utf_8());
960        assert!(!regex.is_once());
961
962        let source = r"
963/a/u
964";
965        let result = parse(source.as_ref());
966
967        let node = result.node();
968        let regex = node.as_program_node().unwrap().statements().body().iter().next().unwrap();
969        let regex = regex.as_regular_expression_node().unwrap();
970        assert!(!regex.is_ignore_case());
971        assert!(!regex.is_extended());
972        assert!(!regex.is_multi_line());
973        assert!(!regex.is_euc_jp());
974        assert!(!regex.is_ascii_8bit());
975        assert!(!regex.is_windows_31j());
976        assert!(regex.is_utf_8());
977        assert!(!regex.is_once());
978
979        let source = r"
980/a/o
981";
982        let result = parse(source.as_ref());
983
984        let node = result.node();
985        let regex = node.as_program_node().unwrap().statements().body().iter().next().unwrap();
986        let regex = regex.as_regular_expression_node().unwrap();
987        assert!(!regex.is_ignore_case());
988        assert!(!regex.is_extended());
989        assert!(!regex.is_multi_line());
990        assert!(!regex.is_euc_jp());
991        assert!(!regex.is_ascii_8bit());
992        assert!(!regex.is_windows_31j());
993        assert!(!regex.is_utf_8());
994        assert!(regex.is_once());
995    }
996
997    #[test]
998    fn visitor_traversal_test() {
999        use crate::{Node, Visit};
1000
1001        #[derive(Default)]
1002        struct NodeCounts {
1003            pre_parent: usize,
1004            post_parent: usize,
1005            pre_leaf: usize,
1006            post_leaf: usize,
1007        }
1008
1009        #[derive(Default)]
1010        struct CountingVisitor {
1011            counts: NodeCounts,
1012        }
1013
1014        impl Visit<'_> for CountingVisitor {
1015            fn visit_branch_node_enter(&mut self, _node: Node<'_>) {
1016                self.counts.pre_parent += 1;
1017            }
1018
1019            fn visit_branch_node_leave(&mut self) {
1020                self.counts.post_parent += 1;
1021            }
1022
1023            fn visit_leaf_node_enter(&mut self, _node: Node<'_>) {
1024                self.counts.pre_leaf += 1;
1025            }
1026
1027            fn visit_leaf_node_leave(&mut self) {
1028                self.counts.post_leaf += 1;
1029            }
1030        }
1031
1032        let source = r"
1033module Example
1034  x = call_func(3, 4)
1035  y = x.call_func 5, 6
1036end
1037";
1038        let result = parse(source.as_ref());
1039        let node = result.node();
1040        let mut visitor = CountingVisitor::default();
1041        visitor.visit(&node);
1042
1043        assert_eq!(7, visitor.counts.pre_parent);
1044        assert_eq!(7, visitor.counts.post_parent);
1045        assert_eq!(6, visitor.counts.pre_leaf);
1046        assert_eq!(6, visitor.counts.post_leaf);
1047    }
1048
1049    #[test]
1050    fn visitor_lifetime_test() {
1051        use crate::{Node, Visit};
1052
1053        #[derive(Default)]
1054        struct StackingNodeVisitor<'a> {
1055            stack: Vec<Node<'a>>,
1056            max_depth: usize,
1057        }
1058
1059        impl<'pr> Visit<'pr> for StackingNodeVisitor<'pr> {
1060            fn visit_branch_node_enter(&mut self, node: Node<'pr>) {
1061                self.stack.push(node);
1062            }
1063
1064            fn visit_branch_node_leave(&mut self) {
1065                self.stack.pop();
1066            }
1067
1068            fn visit_leaf_node_leave(&mut self) {
1069                self.max_depth = self.max_depth.max(self.stack.len());
1070            }
1071        }
1072
1073        let source = r"
1074module Example
1075  x = call_func(3, 4)
1076  y = x.call_func 5, 6
1077end
1078";
1079        let result = parse(source.as_ref());
1080        let node = result.node();
1081        let mut visitor = StackingNodeVisitor::default();
1082        visitor.visit(&node);
1083
1084        assert_eq!(0, visitor.stack.len());
1085        assert_eq!(5, visitor.max_depth);
1086    }
1087
1088    #[test]
1089    fn integer_value_test() {
1090        let result = parse("0xA".as_ref());
1091        let integer = result.node().as_program_node().unwrap().statements().body().iter().next().unwrap().as_integer_node().unwrap().value();
1092        let value: i32 = integer.try_into().unwrap();
1093
1094        assert_eq!(value, 10);
1095    }
1096
1097    #[test]
1098    fn integer_zero_value_test() {
1099        for source in ["0", "-0"] {
1100            let result = parse(source.as_ref());
1101            let integer = result.node().as_program_node().unwrap().statements().body().iter().next().unwrap().as_integer_node().unwrap().value();
1102            let value: i32 = integer.try_into().unwrap();
1103
1104            assert_eq!(value, 0);
1105        }
1106    }
1107
1108    #[test]
1109    fn integer_small_value_to_u32_digits_test() {
1110        let result = parse("0xA".as_ref());
1111        let integer = result.node().as_program_node().unwrap().statements().body().iter().next().unwrap().as_integer_node().unwrap().value();
1112        let (negative, digits) = integer.to_u32_digits();
1113
1114        assert!(!negative);
1115        assert_eq!(digits, &[10]);
1116    }
1117
1118    #[test]
1119    fn integer_zero_to_u32_digits_test() {
1120        let result = parse("0".as_ref());
1121        let integer = result.node().as_program_node().unwrap().statements().body().iter().next().unwrap().as_integer_node().unwrap().value();
1122        let (negative, digits) = integer.to_u32_digits();
1123
1124        assert!(!negative);
1125        assert_eq!(digits, &[0]);
1126    }
1127
1128    #[test]
1129    fn integer_two_digit_value_to_u32_digits_test() {
1130        let result = parse("0xFFFFFFFF00000001".as_ref());
1131        let integer = result.node().as_program_node().unwrap().statements().body().iter().next().unwrap().as_integer_node().unwrap().value();
1132        let (negative, digits) = integer.to_u32_digits();
1133
1134        assert!(!negative);
1135        assert_eq!(digits, &[1, 4_294_967_295]);
1136    }
1137
1138    #[test]
1139    fn integer_large_value_to_u32_digits_test() {
1140        let result = parse("0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF".as_ref());
1141        let integer = result.node().as_program_node().unwrap().statements().body().iter().next().unwrap().as_integer_node().unwrap().value();
1142        let (negative, digits) = integer.to_u32_digits();
1143
1144        assert!(!negative);
1145        assert_eq!(digits, &[4_294_967_295, 4_294_967_295, 4_294_967_295, 2_147_483_647]);
1146    }
1147
1148    #[test]
1149    fn float_value_test() {
1150        let result = parse("1.0".as_ref());
1151        let value: f64 = result.node().as_program_node().unwrap().statements().body().iter().next().unwrap().as_float_node().unwrap().value();
1152
1153        assert!((value - 1.0).abs() < f64::EPSILON);
1154    }
1155
1156    #[test]
1157    fn regex_value_test() {
1158        let result = parse(b"//");
1159        let node = result.node().as_program_node().unwrap().statements().body().iter().next().unwrap().as_regular_expression_node().unwrap();
1160        assert_eq!(node.unescaped(), b"");
1161    }
1162
1163    #[test]
1164    fn node_field_lifetime_test() {
1165        // The code below wouldn't typecheck prior to https://github.com/ruby/prism/pull/2519,
1166        // but we need to stop clippy from complaining about it.
1167        #![allow(clippy::needless_pass_by_value)]
1168
1169        use crate::Node;
1170
1171        #[derive(Default)]
1172        struct Extract<'pr> {
1173            scopes: Vec<crate::ConstantId<'pr>>,
1174        }
1175
1176        impl<'pr> Extract<'pr> {
1177            fn push_scope(&mut self, path: Node<'pr>) {
1178                if let Some(cread) = path.as_constant_read_node() {
1179                    self.scopes.push(cread.name());
1180                } else if let Some(cpath) = path.as_constant_path_node() {
1181                    if let Some(parent) = cpath.parent() {
1182                        self.push_scope(parent);
1183                    }
1184                    self.scopes.push(cpath.name().unwrap());
1185                } else {
1186                    panic!("Wrong node kind!");
1187                }
1188            }
1189        }
1190
1191        let source = "Some::Random::Constant";
1192        let result = parse(source.as_ref());
1193        let node = result.node();
1194        let mut extractor = Extract::default();
1195        extractor.push_scope(node.as_program_node().unwrap().statements().body().iter().next().unwrap());
1196        assert_eq!(3, extractor.scopes.len());
1197    }
1198
1199    #[test]
1200    fn parse_with_options_frozen_string_literal_test() {
1201        use super::{parse_with_options, Options};
1202
1203        let source = b"\"foo\"";
1204        let options = Options::default().frozen_string_literal(Some(true)).build();
1205        let result = parse_with_options(source, &options);
1206
1207        let node = result.node();
1208        let string = node.as_program_node().unwrap().statements().body().iter().next().unwrap();
1209        let string = string.as_string_node().unwrap();
1210        assert!(string.is_frozen());
1211    }
1212
1213    #[test]
1214    fn parse_with_options_filepath_test() {
1215        use super::{parse_with_options, Options};
1216
1217        let source = b"__FILE__";
1218        let options = Options::default().filepath("test.rb").build();
1219        let result = parse_with_options(source, &options);
1220        assert!(result.errors().next().is_none());
1221    }
1222
1223    #[test]
1224    fn parse_with_options_line_test() {
1225        use super::{parse_with_options, Options};
1226
1227        let source = b"1 + 2";
1228        let options = Options::default().line(10).build();
1229        let result = parse_with_options(source, &options);
1230        assert!(result.errors().next().is_none());
1231    }
1232
1233    #[test]
1234    #[allow(deprecated)]
1235    fn parse_with_options_cruby4_0_test() {
1236        use super::{parse_with_options, Options, Version};
1237
1238        let source = b"private def foo = puts 'Hello'";
1239        let newer_source = b"def foo(&nil); end";
1240
1241        for version in [Version::CRuby4_0, Version::CRuby3_5] {
1242            let options = Options::default().version(version).build();
1243            let result = parse_with_options(source, &options);
1244            assert!(result.errors().next().is_none());
1245
1246            let call = result.node().as_program_node().unwrap().statements().body().first().unwrap().as_call_node().unwrap();
1247            assert_eq!(call.name().as_slice(), b"private");
1248            let method = call.arguments().unwrap().arguments().first().unwrap().as_def_node().unwrap();
1249            assert_eq!(method.name().as_slice(), b"foo");
1250
1251            assert!(parse_with_options(newer_source, &options).errors().next().is_some());
1252        }
1253
1254        let earlier_options = Options::default().version(Version::CRuby3_4).build();
1255        assert!(parse_with_options(source, &earlier_options).errors().next().is_some());
1256
1257        let later_options = Options::default().version(Version::CRuby4_1).build();
1258        assert!(parse_with_options(newer_source, &later_options).errors().next().is_none());
1259    }
1260
1261    #[test]
1262    fn parse_with_options_default_line_test() {
1263        use super::{parse_with_options, Options};
1264
1265        let source = b"foo\nbar";
1266        let options = Options::default().filepath("test.rb").build();
1267        let result = parse_with_options(source, &options);
1268
1269        let node = result.node();
1270        let statements: Vec<_> = node.as_program_node().unwrap().statements().body().iter().collect();
1271        assert_eq!(statements[0].location().start_line(), 1);
1272        assert_eq!(statements[1].location().start_line(), 2);
1273    }
1274
1275    #[test]
1276    fn parse_with_options_scopes_test() {
1277        use super::{parse_with_options, Options, Scope};
1278
1279        let source = b"x";
1280        let scope = Scope::default().locals(vec![b"x".to_vec()]);
1281        let options = Options::default().scope(scope).build();
1282        let result = parse_with_options(source, &options);
1283        assert!(result.errors().next().is_none());
1284
1285        let node = result.node();
1286        let stmt = node.as_program_node().unwrap().statements().body().iter().next().unwrap();
1287        assert!(stmt.as_local_variable_read_node().is_some());
1288    }
1289
1290    #[test]
1291    fn diagnostic_message_test() {
1292        let source = b"# encoding: shift_jis\nx = <<~\x82\xA0\n";
1293        let result = parse(source);
1294        let error = result.errors().next().unwrap();
1295
1296        let message = error.message();
1297        assert!(std::str::from_utf8(message).is_err());
1298        assert!(message.windows(2).any(|window| window == b"\x82\xA0"));
1299    }
1300
1301    #[test]
1302    fn malformed_shebang() {
1303        let source = "#!\x00";
1304        let result = parse(source.as_ref());
1305        assert!(result.errors().next().is_none());
1306        assert!(result.warnings().next().is_none());
1307    }
1308}