-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathtokenizeradapter.rb
50 lines (38 loc) · 1002 Bytes
/
tokenizeradapter.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
# The purpose of this class is to serve as an adapter between the
# ShuntingYard component, the Tokenizer, and the Parser.
# The reason for this is that a number of tokens indicate
# a need to call back into the parser, but the ShuntingYard
# class only need to know that what is returned is not an operator
# it should be concerned about
class TokenizerAdapter
def initialize(tokenizer, parser)
@tokenizer = tokenizer
@parser = parser
@escape_tokens = {
:lambda => :parse_defexp
}
end
def each
@tokenizer.each do |token, op, keyword|
if keyword and (m = @escape_tokens[token])
@tokenizer.unget(token)
ss = @parser.send(m)
yield(ss, nil, nil)
else
yield(token,op,keyword)
end
end
end
def get_quoted_exp(unget=:unget)
@tokenizer.get_quoted_exp(unget)
end
def ws
@tokenizer.ws
end
def unget token
@tokenizer.unget(token)
end
def lasttoken
@tokenizer.lasttoken
end
end