Class: Baba::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/baba/parser.rb

Defined Under Namespace

Classes: ParserError

Constant Summary collapse

BREAKING_TOKENS =
[THING, DOES, VAR, FOR, IF, WHILE, RETURN, BREAK]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tokens) ⇒ Parser

Returns a new instance of Parser.



14
15
16
17
# File 'lib/baba/parser.rb', line 14

def initialize(tokens)
  @tokens = tokens
  @current = 0
end

Instance Attribute Details

#currentObject (readonly)

Returns the value of attribute current.



12
13
14
# File 'lib/baba/parser.rb', line 12

def current
  @current
end

#tokensObject (readonly)

Returns the value of attribute tokens.



12
13
14
# File 'lib/baba/parser.rb', line 12

def tokens
  @tokens
end

Instance Method Details

#advanceObject



387
388
389
390
# File 'lib/baba/parser.rb', line 387

def advance
  @current += 1 if !eof?
  previous()
end

#assignmentObject



201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'lib/baba/parser.rb', line 201

def assignment
  expr = expr_or()

  if match(EQUAL)
    equals = previous()
    value = assignment()

    if expr.is_a?(Expr::Variable)
      name = expr.name
      return Expr::Assign.new(name, value)
    elsif expr.is_a?(Expr::Get)
      return Expr::Set.new(expr.object, expr.name, value)
    end

    error(equals, "Invalid assignment target.")
  end

  expr
end

#blockObject



189
190
191
192
193
194
195
196
197
198
199
# File 'lib/baba/parser.rb', line 189

def block
  statements = []
  until check(KEND) || check(ELSE) || eof?
    statements << declaration()
  end

  unless check(ELSE)
    consume(KEND, "Expected '}' or 'end' after block.")
  end
  statements
end

#callObject



317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
# File 'lib/baba/parser.rb', line 317

def call
  expr = primary()

  loop do
    if match(LEFT_PAREN)
      expr = finish_call(expr)
    elsif match(DOT)
      name = consume(IDENTIFIER, "Expected property name '.'.")
      expr = Expr::Get.new(expr, name)
    else
      break
    end
  end

  expr
end

#check(type) ⇒ Object



382
383
384
385
# File 'lib/baba/parser.rb', line 382

def check(type)
  return false if eof?
  peek().type == type
end

#class_declarationObject



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/baba/parser.rb', line 46

def class_declaration
  name = consume(IDENTIFIER, "Expected class name.")

  superclass = if match(LESS)
      consume(IDENTIFIER, "Expected superclass name.")
      Expr::Variable.new(previous())
    end

  consume(COLON, "Expected ':' or '{' before class body.")

  methods = []
  until check(KEND) || eof?
    consume(DOES, "Expected 'does' before method declaration.")
    methods << function("method")
  end

  consume(KEND, "Expected 'end' or '}' after class body.")

  Stmt::Class.new(name, superclass, methods)
end

#comparisonObject



257
258
259
260
261
262
263
264
265
266
267
# File 'lib/baba/parser.rb', line 257

def comparison()
  expr = term()

  while match(GREATER, GREATER_EQUAL, LESS, LESS_EQUAL)
    operator = previous()
    right = term()
    expr = Expr::Binary.new(expr, operator, right)
  end

  return expr
end

#consume(type, message) ⇒ Object



376
377
378
379
380
# File 'lib/baba/parser.rb', line 376

def consume(type, message)
  return advance() if check(type)

  raise(error(peek(), message))
end

#declarationObject



33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/baba/parser.rb', line 33

def declaration
  begin
    return class_declaration() if match(THING)
    return function("function") if match(DOES)
    return var_declaration() if match(VAR)

    return statement()
  rescue ParserError => error
    synchronize()
    return nil
  end
end

#eof?Boolean

Returns:

  • (Boolean)


392
393
394
# File 'lib/baba/parser.rb', line 392

def eof?
  peek().type == EOF
end

#equalityObject



245
246
247
248
249
250
251
252
253
254
255
# File 'lib/baba/parser.rb', line 245

