Class: Argstring::Lexer::AssignmentFinder

Inherits:
Object
  • Object
show all
Defined in:
lib/argstring/lexer/assignment_finder.rb

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ AssignmentFinder

Returns a new instance of AssignmentFinder.



6
7
8
# File 'lib/argstring/lexer/assignment_finder.rb', line 6

def initialize(config)
	@config = config
end

Instance Method Details

#indices(segment, errors:) ⇒ Object

Find all top-level assignment operators in the segment. Returns array of indices into the segment string.



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
# File 'lib/argstring/lexer/assignment_finder.rb', line 12

def indices(segment, errors:)
	indices = []

	i = 0
	in_enclosure = false
	current_encloser = nil

	while i < segment.length
		ch = segment[i]

		if in_enclosure
			if @config.escape_enabled? && ch == @config.escape
				i = advance_past_escape(segment, i)
				next
			end

			if ch == current_encloser[:close]
				in_enclosure = false
				current_encloser = nil
			end
			i += 1
			next
		end

		pair = @config.encloser_open_pair_for(ch)
		if pair
			in_enclosure = true
			current_encloser = pair
			i += 1
			next
		end

		if @config.escape_enabled? && ch == @config.escape
			i = advance_past_escape(segment, i)
			next
		end

		if @config.assignment.include?(ch)
			indices << i
		end

		i += 1
	end

	if in_enclosure
		errors << ParseError.new(
			code: :unterminated_enclosure,
			message: "Unterminated enclosure in segment",
			raw: segment
		)
		return []
	end

	indices
end