Class: Decode::Comment::Tags

Inherits:
Object
  • Object
show all
Defined in:
lib/decode/comment/tags.rb,
sig/decode.rbs

Overview

Represents a collection of documentation tags and their parsing logic.

Constant Summary collapse

PATTERN =

Returns:

  • (Regexp)
/\A\s*@(?<directive>.*?)(\s+(?<remainder>.*?))?\Z/

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(directives) ⇒ Tags

Initialize a new tags parser.



27
28
29
# File 'lib/decode/comment/tags.rb', line 27

def initialize(directives)
	@directives = directives
end

Class Method Details

.build {|directives| ... } ⇒ Object

Build a tags parser with directive mappings.

RBS:

  • () { (Hash[String, Class]) -> void } -> Tags

Yields:

  • (directives)


17
18
19
20
21
22
23
# File 'lib/decode/comment/tags.rb', line 17

def self.build
	directives = Hash.new
	
	yield directives
	
	return self.new(directives)
end

Instance Method Details

#ignore(lines, level = 0) ⇒ Object

Ignore lines at the specified indentation level.



81
82
83
84
85
86
87
88
# File 'lib/decode/comment/tags.rb', line 81

def ignore(lines, level = 0)
	if line = lines.first
		# Is it at the right indentation level:
		return unless valid_indentation?(line, level)
		
		lines.shift
	end
end

#parse(lines, level = 0, &block) ⇒ Object

Parse documentation tags from lines.

RBS:

  • (Array[String] lines, ?Integer level) { (Node | Text) -> void } -> void



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
# File 'lib/decode/comment/tags.rb', line 48

def parse(lines, level = 0, &block)
	while line = lines.first
		# Is it at the right indentation level:
		return unless valid_indentation?(line, level)
		
		# We are going to consume the line:
		lines.shift
		
		# Match it against a tag:
		if match = PATTERN.match(line)
			directive = match[:directive] #: String
			remainder = match[:remainder] #: String
			
			# @type var klass: _Directive?
			if klass = @directives[directive]
				yield klass.parse(
					directive, remainder,
					lines, self, level
				)
			else
				# Ignore unknown directive.
			end
			
		# Or it's just text:
		else
			yield Text.new(line)
		end
	end
end

#valid_indentation?(line, level) ⇒ Boolean

Check if a line has valid indentation for the given level.

Returns:

  • (Boolean)


34
35
36
# File 'lib/decode/comment/tags.rb', line 34

def valid_indentation?(line, level)
	line.start_with?("  " * level) || line.start_with?("\t" * level)
end