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

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

There is a newer version: 3.5.3
Show newest version
group JavaScript;

/** 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) ::=
<<
// $ANTLR   


<@imports>


<@end>



>>

lexer(grammar, name, tokens, scopes, rules, numRules, labelType="Token",
      filterMode, superClass="org.antlr.runtime.Lexer") ::= <<
var  = function(input, state}>) {
// alternate constructor @todo
// public (CharStream input }>)
// public (CharStream input, RecognizerSharedState state }>) {
    if (!state) {
        state = new org.antlr.runtime.RecognizerSharedState();
    }

    (function(){
        
    }).call(this);

     = new .DFA(this);}; separator="\n">
    .superclass.constructor.call(this, input, state);
    
    
    this.state.ruleMemo = {};
    
    

     = new (input, state}>, this);}; separator="\n">
     = ;}; separator="\n">
    ;}>

    
};

org.antlr.lang.augmentObject(, {
    : }; separator=",\n">
});

(function(){
var HIDDEN = org.antlr.runtime.Token.HIDDEN_CHANNEL,
    EOF = org.antlr.runtime.Token.EOF;
org.antlr.lang.extend(, <@superClassName><@end>, {
     : ,}; separator="\n">
    }>
    getGrammarFileName: function() { return ""; }
});
org.antlr.lang.augmentObject(.prototype, {

    

    

    }; separator=",\n">
}, true); // important to pass true to overwrite default implementations

 
})();
>>

/** A override of Lexer.nextToken() that backtracks over mTokens() looking
 *  for matches.  No error can be generated upon error; just rewind, consume
 *  a token and then try again.  backtracking needs to be set as well.
 *  Make rule memoization happen only at levels above 1 as we start mTokens
 *  at backtracking==1.
 */
filteringNextToken() ::= <<
nextToken: function() {
    while (true) {
        if ( this.input.LA(1)==org.antlr.runtime.CharStream.EOF ) {
            return org.antlr.runtime.Token.EOF_TOKEN;
        }
        this.state.token = null;
        this.state.channel = org.antlr.runtime.Token.DEFAULT_CHANNEL;
        this.state.tokenStartCharIndex = this.input.index();
        this.state.tokenStartCharPositionInLine = this.input.getCharPositionInLine();
        this.state.tokenStartLine = this.input.getLine();
        this.state.text = null;
        try {
            var m = this.input.mark();
            this.state.backtracking=1; 
            this.state.failed=false;
            this.mTokens();
            this.state.backtracking=0;
            
            if ( this.state.failed ) {
                this.input.rewind(m);
                this.input.consume(); 
            }
            else {
                this.emit();
                return this.state.token;
            }
        }
        catch (re) {
            // shouldn't happen in backtracking mode, but...
            if (re instanceof org.antlr.runtime.RecognitionException) {
                this.reportError(re);
                this.recover(re);
            } else {
                throw re;
            }
        }
    }
},

memoize: function(input, ruleIndex, ruleStartIndex) {
    if (this.state.backtracking>1) {
        .superclass.prototype.memoize.call(this, input, ruleIndex, ruleStartIndex);
    }
},

alreadyParsedRule: function(input, ruleIndex) {
    if (this.state.backtracking>1) {
        return .superclass.prototype.alreadyParsedRule.call(this, input, ruleIndex);
    }
    return false;
},


>>

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

filteringActionGate() ::= "this.state.backtracking===1"

/** How to generate a parser */
genericParser(grammar, name, scopes, tokens, tokenNames, rules, numRules,
              bitsets, inputStreamType, superClass,
              ASTLabelType="Object", labelType, members, rewriteElementType) ::= <<

var  = function(input, state}>) {
    if (!state) {
        state = new org.antlr.runtime.RecognizerSharedState();
    }

    (function(){
        
    }).call(this);

    .superclass.constructor.call(this, input, state);

     = new .DFA(this);}; separator="\n">

        
         = new (input, state}>, this);}; separator="\n">
          = .;}; separator="\n">
         ;}>

    /* @todo only create adaptor if output=AST */
    this.adaptor = new org.antlr.runtime.tree.CommonTreeAdaptor();<\n>
};

