-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathscanner.rb
More file actions
224 lines (192 loc) · 5.5 KB
/
Copy pathscanner.rb
File metadata and controls
224 lines (192 loc) · 5.5 KB
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# The purpose of the Scanner is to present a narrow interface to read characters from, with support for lookahead / unget.
# Why not StringScanner? Well, it's a Ruby C-extension, and I want to get the compiler self-hosted as soon as possible,
# so I'm sticking to something simple. The code below is sufficient to write recursive descent parsers in a pretty
# concise style in Ruby
class Scanner
attr_reader :col, :lineno, :filename # @filename holds the name of the file the parser reads from
attr_reader :last_ws_consumed_newline # Tracks if the last ws() call consumed a newline
attr_reader :had_ws_before_token # Tracks if whitespace was consumed before the current token
# Position = Struct.new(:filename, :lineno, :col)
class Position
def initialize(filename,lineno,col)
@filename = filename
@lineno = lineno
@col = col
end
attr_accessor :filename, :lineno, :col
def inspect
# FIXME: If these are inlined, they are treated incorrectly.
l = self.lineno
c = self.col
"line #{l}, col #{c} in #{self.filename}"
end
def short
"#{File.basename(self.filename)}, @#{self.lineno},#{self.col}"
end
end
class ScannerString < String
attr_accessor :position
end
# Return the current position of the parser in one convenient object...
def position
Position.new(@filename,@lineno,@col)
end
# Set the parser position from a Position object (for backtracking)
def position=(pos)
@filename = pos.filename
@lineno = pos.lineno
@col = pos.col
end
def initialize(io)
@io = io
@buf = ""
@lineno = 1
@col = 1
@last_ws_consumed_newline = false
@had_ws_before_token = false
# set filename if io is an actual file (instead of STDIN)
# otherwhise, indicate it comes from a stream
@filename = io.is_a?(File) && File.file?(io) ? File.expand_path(io.path) : "<stream>"
end
def fill
if @buf.empty?
if c = @io.getc
@buf = c.chr
end
end
end
def peek
fill
return @buf[-1]
end
LF="\n"
def get
fill
pos = position
ch = @buf.slice!(-1,1)
@col += 1
if ch == LF
@lineno += 1
@col = 1
end
return nil if !ch
s = ScannerString.new(ch)
s.position = pos
return s
end
def unget(c)
@buf << c.reverse
if c.respond_to?(:position)
pos = c.position
if pos
@lineno = pos.lineno
@filename = pos.filename
@col = pos.col
return
end
end
if c.is_a?(String)
@col -= c.length
#ten = 10 # FIXME: This is a temporary hack to get the compiler linking
#@lineno -= c.count(ten.chr)
@lineno -= c.count(LF)
else
@col -= 1
end
end
# If &block is passed, it is a callback to parse an expression
# that will be passed on to the method.
EMPTY=""
def expect(str,&block)
return buf if str == EMPTY
return str.expect(self,&block) if str.respond_to?(:expect)
expect_str(str,&block)
end
def expect_str(str, &block)
str = str.to_s
return false if peek != str[0]
buf = ScannerString.new
buf.position = self.position
str.each_byte do |s|
c = peek
if !c || c.ord != s
unget(buf) if !buf.empty?
return false
end
buf << get
end
return buf
end
# Avoid initialization on every call. Hacky workaround.
# Note: semicolon removed from WS - it's an operator
WS = [9,10,13,32,?#.ord]
C = ?#
# ws ::= ([\t\b\r ] | '#' [~\n]* '\n' | '\\' '\n')*
def ws
@last_ws_consumed_newline = false
@had_ws_before_token = false
while (c = peek) && (WS.member?(c.ord) || c == "\\")
if c == "\\"
# Check if it's line continuation: backslash followed by newline
get
if peek == LF
get # consume the newline
@last_ws_consumed_newline = true
@had_ws_before_token = true
else
# Not line continuation, put the backslash back
unget("\\")
break
end
else
get
@had_ws_before_token = true # Track that whitespace was consumed
@last_ws_consumed_newline = true if c.ord == 10 # Track newline consumption
if c == C
while (c = get) && c != LF do; end
@last_ws_consumed_newline = true # Comments end with newline
end
end
end
end
# nolfws ::= [\t\r ]*
NOLFWS = [9,13,32]
def nolfws
@had_ws_before_token = false
had_any = false
while (c = peek) && NOLFWS.member?(c.ord)
get
had_any = true
end
# Check if we stopped at a newline that's followed by . (method chaining)
# If so, skip the newline and continue as if it were whitespace
if peek == LF && peek_past_newline_is_dot?
get # consume the newline
had_any = true
nolfws # recursively skip more whitespace
end
@had_ws_before_token = had_any
end
# Helper: Check if there's a . at the start of the next line (after newline + spaces)
def peek_past_newline_is_dot?
return false unless peek == LF
# Save state
saved_pos = @position
saved_lineno = @lineno
saved_col = @col
# Collect characters we consume
consumed = []
consumed << get # consume newline
# Skip horizontal whitespace only
while (c = peek) && [9, 32].member?(c.ord)
consumed << get
end
# Check for .
result = (peek == ?.)
# Restore position by ungetting in reverse
consumed.reverse.each { |ch| unget(ch) }
@lineno = saved_lineno
@col = saved_col
result
end
end