-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdot_lexer.rb
110 lines (100 loc) · 2.94 KB
/
dot_lexer.rb
1
2
3
4
5
6
7
8
9
10
11
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
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
# DotLexer class
# DotLexer determines the constant type of the token
class DotLexer
@is_illegal_token = false
# reads input and determines the text of the token and the type of the token
def next_token
# if there was an illegal token
if @is_illegal_token
@is_illegal_token = false
if @token == ";" # if there was a end-of-line semicolon
return Token.new(@token, Token::SEMI)
elsif @token == "="
return Token.new(@token, Token::EQUALS)
end
while @token.match(/\W/) do
puts "illegal char: #{@token}"
@token = gets(1)
end
else # getting the next token
@token = gets(1)
end
@temp = ""
# if its the end of the file
if @token == nil
return Token.new("", Token::EOF)
end
# eating white-space until there is a character
if @token.match(/\n/) || @token.match(/\s/)
while @token.match(/\n/) || @token.match(/\s/) do
@token = gets(1)
end
end
# if the token is a single character
if @token == "{"
return Token.new(@token, Token::LCURLY)
elsif @token == "}"
return Token.new(@token, Token::RCURLY)
elsif @token == ";"
return Token.new(@token, Token::SEMI)
elsif @token == "["
return Token.new(@token, Token::LBRACK)
elsif @token == "]"
return Token.new(@token, Token::RBRACK)
elsif @token == "="
return Token.new(@token, Token::EQUALS)
elsif @token == ","
return Token.new(@token, Token::COMMA)
end
# the token is a string
if @token == "\""
until @token == " " do
@temp = @temp + @token
@token = gets(1)
if @token == "\""
@temp = @temp + @token
break
end
end
return Token.new(@temp, Token::STRING)
end
# if token is a digit
if @token.match(/\d/)
until @token == " " do
@temp = @temp + @token
@token = gets(1)
end
return Token.new(@temp, Token::INT)
end
# if token is a word
if @token.match(/\w/)
until @token.match(/\n/) || @token.match(/\s/) do
if @token.match(/\W/) # there is an illegal token
@is_illegal_token = true
break
end
@temp = @temp + @token
@token = gets(1)
end
if @temp.downcase == "digraph"
return Token.new(@temp, Token::DIGRAPH)
elsif @temp.downcase == "subgraph"
return Token.new(@temp, Token::SUBGRAPH)
else
return Token.new(@temp, Token::ID)
end
end
# if token is a string of non alpha/digit characters
if @token.match(/\W/)
until @token.match(/\n/) || @token.match(/\s/) do
@temp = @temp + @token
@token = gets(1)
end
@token = @temp
end
# if token is a multi-character word
if @token == "->"
Token.new(@token, Token::ARROW)
end
end
end