def equality()
  expr = comparison()

  while match(NOT_EQUAL, EQUAL_EQUAL)
    operator = previous()
    right = comparison()
    expr = Expr::Binary.new(expr, operator, right)
  end

  return expr
end

#error(token, message) ⇒ Object



404
405
406
407
# File 'lib/baba/parser.rb', line 404

def error(token, message)
  Baba.parser_error(token, message)
  ParserError.new
end

#expr_andObject



233
234
235
236
237
238
239
240
241
242
243
# File 'lib/baba/parser.rb', line 233

def expr_and
  expr = equality()

  while match(AND)
    operator = previous()
    right = equality()
    expr = Expr::Logical.new(expr, operator, right)
  end

  expr
end

#expr_orObject



221
222
223
224
225
226
227
228
229
230
231
# File 'lib/baba/parser.rb', line 221

def expr_or
  expr = expr_and()

  while match(OR)
    operator = previous()
    right = expr_and()
    expr = Expr::Logical.new(expr, operator, right)
  end

  expr
end

#expressionObject



29
30
31
# File 'lib/baba/parser.rb', line 29

def expression
  assignment()
end

#expression_statementObject



166
167
168
169
170
# File 'lib/baba/parser.rb', line 166

def expression_statement
  expr = expression()
  # consume(SEMICOLON, "Expected ';' after expression")
  Stmt::Expression.new(expr)
end

#factorObject



281
282
283
284
285
286
287
288
289
290
291
# File 'lib/baba/parser.rb', line 281

def factor
  expr = unary()

  while match(SLASH, STAR)
    operator = previous()
    right = unary()
    expr = Expr::Binary.new(expr, operator, right)
  end

  return expr
end

#finish_call(callee) ⇒ Object



303
304
305
306
307
308
309
310
311
312
313
314
315
# File 'lib/baba/parser.rb', line 303

def finish_call(callee)
  arguments = []
  unless check(RIGHT_PAREN)
    loop do
      arguments << expression()
      break unless match(COMMA)
    end
  end

  paren = consume(RIGHT_PAREN, "Expected ')' after arguments.")

  Expr::Call.new(callee, paren, arguments)
end

#for_statementObject



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
# File 'lib/baba/parser.rb', line 89

def for_statement
  initializer = if check(COMMA)
      nil
    elsif match(VAR)
      var_declaration()
    else
      expression_statement()
    end
  consume(COMMA, "Expected ',' after loop initializer")

  condition = unless check(COMMA)
      expression()
    else
      nil
    end
  consume(COMMA, "Expected ',' after loop condition")

  increment = unless check(COLON)
      expression()
    else
      nil
    end

  body = statement()

  unless increment.nil?
    body = Stmt::Block.new([body, Stmt::Expression.new(increment)])
  end

  if condition.nil?
    condition = Expr::Literal.new(true)
  end

  body = Stmt::While.new(condition, body)

  unless initializer.nil?
    body = Stmt::Block.new([initializer, body])
  end

  body
end

#function(kind) ⇒ Object



172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/baba/parser.rb', line 172

def function(kind)
  name = consume(IDENTIFIER, "Expecteded #{kind} name.")
  consume(LEFT_PAREN, "Expected '(' after #{kind} + name.")
  parameters = []
  unless check(RIGHT_PAREN)
    loop do
      parameters << consume(IDENTIFIER, "Expected parameter name.")
      break unless match(COMMA)
    end
  end
  consume(RIGHT_PAREN, "Expected ')' after parameters.")

  consume(COLON, "Expected ':' or '{' before #{kind} body.")
  body = block()
  Stmt::Function.new(name, parameters, body)
end

#if_statementObject



131
132
133
134
135
136
137
138
139
# File 'lib/baba/parser.rb', line 131

def if_statement
  condition = expression()

  then_branch = statement()
  else_branch = nil
  else_branch = statement() if match(ELSE)

  return Stmt::If.new(condition, then_branch, else_branch)
end

#match(*types) ⇒ Object



365
366
367
368
369
370
371
372
373
374
# File 'lib/baba/parser.rb', line 365

