All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.antlr.codegen.templates.Ruby.Ruby.stg Maven / Gradle / Ivy

There is a newer version: 3.5.3
Show newest version
/******************************************************************************
 *********************  M A J O R   C O M P O N E N T S  **********************
 ******************************************************************************/

// System.Boolean.ToString() returns "True" and "False", but the proper C# literals are "true" and "false"
// The Java version of Boolean returns "true" and "false", so they map to themselves here.
booleanLiteral ::= [
	"True":"true",
	"False":"false",
	"true":"true",
	"false":"false",
	default:"false"
]

/** The overall file structure of a recognizer; stores methods
  * for rules and cyclic DFAs plus support code.
  */
outputFile(LEXER, PARSER, TREE_PARSER, actionScope, actions, docComment, recognizer, name,
  tokens, tokenNames, rules, cyclicDFAs, bitsets, buildTemplate, buildAST, rewriteMode,
  profile, backtracking, synpreds, memoize, numRules, fileName, ANTLRVersion, generatedTimestamp,
  trace, scopes, superClass, literals) ::=
<<
#!/usr/bin/env ruby
#
# 
# --
# Generated using ANTLR version: 
# Ruby runtime library version: 
# Input grammar file: 
# Generated at: 
#

# ~~~\> start load path setup
this_directory = File.expand_path( File.dirname( __FILE__ ) )
$LOAD_PATH.unshift( this_directory ) unless $LOAD_PATH.include?( this_directory )

antlr_load_failed = proc do
  load_path = $LOAD_PATH.map { |dir| '  - ' \<\< dir }.join( $/ )
  raise LoadError, \<\<-END.strip!

Failed to load the ANTLR3 runtime library (version ):

Ensure the library has been installed on your system and is available
on the load path. If rubygems is available on your system, this can
be done with the command:

  gem install antlr3

Current load path:
#{ load_path }

  END
end

defined?( ANTLR3 ) or begin

  # 1: try to load the ruby antlr3 runtime library from the system path
  require 'antlr3'

rescue LoadError

  # 2: try to load rubygems if it isn't already loaded
  defined?( Gem ) or begin
    require 'rubygems'
  rescue LoadError
    antlr_load_failed.call
  end

  # 3: try to activate the antlr3 gem
  begin
    Gem.activate( 'antlr3', '~> ' )
  rescue Gem::LoadError
    antlr_load_failed.call
  end

  require 'antlr3'

end
# \<~~~ end load path setup














if __FILE__ == $0 and ARGV.first != '--'
  
end

>>

tokenDataModule() ::= <<
# TokenData defines all of the token type integer values
# as constants, which will be included in all
# ANTLR-generated recognizers.
const_defined?( :TokenData ) or TokenData = ANTLR3::TokenScheme.new

module TokenData


  # define the token constants
  define_tokens(  => }; anchor, wrap="\n", separator=", "> )




  # register the proper human-readable name or literal value
  # for each token type
  #
  # this is necessary because anonymous tokens, which are
  # created from literal values in the grammar, do not
  # have descriptive names
  register_names( }; separator=", ", anchor, wrap="\n"> )



  
  
end<\n>
>>

rootGrammarOutputFile() ::= <<
module 
  
  
  
  
end
>>

delegateGrammarOutputFile() ::= <<
require ''


  

>>

delegateGrammarModuleHead(gram) ::= <<

module 

<\n>
class 

>>

delegateGrammarModuleTail(gram) ::= <<

end # module 

end # class 
<\n>

>>
/* * * * * * * * * * R E C O G N I Z E R   C L A S S E S * * * * * * * * * */

parser(
  grammar, name, scopes, tokens, tokenNames, rules, numRules, bitsets,
  ASTLabelType="Object", superClass="ANTLR3::Parser", labelType="ANTLR3::Token",
  members={}
) ::= <<


class Parser \< 
  
end # class Parser \< 


at_exit { Parser.main( ARGV ) } if __FILE__ == $0

>>

/** How to generate a tree parser; same as parser except the
  * input stream is a different type.
  */
treeParser(grammar, name, scopes, tokens, tokenNames, globalAction, rules, numRules, bitsets, filterMode, labelType={}, ASTLabelType="Object", superClass="ANTLR3::TreeParser", members={}) ::= <<


