Class: ANTLR3::Template::GroupFile::Parser

Inherits:
Parser show all
Includes:
TokenData
Defined in:
lib/antlr3/template/group-file-parser.rb

Constant Summary collapse

RULE_METHODS =
[ :group_spec, :group_name, :member, :parameter_declaration, 
:parameters, :parameter ].freeze
TOKENS_FOLLOWING_group_name_IN_group_spec_85 =
Set[ 1, 5 ]
TOKENS_FOLLOWING_member_IN_group_spec_108 =
Set[ 1, 5 ]
TOKENS_FOLLOWING_T__10_IN_group_name_128 =
Set[ 4 ]
TOKENS_FOLLOWING_CONSTANT_IN_group_name_144 =
Set[ 11 ]
TOKENS_FOLLOWING_T__11_IN_group_name_146 =
Set[ 4 ]
TOKENS_FOLLOWING_CONSTANT_IN_group_name_169 =
Set[ 1, 12 ]
TOKENS_FOLLOWING_T__12_IN_group_name_177 =
Set[ 1 ]
TOKENS_FOLLOWING_ID_IN_member_199 =
Set[ 5, 13, 14, 17, 18 ]
TOKENS_FOLLOWING_parameter_declaration_IN_member_203 =
Set[ 13 ]
TOKENS_FOLLOWING_T__13_IN_member_210 =
Set[ 5, 6, 7 ]
TOKENS_FOLLOWING_ID_IN_member_220 =
Set[ 1 ]
TOKENS_FOLLOWING_TEMPLATE_IN_member_230 =
Set[ 1 ]
TOKENS_FOLLOWING_STRING_IN_member_242 =
Set[ 1 ]
TOKENS_FOLLOWING_T__14_IN_parameter_declaration_276 =
Set[ 5, 14, 15, 17, 18 ]
TOKENS_FOLLOWING_parameters_IN_parameter_declaration_280 =
Set[ 15 ]
TOKENS_FOLLOWING_T__15_IN_parameter_declaration_287 =
Set[ 1 ]
TOKENS_FOLLOWING_parameters_IN_parameter_declaration_293 =
Set[ 1 ]
TOKENS_FOLLOWING_parameter_IN_parameters_317 =
Set[ 1, 16 ]
TOKENS_FOLLOWING_T__16_IN_parameters_322 =
Set[ 5, 14, 17, 18 ]
TOKENS_FOLLOWING_parameter_IN_parameters_324 =
Set[ 1, 16 ]
TOKENS_FOLLOWING_T__17_IN_parameter_342 =
Set[ 5 ]
TOKENS_FOLLOWING_ID_IN_parameter_346 =
Set[ 1 ]
TOKENS_FOLLOWING_T__18_IN_parameter_354 =
Set[ 5 ]
TOKENS_FOLLOWING_ID_IN_parameter_358 =
Set[ 1 ]
TOKENS_FOLLOWING_ID_IN_parameter_368 =
Set[ 1, 19 ]
TOKENS_FOLLOWING_T__19_IN_parameter_382 =
Set[ 7 ]
TOKENS_FOLLOWING_STRING_IN_parameter_386 =
Set[ 1 ]

Constants included from Constants

Constants::BUILT_IN_TOKEN_NAMES, Constants::DEFAULT, Constants::DOWN, Constants::EOF, Constants::EOF_TOKEN, Constants::EOR_TOKEN_TYPE, Constants::HIDDEN, Constants::INVALID, Constants::INVALID_TOKEN, Constants::MEMO_RULE_FAILED, Constants::MEMO_RULE_UNKNOWN, Constants::MIN_TOKEN_TYPE, Constants::SKIP_TOKEN, Constants::UP

Instance Attribute Summary

Attributes inherited from Recognizer

#input, #state

Attributes included from ANTLR3::TokenFactory

#token_class

Instance Method Summary collapse

Methods inherited from Parser

associated_lexer, main, #missing_symbol, #source_name, #token_stream=

Methods inherited from Recognizer

