-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathatom.rb
More file actions
51 lines (46 loc) · 1.56 KB
/
atom.rb
File metadata and controls
51 lines (46 loc) · 1.56 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
require 'sym'
module Tokens
class Atom
def self.expect(s)
tmp = ""
c = s.peek
return Sym.expect(s) if c == ?: # This is a hack. Shoud be handled separately
if c == ?@ || c == ?$
tmp << s.get
tmp << s.get if c == ?@ && s.peek == ?@
end
if (c = s.peek) && (c == ?_ || (?a .. ?z).member?(c) || (?A .. ?Z).member?(c))
tmp << s.get
while (c = s.peek) && (ALNUM.member?(c) || ?_ == c)
tmp << s.get
end
elsif tmp == "$"
# Handle special global variables
# Single-character specials: $!, $@, $&, $`, $', $+, $,, $., $/, $:, $;, $<, $=, $>, $?, $\, and $~
# Also $- followed by a single alphanumeric character (e.g., $-0, $-a, $-w)
c = s.peek
if c == ?-
tmp << s.get # consume '-'
# Check if there's an alphanumeric after the dash
c = s.peek
if c && (ALNUM.member?(c) || c == ?_)
tmp << s.get # consume the character after the dash
end
elsif c && (c == ?! || c == ?@ || c == ?& || c == ?` || c == ?' || c == ?+ ||
c == ?, || c == ?. || c == ?/ || c == ?: || c == ?; || c == ?< ||
c == ?= || c == ?> || c == ?? || c == ?\\ || c == ?~)
tmp << s.get
else
# For other cases, consume one character
tmp << s.get
end
return tmp.to_sym
end
if tmp.size > 0 && (s.peek == ?! || s.peek == ??)
tmp << s.get
end
return nil if tmp == ""
return tmp.to_sym
end
end
end