Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update the JavaScript codegen target #197

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions runtime/JavaScript/build/README
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,8 @@ party open source libraries:
* Unzip the downloaded file.
* Move the unzipped folder to runtime/JavaScript/third/jsdoc-toolkit

* jsunit: Download here: https://sourceforge.net/project/showfiles.php?group_id=28041&package_id=19823&release_id=404277
* Unzip the downloaded file.
* Move the unzipped folder to runtime/JavaScript/tests/jsunit
* jsunit:
* cd runtime/JavaScript/tests && git clone https://github.com/pivotal-legacy/jsunit.git

### BUILD ###
1) In a shell cd to runtime/JavaScript/build.
Expand All @@ -32,7 +31,7 @@ party open source libraries:
Compile Tests:
1) Build the Antlr tool itself (see main Antlr documentation for details).
This is most easily done by cd'ing to the root of the antlr code and running
'mvn -Dmaven.test.skip=true package assembly:assembly'. If using a
'mvn -Dmaven.test.skip=true package'. If using a
different build method, update runtime/JavaScript/build/antlr3.properties to
point to the alternative jar.
2) In a shell cd to runtime/JavaScript/build.
Expand Down
2 changes: 1 addition & 1 deletion runtime/JavaScript/build/antlr3.properties
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,4 @@ antlr3.cli.list=\
org/antlr/runtime/ANTLRFileStream.js

