org.antlr.codegen.templates.JavaScript.JavaScript.stg Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of antlr-complete Show documentation
Show all versions of antlr-complete Show documentation
Complete distribution for ANTLR 3
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) ::= <<
//