def match(*types)
  types.each do |type|
    if check(type)
      advance()
      return true
    end
  end

  false
end

#parseObject



19
20
21
22
23
24
25
26
27
# File 'lib/baba/parser.rb', line 19

def parse
  statements = []

  while !eof?
    statements << declaration()
  end

  statements
end

#peekObject



396
397
398
# File 'lib/baba/parser.rb', line 396

def peek
  @tokens[@current]
end

#previousObject



400
401
402
# File 'lib/baba/parser.rb', line 400

def previous
  @tokens[@current - 1]
end

#primaryObject



334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
# File 'lib/baba/parser.rb', line 334

def primary
  return Expr::Literal.new(false) if match(FALSE)
  return Expr::Literal.new(true) if match(TRUE)
  return Expr::Literal.new(nil) if match(BLANK)
  return Expr::Break.new(previous()) if match(BREAK)
  return Expr::Self.new(previous()) if match(SELF)

  if match(NUMBER, STRING)
    return Expr::Literal.new(previous.literal())
  end

  if match(SUPER)
    keyword = previous()
    consume(DOT, "Expected '.' after super.")
    method = consume(IDENTIFIER, "Expected super thing method name.")
    return Expr::Super.new(keyword, method)
  end

  if match(IDENTIFIER)
    return Expr::Variable.new(previous())
  end

  if match(LEFT_PAREN)
    expr = expression()
    consume(RIGHT_PAREN, "Expected ')' after expression.")
    return Expr::Grouping.new(expr)
  end

  raise error(peek(), "Expect expression.")
end

#rb_eval_statementObject



141
142
143
144
145
# File 'lib/baba/parser.rb', line 141

def rb_eval_statement
  value = expression()
  # consume(SEMICOLON, "Expected ';' after value.")
  Stmt::RBEval.new(value)
end

#return_statementObject



147
148
149
150
151
152
153
154
155
156
157
# File 'lib/baba/parser.rb', line 147

def return_statement
  keyword = previous()

  value = if match(SEMICOLON)
      nil
    else
      expression()
    end

  Stmt::Return.new(keyword, value)
end

#statementObject



77
78
79
80
81
82
83
84
85
86
87
# File 'lib/baba/parser.rb', line 77

def statement
  return for_statement() if match(FOR)
  return if_statement() if match(IF)
  return rb_eval_statement() if match(RBEVAL)
  return return_statement() if match(RETURN)
  return while_statement() if match(WHILE)
  return Stmt::Block.new(block()) if match(COLON)
  return Stmt::Include.new(expression()) if match(INCLUDE)

  return expression_statement()
end

#synchronizeObject



411
412
413
414
415
416
417
418
419
# File 'lib/baba/parser.rb', line 411

def synchronize
  advance()
  until eof?
    break if previous().type == SEMICOLON
    break if BREAKING_TOKENS.include?(peek().type)

    advance()
  end
end

#termObject



269
270
271
272
273
274
275
276
277
278
279
# File 'lib/baba/parser.rb', line 269

def term
  expr = factor()

  while match(MINUS, PLUS)
    operator = previous()
    right = factor()
    expr = Expr::Binary.new(expr, operator, right)
  end

  return expr
end

#unaryObject



293
294
295
296
297
298
299
300
301
# File 'lib/baba/parser.rb', line 293

def unary
  if match(NOT, MINUS)
    operator = previous()
    right = unary()
    return Expr::Unary.new(operator, right)
  end

  return call()
end

#var_declarationObject



67
68
69
70
71
72
73
74
75
# File 'lib/baba/parser.rb', line 67

def var_declaration
  name = consume(IDENTIFIER, "Expected variable name")

  initializer = nil
  initializer = expression() if match(EQUAL)

  # consume(SEMICOLON, "Expected ';' after variable declaration.")
  Stmt::Var.new(name, initializer)
end

#while_statementObject



159
160
161
162
163
164
# File 'lib/baba/parser.rb', line 159

def while_statement
  condition = expression()
  body = statement()

  return Stmt::While.new(condition, body)
end