Skip to content

Commit 95fd266

Browse files
Jim Idlesharknoonjosufraericvergnaud
authored
Feature/docclean Greatly improve the godoc comments in the runtime (#4169)
* doc: Updates to some of the Go doc comments to start a ful ldocumentation cleanup Signed-off-by: Jim.Idle <[email protected]> * doc: More documentation fixes. Using this as a method of forcing myself to read every line of code in the runtime, and therefore discover mistakes in the original implementation. And, of course, actually working docs for the Go runtime, can only be a good thing. Signed-off-by: Jim.Idle <[email protected]> * doc: More documentation fixes Also changes the exporet level of a some variables and funcs that were not correct, even though no user has currently needed them it would seem. Signed-off-by: Jim.Idle <[email protected]> * doc: Many updates to document exported fuctions correctly and reformat the ingerited Java code It looks like a massive amount of changes, but it is almost all doc or changing exports or renaming unused paramters etc to make the Go linter happy. No actual code changes yet. Signed-off-by: Jim.Idle <[email protected]> * doc: More additions and corrections to the Go documentation for the runtime Signed-off-by: Jim.Idle <[email protected]> * doc: Final clean of exported func and type documentation There will be more to do here as there are a lot of things that are hidden internal to the antlr package that probably should not be. There are also a lot of exported funcs and types without any documentation, that will eventually need to be cleaned up. Signed-off-by: Jim.Idle <[email protected]> * Changed Parser typings (#4149) Signed-off-by: Josua Frank <[email protected]> Co-authored-by: Josua Frank <[email protected]> Signed-off-by: Jim.Idle <[email protected]> * fix: Fixes the failing go runtime test suite which was missing the /v4 off the replace option on the go.mod file (#4163) Arrrgh! Signed-off-by: Jim.Idle <[email protected]> * present antlr before versioning (#4156) Signed-off-by: Jim.Idle <[email protected]> * fix: Prevent use of labels such as start= from generating code that clashes with builtin funcs (#4161) Signed-off-by: Jim.Idle <[email protected]> * Feature/gotestfix (#4168) * fix: Fixes the failing go runtime test suite which was missing the /v4 off the replace option on the go.mod file Arrrgh! Signed-off-by: Jim.Idle <[email protected]> * present antlr before versioning (#4156) Signed-off-by: Jim.Idle <[email protected]> * fix: Prevent use of labels such as start= from generating code that clashes with builtin funcs (#4161) Signed-off-by: Jim.Idle <[email protected]> * fix: Cater for the fact that some test rules use start as a label or rule name As a fix for other cvode gen errors when start, end, or exception are used as label names, they are now translated to have a suffix of `_` at code gen time. However, the runtime tests sometimes use start as a rule name and so we must now cater for this in the tests. Signed-off-by: Jim.Idle <[email protected]> --------- Signed-off-by: Jim.Idle <[email protected]> Co-authored-by: ericvergnaud <[email protected]> Signed-off-by: Jim.Idle <[email protected]> --------- Signed-off-by: Jim.Idle <[email protected]> Signed-off-by: Josua Frank <[email protected]> Co-authored-by: Josua Frank <[email protected]> Co-authored-by: Josua Frank <[email protected]> Co-authored-by: ericvergnaud <[email protected]>
1 parent c0f90d8 commit 95fd266

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+1393
-1304
lines changed

runtime/Go/antlr/v4/antlrdoc.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,19 @@ or translating structured text or binary files. It's widely used to build langua
88
From a grammar, ANTLR generates a parser that can build parse trees and also generates a listener interface
99
(or visitor) that makes it easy to respond to the recognition of phrases of interest.
1010
11+
# Go Runtime
12+
13+
At version 4.11.x and prior, the Go runtime was not properly versioned for go modules. After this point, the runtime
14+
source code is held in the `runtime/Go/antlr/v4` directory, and the go.mod file is updated to reflect the version of
15+
ANTLR4 that it is compatible with (I.E. uses the /v4 path). The runtime is now available as a go module, and can be
16+
imported as `github.com/antlr/antlr4/runtime/Go/antlr/v4` (the go get command should also be used with this path). See
17+
the main documentation for the ANTLR4 project for more information.
18+
19+
This means that if you are using the source code without modules, you should also use the source code in /v4. Though
20+
we highly recommend that you use go modules, as they are now idiomatic Go.
21+
22+
I am aware that this change will prove Hyrum's Law, but am prepared to live with it for teh common good. JI
23+
1124
# Code Generation
1225
1326
ANTLR supports the generation of code in a number of [target languages], and the generated code is supported by a
@@ -58,7 +71,7 @@ From the command line at the root of your package “myproject” you can then s
5871
5972
# Copyright Notice
6073
61-
Copyright (c) 2012-2022 The ANTLR Project. All rights reserved.
74+
Copyright (c) 2012-2023 The ANTLR Project. All rights reserved.
6275
6376
Use of this file is governed by the BSD 3-clause license, which can be found in the [LICENSE.txt] file in the project root.
6477

runtime/Go/antlr/v4/atn.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,11 @@ var ATNInvalidAltNumber int
2020
// [ALL(*)]: https://www.antlr.org/papers/allstar-techreport.pdf
2121
// [Recursive Transition Network]: https://en.wikipedia.org/wiki/Recursive_transition_network
2222
type ATN struct {
23-
// DecisionToState is the decision points for all rules, subrules, optional
24-
// blocks, ()+, ()*, etc. Each subrule/rule is a decision point, and we must track them so we
23+
24+
// DecisionToState is the decision points for all rules, sub-rules, optional
25+
// blocks, ()+, ()*, etc. Each sub-rule/rule is a decision point, and we must track them, so we
2526
// can go back later and build DFA predictors for them. This includes
26-
// all the rules, subrules, optional blocks, ()+, ()* etc...
27+
// all the rules, sub-rules, optional blocks, ()+, ()* etc...
2728
DecisionToState []DecisionState
2829

2930
// grammarType is the ATN type and is used for deserializing ATNs from strings.
@@ -51,6 +52,8 @@ type ATN struct {
5152
// specified, and otherwise is nil.
5253
ruleToTokenType []int
5354

55+
// ATNStates is a list of all states in the ATN, ordered by state number.
56+
//
5457
states []ATNState
5558

5659
mu sync.Mutex

runtime/Go/antlr/v4/atn_config.go

Lines changed: 44 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,29 +10,44 @@ import (
1010

1111
// ATNConfig is a tuple: (ATN state, predicted alt, syntactic, semantic
1212
// context). The syntactic context is a graph-structured stack node whose
13-
// path(s) to the root is the rule invocation(s) chain used to arrive at the
13+
// path(s) to the root is the rule invocation(s) chain used to arrive in the
1414
// state. The semantic context is the tree of semantic predicates encountered
1515
// before reaching an ATN state.
1616
type ATNConfig interface {
17+
18+
// Equals compares this ATNConfig to another for equality
1719
Equals(o Collectable[ATNConfig]) bool
20+
21+
// Hash returns the hash code for this ATNConfig for use in maps and comparisons
1822
Hash() int
1923

24+
// GetState returns the ATN state associated with this configuration
2025
GetState() ATNState
26+
// GetAlt returns the alternative associated with this configuration
2127
GetAlt() int
28+
// GetSemanticContext returns the semantic context associated with this configuration
2229
GetSemanticContext() SemanticContext
2330

31+
// GetContext returns the rule invocation stack associated with this configuration
2432
GetContext() PredictionContext
33+
// SetContext sets the rule invocation stack associated with this configuration
2534
SetContext(PredictionContext)
2635

36+
// GetReachesIntoOuterContext returns the count of references to an outer context from this configuration
2737
GetReachesIntoOuterContext() int
38+
// SetReachesIntoOuterContext sets the count of references to an outer context from this configuration
2839
SetReachesIntoOuterContext(int)
2940

41+
// String returns a string representation of the configuration
3042
String() string
3143

3244
getPrecedenceFilterSuppressed() bool
3345
setPrecedenceFilterSuppressed(bool)
3446
}
3547

48+
// BaseATNConfig is a base implementation of ATNConfig. Thi si s done to emulate Java's ability to have multiple
49+
// constructors for a single class. This is not idiomatic Go, but it works for now.
50+
// TODO: this isn't the way to do this I think, but it will take time to rework - JI Also, getters and setters are not Go. Might be better to just access the fields, though the compiler will probably eliminate the calls
3651
type BaseATNConfig struct {
3752
precedenceFilterSuppressed bool
3853
state ATNState
@@ -42,7 +57,8 @@ type BaseATNConfig struct {
4257
reachesIntoOuterContext int
4358
}
4459

45-
func NewBaseATNConfig7(old *BaseATNConfig) ATNConfig { // TODO: Dup
60+
//goland:noinspection GoUnusedExportedFunction
61+
func NewBaseATNConfig7(old *BaseATNConfig) ATNConfig { // TODO: Dup - maybe delete this
4662
return &BaseATNConfig{
4763
state: old.state,
4864
alt: old.alt,
@@ -52,10 +68,12 @@ func NewBaseATNConfig7(old *BaseATNConfig) ATNConfig { // TODO: Dup
5268
}
5369
}
5470

71+
// NewBaseATNConfig6 creates a new BaseATNConfig instance given a state, alt and context only
5572
func NewBaseATNConfig6(state ATNState, alt int, context PredictionContext) *BaseATNConfig {
5673
return NewBaseATNConfig5(state, alt, context, SemanticContextNone)
5774
}
5875

76+
// NewBaseATNConfig5 creates a new BaseATNConfig instance given a state, alt, context and semantic context
5977
func NewBaseATNConfig5(state ATNState, alt int, context PredictionContext, semanticContext SemanticContext) *BaseATNConfig {
6078
if semanticContext == nil {
6179
panic("semanticContext cannot be nil") // TODO: Necessary?
@@ -64,22 +82,28 @@ func NewBaseATNConfig5(state ATNState, alt int, context PredictionContext, seman
6482
return &BaseATNConfig{state: state, alt: alt, context: context, semanticContext: semanticContext}
6583
}
6684

85+
// NewBaseATNConfig4 creates a new BaseATNConfig instance given an existing config, and a state only
6786
func NewBaseATNConfig4(c ATNConfig, state ATNState) *BaseATNConfig {
6887
return NewBaseATNConfig(c, state, c.GetContext(), c.GetSemanticContext())
6988
}
7089

90+
// NewBaseATNConfig3 creates a new BaseATNConfig instance given an existing config, a state and a semantic context
7191
func NewBaseATNConfig3(c ATNConfig, state ATNState, semanticContext SemanticContext) *BaseATNConfig {
7292
return NewBaseATNConfig(c, state, c.GetContext(), semanticContext)
7393
}
7494

95+
// NewBaseATNConfig2 creates a new BaseATNConfig instance given an existing config, and a context only
7596
func NewBaseATNConfig2(c ATNConfig, semanticContext SemanticContext) *BaseATNConfig {
7697
return NewBaseATNConfig(c, c.GetState(), c.GetContext(), semanticContext)
7798
}
7899

100+
// NewBaseATNConfig1 creates a new BaseATNConfig instance given an existing config, a state, and a context only
79101
func NewBaseATNConfig1(c ATNConfig, state ATNState, context PredictionContext) *BaseATNConfig {
80102
return NewBaseATNConfig(c, state, context, c.GetSemanticContext())
81103
}
82104

105+
// NewBaseATNConfig creates a new BaseATNConfig instance given an existing config, a state, a context and a semantic context, other 'constructors'
106+
// are just wrappers around this one.
83107
func NewBaseATNConfig(c ATNConfig, state ATNState, context PredictionContext, semanticContext SemanticContext) *BaseATNConfig {
84108
if semanticContext == nil {
85109
panic("semanticContext cannot be nil")
@@ -103,29 +127,37 @@ func (b *BaseATNConfig) setPrecedenceFilterSuppressed(v bool) {
103127
b.precedenceFilterSuppressed = v
104128
}
105129

130+
// GetState returns the ATN state associated with this configuration
106131
func (b *BaseATNConfig) GetState() ATNState {
107132
return b.state
108133
}
109134

135+
// GetAlt returns the alternative associated with this configuration
110136
func (b *BaseATNConfig) GetAlt() int {
111137
return b.alt
112138
}
113139

140+
// SetContext sets the rule invocation stack associated with this configuration
114141
func (b *BaseATNConfig) SetContext(v PredictionContext) {
115142
b.context = v
116143
}
144+
145+
// GetContext returns the rule invocation stack associated with this configuration
117146
func (b *BaseATNConfig) GetContext() PredictionContext {
118147
return b.context
119148
}
120149

150+
// GetSemanticContext returns the semantic context associated with this configuration
121151
func (b *BaseATNConfig) GetSemanticContext() SemanticContext {
122152
return b.semanticContext
123153
}
124154

155+
// GetReachesIntoOuterContext returns the count of references to an outer context from this configuration
125156
func (b *BaseATNConfig) GetReachesIntoOuterContext() int {
126157
return b.reachesIntoOuterContext
127158
}
128159

160+
// SetReachesIntoOuterContext sets the count of references to an outer context from this configuration
129161
func (b *BaseATNConfig) SetReachesIntoOuterContext(v int) {
130162
b.reachesIntoOuterContext = v
131163
}
@@ -182,6 +214,7 @@ func (b *BaseATNConfig) Hash() int {
182214
return murmurFinish(h, 4)
183215
}
184216

217+
// String returns a string representation of the BaseATNConfig, usually used for debugging purposes
185218
func (b *BaseATNConfig) String() string {
186219
var s1, s2, s3 string
187220

@@ -200,6 +233,9 @@ func (b *BaseATNConfig) String() string {
200233
return fmt.Sprintf("(%v,%v%v%v%v)", b.state, b.alt, s1, s2, s3)
201234
}
202235

236+
// LexerATNConfig represents a lexer ATN configuration which tracks the lexer action, and which "inherits" from the
237+
// BaseATNConfig struct.
238+
// TODO: Stop using a pointer and embed the struct instead as this saves allocations. Same for the LexerATNConfig "constructors"
203239
type LexerATNConfig struct {
204240
*BaseATNConfig
205241
lexerActionExecutor *LexerActionExecutor
@@ -241,6 +277,7 @@ func NewLexerATNConfig2(c *LexerATNConfig, state ATNState, context PredictionCon
241277
}
242278
}
243279

280+
//goland:noinspection GoUnusedExportedFunction
244281
func NewLexerATNConfig1(state ATNState, alt int, context PredictionContext) *LexerATNConfig {
245282
return &LexerATNConfig{BaseATNConfig: NewBaseATNConfig5(state, alt, context, SemanticContextNone)}
246283
}
@@ -271,29 +308,29 @@ func (l *LexerATNConfig) Equals(other Collectable[ATNConfig]) bool {
271308
if l == other {
272309
return true
273310
}
274-
var othert, ok = other.(*LexerATNConfig)
311+
var otherT, ok = other.(*LexerATNConfig)
275312

276313
if l == other {
277314
return true
278315
} else if !ok {
279316
return false
280-
} else if l.passedThroughNonGreedyDecision != othert.passedThroughNonGreedyDecision {
317+
} else if l.passedThroughNonGreedyDecision != otherT.passedThroughNonGreedyDecision {
281318
return false
282319
}
283320

284321
var b bool
285322

286323
if l.lexerActionExecutor != nil {
287-
b = !l.lexerActionExecutor.Equals(othert.lexerActionExecutor)
324+
b = !l.lexerActionExecutor.Equals(otherT.lexerActionExecutor)
288325
} else {
289-
b = othert.lexerActionExecutor != nil
326+
b = otherT.lexerActionExecutor != nil
290327
}
291328

292329
if b {
293330
return false
294331
}
295332

296-
return l.BaseATNConfig.Equals(othert.BaseATNConfig)
333+
return l.BaseATNConfig.Equals(otherT.BaseATNConfig)
297334
}
298335

299336
func checkNonGreedyDecision(source *LexerATNConfig, target ATNState) bool {

0 commit comments

Comments
 (0)