Class: RuboCop::AST::NodePattern::Parser

Inherits:
Racc::Parser
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/rubocop/ast/node_pattern/parser.rb,
lib/rubocop/ast/node_pattern/with_meta.rb,
lib/rubocop/ast/node_pattern/parser.racc.rb

Overview

Parser for NodePattern Note: class reopened in `parser.racc`

Doc on how this fits in the compiling process:

/docs/modules/ROOT/pages/node_pattern.adoc

Direct Known Subclasses

WithMeta

Defined Under Namespace

Classes: WithMeta

Constant Summary collapse

Builder =
NodePattern::Builder
Lexer =
NodePattern::Lexer
Racc_arg =
[
racc_action_table,
racc_action_check,
racc_action_default,
racc_action_pointer,
racc_goto_table,
racc_goto_check,
racc_goto_default,
racc_goto_pointer,
racc_nt_base,
racc_reduce_table,
racc_token_table,
racc_shift_n,
racc_reduce_n,
racc_use_result_var ]
Racc_token_to_s_table =
[
"$end",
"error",
"tSYMBOL",
"tNUMBER",
"tSTRING",
"tWILDCARD",
"tPARAM_NAMED",
"tPARAM_CONST",
"tPARAM_NUMBER",
"tFUNCTION_CALL",
"tPREDICATE",
"tNODE_TYPE",
"tARG_LIST",
"tUNIFY",
"tREGEXP",
"\"(\"",
"\")\"",
"\"[\"",
"\"]\"",
"\"!\"",
"\"^\"",
"\"`\"",
"\"$\"",
"\"{\"",
"\"}\"",
"\"<\"",
"\">\"",
"\"?\"",
"\"*\"",
"\"+\"",
"\"...\"",
"\",\"",
"\"|\"",
"$start",
"node_pattern",
"node_pattern_no_union",
"union",
"variadic_pattern_list",
"node_pattern_list",
"args",
"atom",
"separated_variadic_patterns",
"variadic_pattern",
"repetition",
"opt_capture",
"opt_rest",
"rest",
"arg_list" ]
Racc_debug_parser =
false

Instance Method Summary collapse

Constructor Details

#initialize(builder = self.class::Builder.new) ⇒ Parser

Returns a new instance of Parser.


19
20
21
22
# File 'lib/rubocop/ast/node_pattern/parser.rb', line 19

def initialize(builder = self.class::Builder.new)
  super()
  @builder = builder
end

Instance Method Details

#_reduce_10(val, _values) ⇒ Object


333
334
335
# File 'lib/rubocop/ast/node_pattern/parser.racc.rb', line 333

def _reduce_10(val, _values)
 emit_call :predicate, *val
end

#_reduce_11(val, _values) ⇒ Object


337
338
339
# File 'lib/rubocop/ast/node_pattern/parser.racc.rb', line 337

def _reduce_11(val, _values)
 emit_call :node_type, *val
end

#_reduce_13(val, _values) ⇒ Object

reduce 12 omitted


343
344
345
# File 'lib/rubocop/ast/node_pattern/parser.racc.rb', line 343

def _reduce_13(val, _values)
 emit_atom :symbol, *val
end

#_reduce_14(val, _values) ⇒ Object


347
348
349
# File 'lib/rubocop/ast/node_pattern/parser.racc.rb', line 347

def _reduce_14(val, _values)
 emit_atom :number, *val
end

#_reduce_15(val, _values) ⇒ Object


351
352
353
# File 'lib/rubocop/ast/node_pattern/parser.racc.rb', line 351

def _reduce_15(val, _values)
 emit_atom :string, *val
end

#_reduce_16(val, _values) ⇒ Object


355
356
357
# File 'lib/rubocop/ast/node_pattern/parser.racc.rb', line 355

def _reduce_16(val, _values)
 emit_atom :const, *val
end

#_reduce_17(val, _values) ⇒ Object


359
360
361
# File 'lib/rubocop/ast/node_pattern/parser.racc.rb', line 359

def _reduce_17(val, _values)
 emit_atom :named_parameter, *val
end

#_reduce_18(val, _values) ⇒ Object


363
364
365
# File 'lib/rubocop/ast/node_pattern/parser.racc.rb', line 363

def _reduce_18(val, _values)
 emit_atom :positional_parameter, *val
end

#_reduce_19(val, _values) ⇒ Object


367
368
369
# File 'lib/rubocop/ast/node_pattern/parser.racc.rb', line 367

def _reduce_19(val, _values)
 emit_atom :regexp, *val
end

#_reduce_2(val, _values) ⇒ Object

reduce 1 omitted


301
302
303
# File 'lib/rubocop/ast/node_pattern/parser.racc.rb', line 301

def _reduce_2(val, _values)
 enforce_unary(val[0])
end

#_reduce_20(val, _values) ⇒ Object


371
372
373
# File 'lib/rubocop/ast/node_pattern/parser.racc.rb', line 371

def _reduce_20(val, _values)
 emit_atom :wildcard, *val
end

#_reduce_21(val, _values) ⇒ Object


375
376
377
# File 'lib/rubocop/ast/node_pattern/parser.racc.rb', line 375

def _reduce_21(val, _values)
 emit_atom :unify, *val
end

#_reduce_22(val, _values) ⇒ Object


379
380
381
# File 'lib/rubocop/ast/node_pattern/parser.racc.rb', line 379

def _reduce_22(val, _values)
 emit_union(*val)
end

#_reduce_25(val, _values) ⇒ Object

reduce 24 omitted


387
388
389
390
391
# File 'lib/rubocop/ast/node_pattern/parser.racc.rb', line 387