Scope, #already_parsed_rule?, #antlr_version, #antlr_version_string, #backtrack, #backtracking?, #backtracking_level, #backtracking_level=, #begin_resync, #combine_follows, #compute_context_sensitive_rule_follow, #compute_error_recovery_set, #consume_until, #current_symbol, debug?, define_return_scope, #display_recognition_error, #each_delegate, #emit_error_message, #end_resync, #error_header, #error_message, generic_return_scope, #grammar_file_name, imported_grammars, master, master_grammars, #match, #match_any, #memoize, #mismatch_is_missing_token?, #mismatch_is_unwanted_token?, #missing_symbol, #number_of_syntax_errors, profile?, #recover, #recover_from_mismatched_element, #recover_from_mismatched_set, #recover_from_mismatched_token, #report_error, #reset, #resync, return_scope_members, #rule_memoization, rules, #syntactic_predicate?, #syntax_errors?, token_class, #token_error_display, #trace_in, #trace_out

Methods included from ANTLR3::TokenFactory

#create_token

Methods included from Error

EarlyExit, FailedPredicate, MismatchedNotSet, MismatchedRange, MismatchedSet, MismatchedToken, MismatchedTreeNode, MissingToken, NoViableAlternative, RewriteCardinalityError, RewriteEarlyExit, RewriteEmptyStream, UnwantedToken

Constructor Details

#initialize(input, options = {}) ⇒ Parser

Returns a new instance of Parser.


111
112
113
114
115
# File 'lib/antlr3/template/group-file-parser.rb', line 111

def initialize( input, options = {} )
  super( input, options )


end

Instance Method Details

#extract_template(token) ⇒ Object


151
152
153
154
155
156
157
158
# File 'lib/antlr3/template/group-file-parser.rb', line 151

def extract_template( token )
  case token.type
  when TEMPLATE
    token.text.gsub( /\A<<<\r?\n?|\r?\n?>>>\Z/, '' )
  when STRING
    unescape( token.text[1...-1] )
  end
end

#fetch_group(namespace, name) ⇒ Object


117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/antlr3/template/group-file-parser.rb', line 117

def fetch_group( namespace, name )
  if namespace.const_defined?( name )
    group = namespace.const_get( name )
    unless group.is_a?( ANTLR3::Template::Group )
    
    end
  else
    group = ANTLR3::Template::Group.new
    namespace.const_set( name, group )
  end
  return( group )
end

#group(namespace = ::Object) ⇒ Object


160
161
162
# File 'lib/antlr3/template/group-file-parser.rb', line 160

def group( namespace = ::Object )
  group_spec( namespace )
end

#group_name(namespace) ⇒ Object

parser rule group_name

(in GroupFile.g) 86:1: group_name[ namespace ] returns [ group ] : 'group' (mod= CONSTANT '::' )* name= CONSTANT ( ';' )? ;


249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
# File 'lib/antlr3/template/group-file-parser.rb', line 249

def group_name( namespace )
  # -> uncomment the next line to manually enable rule tracing
  # trace_in( __method__, 2 )
  group = nil
  mod = nil
  name = nil

  begin
    # at line 87:5: 'group' (mod= CONSTANT '::' )* name= CONSTANT ( ';' )?
    match( T__10, TOKENS_FOLLOWING_T__10_IN_group_name_128 )
    # at line 88:5: (mod= CONSTANT '::' )*
    while true # decision 3
      alt_3 = 2
      look_3_0 = @input.peek( 1 )

      if ( look_3_0 == CONSTANT )
        look_3_1 = @input.peek( 2 )

        if ( look_3_1 == T__11 )
          alt_3 = 1

        end

      end
      case alt_3
      when 1
        # at line 89:7: mod= CONSTANT '::'
        mod = match( CONSTANT, TOKENS_FOLLOWING_CONSTANT_IN_group_name_144 )
        match( T__11, TOKENS_FOLLOWING_T__11_IN_group_name_146 )
        # --> action
         namespace = namespace.const_get( mod.text ) 
        # <-- action

      else
        break # out of loop for decision 3
      end
    end # loop for decision 3
    name = match( CONSTANT, TOKENS_FOLLOWING_CONSTANT_IN_group_name_169 )
    # --> action
     group = fetch_group( namespace, name.text ) 
    # <-- action
    # at line 93:5: ( ';' )?
    alt_4 = 2
    look_4_0 = @input.peek( 1 )

    if ( look_4_0 == T__12 )
      alt_4 = 1
    end
    case alt_4
    when 1
      # at line 93:5: ';'
      match( T__12, TOKENS_FOLLOWING_T__12_IN_group_name_177 )

    end

  rescue ANTLR3::Error::RecognitionError => re
    report_error(re)
    recover(re)

  ensure
    # -> uncomment the next line to manually enable rule tracing
    # trace_out( __method__, 2 )

  end
  
  return group