class TreeParser \< 
  
end # class TreeParser \< 


at_exit { TreeParser.main( ARGV ) } if __FILE__ == $0

>>

parserBody(grammar, name, scopes, tokens, tokenNames, rules, numRules, bitsets, inputStreamType, superClass, filterMode, labelType, members, rewriteElementType, actionScope, ASTLabelType="Object") ::= <<
@grammar_home = 
<\n>
<@mixins()>

RULE_METHODS = [ }; separator=", ", wrap="\n", anchor> ].freeze

<\n>}>
}>

masters( }; separator=", "> )<\n>


imports( }; separator=", "> )<\n>


include TokenData

begin
  generated_using( "", "", "" )
rescue NoMethodError => error
  # ignore
end


require ''
include ::TokenData<\n><\n>


<@additionalMembers()>

# - - - - - - - - - - - - Rules - - - - - - - - - - - - -
<\n>}>


# - - - - - - - - - - Delegated Rules - - - - - - - - - - -
<\n>}>


# - - - - - - - - - - DFA definitions - - - - - - - - - - -
}>

private

def initialize_dfas
  super rescue nil
  }>
end


_IN__ = Set[ }; separator=", "> ]<\n>}>
>>

parserConstructor() ::= <<
def initialize( , }>input, options = {} )
  super( input, options )

  @state.rule_memory = {}

  <\n>}>}>
  
   = <\n>}> = <\n>}><\n>}><@init()>
end
>>


/* * * * * * * * * * * * * R U L E   M E T H O D S * * * * * * * * * * * * */

/** A simpler version of a rule template that is specific to the
  * imaginary rules created for syntactic predicates.  As they
  * never have return values nor parameters etc..., just give
  * simplest possible method.  Don't do any of the normal
  * memoization stuff in here either; it's a waste. As
  * predicates cannot be inlined into the invoking rule, they
  * need to be in a rule by themselves.
  */
synpredRule(ruleName, ruleDescriptor, block, description, nakedBlock) ::= <<
#
# syntactic predicate 
#
# (in )
# 
#
# This is an imaginary rule inserted by ANTLR to
# implement a syntactic predicate decision
#
def (  )
  
  
ensure
  
end
>>


/** How to generate code for a rule.  This includes any return
  * type data aggregates required for multiple return values.
  */
rule(ruleName, ruleDescriptor, block, emptyRule, description, exceptions, finally, memoize) ::= <<


#
# parser rule 
#
# (in )
# 
#
def (  )
  
  <@body><@end>

  return 
end


 : rescue nil<\n>

>>

delegateRule(ruleDescriptor) ::= <<
# delegated rule 
def (  )
  }>})>
end
>>
// HELPERS

recognizerClassName() ::= <<
TreeParserParserLexer
>>

initializeDirectDelegate() ::= <<
@ = ::.new(
  , }>self, input, options.merge( :state => @state )
)
>>

initializeDelegator() ::= <<
@ = 
>>

altSwitchCase(altNum,alt) ::= <<
when 
  <@prealt()>
  
>>

blockBody() ::= <<
<@decision><@end>
case alt_
}; separator="\n">
end
>>

catch(decl, action) ::= <<
# - - - - - - @catch  - - - - - -
rescue 
  <\n>
>>

closureBlockLoop() ::= <<
while true # decision 
  alt_ = 
  <@decisionBody><@end>
  case alt_
  }; separator="\n">
  else
    break # out of loop for decision 
  end
end # loop for decision 
>>

delegateName(d) ::= <<

>>

element(e) ::= <<
<\n>
>>

execForcedAction(action) ::= ""

globalAttributeScopeClass(scope) ::= <<
@@ = Scope( }; separator=", "> )<\n>
>>

globalAttributeScopeStack(scope) ::= <<
@_stack = []<\n>
>>

noRewrite(rewriteBlockLevel, treeLevel) ::= ""

parameterScope(scope) ::= <<
}; separator=", ">
>>

positiveClosureBlockLoop() ::= <<
match_count_ = 0
while true
  alt_ = 
  <@decisionBody><@end>
  case alt_
  }; separator="\n">
  else
    match_count_ > 0 and break
    
    eee = EarlyExit()
    <@earlyExitException()><\n>
    raise eee
  end
  match_count_ += 1
