Class: Zon::Parser
- Inherits:
-
Object
- Object
- Zon::Parser
- Defined in:
- lib/zon/parser.rb
Constant Summary collapse
- STRUCT =
0- ARRAY =
1
Instance Method Summary collapse
-
#initialize(tokens) ⇒ Parser
constructor
A new instance of Parser.
- #next ⇒ Object
- #next_equals ⇒ Object
- #next_key ⇒ Object
- #next_token?(type) ⇒ Boolean
- #next_value ⇒ Object
- #parse ⇒ Object
- #peek_token ⇒ Object
Constructor Details
#initialize(tokens) ⇒ Parser
Returns a new instance of Parser.
11 12 13 14 |
# File 'lib/zon/parser.rb', line 11 def initialize(tokens) @tokens = tokens @curr = nil end |
Instance Method Details
#next ⇒ Object
16 17 18 19 20 21 22 |
# File 'lib/zon/parser.rb', line 16 def next begin @curr = @tokens.next rescue @curr = nil end end |
#next_equals ⇒ Object
135 136 137 138 139 140 141 |
# File 'lib/zon/parser.rb', line 135 def next_equals self.next if not @curr or not @curr.type == Lexer::TokenType::EQUALS raise "Expected '=' token but got '#{@curr.value}'" end end |
#next_key ⇒ Object
91 92 93 94 95 96 97 98 99 |
# File 'lib/zon/parser.rb', line 91 def next_key self.next if @curr and @curr.type == Lexer::TokenType::LITERAL @curr.value[1..].to_sym # create sym from string without the '.' elsif raise "Expected literal token but got '#{@curr.value}'" end end |
#next_token?(type) ⇒ Boolean
79 80 81 |
# File 'lib/zon/parser.rb', line 79 def next_token?(type) self.peek_token.type == type end |
#next_value ⇒ Object
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 |
# File 'lib/zon/parser.rb', line 101 def next_value self.next raise "No token for value" if not @curr value = nil if @curr.type == Lexer::TokenType::LITERAL value = @curr.value[1..].to_sym elsif @curr.type == Lexer::TokenType::STRING value = @curr.value[1..-2] elsif @curr.type == Lexer::TokenType::NUMBER begin if @curr.value.include? "." value = Float(@curr.value) else value = Integer(@curr.value) end rescue raise "Unable to parse number from value '#{@curr.value}'" end elsif @curr.type == Lexer::TokenType::BOOLEAN if @curr.value == "true" value = true else value = false end end raise "Unable to parse value from '#{@curr.value}'" if not value value end |
#parse ⇒ Object
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 |
# File 'lib/zon/parser.rb', line 24 def parse if self.next_token? Lexer::TokenType::LBRACE self.next if self.next_token? Lexer::TokenType::RBRACE # '.{ }' self.next return {} # this could either be a array or a struct... we don't know end v = self.parse if v.is_a? Symbol and self.next_token? Lexer::TokenType::EQUALS # This is a Zon struct, i.e. translate it to a Ruby hash k = v self.next_equals # skip equals v = self.parse h = {k => v} loop do # fetch remaining key value pairs self.next if self.next_token? Lexer::TokenType::COMMA if self.next_token? Lexer::TokenType::RBRACE self.next break end k = self.next_key self.next_equals v = self.parse h[k] = v end return h else # Must be an array a = [v] loop do # fetch remaining values self.next if self.next_token? Lexer::TokenType::COMMA if self.next_token? Lexer::TokenType::RBRACE self.next break end a.append self.parse end return a end else return self.next_value end end |
#peek_token ⇒ Object
83 84 85 86 87 88 89 |
# File 'lib/zon/parser.rb', line 83 def peek_token begin @tokens.peek rescue nil end end |