Skip to content
Merged
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
2 changes: 1 addition & 1 deletion doc/go-target.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ And the `generate.sh` file will look similar to this:

#!/bin/sh

alias antlr4='java -Xmx500M -cp "./antlr4-4.11.0-complete.jar:$CLASSPATH" org.antlr.v4.Tool'
alias antlr4='java -Xmx500M -cp "./antlr-4.11.1-complete.jar:$CLASSPATH" org.antlr.v4.Tool'
antlr4 -Dlanguage=Go -no-visitor -package parser *.g4
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -858,7 +858,7 @@ ListLabelName(label) ::= "_<label>"
CaptureNextToken(d) ::= "<d.varName> = TokenStream.LT(1);"
CaptureNextTokenType(d) ::= "<d.varName> = TokenStream.LA(1);"

StructDecl(struct,ctorAttrs,attrs,getters,dispatchMethods,interfaces,extensionMembers,
StructDecl(struct,ctorAttrs,attrs,getters,dispatchMethods,interfaces,extensionMembers,signatures,
superClass={ParserRuleContext}) ::= <<
public partial class <struct.escapedName> : <if(contextSuperClass)><contextSuperClass><else>ParserRuleContext<endif><if(interfaces)>, <interfaces; separator=", "><endif> {
<attrs:{a | public <a>;}; separator="\n">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -575,7 +575,7 @@ public:

>>

StructDecl(struct, ctorAttrs, attrs, getters, dispatchMethods, interfaces, extensionMembers) ::= <<
StructDecl(struct, ctorAttrs, attrs, getters, dispatchMethods, interfaces, extensionMembers, signatures) ::= <<
//----------------- <struct.escapedName> ------------------------------------------------------------------

<if (ctorAttrs)>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -681,7 +681,7 @@ ListLabelName(label) ::= "<label>"
CaptureNextToken(d) ::= "<d.varName> = tokenStream.LT(1);"
CaptureNextTokenType(d) ::= "<d.varName> = tokenStream.LA(1)!;"

StructDecl(struct,ctorAttrs,attrs,getters,dispatchMethods,interfaces,extensionMembers)
StructDecl(struct,ctorAttrs,attrs,getters,dispatchMethods,interfaces,extensionMembers,signatures)
::= <<
class <struct.escapedName> extends <if(contextSuperClass)><contextSuperClass><else>ParserRuleContext<endif><if(interfaces)> implements <interfaces; separator=", "><endif> {
<attrs:{a | <a>;}; separator="\n">
Expand Down
31 changes: 18 additions & 13 deletions tool/resources/org/antlr/v4/tool/templates/codegen/Go/Go.stg
Original file line number Diff line number Diff line change
Expand Up @@ -919,25 +919,25 @@ RuleContextListDecl(rdecl) ::= "<rdecl.escapedName> []I<rdecl.ctxName>"
AttributeDecl(d) ::= "<d.escapedName> <d.type><if(d.initValue)>// TODO = <d.initValue><endif>"

ContextTokenGetterDecl(t) ::= <<
<t.escapedName; format="cap">() antlr.TerminalNode {
<t.escapedName; format="cap">() antlr.TerminalNode<if(!t.signature)> {
return s.GetToken(<parser.name><t.escapedName>, 0)
}
}<endif>
>>

ContextTokenListGetterDecl(t) ::= <<
All<t.escapedName; format="cap">() []antlr.TerminalNode {
All<t.escapedName; format="cap">() []antlr.TerminalNode<if(!t.signature)> {
return s.GetTokens(<parser.name><t.escapedName>)
}
}<endif>
>>

ContextTokenListIndexedGetterDecl(t) ::= <<
<t.escapedName; format="cap">(i int) antlr.TerminalNode {
<t.escapedName; format="cap">(i int) antlr.TerminalNode<if(!t.signature)> {
return s.GetToken(<parser.name><t.escapedName>, i)
}
}<endif>
>>

ContextRuleGetterDecl(r) ::= <<
<r.escapedName; format="cap">() I<r.ctxName> {
<r.escapedName; format="cap">() I<r.ctxName><if(!r.signature)> {
var t antlr.RuleContext;
for _, ctx := range s.GetChildren() {
if _, ok := ctx.(I<r.ctxName>); ok {
Expand All @@ -951,11 +951,11 @@ ContextRuleGetterDecl(r) ::= <<
}

return t.(I<r.ctxName>)
}
}<endif>
>>

ContextRuleListGetterDecl(r) ::= <<
All<r.escapedName; format="cap">() []I<r.ctxName> {
All<r.escapedName; format="cap">() []I<r.ctxName><if(!r.signature)> {
children := s.GetChildren()
len := 0
for _, ctx := range children {
Expand All @@ -974,11 +974,11 @@ All<r.escapedName; format="cap">() []I<r.ctxName> {
}

return tst
}
}<endif>
>>

ContextRuleListIndexedGetterDecl(r) ::= <<
<r.escapedName; format="cap">(i int) I<r.ctxName> {
<r.escapedName; format="cap">(i int) I<r.ctxName><if(!r.signature)> {
var t antlr.RuleContext;
j := 0
for _, ctx := range s.GetChildren() {
Expand All @@ -996,7 +996,7 @@ ContextRuleListIndexedGetterDecl(r) ::= <<
}

return t.(I<r.ctxName>)
}
}<endif>
>>

LexerRuleContext() ::= "RuleContext"
Expand All @@ -1014,7 +1014,7 @@ ListLabelName(label) ::= "<label>"
CaptureNextToken(d) ::= "<d.varName> = p.GetTokenStream().LT(1)"
CaptureNextTokenType(d) ::= "<d.varName> = p.GetTokenStream().LA(1)"

StructDecl(struct, ctorAttrs, attrs, getters, dispatchMethods, interfaces, extensionMembers) ::= <<
StructDecl(struct, ctorAttrs, attrs, getters, dispatchMethods, interfaces, extensionMembers, signatures) ::= <<
// I<struct.escapedName> is an interface to support dynamic dispatch.
type I<struct.escapedName> interface {
antlr.ParserRuleContext
Expand Down Expand Up @@ -1093,6 +1093,11 @@ Get<a.escapedName; format="cap">() <a.type>}; separator="\n\n">
Set<a.escapedName; format="cap">(<a.type>)}; separator="\n\n">
<endif>

<if(signatures)>

// Getter signatures
<signatures:{s | <s>}; separator="\n">
<endif>

// Is<struct.escapedName> differentiates from other interfaces.
Is<struct.escapedName>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -782,7 +782,7 @@ ListLabelName(label) ::= "<label>"
CaptureNextToken(d) ::= "<d.varName> = _input.LT(1);"
CaptureNextTokenType(d) ::= "<d.varName> = _input.LA(1);"

StructDecl(struct,ctorAttrs,attrs,getters,dispatchMethods,interfaces,extensionMembers)
StructDecl(struct,ctorAttrs,attrs,getters,dispatchMethods,interfaces,extensionMembers,signatures)
::= <<
@SuppressWarnings("CheckReturnValue")
public static class <struct.escapedName> extends <if(contextSuperClass)><contextSuperClass><else>ParserRuleContext<endif><if(interfaces)> implements <interfaces; separator=", "><endif> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -657,7 +657,7 @@ ListLabelName(label) ::= "<label>"
CaptureNextToken(d) ::= "<d.varName> = self._input.LT(1)"
CaptureNextTokenType(d) ::= "<d.varName> = this._input.LA(1);"

StructDecl(struct,ctorAttrs,attrs,getters,dispatchMethods,interfaces,extensionMembers) ::= <<
StructDecl(struct,ctorAttrs,attrs,getters,dispatchMethods,interfaces,extensionMembers,signatures) ::= <<
class <struct.escapedName> extends <if(contextSuperClass)><contextSuperClass><else>antlr4.ParserRuleContext<endif> {

constructor(parser, parent, invokingState<struct.ctorAttrs:{a | , <a.escapedName>}>) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -923,7 +923,7 @@ ListLabelName(label) ::= "<label>"
CaptureNextToken(d) ::= "$<d.varName> = \$this->input->LT(1);"
CaptureNextTokenType(d) ::= "$<d.varName> = $this->input->LA(1);"

StructDecl(struct,ctorAttrs,attrs,getters,dispatchMethods,interfaces,extensionMembers) ::= <<
StructDecl(struct,ctorAttrs,attrs,getters,dispatchMethods,interfaces,extensionMembers,signatures) ::= <<
class <struct.name> extends <if(contextSuperClass)><contextSuperClass><else>ParserRuleContext<endif><if(interfaces)> implements <interfaces; separator=", "><endif>
{
<PropertiesDecl(struct)>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -637,7 +637,7 @@ ListLabelName(label) ::= "<label>"
CaptureNextToken(d) ::= "<d.varName> = self._input.LT(1)"
CaptureNextTokenType(d) ::= "<d.varName> = self._input.LA(1)"

StructDecl(struct,ctorAttrs,attrs,getters,dispatchMethods,interfaces,extensionMembers) ::= <<
StructDecl(struct,ctorAttrs,attrs,getters,dispatchMethods,interfaces,extensionMembers,signatures) ::= <<
class <struct.escapedName>(<if(contextSuperClass)><contextSuperClass><else>ParserRuleContext<endif>):

def __init__(self, parser, parent=None, invokingState=-1<struct.ctorAttrs:{a | , <a.escapedName>=None}>):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -650,7 +650,7 @@ ListLabelName(label) ::= "<label>"
CaptureNextToken(d) ::= "<d.varName> = self._input.LT(1)"
CaptureNextTokenType(d) ::= "<d.varName> = self._input.LA(1)"

StructDecl(struct,ctorAttrs,attrs,getters,dispatchMethods,interfaces,extensionMembers) ::= <<
StructDecl(struct,ctorAttrs,attrs,getters,dispatchMethods,interfaces,extensionMembers,signatures) ::= <<
class <struct.escapedName>(<if(contextSuperClass)><contextSuperClass><else>ParserRuleContext<endif>):
__slots__ = 'parser'

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -812,7 +812,7 @@ ListLabelName(label) ::= "<label>"
CaptureNextToken(d) ::= "<d.varName> = try _input.LT(1)"
CaptureNextTokenType(d) ::= "<d.varName> = try _input.LA(1)"

StructDecl(struct,ctorAttrs,attrs,getters,dispatchMethods,interfaces,extensionMembers,
StructDecl(struct,ctorAttrs,attrs,getters,dispatchMethods,interfaces,extensionMembers,signatures,
superClass={ParserRuleContext}) ::= <<

<accessLevelNotOpen(parser)> class <struct.escapedName>: <if(contextSuperClass)><contextSuperClass><else>ParserRuleContext<endif><if(interfaces)>, <interfaces; separator=", "><endif> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,23 @@

public abstract class ContextGetterDecl extends Decl {
public ContextGetterDecl(OutputModelFactory factory, String name) {
this(factory, name, false);
}

public ContextGetterDecl(OutputModelFactory factory, String name, boolean signature) {
super(factory, name);
this.signature = signature;
}

/** Not used for output; just used to distinguish between decl types
* to avoid dups.
*/
public String getArgType() { return ""; }; // assume no args

private boolean signature = false;
Copy link
Member

@KvanTTT KvanTTT Oct 31, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I understand correctly, it should be marked with final.

public boolean getSignature() { return signature; }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to use getter method if signature is final.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call: #3947

abstract ContextGetterDecl getSignatureDecl();

@Override
public int hashCode() {
int hash = MurmurHash.initialize();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,17 @@ public class ContextRuleGetterDecl extends ContextGetterDecl {
public boolean optional;

public ContextRuleGetterDecl(OutputModelFactory factory, String name, String ctxName, boolean optional) {
super(factory, name);
this(factory, name, ctxName, optional, false);
}

public ContextRuleGetterDecl(OutputModelFactory factory, String name, String ctxName, boolean optional, boolean signature) {
super(factory, name, signature);
this.ctxName = ctxName;
this.optional = optional;
}

@Override
public ContextGetterDecl getSignatureDecl() {
return new ContextRuleGetterDecl(factory, name, ctxName, optional, true);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,16 @@
public class ContextRuleListGetterDecl extends ContextGetterDecl {
public String ctxName;
public ContextRuleListGetterDecl(OutputModelFactory factory, String name, String ctxName) {
super(factory, name);
this(factory, name, ctxName, false);
}

public ContextRuleListGetterDecl(OutputModelFactory factory, String name, String ctxName, boolean signature) {
super(factory, name, signature);
this.ctxName = ctxName;
}

@Override
public ContextGetterDecl getSignatureDecl() {
return new ContextRuleListGetterDecl(factory, name, ctxName, true);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,20 @@

public class ContextRuleListIndexedGetterDecl extends ContextRuleListGetterDecl {
public ContextRuleListIndexedGetterDecl(OutputModelFactory factory, String name, String ctxName) {
super(factory, name, ctxName);
this(factory, name, ctxName, false);
}

public ContextRuleListIndexedGetterDecl(OutputModelFactory factory, String name, String ctxName, boolean signature) {
super(factory, name, ctxName, signature);
}

@Override
public String getArgType() {
return "int";
}

@Override
public ContextGetterDecl getSignatureDecl() {
return new ContextRuleListIndexedGetterDecl(factory, name, ctxName, true);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,16 @@ public class ContextTokenGetterDecl extends ContextGetterDecl {
public boolean optional;

public ContextTokenGetterDecl(OutputModelFactory factory, String name, boolean optional) {
super(factory, name);
this(factory, name, optional, false);
}

public ContextTokenGetterDecl(OutputModelFactory factory, String name, boolean optional, boolean signature) {
super(factory, name, signature);
this.optional = optional;
}

@Override
public ContextGetterDecl getSignatureDecl() {
return new ContextTokenGetterDecl(factory, name, optional, true);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,15 @@
*/
public class ContextTokenListGetterDecl extends ContextGetterDecl {
public ContextTokenListGetterDecl(OutputModelFactory factory, String name) {
super(factory, name);
this(factory, name, false);
}

public ContextTokenListGetterDecl(OutputModelFactory factory, String name, boolean signature) {
super(factory, name, signature);
}

@Override
public ContextGetterDecl getSignatureDecl() {
return new ContextTokenListGetterDecl(factory, name, true);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,20 @@

public class ContextTokenListIndexedGetterDecl extends ContextTokenListGetterDecl {
public ContextTokenListIndexedGetterDecl(OutputModelFactory factory, String name) {
super(factory, name);
this(factory, name, false);
}

public ContextTokenListIndexedGetterDecl(OutputModelFactory factory, String name, boolean signature) {
super(factory, name, signature);
}

@Override
public String getArgType() {
return "int";
}

@Override
public ContextGetterDecl getSignatureDecl() {
return new ContextTokenListIndexedGetterDecl(factory, name, true);
}
}
7 changes: 6 additions & 1 deletion tool/src/org/antlr/v4/codegen/model/decl/StructDecl.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ public class StructDecl extends Decl {
@ModelElement public List<? super DispatchMethod> dispatchMethods;
@ModelElement public List<OutputModelObject> interfaces;
@ModelElement public List<OutputModelObject> extensionMembers;
// Used to generate method signatures in Go target interfaces
@ModelElement public OrderedHashSet<Decl> signatures = new OrderedHashSet<Decl>();

// Track these separately; Go target needs to generate getters/setters
// Do not make them templates; we only need the Decl object not the ST
Expand Down Expand Up @@ -71,7 +73,10 @@ public void addDispatchMethods(Rule r) {
public void addDecl(Decl d) {
d.ctx = this;

if ( d instanceof ContextGetterDecl ) getters.add(d);
if ( d instanceof ContextGetterDecl ) {
getters.add(d);
signatures.add(((ContextGetterDecl) d).getSignatureDecl());
}
else attrs.add(d);

// add to specific "lists"
Expand Down