end

#group_spec(namespace) ⇒ Object

parser rule group_spec

(in GroupFile.g) 79:1: group_spec[ namespace ] returns [ group ] : ( group_name[ $namespace ] | ) ( member[ $group ] )* ;


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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
# File 'lib/antlr3/template/group-file-parser.rb', line 172

def group_spec( namespace )
  # -> uncomment the next line to manually enable rule tracing
  # trace_in( __method__, 1 )
  group = nil
  group_name1 = nil

  begin
    # at line 80:5: ( group_name[ $namespace ] | ) ( member[ $group ] )*
    # at line 80:5: ( group_name[ $namespace ] | )
    alt_1 = 2
    look_1_0 = @input.peek( 1 )

    if ( look_1_0 == T__10 )
      alt_1 = 1
    elsif ( look_1_0 == EOF || look_1_0 == ID )
      alt_1 = 2
    else
      raise NoViableAlternative( "", 1, 0 )
    end
    case alt_1
    when 1
      # at line 80:7: group_name[ $namespace ]
      @state.following.push( TOKENS_FOLLOWING_group_name_IN_group_spec_85 )
      group_name1 = group_name( namespace )
      @state.following.pop
      # --> action
       group = group_name1 
      # <-- action

    when 2
      # at line 81:7: 
      # --> action
       group = ANTLR3::Template::Group.new 
      # <-- action

    end
    # at line 83:5: ( member[ $group ] )*
    while true # decision 2
      alt_2 = 2
      look_2_0 = @input.peek( 1 )

      if ( look_2_0 == ID )
        alt_2 = 1

      end
      case alt_2
      when 1
        # at line 83:5: member[ $group ]
        @state.following.push( TOKENS_FOLLOWING_member_IN_group_spec_108 )
        member( group )
        @state.following.pop

      else
        break # out of loop for decision 2
      end
    end # loop for decision 2

  rescue ANTLR3::Error::RecognitionError => re
    report_error(re)
    recover(re)

  ensure
    # -> uncomment the next line to manually enable rule tracing
    # trace_out( __method__, 1 )

  end
  
  return group
end

#member(group) ⇒ Object

parser rule member

(in GroupFile.g) 96:1: member[ group ] : name= ID ( parameter_declaration )? '::=' (aliased= ID | TEMPLATE | STRING ) ;


324
325
326
327
328
329
330
331
332
333
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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
# File 'lib/antlr3/template/group-file-parser.rb', line 324

