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

[Go] Thread-safe ANTLR codegen #2816

Merged
merged 2 commits into from
May 3, 2020
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
3 changes: 2 additions & 1 deletion contributors.txt
Original file line number Diff line number Diff line change
Expand Up @@ -241,4 +241,5 @@ YYYY/MM/DD, github id, Full name, email
2020/02/10, julibert, Julián Bermúdez Ortega, julibert.dev@gmail.com
2020/02/21, StochasticTinkr, Daniel Pitts, github@coloraura.com
2020/03/17, XsongyangX, Song Yang, songyang1218@gmail.com
2020/04/07, deniskyashif, Denis Kyashif, denis.kyashif@gmail.com
2020/04/07, deniskyashif, Denis Kyashif, denis.kyashif@gmail.com
2020/04/30, TristonianJones, Tristan Swadell, tswadell@google.com
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ PositionAdjustingLexerDef() ::= ""
PositionAdjustingLexer() ::= <<
func (p *PositionAdjustingLexer) NextToken() antlr.Token {
if _, ok := p.Interpreter.(*PositionAdjustingLexerATNSimulator); !ok {
lexerDeserializer := antlr.NewATNDeserializer(nil)
lexerAtn := lexerDeserializer.DeserializeFromUInt16(serializedLexerAtn)
p.Interpreter = NewPositionAdjustingLexerATNSimulator(p, lexerAtn, p.Interpreter.DecisionToDFA(), p.Interpreter.SharedContextCache())
p.Virt = p
}
Expand Down
3 changes: 2 additions & 1 deletion runtime/Go/antlr/lexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,8 @@ func (b *BaseLexer) inputStream() CharStream {
return b.input
}

func (b *BaseLexer) setInputStream(input CharStream) {
// SetInputStream resets the lexer input stream and associated lexer state.
func (b *BaseLexer) SetInputStream(input CharStream) {
b.input = nil
b.tokenFactorySourcePair = &TokenSourceCharStreamPair{b, b.input}
b.reset()
Expand Down
49 changes: 24 additions & 25 deletions tool/resources/org/antlr/v4/tool/templates/codegen/Go/Go.stg
Original file line number Diff line number Diff line change
Expand Up @@ -151,9 +151,6 @@ var parserATN = <atn>
var parserATN []uint16
<endif>

var deserializer = antlr.NewATNDeserializer(nil)
var deserializedATN = deserializer.DeserializeFromUInt16(parserATN)

<if(parser.literalNames)>
var literalNames = []string{
<parser.literalNames; null="\"\"", separator=", ", wrap>,
Expand All @@ -179,21 +176,24 @@ var ruleNames = []string{
var ruleNames []string
<endif>

var decisionToDFA = make([]*antlr.DFA, len(deserializedATN.DecisionToState))

func init() {
for index, ds := range deserializedATN.DecisionToState {
decisionToDFA[index] = antlr.NewDFA(ds, index)
}
}

type <parser.name> struct {
<superClass; null="*antlr.BaseParser">
}

// New<parser.name> produces a new parser instance for the optional input antlr.TokenStream.
//
// The *<parser.name> instance produced may be reused by calling the SetInputStream method.
// The initial parser configuration is expensive to construct, and the object is not thread-safe;
// however, if used within a Golang sync.Pool, the construction cost amortizes well and the
// objects can be used in a thread-safe manner.
func New<parser.name>(input antlr.TokenStream) *<parser.name> {
this := new(<parser.name>)

deserializer := antlr.NewATNDeserializer(nil)
deserializedATN := deserializer.DeserializeFromUInt16(parserATN)
decisionToDFA := make([]*antlr.DFA, len(deserializedATN.DecisionToState))
for index, ds := range deserializedATN.DecisionToState {
decisionToDFA[index] = antlr.NewDFA(ds, index)
}
this.BaseParser = antlr.NewBaseParser(input)

this.Interpreter = antlr.NewParserATNSimulator(this, deserializedATN, decisionToDFA, antlr.NewPredictionContextCache())
Expand All @@ -204,6 +204,7 @@ func New<parser.name>(input antlr.TokenStream) *<parser.name> {

return this
}

<if(namedActions.members)>

<namedActions.members>
Expand Down Expand Up @@ -1375,9 +1376,6 @@ var serializedLexerAtn []uint16
<endif>


var lexerDeserializer = antlr.NewATNDeserializer(nil)
var lexerAtn = lexerDeserializer.DeserializeFromUInt16(serializedLexerAtn)

var lexerChannelNames = []string{
"DEFAULT_TOKEN_CHANNEL", "HIDDEN"<if (lexer.channels)>, <lexer.channels:{c | "<c>"}; separator=", ", wrap><endif>,
}
Expand Down Expand Up @@ -1412,26 +1410,27 @@ var lexerRuleNames = []string{
var lexerRuleNames []string
<endif>


type <lexer.name> struct {
*<if(superClass)><superClass><else>antlr.BaseLexer<endif>
channelNames []string
modeNames []string
// TODO: EOF string
}

var lexerDecisionToDFA = make([]*antlr.DFA, len(lexerAtn.DecisionToState))

func init() {
// New<lexer.name> produces a new lexer instance for the optional input antlr.CharStream.
//
// The *<lexer.name> instance produced may be reused by calling the SetInputStream method.
// The initial lexer configuration is expensive to construct, and the object is not thread-safe;
// however, if used within a Golang sync.Pool, the construction cost amortizes well and the
// objects can be used in a thread-safe manner.
func New<lexer.name>(input antlr.CharStream) *<lexer.name> {
l := new(<lexer.name>)
lexerDeserializer := antlr.NewATNDeserializer(nil)
lexerAtn := lexerDeserializer.DeserializeFromUInt16(serializedLexerAtn)
lexerDecisionToDFA := make([]*antlr.DFA, len(lexerAtn.DecisionToState))
for index, ds := range lexerAtn.DecisionToState {
lexerDecisionToDFA[index] = antlr.NewDFA(ds, index)
}
}

func New<lexer.name>(input antlr.CharStream) *<lexer.name> {

l := new(<lexer.name>)

l.BaseLexer = antlr.NewBaseLexer(input)
l.Interpreter = antlr.NewLexerATNSimulator(l, lexerAtn, lexerDecisionToDFA, antlr.NewPredictionContextCache())

Expand Down