end<\n>
>>

returnScope(scope) ::= <<

 = define_return_scope }; separator=", ">

>>

returnStructName(r) ::= "ReturnValue"

ruleAttributeScopeClass ::= globalAttributeScopeClass
ruleAttributeScopeStack ::= globalAttributeScopeStack

ruleBacktrackFailure() ::= <<

@state.backtracking > 0 and raise( ANTLR3::Error::BacktrackingFailed )<\n>

>>

ruleBody() ::= <<

success = false # flag used for memoization<\n>

begin
  <(ruleDescriptor.actions.after):execAction()>

  success = true<\n>


  <\n>}>




# - - - - - - - - @rulecatch - - - - - - - -


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




ensure
  
end
>>

ruleReturnValue() ::= <%





return_value



%>


ruleDeclarations() ::= <<

return_value = .new

# $rule.start = the first token seen before matching
return_value.start = @input.look<\n>

 = nil<\n>}>


_start_index = @input.index<\n>

>>

ruleLabelDef(label) ::= <<
 = nil<\n>
>>

ruleLabelDefs() ::= <<
<[
    ruleDescriptor.tokenLabels,
    ruleDescriptor.tokenListLabels,
    ruleDescriptor.wildcardTreeLabels,
    ruleDescriptor.wildcardTreeListLabels,
    ruleDescriptor.ruleLabels,
    ruleDescriptor.ruleListLabels
 ]:
 { = nil<\n>}
><[
    ruleDescriptor.tokenListLabels,
    ruleDescriptor.ruleListLabels,
    ruleDescriptor.wildcardTreeListLabels
  ]:
  {list_of_ = []<\n>}
>
>>

/* * * * * * * * * * * * * R U L E   H E L P E R S * * * * * * * * * * * * */

traceIn() ::= <<

trace_in( __method__,  )<\n>

# -> uncomment the next line to manually enable rule tracing
# trace_in( __method__,  )<\n>

>>

traceOut() ::= <<

trace_out( __method__,  )<\n>

# -> uncomment the next line to manually enable rule tracing
# trace_out( __method__,  )<\n>

>>

ruleCleanUp() ::= <<


# - - - - - - - rule clean up - - - - - - - -
return_value.stop = @input.look( -1 )<\n>


>>

ruleMemoization(name) ::= <<

# rule memoization
if @state.backtracking > 0 and already_parsed_rule?( __method__ )
  success = true
  return 
end<\n>

>>


ruleScopeSetUp() ::= <<
_stack.push( @@.new )<\n>}>_stack.push( @@.new )<\n>}>
>>

ruleScopeCleanUp() ::= <<
_stack.pop<\n>}>_stack.pop<\n>}>
>>

memoize() ::= <<

memoize( __method__, _start_index, success ) if @state.backtracking > 0<\n>

>>

/** helper template to format a ruby method call */
methodCall(n, del, args) ::= <<
@.(  )
>>

/* * * * * * * * * * * * * L E X E R   P A R T S * * * * * * * * * * * * * */

actionGate() ::= "@state.backtracking == 0"

/** A (...) subrule with multiple alternatives */
block(alts,decls,decision,enclosingBlockLevel,blockLevel,decisionNumber,maxK,maxAlt,description) ::= <<
# at line 
alt_ = 

<@body><@end>
>>

/** A rule block with multiple alternatives */
ruleBlock(alts,decls,decision,enclosingBlockLevel,blockLevel,decisionNumber,maxK,maxAlt,description) ::= <<
# at line 
alt_ = 

<@decision><@end>
case alt_
}; separator="\n">
end
>>

ruleBlockSingleAlt(alts,decls,decision,enclosingBlockLevel,blockLevel,decisionNumber,description) ::= <<

<@prealt()>

>>

/** A special case of a (...) subrule with a single alternative */
blockSingleAlt(alts,decls,decision,enclosingBlockLevel,blockLevel,decisionNumber,description) ::= <<
# at line 

<@prealt()>

>>

/** A (..)+ block with 0 or more alternatives */
positiveClosureBlock(alts,decls,decision,enclosingBlockLevel,blockLevel,decisionNumber,maxK,maxAlt,description) ::= <<
# at file 