org.antlr.lang.augmentObject(, {
    : }; separator=",\n">
});

(function(){
// public class variables
var = }; separator=",\n    ">;

var UP = org.antlr.runtime.Token.UP,
    DOWN = org.antlr.runtime.Token.DOWN;



// public instance methods/vars
org.antlr.lang.extend(, org.antlr.runtime.<@superClassName><@end>, {
    <@members>
    <@end>
    }>

    getTokenNames: function() { return .tokenNames; },
    getGrammarFileName: function() { return ""; }
});
org.antlr.lang.augmentObject(.prototype, {

    


    // Delegated rules
: function() \{ return this..(}; separator=", ">); \}}>



    }; separator=",\n">

}, true); // important to pass true to overwrite default implementations

 

// public class variables
org.antlr.lang.augmentObject(, {

    tokenNames: ["\", "\", "\", "\", ],<\n>

    _in_},
                    words64=it.bits); separator=",\n">
});

})();
>>

parserCtorBody() ::= <<


this.state.ruleMemo = {};<\n> 


 = ;}; separator="\n">
>>

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

>>

/** 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, labelType={}, ASTLabelType="var", superClass="tree.TreeParser", members={}, filterMode) ::= <<

>>

/** 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) ::=
<<
// $ANTLR start ""
_fragment: function() {

    this.traceIn("_fragment", );
    try {
        
    }
    finally {
        this.traceOut("_fragment", );
    }

    

},
// $ANTLR end ""
>>

synpred(name) ::= <<
: function() {
    this.state.backtracking++;
    <@start()>
    var start = this.input.mark();
    try {
        this._fragment(); // can never throw exception
    } catch (re) {
        alert("impossible: "+re.toString());
    }
    var success = !this.state.failed;
    this.input.rewind(start);
    <@stop()>
    this.state.backtracking--;
    this.state.failed=false;
    return success;
}
>>

lexerSynpred(name) ::= <<

>>

ruleMemoization(name) ::= <<

if ( this.state.backtracking>0 && this.alreadyParsedRule(this.input, ) ) { return ; }

>>

/** How to test for failure and return from rule */
checkRuleBacktrackFailure() ::= <<
if (this.state.failed) return ;
>>

/** This rule has failed, exit indicating failure during backtrack */
ruleBacktrackFailure() ::= <<
if (this.state.backtracking>0) {this.state.failed=true; return ;}
>>

/** 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) ::= <<



// :
// $ANTLR start ""

: function() {
    this.traceIn("", );
    
    
    
    
    <@preamble()>
    try {
        
        
        
        <(ruleDescriptor.actions.after):execAction()>
    }

    <\n>}>



    

    catch (re) {
        if (re instanceof org.antlr.runtime.RecognitionException) {
            this.reportError(re);
            this.recover(this.input,re);
            <@setErrorReturnValue()>
        } else {
            throw re;
        }
    }<\n>



    finally {
        this.traceOut("", );
        
        
        
    }
    <@postamble()>
    return ;
}
>>

catch(decl,action) ::= <<
catch () {
    
}
>>

ruleDeclarations() ::= <<

var retval = new ();
retval.start = this.input.LT(1);<\n>

 = null;
}>


var _StartIndex = this.input.index();

>>

ruleScopeSetUp() ::= <<
_stack.push({});}; separator="\n">
_stack.push({});}; separator="\n">
>>

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

ruleLabelDefs() ::= <<
<[ruleDescriptor.tokenLabels,ruleDescriptor.tokenListLabels]
    :{var  = null;}; separator="\n"
>
<[ruleDescriptor.tokenListLabels,ruleDescriptor.ruleListLabels]
    :{var list_=null;}; separator="\n"
>

 = null;}; separator="\n">
>>

lexerRuleLabelDefs() ::= <<
<[ruleDescriptor.tokenLabels,
  ruleDescriptor.tokenListLabels,
  ruleDescriptor.ruleLabels]
    :{var =null;}; separator="\n"
>
;}; separator="\n">
<[ruleDescriptor.tokenListLabels,
  ruleDescriptor.ruleListLabels,
  ruleDescriptor.ruleListLabels]
    :{var list_=null;}; separator="\n"
>
>>

ruleReturnValue() ::= <<





retval



>>

ruleCleanUp() ::= <<


retval.stop = this.input.LT(-1);<\n>


>>

memoize() ::= <<


if ( this.state.backtracking>0 ) { this.memoize(this.input, , _StartIndex); }


>>

/** How to generate a rule in the lexer; naked blocks are used for
 *  fragment rules.
 */