def _reduce_25(val, _values)
        main, repeat_t = val
        emit_unary_op(:repetition, repeat_t, main, repeat_t)

end

#_reduce_26(val, _values) ⇒ Object


393
394
395
396
397
398
399
# File 'lib/rubocop/ast/node_pattern/parser.racc.rb', line 393

def _reduce_26(val, _values)
        opt_capture, bracket, node_pattern_list, opt_rest, close_bracket = val
        node_pattern_list << opt_rest if opt_rest
        main = emit_list :any_order, bracket, node_pattern_list, close_bracket
        emit_capture(opt_capture, main)

end

#_reduce_3(val, _values) ⇒ Object


305
306
307
# File 'lib/rubocop/ast/node_pattern/parser.racc.rb', line 305

def _reduce_3(val, _values)
 emit_list :sequence, *val
end

#_reduce_33(val, _values) ⇒ Object

reduce 32 omitted


413
414
415
# File 'lib/rubocop/ast/node_pattern/parser.racc.rb', line 413

def _reduce_33(val, _values)
 emit_capture(val[0], emit_atom(:rest, val[1]))
end

#_reduce_37(val, _values) ⇒ Object

reduce 36 omitted


423
424
425
# File 'lib/rubocop/ast/node_pattern/parser.racc.rb', line 423

def _reduce_37(val, _values)
 val
end

#_reduce_38(val, _values) ⇒ Object


427
428
429
# File 'lib/rubocop/ast/node_pattern/parser.racc.rb', line 427

def _reduce_38(val, _values)
 val
end

#_reduce_39(val, _values) ⇒ Object


431
432
433
# File 'lib/rubocop/ast/node_pattern/parser.racc.rb', line 431

def _reduce_39(val, _values)
 val[0] << val[2]
end

#_reduce_4(val, _values) ⇒ Object


309
310
311
# File 'lib/rubocop/ast/node_pattern/parser.racc.rb', line 309

def _reduce_4(val, _values)
 emit_list :intersection, *val
end

#_reduce_40(val, _values) ⇒ Object


435
436
437
# File 'lib/rubocop/ast/node_pattern/parser.racc.rb', line 435

def _reduce_40(val, _values)
 val
end

#_reduce_41(val, _values) ⇒ Object


439
440
441
# File 'lib/rubocop/ast/node_pattern/parser.racc.rb', line 439

def _reduce_41(val, _values)
 val[0] << val[1]
end

#_reduce_42(val, _values) ⇒ Object


443
444
445
# File 'lib/rubocop/ast/node_pattern/parser.racc.rb', line 443

def _reduce_42(val, _values)
 val
end

#_reduce_43(val, _values) ⇒ Object


447
448
449
# File 'lib/rubocop/ast/node_pattern/parser.racc.rb', line 447

def _reduce_43(val, _values)
 val[0] << val[1]
end

#_reduce_44(val, _values) ⇒ Object


451
452
453
# File 'lib/rubocop/ast/node_pattern/parser.racc.rb', line 451

def _reduce_44(val, _values)
 [[]]
end

#_reduce_45(val, _values) ⇒ Object


455
456
457
# File 'lib/rubocop/ast/node_pattern/parser.racc.rb', line 455

def _reduce_45(val, _values)
 val[0].last << val[1]; val[0]
end

#_reduce_46(val, _values) ⇒ Object


459
460
461
# File 'lib/rubocop/ast/node_pattern/parser.racc.rb', line 459

def _reduce_46(val, _values)
 val[0] << []
end

#_reduce_5(val, _values) ⇒ Object


313
314
315
# File 'lib/rubocop/ast/node_pattern/parser.racc.rb', line 313

def _reduce_5(val, _values)
 emit_unary_op :negation, *val
end

#_reduce_6(val, _values) ⇒ Object


317
318
319
# File 'lib/rubocop/ast/node_pattern/parser.racc.rb', line 317

def _reduce_6(val, _values)
 emit_unary_op :ascend, *val
end

#_reduce_7(val, _values) ⇒ Object


321
322
323
# File 'lib/rubocop/ast/node_pattern/parser.racc.rb', line 321

def _reduce_7(val, _values)
 emit_unary_op :descend, *val
end

#_reduce_8(val, _values) ⇒ Object


325
326
327
# File 'lib/rubocop/ast/node_pattern/parser.racc.rb', line 325

def _reduce_8(val, _values)
 emit_capture(*val)
end

#_reduce_9(val, _values) ⇒ Object


329
330
331
# File 'lib/rubocop/ast/node_pattern/parser.racc.rb', line 329

def _reduce_9(val, _values)
 emit_call :function_call, *val
end

#_reduce_none(val, _values) ⇒ Object


463
464
465
# File 'lib/rubocop/ast/node_pattern/parser.racc.rb', line 463

def _reduce_none(val, _values)
  val[0]
end

#inspectObject


40
41
42
# File 'lib/rubocop/ast/node_pattern/parser.rb', line 40

def inspect
  "<##{self.class}>"
end

#parse(source) ⇒ NodePattern::Node

(Similar API to `parser` gem) Parses a source and returns the AST.

Parameters:

  • source_buffer (Parser::Source::Buffer, String)

    The source buffer to parse.

Returns:


31
32
33
34
35
36
37
38
# File 'lib/rubocop/ast/node_pattern/parser.rb', line 31

def parse(source)
  @lexer = self.class::Lexer.new(source)
  do_parse
rescue Lexer::Error => e
  raise NodePattern::Invalid, e.message
ensure
  @lexer = nil # Don't keep references
end