<@loopBody>

<@end>
>>

positiveClosureBlockSingleAlt ::= positiveClosureBlock

/** A (..)* block with 0 or more alternatives */
closureBlock(alts,decls,decision,enclosingBlockLevel,blockLevel,decisionNumber,maxK,maxAlt,description) ::= <<
# at line 

<@loopBody>

<@end>
>>

closureBlockSingleAlt ::= closureBlock

/** Optional blocks (x)? are translated to (x|) by before code
  * generation so we can just use the normal block template
  */
optionalBlock ::= block

optionalBlockSingleAlt ::= block

/** An alternative is just a list of elements; at outermost
  * level
  */
alt(elements,altNum,description,autoAST,outerAlt,treeLevel,rew) ::= <<
# at line 

>>

/** match a token optionally with a label in front */
tokenRef(token,label,elementIndex,terminalOptions) ::= <<
 = match( , TOKENS_FOLLOWING__IN__ )
>>

/** ids+=ID */
tokenRefAndListLabel(token,label,elementIndex,terminalOptions) ::= <<

},...)>
>>

/* TRY THIS:
tokenRefAndListLabel(token,label,elementIndex,terminalOptions) ::= <<
list_of_ << match( , TOKENS_FOLLOWING__IN__ )
>>
*/

addToList(label,elem) ::= <<
list_of_ \<\< <\n>
>>

listLabel ::= addToList

/** For now, sets are interval tests and must be tested inline */
matchSet(s,label,elementIndex,terminalOptions,postmatchCode) ::= <<

 = @input.look<\n>

if 
  @input.consume
  

  @state.error_recovery = false<\n>

else
  
  mse = MismatchedSet( nil )
  <@mismatchedSetException()>

  recover mse
  raise mse<\n>

  raise mse<\n>

end
<\n>
>>


matchSetAndListLabel(s,label,elementIndex,postmatchCode) ::= <<

},...)>
>>

matchRuleBlockSet ::= matchSet

wildcard(token,label,elementIndex,terminalOptions) ::= <<

 = @input.look<\n>

match_any
>>

/* TRY THIS:
wildcard(label,elementIndex) ::= <<
 = match_any
>>
*/

wildcardAndListLabel(token,label,elementIndex,terminalOptions) ::= <<

},...)>
>>


/** Match a rule reference by invoking it possibly with
  * arguments and a return value or values.
  */
ruleRef(rule,label,elementIndex,args,scope) ::= <<
@state.following.push( TOKENS_FOLLOWING__IN__ )
 = }, args=args)>
@state.following.pop
>>

/** ids+=ID */
ruleRefAndListLabel(rule,label,elementIndex,args,scope) ::= <<

},...)>
>>

/** match ^(root children) in tree parser */
tree(root, actionsAfterRoot, children, nullableChildList, enclosingTreeLevel, treeLevel) ::= <<



if @input.peek == DOWN
  match( DOWN, nil )
  
  match( UP, nil )
end

match( DOWN, nil )

match( UP, nil )

>>

/** Every predicate is used as a validating predicate (even when
  * it is also hoisted into a prediction expression).
  */
validateSemanticPredicate(pred,description) ::= <<

unless (  )
  
  raise FailedPredicate( "", "" )
end

raise FailedPredicate( "", "" ) unless (  )

>>

dfaState(k,edges,eotPredictsAlt,description,stateNumber,semPredState) ::= <<
look__ = @input.peek(  )<\n>

else

  alt_ = <\n>


  <\n>

<@noViableAltException>
  raise NoViableAlternative( "", ,  )<\n>
<@end>

end
>>

/** Same as a normal DFA state except that we don't examine
  * look for the bypass alternative.  It delays error
  * detection but this is faster, smaller, and more what people
  * expect.  For (X)? people expect "if ( LA(1)==X ) match(X);"
  * and that's it. *  If a semPredState, don't force look
  * lookup; preds might not need.
  */
dfaOptionalBlockState(k,edges,eotPredictsAlt,description,stateNumber,semPredState) ::= <<
look__ = @input.peek(  )<\n>

end
>>