lexerRule(ruleName,nakedBlock,ruleDescriptor,block,memoize) ::= <<
// $ANTLR start 
m: function()  {
    this.traceIn("", );
    
    
    try {

        
        
        
        <\n>

        var _type = this.;
        var _channel = org.antlr.runtime.BaseRecognizer.DEFAULT_TOKEN_CHANNEL;
        
        
        
        
        
        this.state.type = _type;
        this.state.channel = _channel;
        <(ruleDescriptor.actions.after):execAction()>

    }
    finally {
        this.traceOut("", );
        
        
    }
},
// $ANTLR end ""
>>

/** How to generate code for the implicitly-defined lexer grammar rule
 *  that chooses between lexer rules.
 */
tokensRule(ruleName,nakedBlock,args,block,ruleDescriptor) ::= <<
mTokens: function() {
    <\n>
}
>>

// S U B R U L E S

/** A (...) subrule with multiple alternatives */
block(alts,decls,decision,enclosingBlockLevel,blockLevel,decisionNumber,maxK,maxAlt,description) ::= <<
// :
var alt=;

<@predecision()>

<@postdecision()>
<@prebranch()>
switch (alt) {
    
}
<@postbranch()>
>>

/** A rule block with multiple alternatives */
ruleBlock(alts,decls,decision,enclosingBlockLevel,blockLevel,decisionNumber,maxK,maxAlt,description) ::= <<
// :
var alt=;

<@predecision()>

<@postdecision()>
switch (alt) {
    
}
>>

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

<@prealt()>

<@postalt()>
>>

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

<@prealt()>

<@postalt()>
>>

/** A (..)+ block with 1 or more alternatives */
positiveClosureBlock(alts,decls,decision,enclosingBlockLevel,blockLevel,decisionNumber,maxK,maxAlt,description) ::= <<
// :
var cnt=0;

<@preloop()>
loop:
do {
    var alt=;
    <@predecision()>
    
    <@postdecision()>
    switch (alt) {
    
    default :
        if ( cnt >= 1 ) {
            break loop;
        }
        
            var eee = new org.antlr.runtime.EarlyExitException(, this.input);
            <@earlyExitException()>
            throw eee;
    }
    cnt++;
} while (true);
<@postloop()>
>>

positiveClosureBlockSingleAlt ::= positiveClosureBlock

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

<@preloop()>
loop:
do {
    var alt=;
    <@predecision()>
    
    <@postdecision()>
    switch (alt) {
    
    default :
        break loop;
    }
} while (true);
<@postloop()>
>>

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

/** A case in a switch that jumps to an alternative given the alternative
 *  number.  A DFA predicts the alternative and then a simple switch
 *  does the jump to the code that actually matches that alternative.
 */
altSwitchCase() ::= <<
case  :
    <@prealt()>
    
    break;<\n>
>>

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

<@declarations()>


<@cleanup()>

>>

/** What to emit when there is no rewrite.  For auto build
 *  mode, does nothing.
 */
noRewrite(rewriteBlockLevel, treeLevel) ::= ""

// E L E M E N T S

/** Dump the elements one per line */
element() ::= <<
<@prematch()>
<\n>
>>

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