def member( group )
  # -> uncomment the next line to manually enable rule tracing
  # trace_in( __method__, 3 )
  name = nil
  aliased = nil
  __TEMPLATE3__ = nil
  __STRING4__ = nil
  parameter_declaration2 = nil
  # - - - - @init action - - - -
   params = nil 

  begin
    # at line 98:5: name= ID ( parameter_declaration )? '::=' (aliased= ID | TEMPLATE | STRING )
    name = match( ID, TOKENS_FOLLOWING_ID_IN_member_199 )
    # at line 98:13: ( parameter_declaration )?
    alt_5 = 2
    look_5_0 = @input.peek( 1 )

    if ( look_5_0 == ID || look_5_0 == T__14 || look_5_0.between?( T__17, T__18 ) )
      alt_5 = 1
    end
    case alt_5
    when 1
      # at line 98:15: parameter_declaration
      @state.following.push( TOKENS_FOLLOWING_parameter_declaration_IN_member_203 )
      parameter_declaration2 = parameter_declaration
      @state.following.pop
      # --> action
       params = parameter_declaration2 
      # <-- action

    end
    match( T__13, TOKENS_FOLLOWING_T__13_IN_member_210 )
    # at line 99:5: (aliased= ID | TEMPLATE | STRING )
    alt_6 = 3
    case look_6 = @input.peek( 1 )
    when ID then alt_6 = 1
    when TEMPLATE then alt_6 = 2
    when STRING then alt_6 = 3
    else
      raise NoViableAlternative( "", 6, 0 )
    end
    case alt_6
    when 1
      # at line 99:7: aliased= ID
      aliased = match( ID, TOKENS_FOLLOWING_ID_IN_member_220 )
      # --> action
       group.alias_template( name.text, aliased.text ) 
      # <-- action

    when 2
      # at line 100:7: TEMPLATE
      __TEMPLATE3__ = match( TEMPLATE, TOKENS_FOLLOWING_TEMPLATE_IN_member_230 )
      # --> action
       group.define_template( name.text, extract_template( __TEMPLATE3__ ), params ) 
      # <-- action

    when 3
      # at line 101:7: STRING
      __STRING4__ = match( STRING, TOKENS_FOLLOWING_STRING_IN_member_242 )
      # --> action
       group.define_template( name.text, extract_template( __STRING4__ ), params ) 
      # <-- action

    end

  rescue ANTLR3::Error::RecognitionError => re
    report_error(re)
    recover(re)

  ensure
    # -> uncomment the next line to manually enable rule tracing
    # trace_out( __method__, 3 )

  end
  
  return 
end

#parameter(parameters) ⇒ Object

parser rule parameter

(in GroupFile.g) 116:1: parameter[ parameters ] : ( '*' name= ID | '&' name= ID | name= ID ( '=' v= STRING )? );


539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
# File 'lib/antlr3/template/group-file-parser.rb', line 539

def parameter( parameters )
  # -> uncomment the next line to manually enable rule tracing
  # trace_in( __method__, 6 )
  name = nil
  v = nil

  begin
    # at line 117:3: ( '*' name= ID | '&' name= ID | name= ID ( '=' v= STRING )? )
    alt_11 = 3
    case look_11 = @input.peek( 1 )
    when T__17 then alt_11 = 1
    when T__18 then alt_11 = 2
    when ID then alt_11 = 3
    else
      raise NoViableAlternative( "", 11, 0 )
    end
    case alt_11
    when 1
      # at line 117:5: '*' name= ID
      match( T__17, TOKENS_FOLLOWING_T__17_IN_parameter_342 )
      name = match( ID, TOKENS_FOLLOWING_ID_IN_parameter_346 )
      # --> action
       parameters.splat = name.text 
      # <-- action

    when 2
      # at line 118:5: '&' name= ID
      match( T__18, TOKENS_FOLLOWING_T__18_IN_parameter_354 )
      name = match( ID, TOKENS_FOLLOWING_ID_IN_parameter_358 )
      # --> action
       parameters.block = name.text 
      # <-- action

    when 3
      # at line 119:5: name= ID ( '=' v= STRING )?
      name = match( ID, TOKENS_FOLLOWING_ID_IN_parameter_368 )
      # --> action
       param = ANTLR3::Template::Parameter.new( name.text ) 
      # <-- action
      # at line 120:5: ( '=' v= STRING )?
      alt_10 = 2
      look_10_0 = @input.peek( 1 )

      if ( look_10_0 == T__19 )
        alt_10 = 1
      end
      case alt_10
      when 1
        # at line 120:7: '=' v= STRING
        match( T__19, TOKENS_FOLLOWING_T__19_IN_parameter_382 )
        v = match( STRING, TOKENS_FOLLOWING_STRING_IN_parameter_386 )
        # --> action
         param.default = v.text 
        # <-- action

      end
      # --> action
       parameters.add( param ) 
      # <-- action

    end
  rescue ANTLR3::Error::RecognitionError => re
    report_error(re)
    recover(re)

  ensure
    # -> uncomment the next line to manually enable rule tracing
    # trace_out( __method__, 6 )

  end
  
  return 
end

#parameter_declarationObject

parser rule parameter_declaration

(in GroupFile.g) 105:1: parameter_declaration returns [ list ] : ( '(' ( parameters )? ')' | parameters );


410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
# File 'lib/antlr3/template/group-file-parser.rb', line 410