/** A DFA state that is actually the loopback decision of a
  * closure loop.  If end-of-token (EOT) predicts any of the
  * targets then it should act like a default clause (i.e., no
  * error can be generated). This is used only in the lexer so
  * that for ('a')* on the end of a rule anything other than 'a'
  * predicts exiting. *  If a semPredState, don't force
  * look lookup; preds might not need.
  */
dfaLoopbackState(k,edges,eotPredictsAlt,description,stateNumber,semPredState) ::= <<
look__ = @input.peek(  )<\n>
<\n>

else
  alt_ = <\n>

end
>>


/** An accept state indicates a unique alternative has been
  * predicted
  */
dfaAcceptState(alt) ::= "alt_ = "

/** A simple edge with an expression.  If the expression is
  * satisfied, enter to the target state.  To handle gated
  * productions, we may have to evaluate some predicates for
  * this edge.
  */
dfaEdge(labelExpr, targetState, predicates) ::= <<
if (  ) and (  )
  
>>


/** A DFA state where a SWITCH may be generated.  The code
  * generator decides if this is possible:
  * CodeGenerator.canGenerateSwitch().
  */
dfaStateSwitch(k,edges,eotPredictsAlt,description,stateNumber,semPredState) ::= <<
case look_ = @input.peek(  )

else

  alt_ = <\n>


  <\n>

<@noViableAltException>
  raise NoViableAlternative( "", ,  )<\n>
<@end>

end
>>


dfaOptionalBlockStateSwitch(k, edges, eotPredictsAlt, description, stateNumber, semPredState) ::= <<
case look_ = @input.peek(  )

end
>>

dfaLoopbackStateSwitch(k, edges, eotPredictsAlt, description, stateNumber, semPredState) ::= <<
case look_ = @input.peek(  )


else
  alt_ = 

end
>>

dfaEdgeSwitch(labels, targetState) ::= <<
when }; separator=", "> then 
>>

/** The code to initiate execution of a cyclic DFA; this is used
  * in the rule to predict an alt just like the fixed DFA case.
  * The  attribute is inherited via the parser, lexer, ...
  */
dfaDecision(decisionNumber, description) ::= <<
alt_ = @dfa.predict( @input )
>>

/** Generate the tables and support code needed for the DFAState
  * object argument.  Unless there is a semantic predicate (or
  * syn pred, which become sem preds), all states should be
  * encoded in the state tables. Consequently,
  * cyclicDFAState/cyclicDFAEdge,eotDFAEdge templates are not
  * used except for special DFA states that cannot be encoded as
  * a transition table.
  */
cyclicDFA(dfa) ::= <<
class DFA \< ANTLR3::DFA
  EOT = unpack(  )
  EOF = unpack(  )
  MIN = unpack(  )
  MAX = unpack(  )
  ACCEPT = unpack(  )
  SPECIAL = unpack(  )
  TRANSITION = [
     )}; separator=",\n">
  ].freeze

  ( 0 ... MIN.length ).zip( MIN, MAX ) do | i, a, z |
    if a \> 0 and z \< 0
      MAX[ i ] %= 0x10000
    end
  end

  @decision = 

  <@errorMethod()>


  def description
    \<\<-'__dfa_description__'.strip!
      
    __dfa_description__
  end<\n>

end<\n>
>>


specialStateTransitionMethod(dfa) ::= <<
def special_state_transition_for_dfa(s, input)
  case s
  
  }; separator="\n">
  end

  @state.backtracking > 0 and raise ANTLR3::Error::BacktrackingFailed<\n>

  nva = ANTLR3::Error::NoViableAlternative.new( @dfa.description, , s, input )
  @dfa.error( nva )
  raise nva
end
>>

cyclicDFASynpred( name ) ::= <<
def () @recognizer. end<\n>
>>

cyclicDFAInit(dfa) ::= <<

@dfa = DFA.new( self,  ) do |s|
  case s
  
  }; separator="\n">
  end

  if s \< 0

    @state.backtracking > 0 and raise ANTLR3::Error::BacktrackingFailed<\n>

    nva = ANTLR3::Error::NoViableAlternative.new( @dfa.description, , s, input )
    @dfa.error( nva )
    raise nva
  end

  s
end<\n>

@dfa = DFA.new( self,  )<\n>

>>


/** A special state in a cyclic DFA; special means has a
  * semantic predicate or it's a huge set of symbols to check.
  */
