Class: Nodaire::Indental::Lexer
- Inherits:
-
Lexer
- Object
- Lexer
- Nodaire::Indental::Lexer
show all
- Defined in:
- lib/nodaire/indental/lexer.rb
Defined Under Namespace
Classes: Token
Constant Summary
Constants inherited
from Lexer
Lexer::JS_WRAPPER_REGEXP
Class Method Summary
collapse
Methods inherited from Lexer
lines_with_number, strip_js_wrapper
Class Method Details
.category_token(string, line_num) ⇒ Object
38
39
40
|
# File 'lib/nodaire/indental/lexer.rb', line 38
def self.category_token(string, line_num)
Token.new :category, normalize(string), nil, line_num
end
|
.error_token(message, line_num) ⇒ Object
57
58
59
|
# File 'lib/nodaire/indental/lexer.rb', line 57
def self.error_token(message, line_num)
Token.new :error, nil, normalize(message), line_num
end
|
.key_or_list_token(string, line_num) ⇒ Object
42
43
44
45
46
47
48
49
50
51
|
# File 'lib/nodaire/indental/lexer.rb', line 42
def self.key_or_list_token(string, line_num)
key_value = string.match(/^(.+?) :( .+)?$/)
if key_value
key, value = key_value.captures
Token.new :key_value, normalize(key), normalize(value), line_num
else
Token.new :list_name, normalize(string), nil, line_num
end
end
|
.list_item_token(string, line_num) ⇒ Object
53
54
55
|
# File 'lib/nodaire/indental/lexer.rb', line 53
def self.list_item_token(string, line_num)
Token.new :list_item, nil, normalize(string), line_num
end
|
.normalize(input) ⇒ Object
61
62
63
|
# File 'lib/nodaire/indental/lexer.rb', line 61
def self.normalize(input)
Nodaire.squeeze(input)
end
|
.spaces_indent?(line) ⇒ Boolean
33
34
35
36
|
# File 'lib/nodaire/indental/lexer.rb', line 33
def self.spaces_indent?(line)
indent = line.match(/^\s*/)[0]
indent.match(/[^ ]/).nil?
end
|
.token_for_line(line, num) ⇒ Object
22
23
24
25
26
27
28
29
30
31
|
# File 'lib/nodaire/indental/lexer.rb', line 22
def self.token_for_line(line, num)
return error_token(INDENT_CHARS_ERROR, num) unless spaces_indent?(line)
case line.match(/^\s*/)[0].size
when 0 then category_token(line, num)
when 2 then key_or_list_token(line, num)
when 4 then list_item_token(line, num)
else error_token(INDENT_LEVEL_ERROR, num)
end
end
|
.tokenize(source) ⇒ Object
16
17
18
19
20
|
# File 'lib/nodaire/indental/lexer.rb', line 16
def self.tokenize(source)
lines_with_number(strip_js_wrapper(source))
.reject { |line, _| line.match(/^\s*(;.*)?$/) }
.map { |line, num| token_for_line(line, num) }
end
|