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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
|
# File 'lib/vinter/lexer.rb', line 36
def tokenize
until @position >= @input.length
chunk = @input[@position..-1]
if match = chunk.match(/\A(==|!=|=>|->|\.\.)/)
@tokens << {
type: :operator,
value: match[0],
line: @line_num,
column: @column
}
@column += match[0].length
@position += match[0].length
next
end
if chunk.start_with?('...')
@tokens << {
type: :ellipsis,
value: '...',
line: @line_num,
column: @column
}
@column += 3
@position += 3
next
end
if match = chunk.match(/\A(\s+)/)
whitespace = match[0]
whitespace.each_char do |c|
if c == "\n"
@line_num += 1
@column = 1
else
@column += 1
end
end
@position += whitespace.length
next
end
match_found = false
TOKEN_TYPES.each do |type, pattern|
if match = chunk.match(/\A(#{pattern})/)
value = match[0]
token = {
type: type,
value: value,
line: @line_num,
column: @column
}
@tokens << token unless type == :whitespace
if value.include?("\n")
lines = value.split("\n")
@line_num += lines.size - 1
if lines.size > 1
@column = lines.last.length + 1
else
@column += value.length
end
else
@column += value.length
end
@position += value.length
match_found = true
break
end
end
unless match_found
@tokens << {
type: :unknown,
value: chunk[0],
line: @line_num,
column: @column
}
if chunk[0] == "\n"
@line_num += 1
@column = 1
else
@column += 1
end
@position += 1
end
end
@tokens
end
|