cyclicDFAState(decisionNumber, stateNumber, edges, needErrorClause, semPredState) ::= <<
look__ = @input.peek

index__ = @input.index
@input.rewind( @input.last_marker, false )<\n>

s = -1
end
 
@input.seek( index__ )<\n>

>>

/** Just like a fixed DFA edge, test the look and indicate
  * what state to jump to next if successful.  Again, this is
  * for special states.
  */
cyclicDFAEdge(labelExpr, targetStateNumber, edgeNumber, predicates) ::= <<
if (  ) and (  )
  s = <\n>
>>

/** An edge pointing at end-of-token; essentially matches any
  * char; always jump to the target.
  */
eotDFAEdge(targetStateNumber, edgeNumber, predicates) ::= <<
e
  s = <\n>
>>

andPredicates(left,right) ::= "(  ) and (  )"

orPredicates(operands) ::= " )}; separator=\" or \">"

notPredicate(pred) ::= "not (  )"

evalPredicate(pred,description) ::= "(  )"

evalSynPredicate(pred,description) ::= <<
syntactic_predicate?( :}> )
>>

lookaheadTest(atom, k, atomAsInt) ::= "look__ == "

/** Sometimes a look test cannot assume that LA(k) is in a
  * temp variable somewhere.  Must ask for the look
  * directly.
  */
isolatedLookaheadTest(atom, k, atomAsInt) ::= "@input.peek() == "

lookaheadRangeTest(lower, upper, k, rangeNumber, lowerAsInt, upperAsInt) ::= <<
look__.between?( ,  )
>>

isolatedLookaheadRangeTest(lower, upper, k, rangeNumber, lowerAsInt, upperAsInt) ::= <<
@input.peek(  ).between?( ,  )
>>

setTest(ranges) ::= <<

>>

parameterAttributeRef(attr) ::= ""

parameterSetAttributeRef(attr,expr) ::= " = "

scopeAttributeRef(scope, attr, index, negIndex) ::= <<

@_stack[ - ].


@_stack[  ].

@_stack.last.


>>


scopeSetAttributeRef(scope, attr, expr, index, negIndex) ::= <<

@_stack[ - ]. = 


@_stack[  ]. = 

@_stack.last. = 


>>


/** $x is either global scope or x is rule with dynamic scope;
  * refers to stack itself not top of stack.  This is useful for
  * predicates like {$function.size()>0 &&
  * $function::name.equals("foo")}?
  */
isolatedDynamicScopeRef(scope) ::= "@_stack"

/** reference an attribute of rule; might only have single
  * return value
  */
ruleLabelRef(referencedRule, scope, attr) ::= <<

( .nil? ? nil : . )



>>

returnAttributeRef(ruleDescriptor, attr) ::= <<

return_value.



>>

returnSetAttributeRef(ruleDescriptor, attr, expr) ::= <<

return_value. = 

 = 

>>

/** How to translate $tokenLabel */
tokenLabelRef(label) ::= ""

/** ids+=ID {$ids} or e+=expr {$e} */
listLabelRef(label) ::= "list_of_"

tokenLabelPropertyRef_text(scope, attr) ::= ".text"
tokenLabelPropertyRef_type(scope, attr) ::= ".type"
tokenLabelPropertyRef_line(scope, attr) ::= ".line"
tokenLabelPropertyRef_pos(scope, attr) ::= ".column"
tokenLabelPropertyRef_channel(scope, attr) ::= ".channel"
tokenLabelPropertyRef_index(scope, attr) ::= ".index"
tokenLabelPropertyRef_tree(scope, attr) ::= "tree_for_"

ruleLabelPropertyRef_start(scope, attr) ::= ".start"
ruleLabelPropertyRef_stop(scope, attr) ::= ".stop"
ruleLabelPropertyRef_tree(scope, attr) ::= ".tree"

ruleLabelPropertyRef_text(scope, attr) ::= <<

(
  @input.token_stream.to_s(
    @input.tree_adaptor.token_start_index( .start ),
    @input.tree_adaptor.token_stop_index( .start )
  ) if 
)

(  && @input.to_s( .start, .stop ) )

>>
ruleLabelPropertyRef_st(scope, attr) ::= "(  && .template )"

