-
Notifications
You must be signed in to change notification settings - Fork 1
/
common.rb
61 lines (51 loc) · 1.01 KB
/
common.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
require "pp"
require "json"
class Token
attr_reader :kind, :value
# kind:
# str: string
# kw: keyword
# int: integer
# sym: symbol
# ident: identifier
def initialize(kind, value, lineno)
@kind = kind
@value = value
@lineno = lineno
end
def to_line
JSON.generate([@kind, @value])
end
def self.from_line(line)
if line.start_with?("[")
kind_str, str = JSON.parse(line)
Token.new(kind_str.to_sym, str, -1) # TODO lineno
else
nil
end
end
def to_s
"(Token kind=#{@kind} value=(_#{@value}_) lineno=#{@lineno})"
end
def is(kind, str)
@kind == kind && @value == str
end
end
def p_e(*args)
args.each {|arg| $stderr.puts arg.inspect }
end
def pp_e(*args)
args.each {|arg| $stderr.puts arg.pretty_inspect }
end
def not_yet_impl(*args)
"Not yet implemented" +
args
.map {|arg| " (#{ arg.inspect })" }
.join("")
end
def panic(*args)
"PANIC" +
args
.map {|arg| " (#{ arg.inspect })" }
.join("")
end