def parameter_declaration
  # -> uncomment the next line to manually enable rule tracing
  # trace_in( __method__, 4 )
  list = nil
  parameters5 = nil
  parameters6 = nil
  # - - - - @init action - - - -
   list = nil 

  begin
    # at line 107:3: ( '(' ( parameters )? ')' | parameters )
    alt_8 = 2
    look_8_0 = @input.peek( 1 )

    if ( look_8_0 == T__14 )
      alt_8 = 1
    elsif ( look_8_0 == ID || look_8_0.between?( T__17, T__18 ) )
      alt_8 = 2
    else
      raise NoViableAlternative( "", 8, 0 )
    end
    case alt_8
    when 1
      # at line 107:5: '(' ( parameters )? ')'
      match( T__14, TOKENS_FOLLOWING_T__14_IN_parameter_declaration_276 )
      # at line 107:9: ( parameters )?
      alt_7 = 2
      look_7_0 = @input.peek( 1 )

      if ( look_7_0 == ID || look_7_0.between?( T__17, T__18 ) )
        alt_7 = 1
      end
      case alt_7
      when 1
        # at line 107:11: parameters
        @state.following.push( TOKENS_FOLLOWING_parameters_IN_parameter_declaration_280 )
        parameters5 = parameters
        @state.following.pop
        # --> action
         list = parameters5 
        # <-- action

      end
      match( T__15, TOKENS_FOLLOWING_T__15_IN_parameter_declaration_287 )

    when 2
      # at line 108:5: parameters
      @state.following.push( TOKENS_FOLLOWING_parameters_IN_parameter_declaration_293 )
      parameters6 = parameters
      @state.following.pop
      # --> action
       list = parameters6 
      # <-- action

    end
  rescue ANTLR3::Error::RecognitionError => re
    report_error(re)
    recover(re)

  ensure
    # -> uncomment the next line to manually enable rule tracing
    # trace_out( __method__, 4 )

  end
  
  return list
end

#parametersObject

parser rule parameters

(in GroupFile.g) 111:1: parameters returns [ list ] : parameter[ $list ] ( ',' parameter[ $list ] )* ;


485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
# File 'lib/antlr3/template/group-file-parser.rb', line 485

def parameters
  # -> uncomment the next line to manually enable rule tracing
  # trace_in( __method__, 5 )
  list = nil
  # - - - - @init action - - - -
   list = ANTLR3::Template::ParameterList.new 

  begin
    # at line 113:5: parameter[ $list ] ( ',' parameter[ $list ] )*
    @state.following.push( TOKENS_FOLLOWING_parameter_IN_parameters_317 )
    parameter( list )
    @state.following.pop
    # at line 113:24: ( ',' parameter[ $list ] )*
    while true # decision 9
      alt_9 = 2
      look_9_0 = @input.peek( 1 )

      if ( look_9_0 == T__16 )
        alt_9 = 1

      end
      case alt_9
      when 1
        # at line 113:26: ',' parameter[ $list ]
        match( T__16, TOKENS_FOLLOWING_T__16_IN_parameters_322 )
        @state.following.push( TOKENS_FOLLOWING_parameter_IN_parameters_324 )
        parameter( list )
        @state.following.pop

      else
        break # out of loop for decision 9
      end
    end # loop for decision 9

  rescue ANTLR3::Error::RecognitionError => re
    report_error(re)
    recover(re)

  ensure
    # -> uncomment the next line to manually enable rule tracing
    # trace_out( __method__, 5 )

  end
  
  return list
end

#unescape(text) ⇒ Object


130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/antlr3/template/group-file-parser.rb', line 130

def unescape( text )
  text.gsub( /\\(?:([abefnrstv])|([0-7]{3})|x([0-9a-fA-F]{2})|(.))/ ) do
    if $1
      case $1[0]
      when ?a then "\a"
      when ?b then "\b"
      when ?e then "\e"
      when ?f then "\f"
      when ?n then "\n"
      when ?r then "\r"
      when ?s then "\s"
      when ?t then "\t"
      when ?v then "\v"
      end
    elsif $2 then $2.to_i( 8 ).chr
    elsif $3 then $3.to_i( 16 ).chr
    elsif $4 then $4
    end
  end
end