/******************************************************************************
 *****************  L E X E R - O N L Y   T E M P L A T E S  ******************
 ******************************************************************************/

lexerSynpred(name) ::= ""

lexer(grammar, name, tokens, scopes, rules, numRules, labelType="ANTLR3::Token", filterMode, superClass="ANTLR3::Lexer") ::= <<


class Lexer \< 
  @grammar_home = 

  <\n>

  include TokenData

  include ANTLR3::FilterMode<\n>

  <\n>}>

  begin
    generated_using( "", "", "" )
  rescue NoMethodError => error
    # ignore
  end

  RULE_NAMES   = [ "}; separator=", ", wrap="\n", anchor> ].freeze
  RULE_METHODS = [ }; separator=", ", wrap="\n", anchor> ].freeze


  masters( }; separator=", "> )<\n>


  imports( }; separator=", "> )<\n>


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


    @state.rule_memory = {}<\n>


     = <\n>}> = <\n>}><\n>}>
  end

  

  # - - - - - - - - - - - lexer rules - - - - - - - - - - - -
  <\n>}>


  # - - - - - - - - - - delegated rules - - - - - - - - - - -
  <\n><\n>}>



  # - - - - - - - - - - DFA definitions - - - - - - - - - - -
  

  private

  def initialize_dfas
    super rescue nil
    
  end


end # class Lexer \< 


at_exit { Lexer.main( ARGV ) } if __FILE__ == $0

>>


lexerRuleLabelDefs() ::= <<

# - - - - label initialization - - - -
<[ruleDescriptor.tokenLabels,ruleDescriptor.tokenListLabels,ruleDescriptor.ruleLabels,ruleDescriptor.charLabels]:{it |  = nil<\n>}>
<[ruleDescriptor.tokenListLabels,ruleDescriptor.ruleListLabels]:{it | list_of_ = [] unless defined?(list_of_)<\n>}>

>>


/** How to generate a rule in the lexer; naked blocks are used
  * for fragment rules.
  */
lexerRule(ruleName,nakedBlock,ruleDescriptor,block,memoize) ::= <<
# lexer rule  ()
# (in )
def (  )
  


  # backtracking success
  success = false<\n>



  })>

  # - - - - main rule block - - - -
  


  type = 
  channel = ANTLR3::DEFAULT_CHANNEL
  

  # - - - - main rule block - - - -
  
  

  @state.type = type
  @state.channel = channel
<(ruleDescriptor.actions.after):execAction()>


  success = false<\n>

ensure
  
end


 :<\n>
 !>
>>


/** Isolated $RULE ref ok in lexer as it's a Token */
lexerRuleLabel(label) ::= ""
lexerRuleLabelPropertyRef_line(scope, attr) ::= ".line"
lexerRuleLabelPropertyRef_type(scope, attr) ::= ".type"
lexerRuleLabelPropertyRef_pos(scope, attr) ::= ".column"
lexerRuleLabelPropertyRef_channel(scope, attr) ::= ".channel"
lexerRuleLabelPropertyRef_index(scope, attr) ::= ".index"
lexerRuleLabelPropertyRef_text(scope, attr) ::= ".text"


/** How to generate code for the implicitly-defined lexer
  * grammar rule that chooses between lexer rules.
  */
tokensRule(ruleName,nakedBlock,args,block,ruleDescriptor) ::= <<
# main rule used to study the input at the current position,
# and choose the proper lexer rule to call in order to
# fetch the next token
#
# usually, you don't make direct calls to this method,
# but instead use the next_token method, which will
# build and emit the actual next token
def 
  
end
>>

lexerRulePropertyRef_text(scope, attr) ::= "self.text"
lexerRulePropertyRef_type(scope, attr) ::= "type"
lexerRulePropertyRef_line(scope, attr) ::= "@state.token_start_line"
lexerRulePropertyRef_pos(scope, attr)  ::= "@state.token_start_column"

/** Undefined, but present for consistency with Token
  * attributes; set to -1
  */
lexerRulePropertyRef_index(scope, attr) ::= "-1"
lexerRulePropertyRef_channel(scope, attr) ::= "channel"
lexerRulePropertyRef_start(scope, attr) ::= "@state.token_start_position"
lexerRulePropertyRef_stop(scope, attr) ::= "( self.character_index - 1 )"