# path to antlr tool jar
antlr3.tool=../../target/antlr-master-3.2.1-SNAPSHOT-completejar.jar
antlr3.tool=../../antlr-complete/target/antlr-complete-3.5.3-SNAPSHOT.jar
4 changes: 2 additions & 2 deletions runtime/JavaScript/tests/functional/t009lexer.html
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@
token = lexer.nextToken();
fail("nextToken should have thrown error on invalid input");
} catch (e) {
assertEquals(e.a, '0');
assertEquals(e.b, '9');
/* TODO: This shoud provide more useful information */
assertEquals(e.expecting, null);
assertEquals(e.getUnexpectedType(), 'a');
assertEquals(e.charPositionInLine, 1);
assertEquals(e.line, 1);
Expand Down
8 changes: 4 additions & 4 deletions runtime/JavaScript/tests/functional/t044trace.html
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,10 @@

parser.a();
assertArrayEquals(lexer.traces,
[ '>T__6', '<T__6', '>WS', '<WS', '>INT', '<INT', '>WS', '<WS',
'>T__8', '<T__8', '>WS', '<WS', '>INT', '<INT', '>WS', '<WS',
'>T__8', '<T__8', '>WS', '<WS', '>INT', '<INT', '>WS', '<WS',
'>T__7', '<T__7']
[ '>T__7', '<T__7', '>WS', '<WS', '>INT', '<INT', '>WS', '<WS',
'>T__6', '<T__6', '>WS', '<WS', '>INT', '<INT', '>WS', '<WS',
'>T__6', '<T__6', '>WS', '<WS', '>INT', '<INT', '>WS', '<WS',
'>T__8', '<T__8']
);
assertArrayEquals(parser.traces,
[ '>a', '>synpred1_t044trace_fragment', '<synpred1_t044trace_fragment', '>b', '>c',
Expand Down
101 changes: 53 additions & 48 deletions tool/src/main/resources/org/antlr/codegen/templates/JavaScript/AST.stg
Original file line number Diff line number Diff line change
Expand Up @@ -25,23 +25,32 @@
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
group AST;

finishedBacktracking(block) ::= <<
<if(backtracking)>
if( <actions.(actionScope).synpredgate> ) {
<block>
}
<else>
<block>
<endif>
>>

@outputFile.imports() ::= <<
<@super.imports()>
>>

@genericParser.members() ::= <<
<@super.members()>
<parserMembers()>
<astAccessor()>
>>

/** Add an adaptor property that knows how to build trees */
parserMembers() ::= <<
astAccessor() ::= <<
<!protected TreeAdaptor adaptor = new CommonTreeAdaptor();<\n>!>
setTreeAdaptor: function(adaptor) {
this.adaptor = adaptor;
<grammar.directDelegates:{g|<g:delegateName()>.setTreeAdaptor(this.adaptor);}>
<grammar.directDelegates:{g|<g:delegateName()>.setTreeAdaptor(this.adaptor);}; separator="\n">
},
getTreeAdaptor: function() {
return this.adaptor;
Expand All @@ -60,12 +69,14 @@ var root_0 = null;<\n>

ruleLabelDefs() ::= <<
<super.ruleLabelDefs()>
<ruleDescriptor.tokenLabels:{var <it.label.text>_tree=null;}; separator="\n">
<ruleDescriptor.tokenListLabels:{var <it.label.text>_tree=null;}; separator="\n">
<[ruleDescriptor.tokenLabels,ruleDescriptor.wildcardTreeLabels,
ruleDescriptor.wildcardTreeListLabels]
:{it | var <it.label.text>_tree=null;}; separator="\n">
<ruleDescriptor.tokenListLabels:{it | var <it.label.text>_tree=null;}; separator="\n">
<ruleDescriptor.allTokenRefsInAltsWithRewrites
:{var stream_<it>=new org.antlr.runtime.tree.RewriteRuleTokenStream(this.adaptor,"token <it>");}; separator="\n">
:{it | var stream_<it>=new org.antlr.runtime.tree.RewriteRuleTokenStream(this.adaptor,"token <it>");}; separator="\n">
<ruleDescriptor.allRuleRefsInAltsWithRewrites
:{var stream_<it>=new org.antlr.runtime.tree.RewriteRuleSubtreeStream(this.adaptor,"rule <it>");}; separator="\n">
:{it | var stream_<it>=new org.antlr.runtime.tree.RewriteRuleSubtreeStream(this.adaptor,"rule <it>");}; separator="\n">
>>

/** When doing auto AST construction, we must define some variables;
Expand All @@ -86,57 +97,53 @@ root_0 = this.adaptor.nil();<\n>
// T r a c k i n g R u l e E l e m e n t s

/** ID and track it for use in a rewrite rule */
tokenRefTrack(token,label,elementIndex,terminalOptions) ::= <<
tokenRefTrack(token,label,elementIndex,terminalOptions={}) ::= <<
<tokenRefBang(...)> <! Track implies no auto AST construction!>
<if(backtracking)>if ( <actions.(actionScope).synpredgate> ) <endif>stream_<token>.add(<label>);<\n>
<finishedBacktracking({stream_<token>.add(<label>);})>
>>

/** ids+=ID and track it for use in a rewrite rule; adds to ids *and*
* to the tracking list stream_ID for use in the rewrite.
*/
tokenRefTrackAndListLabel(token,label,elementIndex,terminalOptions) ::= <<
tokenRefTrackAndListLabel(token,label,elementIndex,terminalOptions={}) ::= <<
<tokenRefTrack(...)>
<listLabel(elem=label,...)>
>>

/** ^(ID ...) track for rewrite */
tokenRefRuleRootTrack(token,label,elementIndex,terminalOptions) ::= <<
tokenRefRuleRootTrack(token,label,elementIndex,terminalOptions={}) ::= <<
<tokenRefBang(...)>
<if(backtracking)>if ( <actions.(actionScope).synpredgate> ) <endif>stream_<token>.add(<label>);<\n>
<finishedBacktracaking({stream_<token>.add(<label>);})>
>>

/** Match ^(label+=TOKEN ...) track for rewrite */
tokenRefRuleRootTrackAndListLabel(token,label,elementIndex,terminalOptions) ::= <<
tokenRefRuleRootTrackAndListLabel(token,label,elementIndex,terminalOptions={}) ::= <<
<tokenRefRuleRootTrack(...)>
<listLabel(elem=label,...)>
>>

wildcardTrack(label,elementIndex) ::= <<
<super.wildcard(...)>
>>

/** rule when output=AST and tracking for rewrite */
ruleRefTrack(rule,label,elementIndex,args,scope) ::= <<
<super.ruleRef(...)>
<if(backtracking)>if ( <actions.(actionScope).synpredgate> ) <endif>stream_<rule.name>.add(<label>.getTree());
<finishedBacktracking({stream_<rule.name>.add(<label>.getTree());})>
>>

/** x+=rule when output=AST and tracking for rewrite */
ruleRefTrackAndListLabel(rule,label,elementIndex,args,scope) ::= <<
<ruleRefTrack(...)>
<listLabel(elem=label+".getTree()",...)>
<listLabel(label, {<label>.getTree()})>
>>

/** ^(rule ...) rewrite */
ruleRefRuleRootTrack(rule,label,elementIndex,args,scope) ::= <<
<ruleRefRuleRoot(...)>
<if(backtracking)>if ( <actions.(actionScope).synpredgate> ) <endif>stream_<rule>.add(<label>.getTree());
<finishedBacktracking({stream_<rule>.add(<label>.getTree());})>
>>

/** ^(x+=rule ...) rewrite */
ruleRefRuleRootTrackAndListLabel(rule,label,elementIndex,args,scope) ::= <<
<ruleRefRuleRootTrack(...)>
<listLabel(elem=label+".getTree()",...)>
<listLabel(label, {<label>.getTree()})>
>>

// R e w r i t e
Expand All @@ -148,8 +155,8 @@ rewriteCode(
referencedTokenListLabels,
referencedRuleLabels,
referencedRuleListLabels,
referencedWildcardLabels,
referencedWildcardListLabels,
referencedWildcardLabels,
referencedWildcardListLabels,
rewriteBlockLevel, enclosingTreeLevel, treeLevel) ::=
<<

Expand All @@ -159,9 +166,7 @@ rewriteCode(
// rule labels: <referencedRuleLabels; separator=", ">
// token list labels: <referencedTokenListLabels; separator=", ">
// rule list labels: <referencedRuleListLabels; separator=", ">
<if(backtracking)>
if ( <actions.(actionScope).synpredgate> ) {<\n>
<endif>
<finishedBacktracking({
<prevRuleRootRef()>.tree = root_0;
<rewriteCodeLabels()>
root_0 = this.adaptor.nil();
Expand All @@ -173,9 +178,10 @@ root_0 = this.adaptor.nil();
this.input.replaceChildren(this.adaptor.getParent(retval.start),
this.adaptor.getChildIndex(retval.start),
this.adaptor.getChildIndex(_last),
retval.tree);
retval.tree);<\n>
<endif>
<endif>

<! if parser or tree-parser && rewrite!=true, we need to set result !>
<if(!TREE_PARSER)>
<prevRuleRootRef()>.tree = root_0;
Expand All @@ -184,34 +190,32 @@ this.input.replaceChildren(this.adaptor.getParent(retval.start),
<prevRuleRootRef()>.tree = root_0;
<endif>
<endif>
<if(backtracking)>
}
<endif>
})>
>>

rewriteCodeLabels() ::= <<
<referencedTokenLabels
:{var stream_<it>=new org.antlr.runtime.tree.RewriteRule<rewriteElementType>Stream(this.adaptor,"token <it>",<it>);};
:{it | var stream_<it>=new org.antlr.runtime.tree.RewriteRule<rewriteElementType>Stream(this.adaptor,"token <it>",<it>);};
separator="\n"
>
<referencedTokenListLabels
:{var stream_<it>=new org.antlr.runtime.tree.RewriteRule<rewriteElementType>Stream(this.adaptor,"token <it>", list_<it>);};
:{it | var stream_<it>=new org.antlr.runtime.tree.RewriteRule<rewriteElementType>Stream(this.adaptor,"token <it>", list_<it>);};
separator="\n"
>
<referencedWildcardLabels
:{var stream_<it>=new org.antlr.runtime.tree.RewriteRuleSubtreeStream(this.adaptor,"wildcard <it>",<it>);};
:{it | var stream_<it>=new org.antlr.runtime.tree.RewriteRuleSubtreeStream(this.adaptor,"wildcard <it>",<it>);};
separator="\n"
>
<referencedWildcardListLabels
:{var stream_<it>=new org.antlr.runtime.tree.RewriteRuleSubtreeStream(this.adaptor,"wildcard <it>",list_<it>);};
:{it | var stream_<it>=new org.antlr.runtime.tree.RewriteRuleSubtreeStream(this.adaptor,"wildcard <it>",list_<it>);};
separator="\n"
>
<referencedRuleLabels
:{var stream_<it>=new org.antlr.runtime.tree.RewriteRuleSubtreeStream(this.adaptor,"token <it>",<it>!=null?<it>.tree:null);};
:{it | var stream_<it>=new org.antlr.runtime.tree.RewriteRuleSubtreeStream(this.adaptor,"token <it>",<it>!=null?<it>.tree:null);};
separator="\n"
>
<referencedRuleListLabels
:{var stream_<it>=new org.antlr.runtime.tree.RewriteRuleSubtreeStream(this.adaptor,"token <it>",list_<it>);};
:{it | var stream_<it>=new org.antlr.runtime.tree.RewriteRuleSubtreeStream(this.adaptor,"token <it>",list_<it>);};
separator="\n"
>
>>
Expand Down Expand Up @@ -261,13 +265,14 @@ while ( <referencedElements:{el | stream_<el>.hasNext()}; separator="||"> ) {
>>

rewriteAlt(a) ::= <<
// <a.description>
<if(a.pred)>
if (<a.pred>) {
// a.description
<a.alt>
}<\n>
<else>
{
// a.description
<a.alt>
}<\n>
<endif>
Expand All @@ -294,7 +299,7 @@ rewriteElement(e) ::= <<
>>

/** Gen ID or ID[args] */
rewriteTokenRef(token,elementIndex,terminalOptions,args) ::= <<
rewriteTokenRef(token,elementIndex,args,terminalOptions={}) ::= <<
this.adaptor.addChild(root_<treeLevel>, <createRewriteNodeFromElement(...)>);<\n>
>>

Expand All @@ -317,16 +322,16 @@ root_<treeLevel> = this.adaptor.becomeRoot(stream_<label>.nextNode(), root_<tree
rewriteTokenListLabelRefRoot ::= rewriteTokenLabelRefRoot

/** Gen ^(ID ...) or ^(ID[args] ...) */
rewriteTokenRefRoot(token,elementIndex,terminalOptions,args) ::= <<
rewriteTokenRefRoot(token,elementIndex,args,terminalOptions={}) ::= <<
root_<treeLevel> = this.adaptor.becomeRoot(<createRewriteNodeFromElement(...)>, root_<treeLevel>);<\n>
>>

rewriteImaginaryTokenRef(args,token,terminalOptions,elementIndex) ::= <<
this.adaptor.addChild(root_<treeLevel>, <createImaginaryNode(tokenType=token, ...)>);<\n>
rewriteImaginaryTokenRef(args,token,elementIndex,terminalOptions={}) ::= <<
this.adaptor.addChild(root_<treeLevel>, <createImaginaryNode(token, args, terminalOptions)>);<\n>
>>

rewriteImaginaryTokenRefRoot(args,token,terminalOptions,elementIndex) ::= <<
root_<treeLevel> = this.adaptor.becomeRoot(<createImaginaryNode(tokenType=token, ...)>, root_<treeLevel>);<\n>
rewriteImaginaryTokenRefRoot(args,token,elementIndex,terminalOptions={}) ::= <<
root_<treeLevel> = this.adaptor.becomeRoot(<createImaginaryNode(token, args, terminalOptions)>, root_<treeLevel>);<\n>
>>

/** plain -> {foo} action */
Expand Down Expand Up @@ -381,16 +386,16 @@ rewriteWildcardLabelRef(label) ::= <<
this.adaptor.addChild(root_<treeLevel>, stream_<label>.nextTree());<\n>
>>

createImaginaryNode(tokenType,terminalOptions,args) ::= <<
createImaginaryNode(tokenType,args,terminalOptions={}) ::= <%
<if(terminalOptions.node)>
<! new MethodNode(IDLabel, args) !>
new <terminalOptions.node>(<tokenType><if(args)>, <args; separator=", "><endif>)
<else>
this.adaptor.create(<tokenType>, <args; separator=", "><if(!args)>"<tokenType>"<endif>)
<endif>
>>
%>

createRewriteNodeFromElement(token,terminalOptions,args) ::= <<
createRewriteNodeFromElement(token,args,terminalOptions={}) ::= <%
<if(terminalOptions.node)>
new <terminalOptions.node>(stream_<token>.nextToken()<if(args)>, <args; separator=", "><endif>)
<else>
Expand All @@ -400,4 +405,4 @@ this.adaptor.create(<token>, <args; separator=", ">)
stream_<token>.nextNode()
<endif>
<endif>
>>
%>
Loading