/** A lexer rule reference */
lexerRuleRef(rule,label,args,elementIndex,scope) ::= <<

_start_ = self.character_index
},del=scope,args=args)>
 = create_token do |t|
  t.input   = @input
  t.type    = ANTLR3::INVALID_TOKEN_TYPE
  t.channel = ANTLR3::DEFAULT_CHANNEL
  t.start   = _start_
  t.stop    = self.character_index - 1
end

}, del=scope, args=args)>

>>


/** i+=INT in lexer */
lexerRuleRefAndListLabel(rule,label,args,elementIndex,scope) ::= <<

},...)>
>>


/** Match . wildcard in lexer */
wildcardChar(label, elementIndex) ::= <<

 = @input.peek<\n>

match_any
>>

wildcardCharListLabel(label, elementIndex) ::= <<

},...)>
>>

/** match a character */
charRef(char,label) ::= <<

 = @input.peek<\n>

match(  )
>>

/** match a character range */
charRangeRef(a,b,label) ::= <<

 = @input.peek<\n>

match_range( ,  )
>>

filteringNextToken() ::= ""
filteringActionGate() ::= "@state.backtracking == 1"

/** Match a string literal */
lexerStringRef(string,label,elementIndex) ::= <<

_start = self.character_index
match(  )
 = create_token do |t|
  t.input   = @input
  t.type    = ANTLR3::INVALID_TOKEN_TYPE
  t.channel = ANTLR3::DEFAULT_CHANNEL
  t.start   = _start
  t.stop    = character_index - 1
end

match(  )

>>


/** EOF in the lexer */
lexerMatchEOF(label,elementIndex) ::= <<

_start_ = character_index
match( ANTLR3::EOF )
 = create_token do |t|
  t.input   = @input
  t.type    = ANTLR3::INVALID_TOKEN_TYPE
  t.channel = ANTLR3::DEFAULT_CHANNEL
  t.start   = _start_
  t.stop    = character_index - 1
end<\n>

match( ANTLR3::EOF )<\n>

>>

// used for left-recursive rules
recRuleDefArg()                       ::= "int "
recRuleArg()                          ::= "_p"
recRuleAltPredicate(ruleName,opPrec)  ::= " \<= "
recRuleSetResultAction()              ::= "root_0=$_primary.tree;"
recRuleSetReturnAction(src,name)      ::= "$=$.;"

/** $start in parser rule */
rulePropertyRef_start(scope, attr) ::= "return_value.start"

/** $stop in parser rule */
rulePropertyRef_stop(scope, attr) ::= "return_value.stop"

/** $tree in parser rule */
rulePropertyRef_tree(scope, attr) ::= "return_value.tree"

/** $text in parser rule */
rulePropertyRef_text(scope, attr) ::= "@input.to_s( return_value.start, @input.look( -1 ) )"

/** $template in parser rule */
rulePropertyRef_st(scope, attr) ::= "return_value.template"

ruleSetPropertyRef_tree(scope, attr, expr) ::= "return_value.tree = "

ruleSetPropertyRef_st(scope, attr, expr) ::= "return_value.template = "

/** How to execute an action */
execAction(action) ::= <<

# syntactic predicate action gate test
if 
  # --> action
  
  # \<-- action
end

# --> action

# \<-- action

>>

codeFileExtension() ::= ".rb"

true()  ::= "true"
false() ::= "false"

action(name, code) ::= <<

# - - - - @ action - - - -
<\n>

>>

autoloadDelegates() ::= <<

, ""<\n>}>

>>

delegateLexerRule(ruleDescriptor) ::= <<
# delegated lexer rule  ( in the grammar)
def (  )
  }, args=ruleDescriptor.parameterScope.attributes)>
end
>>

rootClassName() ::= <<
::TreeParserParserLexer
>>

grammarClassName() ::= <<
::TreeParserParserLexer
>>

newDelegate(gram) ::= <<
.new( , }>
  self, @input, :state => @state<@delegateOptions()>
)
>>

placeAction(scope, name) ::= <<

# - - - - - - begin action @:: - - - - - -
# <\n>

# - - - - - - end action @:: - - - - - - -<\n>

>>

runtimeLibraryVersion() ::= "1.8.1"