pax_global_header 0000666 0000000 0000000 00000000064 14621155120 0014507 g ustar 00root root 0000000 0000000 52 comment=4d7e18847d8881a176db3d7cca909e3f3942a05f antlr4-go-antlr-c821258/ 0000775 0000000 0000000 00000000000 14621155120 0014712 5 ustar 00root root 0000000 0000000 antlr4-go-antlr-c821258/.gitignore 0000664 0000000 0000000 00000000310 14621155120 0016674 0 ustar 00root root 0000000 0000000 ### Go template # Binaries for programs and plugins *.exe *.exe~ *.dll *.so *.dylib # Test binary, built with `go test -c` *.test # Go workspace file go.work # No Goland stuff in this repo .idea antlr4-go-antlr-c821258/LICENSE 0000664 0000000 0000000 00000002710 14621155120 0015717 0 ustar 00root root 0000000 0000000 Copyright (c) 2012-2023 The ANTLR Project. All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 3. Neither name of copyright holders nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. antlr4-go-antlr-c821258/README.md 0000664 0000000 0000000 00000005701 14621155120 0016174 0 ustar 00root root 0000000 0000000 [](https://goreportcard.com/report/github.com/antlr4-go/antlr) [](https://pkg.go.dev/github.com/antlr4-go/antlr) [](https://github.com/antlr4-go/antlr/releases/latest) [](https://github.com/antlr4-go/antlr/releases/latest) [](https://github.com/antlr4-go/antlr/commit-activity) [](https://opensource.org/licenses/BSD-3-Clause) [](https://GitHub.com/Naereen/StrapDown.js/stargazers/) # ANTLR4 Go Runtime Module Repo IMPORTANT: Please submit PRs via a clone of the https://github.com/antlr/antlr4 repo, and not here. - Do not submit PRs or any change requests to this repo - This repo is read only and is updated by the ANTLR team to create a new release of the Go Runtime for ANTLR - This repo contains the Go runtime that your generated projects should import ## Introduction This repo contains the official modules for the Go Runtime for ANTLR. It is a copy of the runtime maintained at: https://github.com/antlr/antlr4/tree/master/runtime/Go/antlr and is automatically updated by the ANTLR team to create the official Go runtime release only. No development work is carried out in this repo and PRs are not accepted here. The dev branch of this repo is kept in sync with the dev branch of the main ANTLR repo and is updated periodically. ### Why? The `go get` command is unable to retrieve the Go runtime when it is embedded so deeply in the main repo. A `go get` against the `antlr/antlr4` repo, while retrieving the correct source code for the runtime, does not correctly resolve tags and will create a reference in your `go.mod` file that is unclear, will not upgrade smoothly and causes confusion. For instance, the current Go runtime release, which is tagged with v4.13.0 in `antlr/antlr4` is retrieved by go get as: ```sh require ( github.com/antlr/antlr4/runtime/Go/antlr/v4 v4.0.0-20230219212500-1f9a474cc2dc ) ``` Where you would expect to see: ```sh require ( github.com/antlr/antlr4/runtime/Go/antlr/v4 v4.13.0 ) ``` The decision was taken to create a separate org in a separate repo to hold the official Go runtime for ANTLR and from whence users can expect `go get` to behave as expected. # Documentation Please read the official documentation at: https://github.com/antlr/antlr4/blob/master/doc/index.md for tips on migrating existing projects to use the new module location and for information on how to use the Go runtime in general. antlr4-go-antlr-c821258/antlrdoc.go 0000664 0000000 0000000 00000011625 14621155120 0017054 0 ustar 00root root 0000000 0000000 /* Package antlr implements the Go version of the ANTLR 4 runtime. # The ANTLR Tool ANTLR (ANother Tool for Language Recognition) is a powerful parser generator for reading, processing, executing, or translating structured text or binary files. It's widely used to build languages, tools, and frameworks. From a grammar, ANTLR generates a parser that can build parse trees and also generates a listener interface (or visitor) that makes it easy to respond to the recognition of phrases of interest. # Go Runtime At version 4.11.x and prior, the Go runtime was not properly versioned for go modules. After this point, the runtime source code to be imported was held in the `runtime/Go/antlr/v4` directory, and the go.mod file was updated to reflect the version of ANTLR4 that it is compatible with (I.E. uses the /v4 path). However, this was found to be problematic, as it meant that with the runtime embedded so far underneath the root of the repo, the `go get` and related commands could not properly resolve the location of the go runtime source code. This meant that the reference to the runtime in your `go.mod` file would refer to the correct source code, but would not list the release tag such as @4.13.1 - this was confusing, to say the least. As of 4.13.0, the runtime is now available as a go module in its own repo, and can be imported as `github.com/antlr4-go/antlr` (the go get command should also be used with this path). See the main documentation for the ANTLR4 project for more information, which is available at [ANTLR docs]. The documentation for using the Go runtime is available at [Go runtime docs]. This means that if you are using the source code without modules, you should also use the source code in the [new repo]. Though we highly recommend that you use go modules, as they are now idiomatic for Go. I am aware that this change will prove Hyrum's Law, but am prepared to live with it for the common good. Go runtime author: [Jim Idle] jimi@idle.ws # Code Generation ANTLR supports the generation of code in a number of [target languages], and the generated code is supported by a runtime library, written specifically to support the generated code in the target language. This library is the runtime for the Go target. To generate code for the go target, it is generally recommended to place the source grammar files in a package of their own, and use the `.sh` script method of generating code, using the go generate directive. In that same directory it is usual, though not required, to place the antlr tool that should be used to generate the code. That does mean that the antlr tool JAR file will be checked in to your source code control though, so you are, of course, free to use any other way of specifying the version of the ANTLR tool to use, such as aliasing in `.zshrc` or equivalent, or a profile in your IDE, or configuration in your CI system. Checking in the jar does mean that it is easy to reproduce the build as it was at any point in its history. Here is a general/recommended template for an ANTLR based recognizer in Go: . ├── parser │ ├── mygrammar.g4 │ ├── antlr-4.13.1-complete.jar │ ├── generate.go │ └── generate.sh ├── parsing - generated code goes here │ └── error_listeners.go ├── go.mod ├── go.sum ├── main.go └── main_test.go Make sure that the package statement in your grammar file(s) reflects the go package the generated code will exist in. The generate.go file then looks like this: package parser //go:generate ./generate.sh And the generate.sh file will look similar to this: #!/bin/sh alias antlr4='java -Xmx500M -cp "./antlr4-4.13.1-complete.jar:$CLASSPATH" org.antlr.v4.Tool' antlr4 -Dlanguage=Go -no-visitor -package parsing *.g4 depending on whether you want visitors or listeners or any other ANTLR options. Not that another option here is to generate the code into a From the command line at the root of your source package (location of go.mo)d) you can then simply issue the command: go generate ./... Which will generate the code for the parser, and place it in the parsing package. You can then use the generated code by importing the parsing package. There are no hard and fast rules on this. It is just a recommendation. You can generate the code in any way and to anywhere you like. # Copyright Notice Copyright (c) 2012-2023 The ANTLR Project. All rights reserved. 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. [target languages]: https://github.com/antlr/antlr4/tree/master/runtime [LICENSE.txt]: https://github.com/antlr/antlr4/blob/master/LICENSE.txt [ANTLR docs]: https://github.com/antlr/antlr4/blob/master/doc/index.md [new repo]: https://github.com/antlr4-go/antlr [Jim Idle]: https://github.com/jimidle [Go runtime docs]: https://github.com/antlr/antlr4/blob/master/doc/go-target.md */ package antlr antlr4-go-antlr-c821258/atn.go 0000664 0000000 0000000 00000014116 14621155120 0016026 0 ustar 00root root 0000000 0000000 // Copyright (c) 2012-2022 The ANTLR Project. All rights reserved. // Use of this file is governed by the BSD 3-clause license that // can be found in the LICENSE.txt file in the project root. package antlr // ATNInvalidAltNumber is used to represent an ALT number that has yet to be calculated or // which is invalid for a particular struct such as [*antlr.BaseRuleContext] var ATNInvalidAltNumber int // ATN represents an “[Augmented Transition Network]”, though general in ANTLR the term // “Augmented Recursive Transition Network” though there are some descriptions of “[Recursive Transition Network]” // in existence. // // ATNs represent the main networks in the system and are serialized by the code generator and support [ALL(*)]. // // [Augmented Transition Network]: https://en.wikipedia.org/wiki/Augmented_transition_network // [ALL(*)]: https://www.antlr.org/papers/allstar-techreport.pdf // [Recursive Transition Network]: https://en.wikipedia.org/wiki/Recursive_transition_network type ATN struct { // DecisionToState is the decision points for all rules, sub-rules, optional // blocks, ()+, ()*, etc. Each sub-rule/rule is a decision point, and we must track them, so we // can go back later and build DFA predictors for them. This includes // all the rules, sub-rules, optional blocks, ()+, ()* etc... DecisionToState []DecisionState // grammarType is the ATN type and is used for deserializing ATNs from strings. grammarType int // lexerActions is referenced by action transitions in the ATN for lexer ATNs. lexerActions []LexerAction // maxTokenType is the maximum value for any symbol recognized by a transition in the ATN. maxTokenType int modeNameToStartState map[string]*TokensStartState modeToStartState []*TokensStartState // ruleToStartState maps from rule index to starting state number. ruleToStartState []*RuleStartState // ruleToStopState maps from rule index to stop state number. ruleToStopState []*RuleStopState // ruleToTokenType maps the rule index to the resulting token type for lexer // ATNs. For parser ATNs, it maps the rule index to the generated bypass token // type if ATNDeserializationOptions.isGenerateRuleBypassTransitions was // specified, and otherwise is nil. ruleToTokenType []int // ATNStates is a list of all states in the ATN, ordered by state number. // states []ATNState mu Mutex stateMu RWMutex edgeMu RWMutex } // NewATN returns a new ATN struct representing the given grammarType and is used // for runtime deserialization of ATNs from the code generated by the ANTLR tool func NewATN(grammarType int, maxTokenType int) *ATN { return &ATN{ grammarType: grammarType, maxTokenType: maxTokenType, modeNameToStartState: make(map[string]*TokensStartState), } } // NextTokensInContext computes and returns the set of valid tokens that can occur starting // in state s. If ctx is nil, the set of tokens will not include what can follow // the rule surrounding s. In other words, the set will be restricted to tokens // reachable staying within the rule of s. func (a *ATN) NextTokensInContext(s ATNState, ctx RuleContext) *IntervalSet { return NewLL1Analyzer(a).Look(s, nil, ctx) } // NextTokensNoContext computes and returns the set of valid tokens that can occur starting // in state s and staying in same rule. [antlr.Token.EPSILON] is in set if we reach end of // rule. func (a *ATN) NextTokensNoContext(s ATNState) *IntervalSet { a.mu.Lock() defer a.mu.Unlock() iset := s.GetNextTokenWithinRule() if iset == nil { iset = a.NextTokensInContext(s, nil) iset.readOnly = true s.SetNextTokenWithinRule(iset) } return iset } // NextTokens computes and returns the set of valid tokens starting in state s, by // calling either [NextTokensNoContext] (ctx == nil) or [NextTokensInContext] (ctx != nil). func (a *ATN) NextTokens(s ATNState, ctx RuleContext) *IntervalSet { if ctx == nil { return a.NextTokensNoContext(s) } return a.NextTokensInContext(s, ctx) } func (a *ATN) addState(state ATNState) { if state != nil { state.SetATN(a) state.SetStateNumber(len(a.states)) } a.states = append(a.states, state) } func (a *ATN) removeState(state ATNState) { a.states[state.GetStateNumber()] = nil // Just free the memory; don't shift states in the slice } func (a *ATN) defineDecisionState(s DecisionState) int { a.DecisionToState = append(a.DecisionToState, s) s.setDecision(len(a.DecisionToState) - 1) return s.getDecision() } func (a *ATN) getDecisionState(decision int) DecisionState { if len(a.DecisionToState) == 0 { return nil } return a.DecisionToState[decision] } // getExpectedTokens computes the set of input symbols which could follow ATN // state number stateNumber in the specified full parse context ctx and returns // the set of potentially valid input symbols which could follow the specified // state in the specified context. This method considers the complete parser // context, but does not evaluate semantic predicates (i.e. all predicates // encountered during the calculation are assumed true). If a path in the ATN // exists from the starting state to the RuleStopState of the outermost context // without Matching any symbols, Token.EOF is added to the returned set. // // A nil ctx defaults to ParserRuleContext.EMPTY. // // It panics if the ATN does not contain state stateNumber. func (a *ATN) getExpectedTokens(stateNumber int, ctx RuleContext) *IntervalSet { if stateNumber < 0 || stateNumber >= len(a.states) { panic("Invalid state number.") } s := a.states[stateNumber] following := a.NextTokens(s, nil) if !following.contains(TokenEpsilon) { return following } expected := NewIntervalSet() expected.addSet(following) expected.removeOne(TokenEpsilon) for ctx != nil && ctx.GetInvokingState() >= 0 && following.contains(TokenEpsilon) { invokingState := a.states[ctx.GetInvokingState()] rt := invokingState.GetTransitions()[0] following = a.NextTokens(rt.(*RuleTransition).followState, nil) expected.addSet(following) expected.removeOne(TokenEpsilon) ctx = ctx.GetParent().(RuleContext) } if following.contains(TokenEpsilon) { expected.addOne(TokenEOF) } return expected } antlr4-go-antlr-c821258/atn_config.go 0000664 0000000 0000000 00000024653 14621155120 0017362 0 ustar 00root root 0000000 0000000 // Copyright (c) 2012-2022 The ANTLR Project. All rights reserved. // Use of this file is governed by the BSD 3-clause license that // can be found in the LICENSE.txt file in the project root. package antlr import ( "fmt" ) const ( lexerConfig = iota // Indicates that this ATNConfig is for a lexer parserConfig // Indicates that this ATNConfig is for a parser ) // ATNConfig is a tuple: (ATN state, predicted alt, syntactic, semantic // context). The syntactic context is a graph-structured stack node whose // path(s) to the root is the rule invocation(s) chain used to arrive in the // state. The semantic context is the tree of semantic predicates encountered // before reaching an ATN state. type ATNConfig struct { precedenceFilterSuppressed bool state ATNState alt int context *PredictionContext semanticContext SemanticContext reachesIntoOuterContext int cType int // lexerConfig or parserConfig lexerActionExecutor *LexerActionExecutor passedThroughNonGreedyDecision bool } // NewATNConfig6 creates a new ATNConfig instance given a state, alt and context only func NewATNConfig6(state ATNState, alt int, context *PredictionContext) *ATNConfig { return NewATNConfig5(state, alt, context, SemanticContextNone) } // NewATNConfig5 creates a new ATNConfig instance given a state, alt, context and semantic context func NewATNConfig5(state ATNState, alt int, context *PredictionContext, semanticContext SemanticContext) *ATNConfig { if semanticContext == nil { panic("semanticContext cannot be nil") // TODO: Necessary? } pac := &ATNConfig{} pac.state = state pac.alt = alt pac.context = context pac.semanticContext = semanticContext pac.cType = parserConfig return pac } // NewATNConfig4 creates a new ATNConfig instance given an existing config, and a state only func NewATNConfig4(c *ATNConfig, state ATNState) *ATNConfig { return NewATNConfig(c, state, c.GetContext(), c.GetSemanticContext()) } // NewATNConfig3 creates a new ATNConfig instance given an existing config, a state and a semantic context func NewATNConfig3(c *ATNConfig, state ATNState, semanticContext SemanticContext) *ATNConfig { return NewATNConfig(c, state, c.GetContext(), semanticContext) } // NewATNConfig2 creates a new ATNConfig instance given an existing config, and a context only func NewATNConfig2(c *ATNConfig, semanticContext SemanticContext) *ATNConfig { return NewATNConfig(c, c.GetState(), c.GetContext(), semanticContext) } // NewATNConfig1 creates a new ATNConfig instance given an existing config, a state, and a context only func NewATNConfig1(c *ATNConfig, state ATNState, context *PredictionContext) *ATNConfig { return NewATNConfig(c, state, context, c.GetSemanticContext()) } // NewATNConfig creates a new ATNConfig instance given an existing config, a state, a context and a semantic context, other 'constructors' // are just wrappers around this one. func NewATNConfig(c *ATNConfig, state ATNState, context *PredictionContext, semanticContext SemanticContext) *ATNConfig { b := &ATNConfig{} b.InitATNConfig(c, state, c.GetAlt(), context, semanticContext) b.cType = parserConfig return b } func (a *ATNConfig) InitATNConfig(c *ATNConfig, state ATNState, alt int, context *PredictionContext, semanticContext SemanticContext) { a.state = state a.alt = alt a.context = context a.semanticContext = semanticContext a.reachesIntoOuterContext = c.GetReachesIntoOuterContext() a.precedenceFilterSuppressed = c.getPrecedenceFilterSuppressed() } func (a *ATNConfig) getPrecedenceFilterSuppressed() bool { return a.precedenceFilterSuppressed } func (a *ATNConfig) setPrecedenceFilterSuppressed(v bool) { a.precedenceFilterSuppressed = v } // GetState returns the ATN state associated with this configuration func (a *ATNConfig) GetState() ATNState { return a.state } // GetAlt returns the alternative associated with this configuration func (a *ATNConfig) GetAlt() int { return a.alt } // SetContext sets the rule invocation stack associated with this configuration func (a *ATNConfig) SetContext(v *PredictionContext) { a.context = v } // GetContext returns the rule invocation stack associated with this configuration func (a *ATNConfig) GetContext() *PredictionContext { return a.context } // GetSemanticContext returns the semantic context associated with this configuration func (a *ATNConfig) GetSemanticContext() SemanticContext { return a.semanticContext } // GetReachesIntoOuterContext returns the count of references to an outer context from this configuration func (a *ATNConfig) GetReachesIntoOuterContext() int { return a.reachesIntoOuterContext } // SetReachesIntoOuterContext sets the count of references to an outer context from this configuration func (a *ATNConfig) SetReachesIntoOuterContext(v int) { a.reachesIntoOuterContext = v } // Equals is the default comparison function for an ATNConfig when no specialist implementation is required // for a collection. // // An ATN configuration is equal to another if both have the same state, they // predict the same alternative, and syntactic/semantic contexts are the same. func (a *ATNConfig) Equals(o Collectable[*ATNConfig]) bool { switch a.cType { case lexerConfig: return a.LEquals(o) case parserConfig: return a.PEquals(o) default: panic("Invalid ATNConfig type") } } // PEquals is the default comparison function for a Parser ATNConfig when no specialist implementation is required // for a collection. // // An ATN configuration is equal to another if both have the same state, they // predict the same alternative, and syntactic/semantic contexts are the same. func (a *ATNConfig) PEquals(o Collectable[*ATNConfig]) bool { var other, ok = o.(*ATNConfig) if !ok { return false } if a == other { return true } else if other == nil { return false } var equal bool if a.context == nil { equal = other.context == nil } else { equal = a.context.Equals(other.context) } var ( nums = a.state.GetStateNumber() == other.state.GetStateNumber() alts = a.alt == other.alt cons = a.semanticContext.Equals(other.semanticContext) sups = a.precedenceFilterSuppressed == other.precedenceFilterSuppressed ) return nums && alts && cons && sups && equal } // Hash is the default hash function for a parser ATNConfig, when no specialist hash function // is required for a collection func (a *ATNConfig) Hash() int { switch a.cType { case lexerConfig: return a.LHash() case parserConfig: return a.PHash() default: panic("Invalid ATNConfig type") } } // PHash is the default hash function for a parser ATNConfig, when no specialist hash function // is required for a collection func (a *ATNConfig) PHash() int { var c int if a.context != nil { c = a.context.Hash() } h := murmurInit(7) h = murmurUpdate(h, a.state.GetStateNumber()) h = murmurUpdate(h, a.alt) h = murmurUpdate(h, c) h = murmurUpdate(h, a.semanticContext.Hash()) return murmurFinish(h, 4) } // String returns a string representation of the ATNConfig, usually used for debugging purposes func (a *ATNConfig) String() string { var s1, s2, s3 string if a.context != nil { s1 = ",[" + fmt.Sprint(a.context) + "]" } if a.semanticContext != SemanticContextNone { s2 = "," + fmt.Sprint(a.semanticContext) } if a.reachesIntoOuterContext > 0 { s3 = ",up=" + fmt.Sprint(a.reachesIntoOuterContext) } return fmt.Sprintf("(%v,%v%v%v%v)", a.state, a.alt, s1, s2, s3) } func NewLexerATNConfig6(state ATNState, alt int, context *PredictionContext) *ATNConfig { lac := &ATNConfig{} lac.state = state lac.alt = alt lac.context = context lac.semanticContext = SemanticContextNone lac.cType = lexerConfig return lac } func NewLexerATNConfig4(c *ATNConfig, state ATNState) *ATNConfig { lac := &ATNConfig{} lac.lexerActionExecutor = c.lexerActionExecutor lac.passedThroughNonGreedyDecision = checkNonGreedyDecision(c, state) lac.InitATNConfig(c, state, c.GetAlt(), c.GetContext(), c.GetSemanticContext()) lac.cType = lexerConfig return lac } func NewLexerATNConfig3(c *ATNConfig, state ATNState, lexerActionExecutor *LexerActionExecutor) *ATNConfig { lac := &ATNConfig{} lac.lexerActionExecutor = lexerActionExecutor lac.passedThroughNonGreedyDecision = checkNonGreedyDecision(c, state) lac.InitATNConfig(c, state, c.GetAlt(), c.GetContext(), c.GetSemanticContext()) lac.cType = lexerConfig return lac } func NewLexerATNConfig2(c *ATNConfig, state ATNState, context *PredictionContext) *ATNConfig { lac := &ATNConfig{} lac.lexerActionExecutor = c.lexerActionExecutor lac.passedThroughNonGreedyDecision = checkNonGreedyDecision(c, state) lac.InitATNConfig(c, state, c.GetAlt(), context, c.GetSemanticContext()) lac.cType = lexerConfig return lac } //goland:noinspection GoUnusedExportedFunction func NewLexerATNConfig1(state ATNState, alt int, context *PredictionContext) *ATNConfig { lac := &ATNConfig{} lac.state = state lac.alt = alt lac.context = context lac.semanticContext = SemanticContextNone lac.cType = lexerConfig return lac } // LHash is the default hash function for Lexer ATNConfig objects, it can be used directly or via // the default comparator [ObjEqComparator]. func (a *ATNConfig) LHash() int { var f int if a.passedThroughNonGreedyDecision { f = 1 } else { f = 0 } h := murmurInit(7) h = murmurUpdate(h, a.state.GetStateNumber()) h = murmurUpdate(h, a.alt) h = murmurUpdate(h, a.context.Hash()) h = murmurUpdate(h, a.semanticContext.Hash()) h = murmurUpdate(h, f) h = murmurUpdate(h, a.lexerActionExecutor.Hash()) h = murmurFinish(h, 6) return h } // LEquals is the default comparison function for Lexer ATNConfig objects, it can be used directly or via // the default comparator [ObjEqComparator]. func (a *ATNConfig) LEquals(other Collectable[*ATNConfig]) bool { var otherT, ok = other.(*ATNConfig) if !ok { return false } else if a == otherT { return true } else if a.passedThroughNonGreedyDecision != otherT.passedThroughNonGreedyDecision { return false } switch { case a.lexerActionExecutor == nil && otherT.lexerActionExecutor == nil: return true case a.lexerActionExecutor != nil && otherT.lexerActionExecutor != nil: if !a.lexerActionExecutor.Equals(otherT.lexerActionExecutor) { return false } default: return false // One but not both, are nil } return a.PEquals(otherT) } func checkNonGreedyDecision(source *ATNConfig, target ATNState) bool { var ds, ok = target.(DecisionState) return source.passedThroughNonGreedyDecision || (ok && ds.getNonGreedy()) } antlr4-go-antlr-c821258/atn_config_set.go 0000664 0000000 0000000 00000020576 14621155120 0020235 0 ustar 00root root 0000000 0000000 // Copyright (c) 2012-2022 The ANTLR Project. All rights reserved. // Use of this file is governed by the BSD 3-clause license that // can be found in the LICENSE.txt file in the project root. package antlr import ( "fmt" ) // ATNConfigSet is a specialized set of ATNConfig that tracks information // about its elements and can combine similar configurations using a // graph-structured stack. type ATNConfigSet struct { cachedHash int // configLookup is used to determine whether two ATNConfigSets are equal. We // need all configurations with the same (s, i, _, semctx) to be equal. A key // effectively doubles the number of objects associated with ATNConfigs. All // keys are hashed by (s, i, _, pi), not including the context. Wiped out when // read-only because a set becomes a DFA state. configLookup *JStore[*ATNConfig, Comparator[*ATNConfig]] // configs is the added elements that did not match an existing key in configLookup configs []*ATNConfig // TODO: These fields make me pretty uncomfortable, but it is nice to pack up // info together because it saves re-computation. Can we track conflicts as they // are added to save scanning configs later? conflictingAlts *BitSet // dipsIntoOuterContext is used by parsers and lexers. In a lexer, it indicates // we hit a pred while computing a closure operation. Do not make a DFA state // from the ATNConfigSet in this case. TODO: How is this used by parsers? dipsIntoOuterContext bool // fullCtx is whether it is part of a full context LL prediction. Used to // determine how to merge $. It is a wildcard with SLL, but not for an LL // context merge. fullCtx bool // Used in parser and lexer. In lexer, it indicates we hit a pred // while computing a closure operation. Don't make a DFA state from this set. hasSemanticContext bool // readOnly is whether it is read-only. Do not // allow any code to manipulate the set if true because DFA states will point at // sets and those must not change. It not, protect other fields; conflictingAlts // in particular, which is assigned after readOnly. readOnly bool // TODO: These fields make me pretty uncomfortable, but it is nice to pack up // info together because it saves re-computation. Can we track conflicts as they // are added to save scanning configs later? uniqueAlt int } // Alts returns the combined set of alts for all the configurations in this set. func (b *ATNConfigSet) Alts() *BitSet { alts := NewBitSet() for _, it := range b.configs { alts.add(it.GetAlt()) } return alts } // NewATNConfigSet creates a new ATNConfigSet instance. func NewATNConfigSet(fullCtx bool) *ATNConfigSet { return &ATNConfigSet{ cachedHash: -1, configLookup: NewJStore[*ATNConfig, Comparator[*ATNConfig]](aConfCompInst, ATNConfigLookupCollection, "NewATNConfigSet()"), fullCtx: fullCtx, } } // Add merges contexts with existing configs for (s, i, pi, _), // where 's' is the ATNConfig.state, 'i' is the ATNConfig.alt, and // 'pi' is the [ATNConfig].semanticContext. // // We use (s,i,pi) as the key. // Updates dipsIntoOuterContext and hasSemanticContext when necessary. func (b *ATNConfigSet) Add(config *ATNConfig, mergeCache *JPCMap) bool { if b.readOnly { panic("set is read-only") } if config.GetSemanticContext() != SemanticContextNone { b.hasSemanticContext = true } if config.GetReachesIntoOuterContext() > 0 { b.dipsIntoOuterContext = true } existing, present := b.configLookup.Put(config) // The config was not already in the set // if !present { b.cachedHash = -1 b.configs = append(b.configs, config) // Track order here return true } // Merge a previous (s, i, pi, _) with it and save the result rootIsWildcard := !b.fullCtx merged := merge(existing.GetContext(), config.GetContext(), rootIsWildcard, mergeCache) // No need to check for existing.context because config.context is in the cache, // since the only way to create new graphs is the "call rule" and here. We cache // at both places. existing.SetReachesIntoOuterContext(intMax(existing.GetReachesIntoOuterContext(), config.GetReachesIntoOuterContext())) // Preserve the precedence filter suppression during the merge if config.getPrecedenceFilterSuppressed() { existing.setPrecedenceFilterSuppressed(true) } // Replace the context because there is no need to do alt mapping existing.SetContext(merged) return true } // GetStates returns the set of states represented by all configurations in this config set func (b *ATNConfigSet) GetStates() *JStore[ATNState, Comparator[ATNState]] { // states uses the standard comparator and Hash() provided by the ATNState instance // states := NewJStore[ATNState, Comparator[ATNState]](aStateEqInst, ATNStateCollection, "ATNConfigSet.GetStates()") for i := 0; i < len(b.configs); i++ { states.Put(b.configs[i].GetState()) } return states } func (b *ATNConfigSet) GetPredicates() []SemanticContext { predicates := make([]SemanticContext, 0) for i := 0; i < len(b.configs); i++ { c := b.configs[i].GetSemanticContext() if c != SemanticContextNone { predicates = append(predicates, c) } } return predicates } func (b *ATNConfigSet) OptimizeConfigs(interpreter *BaseATNSimulator) { if b.readOnly { panic("set is read-only") } // Empty indicate no optimization is possible if b.configLookup == nil || b.configLookup.Len() == 0 { return } for i := 0; i < len(b.configs); i++ { config := b.configs[i] config.SetContext(interpreter.getCachedContext(config.GetContext())) } } func (b *ATNConfigSet) AddAll(coll []*ATNConfig) bool { for i := 0; i < len(coll); i++ { b.Add(coll[i], nil) } return false } // Compare The configs are only equal if they are in the same order and their Equals function returns true. // Java uses ArrayList.equals(), which requires the same order. func (b *ATNConfigSet) Compare(bs *ATNConfigSet) bool { if len(b.configs) != len(bs.configs) { return false } for i := 0; i < len(b.configs); i++ { if !b.configs[i].Equals(bs.configs[i]) { return false } } return true } func (b *ATNConfigSet) Equals(other Collectable[ATNConfig]) bool { if b == other { return true } else if _, ok := other.(*ATNConfigSet); !ok { return false } other2 := other.(*ATNConfigSet) var eca bool switch { case b.conflictingAlts == nil && other2.conflictingAlts == nil: eca = true case b.conflictingAlts != nil && other2.conflictingAlts != nil: eca = b.conflictingAlts.equals(other2.conflictingAlts) } return b.configs != nil && b.fullCtx == other2.fullCtx && b.uniqueAlt == other2.uniqueAlt && eca && b.hasSemanticContext == other2.hasSemanticContext && b.dipsIntoOuterContext == other2.dipsIntoOuterContext && b.Compare(other2) } func (b *ATNConfigSet) Hash() int { if b.readOnly { if b.cachedHash == -1 { b.cachedHash = b.hashCodeConfigs() } return b.cachedHash } return b.hashCodeConfigs() } func (b *ATNConfigSet) hashCodeConfigs() int { h := 1 for _, config := range b.configs { h = 31*h + config.Hash() } return h } func (b *ATNConfigSet) Contains(item *ATNConfig) bool { if b.readOnly { panic("not implemented for read-only sets") } if b.configLookup == nil { return false } return b.configLookup.Contains(item) } func (b *ATNConfigSet) ContainsFast(item *ATNConfig) bool { return b.Contains(item) } func (b *ATNConfigSet) Clear() { if b.readOnly { panic("set is read-only") } b.configs = make([]*ATNConfig, 0) b.cachedHash = -1 b.configLookup = NewJStore[*ATNConfig, Comparator[*ATNConfig]](aConfCompInst, ATNConfigLookupCollection, "NewATNConfigSet()") } func (b *ATNConfigSet) String() string { s := "[" for i, c := range b.configs { s += c.String() if i != len(b.configs)-1 { s += ", " } } s += "]" if b.hasSemanticContext { s += ",hasSemanticContext=" + fmt.Sprint(b.hasSemanticContext) } if b.uniqueAlt != ATNInvalidAltNumber { s += ",uniqueAlt=" + fmt.Sprint(b.uniqueAlt) } if b.conflictingAlts != nil { s += ",conflictingAlts=" + b.conflictingAlts.String() } if b.dipsIntoOuterContext { s += ",dipsIntoOuterContext" } return s } // NewOrderedATNConfigSet creates a config set with a slightly different Hash/Equal pair // for use in lexers. func NewOrderedATNConfigSet() *ATNConfigSet { return &ATNConfigSet{ cachedHash: -1, // This set uses the standard Hash() and Equals() from ATNConfig configLookup: NewJStore[*ATNConfig, Comparator[*ATNConfig]](aConfEqInst, ATNConfigCollection, "ATNConfigSet.NewOrderedATNConfigSet()"), fullCtx: false, } } antlr4-go-antlr-c821258/atn_deserialization_options.go 0000664 0000000 0000000 00000003425 14621155120 0023050 0 ustar 00root root 0000000 0000000 // Copyright (c) 2012-2022 The ANTLR Project. All rights reserved. // Use of this file is governed by the BSD 3-clause license that // can be found in the LICENSE.txt file in the project root. package antlr import "errors" var defaultATNDeserializationOptions = ATNDeserializationOptions{true, true, false} type ATNDeserializationOptions struct { readOnly bool verifyATN bool generateRuleBypassTransitions bool } func (opts *ATNDeserializationOptions) ReadOnly() bool { return opts.readOnly } func (opts *ATNDeserializationOptions) SetReadOnly(readOnly bool) { if opts.readOnly { panic(errors.New("cannot mutate read only ATNDeserializationOptions")) } opts.readOnly = readOnly } func (opts *ATNDeserializationOptions) VerifyATN() bool { return opts.verifyATN } func (opts *ATNDeserializationOptions) SetVerifyATN(verifyATN bool) { if opts.readOnly { panic(errors.New("cannot mutate read only ATNDeserializationOptions")) } opts.verifyATN = verifyATN } func (opts *ATNDeserializationOptions) GenerateRuleBypassTransitions() bool { return opts.generateRuleBypassTransitions } func (opts *ATNDeserializationOptions) SetGenerateRuleBypassTransitions(generateRuleBypassTransitions bool) { if opts.readOnly { panic(errors.New("cannot mutate read only ATNDeserializationOptions")) } opts.generateRuleBypassTransitions = generateRuleBypassTransitions } //goland:noinspection GoUnusedExportedFunction func DefaultATNDeserializationOptions() *ATNDeserializationOptions { return NewATNDeserializationOptions(&defaultATNDeserializationOptions) } func NewATNDeserializationOptions(other *ATNDeserializationOptions) *ATNDeserializationOptions { o := new(ATNDeserializationOptions) if other != nil { *o = *other o.readOnly = false } return o } antlr4-go-antlr-c821258/atn_deserializer.go 0000664 0000000 0000000 00000037330 14621155120 0020573 0 ustar 00root root 0000000 0000000 // Copyright (c) 2012-2022 The ANTLR Project. All rights reserved. // Use of this file is governed by the BSD 3-clause license that // can be found in the LICENSE.txt file in the project root. package antlr import ( "fmt" "strconv" ) const serializedVersion = 4 type loopEndStateIntPair struct { item0 *LoopEndState item1 int } type blockStartStateIntPair struct { item0 BlockStartState item1 int } type ATNDeserializer struct { options *ATNDeserializationOptions data []int32 pos int } func NewATNDeserializer(options *ATNDeserializationOptions) *ATNDeserializer { if options == nil { options = &defaultATNDeserializationOptions } return &ATNDeserializer{options: options} } //goland:noinspection GoUnusedFunction func stringInSlice(a string, list []string) int { for i, b := range list { if b == a { return i } } return -1 } func (a *ATNDeserializer) Deserialize(data []int32) *ATN { a.data = data a.pos = 0 a.checkVersion() atn := a.readATN() a.readStates(atn) a.readRules(atn) a.readModes(atn) sets := a.readSets(atn, nil) a.readEdges(atn, sets) a.readDecisions(atn) a.readLexerActions(atn) a.markPrecedenceDecisions(atn) a.verifyATN(atn) if a.options.GenerateRuleBypassTransitions() && atn.grammarType == ATNTypeParser { a.generateRuleBypassTransitions(atn) // Re-verify after modification a.verifyATN(atn) } return atn } func (a *ATNDeserializer) checkVersion() { version := a.readInt() if version != serializedVersion { panic("Could not deserialize ATN with version " + strconv.Itoa(version) + " (expected " + strconv.Itoa(serializedVersion) + ").") } } func (a *ATNDeserializer) readATN() *ATN { grammarType := a.readInt() maxTokenType := a.readInt() return NewATN(grammarType, maxTokenType) } func (a *ATNDeserializer) readStates(atn *ATN) { nstates := a.readInt() // Allocate worst case size. loopBackStateNumbers := make([]loopEndStateIntPair, 0, nstates) endStateNumbers := make([]blockStartStateIntPair, 0, nstates) // Preallocate states slice. atn.states = make([]ATNState, 0, nstates) for i := 0; i < nstates; i++ { stype := a.readInt() // Ignore bad types of states if stype == ATNStateInvalidType { atn.addState(nil) continue } ruleIndex := a.readInt() s := a.stateFactory(stype, ruleIndex) if stype == ATNStateLoopEnd { loopBackStateNumber := a.readInt() loopBackStateNumbers = append(loopBackStateNumbers, loopEndStateIntPair{s.(*LoopEndState), loopBackStateNumber}) } else if s2, ok := s.(BlockStartState); ok { endStateNumber := a.readInt() endStateNumbers = append(endStateNumbers, blockStartStateIntPair{s2, endStateNumber}) } atn.addState(s) } // Delay the assignment of loop back and end states until we know all the state // instances have been initialized for _, pair := range loopBackStateNumbers { pair.item0.loopBackState = atn.states[pair.item1] } for _, pair := range endStateNumbers { pair.item0.setEndState(atn.states[pair.item1].(*BlockEndState)) } numNonGreedyStates := a.readInt() for j := 0; j < numNonGreedyStates; j++ { stateNumber := a.readInt() atn.states[stateNumber].(DecisionState).setNonGreedy(true) } numPrecedenceStates := a.readInt() for j := 0; j < numPrecedenceStates; j++ { stateNumber := a.readInt() atn.states[stateNumber].(*RuleStartState).isPrecedenceRule = true } } func (a *ATNDeserializer) readRules(atn *ATN) { nrules := a.readInt() if atn.grammarType == ATNTypeLexer { atn.ruleToTokenType = make([]int, nrules) } atn.ruleToStartState = make([]*RuleStartState, nrules) for i := range atn.ruleToStartState { s := a.readInt() startState := atn.states[s].(*RuleStartState) atn.ruleToStartState[i] = startState if atn.grammarType == ATNTypeLexer { tokenType := a.readInt() atn.ruleToTokenType[i] = tokenType } } atn.ruleToStopState = make([]*RuleStopState, nrules) for _, state := range atn.states { if s2, ok := state.(*RuleStopState); ok { atn.ruleToStopState[s2.ruleIndex] = s2 atn.ruleToStartState[s2.ruleIndex].stopState = s2 } } } func (a *ATNDeserializer) readModes(atn *ATN) { nmodes := a.readInt() atn.modeToStartState = make([]*TokensStartState, nmodes) for i := range atn.modeToStartState { s := a.readInt() atn.modeToStartState[i] = atn.states[s].(*TokensStartState) } } func (a *ATNDeserializer) readSets(_ *ATN, sets []*IntervalSet) []*IntervalSet { m := a.readInt() // Preallocate the needed capacity. if cap(sets)-len(sets) < m { isets := make([]*IntervalSet, len(sets), len(sets)+m) copy(isets, sets) sets = isets } for i := 0; i < m; i++ { iset := NewIntervalSet() sets = append(sets, iset) n := a.readInt() containsEOF := a.readInt() if containsEOF != 0 { iset.addOne(-1) } for j := 0; j < n; j++ { i1 := a.readInt() i2 := a.readInt() iset.addRange(i1, i2) } } return sets } func (a *ATNDeserializer) readEdges(atn *ATN, sets []*IntervalSet) { nedges := a.readInt() for i := 0; i < nedges; i++ { var ( src = a.readInt() trg = a.readInt() ttype = a.readInt() arg1 = a.readInt() arg2 = a.readInt() arg3 = a.readInt() trans = a.edgeFactory(atn, ttype, src, trg, arg1, arg2, arg3, sets) srcState = atn.states[src] ) srcState.AddTransition(trans, -1) } // Edges for rule stop states can be derived, so they are not serialized for _, state := range atn.states { for _, t := range state.GetTransitions() { var rt, ok = t.(*RuleTransition) if !ok { continue } outermostPrecedenceReturn := -1 if atn.ruleToStartState[rt.getTarget().GetRuleIndex()].isPrecedenceRule { if rt.precedence == 0 { outermostPrecedenceReturn = rt.getTarget().GetRuleIndex() } } trans := NewEpsilonTransition(rt.followState, outermostPrecedenceReturn) atn.ruleToStopState[rt.getTarget().GetRuleIndex()].AddTransition(trans, -1) } } for _, state := range atn.states { if s2, ok := state.(BlockStartState); ok { // We need to know the end state to set its start state if s2.getEndState() == nil { panic("IllegalState") } // Block end states can only be associated to a single block start state if s2.getEndState().startState != nil { panic("IllegalState") } s2.getEndState().startState = state } if s2, ok := state.(*PlusLoopbackState); ok { for _, t := range s2.GetTransitions() { if t2, ok := t.getTarget().(*PlusBlockStartState); ok { t2.loopBackState = state } } } else if s2, ok := state.(*StarLoopbackState); ok { for _, t := range s2.GetTransitions() { if t2, ok := t.getTarget().(*StarLoopEntryState); ok { t2.loopBackState = state } } } } } func (a *ATNDeserializer) readDecisions(atn *ATN) { ndecisions := a.readInt() for i := 0; i < ndecisions; i++ { s := a.readInt() decState := atn.states[s].(DecisionState) atn.DecisionToState = append(atn.DecisionToState, decState) decState.setDecision(i) } } func (a *ATNDeserializer) readLexerActions(atn *ATN) { if atn.grammarType == ATNTypeLexer { count := a.readInt() atn.lexerActions = make([]LexerAction, count) for i := range atn.lexerActions { actionType := a.readInt() data1 := a.readInt() data2 := a.readInt() atn.lexerActions[i] = a.lexerActionFactory(actionType, data1, data2) } } } func (a *ATNDeserializer) generateRuleBypassTransitions(atn *ATN) { count := len(atn.ruleToStartState) for i := 0; i < count; i++ { atn.ruleToTokenType[i] = atn.maxTokenType + i + 1 } for i := 0; i < count; i++ { a.generateRuleBypassTransition(atn, i) } } func (a *ATNDeserializer) generateRuleBypassTransition(atn *ATN, idx int) { bypassStart := NewBasicBlockStartState() bypassStart.ruleIndex = idx atn.addState(bypassStart) bypassStop := NewBlockEndState() bypassStop.ruleIndex = idx atn.addState(bypassStop) bypassStart.endState = bypassStop atn.defineDecisionState(&bypassStart.BaseDecisionState) bypassStop.startState = bypassStart var excludeTransition Transition var endState ATNState if atn.ruleToStartState[idx].isPrecedenceRule { // Wrap from the beginning of the rule to the StarLoopEntryState endState = nil for i := 0; i < len(atn.states); i++ { state := atn.states[i] if a.stateIsEndStateFor(state, idx) != nil { endState = state excludeTransition = state.(*StarLoopEntryState).loopBackState.GetTransitions()[0] break } } if excludeTransition == nil { panic("Couldn't identify final state of the precedence rule prefix section.") } } else { endState = atn.ruleToStopState[idx] } // All non-excluded transitions that currently target end state need to target // blockEnd instead for i := 0; i < len(atn.states); i++ { state := atn.states[i] for j := 0; j < len(state.GetTransitions()); j++ { transition := state.GetTransitions()[j] if transition == excludeTransition { continue } if transition.getTarget() == endState { transition.setTarget(bypassStop) } } } // All transitions leaving the rule start state need to leave blockStart instead ruleToStartState := atn.ruleToStartState[idx] count := len(ruleToStartState.GetTransitions()) for count > 0 { bypassStart.AddTransition(ruleToStartState.GetTransitions()[count-1], -1) ruleToStartState.SetTransitions([]Transition{ruleToStartState.GetTransitions()[len(ruleToStartState.GetTransitions())-1]}) } // Link the new states atn.ruleToStartState[idx].AddTransition(NewEpsilonTransition(bypassStart, -1), -1) bypassStop.AddTransition(NewEpsilonTransition(endState, -1), -1) MatchState := NewBasicState() atn.addState(MatchState) MatchState.AddTransition(NewAtomTransition(bypassStop, atn.ruleToTokenType[idx]), -1) bypassStart.AddTransition(NewEpsilonTransition(MatchState, -1), -1) } func (a *ATNDeserializer) stateIsEndStateFor(state ATNState, idx int) ATNState { if state.GetRuleIndex() != idx { return nil } if _, ok := state.(*StarLoopEntryState); !ok { return nil } maybeLoopEndState := state.GetTransitions()[len(state.GetTransitions())-1].getTarget() if _, ok := maybeLoopEndState.(*LoopEndState); !ok { return nil } var _, ok = maybeLoopEndState.GetTransitions()[0].getTarget().(*RuleStopState) if maybeLoopEndState.(*LoopEndState).epsilonOnlyTransitions && ok { return state } return nil } // markPrecedenceDecisions analyzes the StarLoopEntryState states in the // specified ATN to set the StarLoopEntryState.precedenceRuleDecision field to // the correct value. func (a *ATNDeserializer) markPrecedenceDecisions(atn *ATN) { for _, state := range atn.states { if _, ok := state.(*StarLoopEntryState); !ok { continue } // We analyze the [ATN] to determine if an ATN decision state is the // decision for the closure block that determines whether a // precedence rule should continue or complete. if atn.ruleToStartState[state.GetRuleIndex()].isPrecedenceRule { maybeLoopEndState := state.GetTransitions()[len(state.GetTransitions())-1].getTarget() if s3, ok := maybeLoopEndState.(*LoopEndState); ok { var _, ok2 = maybeLoopEndState.GetTransitions()[0].getTarget().(*RuleStopState) if s3.epsilonOnlyTransitions && ok2 { state.(*StarLoopEntryState).precedenceRuleDecision = true } } } } } func (a *ATNDeserializer) verifyATN(atn *ATN) { if !a.options.VerifyATN() { return } // Verify assumptions for _, state := range atn.states { if state == nil { continue } a.checkCondition(state.GetEpsilonOnlyTransitions() || len(state.GetTransitions()) <= 1, "") switch s2 := state.(type) { case *PlusBlockStartState: a.checkCondition(s2.loopBackState != nil, "") case *StarLoopEntryState: a.checkCondition(s2.loopBackState != nil, "") a.checkCondition(len(s2.GetTransitions()) == 2, "") switch s2.transitions[0].getTarget().(type) { case *StarBlockStartState: _, ok := s2.transitions[1].getTarget().(*LoopEndState) a.checkCondition(ok, "") a.checkCondition(!s2.nonGreedy, "") case *LoopEndState: var _, ok = s2.transitions[1].getTarget().(*StarBlockStartState) a.checkCondition(ok, "") a.checkCondition(s2.nonGreedy, "") default: panic("IllegalState") } case *StarLoopbackState: a.checkCondition(len(state.GetTransitions()) == 1, "") var _, ok = state.GetTransitions()[0].getTarget().(*StarLoopEntryState) a.checkCondition(ok, "") case *LoopEndState: a.checkCondition(s2.loopBackState != nil, "") case *RuleStartState: a.checkCondition(s2.stopState != nil, "") case BlockStartState: a.checkCondition(s2.getEndState() != nil, "") case *BlockEndState: a.checkCondition(s2.startState != nil, "") case DecisionState: a.checkCondition(len(s2.GetTransitions()) <= 1 || s2.getDecision() >= 0, "") default: var _, ok = s2.(*RuleStopState) a.checkCondition(len(s2.GetTransitions()) <= 1 || ok, "") } } } func (a *ATNDeserializer) checkCondition(condition bool, message string) { if !condition { if message == "" { message = "IllegalState" } panic(message) } } func (a *ATNDeserializer) readInt() int { v := a.data[a.pos] a.pos++ return int(v) // data is 32 bits but int is at least that big } func (a *ATNDeserializer) edgeFactory(atn *ATN, typeIndex, _, trg, arg1, arg2, arg3 int, sets []*IntervalSet) Transition { target := atn.states[trg] switch typeIndex { case TransitionEPSILON: return NewEpsilonTransition(target, -1) case TransitionRANGE: if arg3 != 0 { return NewRangeTransition(target, TokenEOF, arg2) } return NewRangeTransition(target, arg1, arg2) case TransitionRULE: return NewRuleTransition(atn.states[arg1], arg2, arg3, target) case TransitionPREDICATE: return NewPredicateTransition(target, arg1, arg2, arg3 != 0) case TransitionPRECEDENCE: return NewPrecedencePredicateTransition(target, arg1) case TransitionATOM: if arg3 != 0 { return NewAtomTransition(target, TokenEOF) } return NewAtomTransition(target, arg1) case TransitionACTION: return NewActionTransition(target, arg1, arg2, arg3 != 0) case TransitionSET: return NewSetTransition(target, sets[arg1]) case TransitionNOTSET: return NewNotSetTransition(target, sets[arg1]) case TransitionWILDCARD: return NewWildcardTransition(target) } panic("The specified transition type is not valid.") } func (a *ATNDeserializer) stateFactory(typeIndex, ruleIndex int) ATNState { var s ATNState switch typeIndex { case ATNStateInvalidType: return nil case ATNStateBasic: s = NewBasicState() case ATNStateRuleStart: s = NewRuleStartState() case ATNStateBlockStart: s = NewBasicBlockStartState() case ATNStatePlusBlockStart: s = NewPlusBlockStartState() case ATNStateStarBlockStart: s = NewStarBlockStartState() case ATNStateTokenStart: s = NewTokensStartState() case ATNStateRuleStop: s = NewRuleStopState() case ATNStateBlockEnd: s = NewBlockEndState() case ATNStateStarLoopBack: s = NewStarLoopbackState() case ATNStateStarLoopEntry: s = NewStarLoopEntryState() case ATNStatePlusLoopBack: s = NewPlusLoopbackState() case ATNStateLoopEnd: s = NewLoopEndState() default: panic(fmt.Sprintf("state type %d is invalid", typeIndex)) } s.SetRuleIndex(ruleIndex) return s } func (a *ATNDeserializer) lexerActionFactory(typeIndex, data1, data2 int) LexerAction { switch typeIndex { case LexerActionTypeChannel: return NewLexerChannelAction(data1) case LexerActionTypeCustom: return NewLexerCustomAction(data1, data2) case LexerActionTypeMode: return NewLexerModeAction(data1) case LexerActionTypeMore: return LexerMoreActionINSTANCE case LexerActionTypePopMode: return LexerPopModeActionINSTANCE case LexerActionTypePushMode: return NewLexerPushModeAction(data1) case LexerActionTypeSkip: return LexerSkipActionINSTANCE case LexerActionTypeType: return NewLexerTypeAction(data1) default: panic(fmt.Sprintf("lexer action %d is invalid", typeIndex)) } } antlr4-go-antlr-c821258/atn_simulator.go 0000664 0000000 0000000 00000002262 14621155120 0020124 0 ustar 00root root 0000000 0000000 // Copyright (c) 2012-2022 The ANTLR Project. All rights reserved. // Use of this file is governed by the BSD 3-clause license that // can be found in the LICENSE.txt file in the project root. package antlr var ATNSimulatorError = NewDFAState(0x7FFFFFFF, NewATNConfigSet(false)) type IATNSimulator interface { SharedContextCache() *PredictionContextCache ATN() *ATN DecisionToDFA() []*DFA } type BaseATNSimulator struct { atn *ATN sharedContextCache *PredictionContextCache decisionToDFA []*DFA } func (b *BaseATNSimulator) getCachedContext(context *PredictionContext) *PredictionContext { if b.sharedContextCache == nil { return context } //visited := NewJMap[*PredictionContext, *PredictionContext, Comparator[*PredictionContext]](pContextEqInst, PredictionVisitedCollection, "Visit map in getCachedContext()") visited := NewVisitRecord() return getCachedBasePredictionContext(context, b.sharedContextCache, visited) } func (b *BaseATNSimulator) SharedContextCache() *PredictionContextCache { return b.sharedContextCache } func (b *BaseATNSimulator) ATN() *ATN { return b.atn } func (b *BaseATNSimulator) DecisionToDFA() []*DFA { return b.decisionToDFA } antlr4-go-antlr-c821258/atn_state.go 0000664 0000000 0000000 00000024364 14621155120 0017234 0 ustar 00root root 0000000 0000000 // Copyright (c) 2012-2022 The ANTLR Project. All rights reserved. // Use of this file is governed by the BSD 3-clause license that // can be found in the LICENSE.txt file in the project root. package antlr import ( "fmt" "os" "strconv" ) // Constants for serialization. const ( ATNStateInvalidType = 0 ATNStateBasic = 1 ATNStateRuleStart = 2 ATNStateBlockStart = 3 ATNStatePlusBlockStart = 4 ATNStateStarBlockStart = 5 ATNStateTokenStart = 6 ATNStateRuleStop = 7 ATNStateBlockEnd = 8 ATNStateStarLoopBack = 9 ATNStateStarLoopEntry = 10 ATNStatePlusLoopBack = 11 ATNStateLoopEnd = 12 ATNStateInvalidStateNumber = -1 ) //goland:noinspection GoUnusedGlobalVariable var ATNStateInitialNumTransitions = 4 type ATNState interface { GetEpsilonOnlyTransitions() bool GetRuleIndex() int SetRuleIndex(int) GetNextTokenWithinRule() *IntervalSet SetNextTokenWithinRule(*IntervalSet) GetATN() *ATN SetATN(*ATN) GetStateType() int GetStateNumber() int SetStateNumber(int) GetTransitions() []Transition SetTransitions([]Transition) AddTransition(Transition, int) String() string Hash() int Equals(Collectable[ATNState]) bool } type BaseATNState struct { // NextTokenWithinRule caches lookahead during parsing. Not used during construction. NextTokenWithinRule *IntervalSet // atn is the current ATN. atn *ATN epsilonOnlyTransitions bool // ruleIndex tracks the Rule index because there are no Rule objects at runtime. ruleIndex int stateNumber int stateType int // Track the transitions emanating from this ATN state. transitions []Transition } func NewATNState() *BaseATNState { return &BaseATNState{stateNumber: ATNStateInvalidStateNumber, stateType: ATNStateInvalidType} } func (as *BaseATNState) GetRuleIndex() int { return as.ruleIndex } func (as *BaseATNState) SetRuleIndex(v int) { as.ruleIndex = v } func (as *BaseATNState) GetEpsilonOnlyTransitions() bool { return as.epsilonOnlyTransitions } func (as *BaseATNState) GetATN() *ATN { return as.atn } func (as *BaseATNState) SetATN(atn *ATN) { as.atn = atn } func (as *BaseATNState) GetTransitions() []Transition { return as.transitions } func (as *BaseATNState) SetTransitions(t []Transition) { as.transitions = t } func (as *BaseATNState) GetStateType() int { return as.stateType } func (as *BaseATNState) GetStateNumber() int { return as.stateNumber } func (as *BaseATNState) SetStateNumber(stateNumber int) { as.stateNumber = stateNumber } func (as *BaseATNState) GetNextTokenWithinRule() *IntervalSet { return as.NextTokenWithinRule } func (as *BaseATNState) SetNextTokenWithinRule(v *IntervalSet) { as.NextTokenWithinRule = v } func (as *BaseATNState) Hash() int { return as.stateNumber } func (as *BaseATNState) String() string { return strconv.Itoa(as.stateNumber) } func (as *BaseATNState) Equals(other Collectable[ATNState]) bool { if ot, ok := other.(ATNState); ok { return as.stateNumber == ot.GetStateNumber() } return false } func (as *BaseATNState) isNonGreedyExitState() bool { return false } func (as *BaseATNState) AddTransition(trans Transition, index int) { if len(as.transitions) == 0 { as.epsilonOnlyTransitions = trans.getIsEpsilon() } else if as.epsilonOnlyTransitions != trans.getIsEpsilon() { _, _ = fmt.Fprintf(os.Stdin, "ATN state %d has both epsilon and non-epsilon transitions.\n", as.stateNumber) as.epsilonOnlyTransitions = false } // TODO: Check code for already present compared to the Java equivalent //alreadyPresent := false //for _, t := range as.transitions { // if t.getTarget().GetStateNumber() == trans.getTarget().GetStateNumber() { // if t.getLabel() != nil && trans.getLabel() != nil && trans.getLabel().Equals(t.getLabel()) { // alreadyPresent = true // break // } // } else if t.getIsEpsilon() && trans.getIsEpsilon() { // alreadyPresent = true // break // } //} //if !alreadyPresent { if index == -1 { as.transitions = append(as.transitions, trans) } else { as.transitions = append(as.transitions[:index], append([]Transition{trans}, as.transitions[index:]...)...) // TODO: as.transitions.splice(index, 1, trans) } //} else { // _, _ = fmt.Fprintf(os.Stderr, "Transition already present in state %d\n", as.stateNumber) //} } type BasicState struct { BaseATNState } func NewBasicState() *BasicState { return &BasicState{ BaseATNState: BaseATNState{ stateNumber: ATNStateInvalidStateNumber, stateType: ATNStateBasic, }, } } type DecisionState interface { ATNState getDecision() int setDecision(int) getNonGreedy() bool setNonGreedy(bool) } type BaseDecisionState struct { BaseATNState decision int nonGreedy bool } func NewBaseDecisionState() *BaseDecisionState { return &BaseDecisionState{ BaseATNState: BaseATNState{ stateNumber: ATNStateInvalidStateNumber, stateType: ATNStateBasic, }, decision: -1, } } func (s *BaseDecisionState) getDecision() int { return s.decision } func (s *BaseDecisionState) setDecision(b int) { s.decision = b } func (s *BaseDecisionState) getNonGreedy() bool { return s.nonGreedy } func (s *BaseDecisionState) setNonGreedy(b bool) { s.nonGreedy = b } type BlockStartState interface { DecisionState getEndState() *BlockEndState setEndState(*BlockEndState) } // BaseBlockStartState is the start of a regular (...) block. type BaseBlockStartState struct { BaseDecisionState endState *BlockEndState } func NewBlockStartState() *BaseBlockStartState { return &BaseBlockStartState{ BaseDecisionState: BaseDecisionState{ BaseATNState: BaseATNState{ stateNumber: ATNStateInvalidStateNumber, stateType: ATNStateBasic, }, decision: -1, }, } } func (s *BaseBlockStartState) getEndState() *BlockEndState { return s.endState } func (s *BaseBlockStartState) setEndState(b *BlockEndState) { s.endState = b } type BasicBlockStartState struct { BaseBlockStartState } func NewBasicBlockStartState() *BasicBlockStartState { return &BasicBlockStartState{ BaseBlockStartState: BaseBlockStartState{ BaseDecisionState: BaseDecisionState{ BaseATNState: BaseATNState{ stateNumber: ATNStateInvalidStateNumber, stateType: ATNStateBlockStart, }, }, }, } } var _ BlockStartState = &BasicBlockStartState{} // BlockEndState is a terminal node of a simple (a|b|c) block. type BlockEndState struct { BaseATNState startState ATNState } func NewBlockEndState() *BlockEndState { return &BlockEndState{ BaseATNState: BaseATNState{ stateNumber: ATNStateInvalidStateNumber, stateType: ATNStateBlockEnd, }, startState: nil, } } // RuleStopState is the last node in the ATN for a rule, unless that rule is the // start symbol. In that case, there is one transition to EOF. Later, we might // encode references to all calls to this rule to compute FOLLOW sets for error // handling. type RuleStopState struct { BaseATNState } func NewRuleStopState() *RuleStopState { return &RuleStopState{ BaseATNState: BaseATNState{ stateNumber: ATNStateInvalidStateNumber, stateType: ATNStateRuleStop, }, } } type RuleStartState struct { BaseATNState stopState ATNState isPrecedenceRule bool } func NewRuleStartState() *RuleStartState { return &RuleStartState{ BaseATNState: BaseATNState{ stateNumber: ATNStateInvalidStateNumber, stateType: ATNStateRuleStart, }, } } // PlusLoopbackState is a decision state for A+ and (A|B)+. It has two // transitions: one to the loop back to start of the block, and one to exit. type PlusLoopbackState struct { BaseDecisionState } func NewPlusLoopbackState() *PlusLoopbackState { return &PlusLoopbackState{ BaseDecisionState: BaseDecisionState{ BaseATNState: BaseATNState{ stateNumber: ATNStateInvalidStateNumber, stateType: ATNStatePlusLoopBack, }, }, } } // PlusBlockStartState is the start of a (A|B|...)+ loop. Technically it is a // decision state; we don't use it for code generation. Somebody might need it, // it is included for completeness. In reality, PlusLoopbackState is the real // decision-making node for A+. type PlusBlockStartState struct { BaseBlockStartState loopBackState ATNState } func NewPlusBlockStartState() *PlusBlockStartState { return &PlusBlockStartState{ BaseBlockStartState: BaseBlockStartState{ BaseDecisionState: BaseDecisionState{ BaseATNState: BaseATNState{ stateNumber: ATNStateInvalidStateNumber, stateType: ATNStatePlusBlockStart, }, }, }, } } var _ BlockStartState = &PlusBlockStartState{} // StarBlockStartState is the block that begins a closure loop. type StarBlockStartState struct { BaseBlockStartState } func NewStarBlockStartState() *StarBlockStartState { return &StarBlockStartState{ BaseBlockStartState: BaseBlockStartState{ BaseDecisionState: BaseDecisionState{ BaseATNState: BaseATNState{ stateNumber: ATNStateInvalidStateNumber, stateType: ATNStateStarBlockStart, }, }, }, } } var _ BlockStartState = &StarBlockStartState{} type StarLoopbackState struct { BaseATNState } func NewStarLoopbackState() *StarLoopbackState { return &StarLoopbackState{ BaseATNState: BaseATNState{ stateNumber: ATNStateInvalidStateNumber, stateType: ATNStateStarLoopBack, }, } } type StarLoopEntryState struct { BaseDecisionState loopBackState ATNState precedenceRuleDecision bool } func NewStarLoopEntryState() *StarLoopEntryState { // False precedenceRuleDecision indicates whether s state can benefit from a precedence DFA during SLL decision making. return &StarLoopEntryState{ BaseDecisionState: BaseDecisionState{ BaseATNState: BaseATNState{ stateNumber: ATNStateInvalidStateNumber, stateType: ATNStateStarLoopEntry, }, }, } } // LoopEndState marks the end of a * or + loop. type LoopEndState struct { BaseATNState loopBackState ATNState } func NewLoopEndState() *LoopEndState { return &LoopEndState{ BaseATNState: BaseATNState{ stateNumber: ATNStateInvalidStateNumber, stateType: ATNStateLoopEnd, }, } } // TokensStartState is the Tokens rule start state linking to each lexer rule start state. type TokensStartState struct { BaseDecisionState } func NewTokensStartState() *TokensStartState { return &TokensStartState{ BaseDecisionState: BaseDecisionState{ BaseATNState: BaseATNState{ stateNumber: ATNStateInvalidStateNumber, stateType: ATNStateTokenStart, }, }, } } antlr4-go-antlr-c821258/atn_type.go 0000664 0000000 0000000 00000000470 14621155120 0017065 0 ustar 00root root 0000000 0000000 // Copyright (c) 2012-2022 The ANTLR Project. All rights reserved. // Use of this file is governed by the BSD 3-clause license that // can be found in the LICENSE.txt file in the project root. package antlr // Represent the type of recognizer an ATN applies to. const ( ATNTypeLexer = 0 ATNTypeParser = 1 ) antlr4-go-antlr-c821258/char_stream.go 0000664 0000000 0000000 00000000546 14621155120 0017536 0 ustar 00root root 0000000 0000000 // Copyright (c) 2012-2022 The ANTLR Project. All rights reserved. // Use of this file is governed by the BSD 3-clause license that // can be found in the LICENSE.txt file in the project root. package antlr type CharStream interface { IntStream GetText(int, int) string GetTextFromTokens(start, end Token) string GetTextFromInterval(Interval) string } antlr4-go-antlr-c821258/common_token_factory.go 0000664 0000000 0000000 00000004060 14621155120 0021460 0 ustar 00root root 0000000 0000000 // Copyright (c) 2012-2022 The ANTLR Project. All rights reserved. // Use of this file is governed by the BSD 3-clause license that // can be found in the LICENSE.txt file in the project root. package antlr // TokenFactory creates CommonToken objects. type TokenFactory interface { Create(source *TokenSourceCharStreamPair, ttype int, text string, channel, start, stop, line, column int) Token } // CommonTokenFactory is the default TokenFactory implementation. type CommonTokenFactory struct { // copyText indicates whether CommonToken.setText should be called after // constructing tokens to explicitly set the text. This is useful for cases // where the input stream might not be able to provide arbitrary substrings of // text from the input after the lexer creates a token (e.g. the // implementation of CharStream.GetText in UnbufferedCharStream panics an // UnsupportedOperationException). Explicitly setting the token text allows // Token.GetText to be called at any time regardless of the input stream // implementation. // // The default value is false to avoid the performance and memory overhead of // copying text for every token unless explicitly requested. copyText bool } func NewCommonTokenFactory(copyText bool) *CommonTokenFactory { return &CommonTokenFactory{copyText: copyText} } // CommonTokenFactoryDEFAULT is the default CommonTokenFactory. It does not // explicitly copy token text when constructing tokens. var CommonTokenFactoryDEFAULT = NewCommonTokenFactory(false) func (c *CommonTokenFactory) Create(source *TokenSourceCharStreamPair, ttype int, text string, channel, start, stop, line, column int) Token { t := NewCommonToken(source, ttype, channel, start, stop) t.line = line t.column = column if text != "" { t.SetText(text) } else if c.copyText && source.charStream != nil { t.SetText(source.charStream.GetTextFromInterval(NewInterval(start, stop))) } return t } func (c *CommonTokenFactory) createThin(ttype int, text string) Token { t := NewCommonToken(nil, ttype, TokenDefaultChannel, -1, -1) t.SetText(text) return t } antlr4-go-antlr-c821258/common_token_stream.go 0000664 0000000 0000000 00000024233 14621155120 0021310 0 ustar 00root root 0000000 0000000 // Copyright (c) 2012-2022 The ANTLR Project. All rights reserved. // Use of this file is governed by the BSD 3-clause license that // can be found in the LICENSE.txt file in the project root. package antlr import ( "strconv" ) // CommonTokenStream is an implementation of TokenStream that loads tokens from // a TokenSource on-demand and places the tokens in a buffer to provide access // to any previous token by index. This token stream ignores the value of // Token.getChannel. If your parser requires the token stream filter tokens to // only those on a particular channel, such as Token.DEFAULT_CHANNEL or // Token.HIDDEN_CHANNEL, use a filtering token stream such a CommonTokenStream. type CommonTokenStream struct { channel int // fetchedEOF indicates whether the Token.EOF token has been fetched from // tokenSource and added to tokens. This field improves performance for the // following cases: // // consume: The lookahead check in consume to preven consuming the EOF symbol is // optimized by checking the values of fetchedEOF and p instead of calling LA. // // fetch: The check to prevent adding multiple EOF symbols into tokens is // trivial with bt field. fetchedEOF bool // index into [tokens] of the current token (next token to consume). // tokens[p] should be LT(1). It is set to -1 when the stream is first // constructed or when SetTokenSource is called, indicating that the first token // has not yet been fetched from the token source. For additional information, // see the documentation of [IntStream] for a description of initializing methods. index int // tokenSource is the [TokenSource] from which tokens for the bt stream are // fetched. tokenSource TokenSource // tokens contains all tokens fetched from the token source. The list is considered a // complete view of the input once fetchedEOF is set to true. tokens []Token } // NewCommonTokenStream creates a new CommonTokenStream instance using the supplied lexer to produce // tokens and will pull tokens from the given lexer channel. func NewCommonTokenStream(lexer Lexer, channel int) *CommonTokenStream { return &CommonTokenStream{ channel: channel, index: -1, tokenSource: lexer, tokens: make([]Token, 0), } } // GetAllTokens returns all tokens currently pulled from the token source. func (c *CommonTokenStream) GetAllTokens() []Token { return c.tokens } func (c *CommonTokenStream) Mark() int { return 0 } func (c *CommonTokenStream) Release(_ int) {} func (c *CommonTokenStream) Reset() { c.fetchedEOF = false c.tokens = make([]Token, 0) c.Seek(0) } func (c *CommonTokenStream) Seek(index int) { c.lazyInit() c.index = c.adjustSeekIndex(index) } func (c *CommonTokenStream) Get(index int) Token { c.lazyInit() return c.tokens[index] } func (c *CommonTokenStream) Consume() { SkipEOFCheck := false if c.index >= 0 { if c.fetchedEOF { // The last token in tokens is EOF. Skip the check if p indexes any fetched. // token except the last. SkipEOFCheck = c.index < len(c.tokens)-1 } else { // No EOF token in tokens. Skip the check if p indexes a fetched token. SkipEOFCheck = c.index < len(c.tokens) } } else { // Not yet initialized SkipEOFCheck = false } if !SkipEOFCheck && c.LA(1) == TokenEOF { panic("cannot consume EOF") } if c.Sync(c.index + 1) { c.index = c.adjustSeekIndex(c.index + 1) } } // Sync makes sure index i in tokens has a token and returns true if a token is // located at index i and otherwise false. func (c *CommonTokenStream) Sync(i int) bool { n := i - len(c.tokens) + 1 // How many more elements do we need? if n > 0 { fetched := c.fetch(n) return fetched >= n } return true } // fetch adds n elements to buffer and returns the actual number of elements // added to the buffer. func (c *CommonTokenStream) fetch(n int) int { if c.fetchedEOF { return 0 } for i := 0; i < n; i++ { t := c.tokenSource.NextToken() t.SetTokenIndex(len(c.tokens)) c.tokens = append(c.tokens, t) if t.GetTokenType() == TokenEOF { c.fetchedEOF = true return i + 1 } } return n } // GetTokens gets all tokens from start to stop inclusive. func (c *CommonTokenStream) GetTokens(start int, stop int, types *IntervalSet) []Token { if start < 0 || stop < 0 { return nil } c.lazyInit() subset := make([]Token, 0) if stop >= len(c.tokens) { stop = len(c.tokens) - 1 } for i := start; i < stop; i++ { t := c.tokens[i] if t.GetTokenType() == TokenEOF { break } if types == nil || types.contains(t.GetTokenType()) { subset = append(subset, t) } } return subset } func (c *CommonTokenStream) LA(i int) int { return c.LT(i).GetTokenType() } func (c *CommonTokenStream) lazyInit() { if c.index == -1 { c.setup() } } func (c *CommonTokenStream) setup() { c.Sync(0) c.index = c.adjustSeekIndex(0) } func (c *CommonTokenStream) GetTokenSource() TokenSource { return c.tokenSource } // SetTokenSource resets the c token stream by setting its token source. func (c *CommonTokenStream) SetTokenSource(tokenSource TokenSource) { c.tokenSource = tokenSource c.tokens = make([]Token, 0) c.index = -1 c.fetchedEOF = false } // NextTokenOnChannel returns the index of the next token on channel given a // starting index. Returns i if tokens[i] is on channel. Returns -1 if there are // no tokens on channel between 'i' and [TokenEOF]. func (c *CommonTokenStream) NextTokenOnChannel(i, _ int) int { c.Sync(i) if i >= len(c.tokens) { return -1 } token := c.tokens[i] for token.GetChannel() != c.channel { if token.GetTokenType() == TokenEOF { return -1 } i++ c.Sync(i) token = c.tokens[i] } return i } // previousTokenOnChannel returns the index of the previous token on channel // given a starting index. Returns i if tokens[i] is on channel. Returns -1 if // there are no tokens on channel between i and 0. func (c *CommonTokenStream) previousTokenOnChannel(i, channel int) int { for i >= 0 && c.tokens[i].GetChannel() != channel { i-- } return i } // GetHiddenTokensToRight collects all tokens on a specified channel to the // right of the current token up until we see a token on DEFAULT_TOKEN_CHANNEL // or EOF. If channel is -1, it finds any non-default channel token. func (c *CommonTokenStream) GetHiddenTokensToRight(tokenIndex, channel int) []Token { c.lazyInit() if tokenIndex < 0 || tokenIndex >= len(c.tokens) { panic(strconv.Itoa(tokenIndex) + " not in 0.." + strconv.Itoa(len(c.tokens)-1)) } nextOnChannel := c.NextTokenOnChannel(tokenIndex+1, LexerDefaultTokenChannel) from := tokenIndex + 1 // If no onChannel to the right, then nextOnChannel == -1, so set 'to' to the last token var to int if nextOnChannel == -1 { to = len(c.tokens) - 1 } else { to = nextOnChannel } return c.filterForChannel(from, to, channel) } // GetHiddenTokensToLeft collects all tokens on channel to the left of the // current token until we see a token on DEFAULT_TOKEN_CHANNEL. If channel is // -1, it finds any non default channel token. func (c *CommonTokenStream) GetHiddenTokensToLeft(tokenIndex, channel int) []Token { c.lazyInit() if tokenIndex < 0 || tokenIndex >= len(c.tokens) { panic(strconv.Itoa(tokenIndex) + " not in 0.." + strconv.Itoa(len(c.tokens)-1)) } prevOnChannel := c.previousTokenOnChannel(tokenIndex-1, LexerDefaultTokenChannel) if prevOnChannel == tokenIndex-1 { return nil } // If there are none on channel to the left and prevOnChannel == -1 then from = 0 from := prevOnChannel + 1 to := tokenIndex - 1 return c.filterForChannel(from, to, channel) } func (c *CommonTokenStream) filterForChannel(left, right, channel int) []Token { hidden := make([]Token, 0) for i := left; i < right+1; i++ { t := c.tokens[i] if channel == -1 { if t.GetChannel() != LexerDefaultTokenChannel { hidden = append(hidden, t) } } else if t.GetChannel() == channel { hidden = append(hidden, t) } } if len(hidden) == 0 { return nil } return hidden } func (c *CommonTokenStream) GetSourceName() string { return c.tokenSource.GetSourceName() } func (c *CommonTokenStream) Size() int { return len(c.tokens) } func (c *CommonTokenStream) Index() int { return c.index } func (c *CommonTokenStream) GetAllText() string { c.Fill() return c.GetTextFromInterval(NewInterval(0, len(c.tokens)-1)) } func (c *CommonTokenStream) GetTextFromTokens(start, end Token) string { if start == nil || end == nil { return "" } return c.GetTextFromInterval(NewInterval(start.GetTokenIndex(), end.GetTokenIndex())) } func (c *CommonTokenStream) GetTextFromRuleContext(interval RuleContext) string { return c.GetTextFromInterval(interval.GetSourceInterval()) } func (c *CommonTokenStream) GetTextFromInterval(interval Interval) string { c.lazyInit() c.Sync(interval.Stop) start := interval.Start stop := interval.Stop if start < 0 || stop < 0 { return "" } if stop >= len(c.tokens) { stop = len(c.tokens) - 1 } s := "" for i := start; i < stop+1; i++ { t := c.tokens[i] if t.GetTokenType() == TokenEOF { break } s += t.GetText() } return s } // Fill gets all tokens from the lexer until EOF. func (c *CommonTokenStream) Fill() { c.lazyInit() for c.fetch(1000) == 1000 { continue } } func (c *CommonTokenStream) adjustSeekIndex(i int) int { return c.NextTokenOnChannel(i, c.channel) } func (c *CommonTokenStream) LB(k int) Token { if k == 0 || c.index-k < 0 { return nil } i := c.index n := 1 // Find k good tokens looking backward for n <= k { // Skip off-channel tokens i = c.previousTokenOnChannel(i-1, c.channel) n++ } if i < 0 { return nil } return c.tokens[i] } func (c *CommonTokenStream) LT(k int) Token { c.lazyInit() if k == 0 { return nil } if k < 0 { return c.LB(-k) } i := c.index n := 1 // We know tokens[n] is valid // Find k good tokens for n < k { // Skip off-channel tokens, but make sure to not look past EOF if c.Sync(i + 1) { i = c.NextTokenOnChannel(i+1, c.channel) } n++ } return c.tokens[i] } // getNumberOfOnChannelTokens counts EOF once. func (c *CommonTokenStream) getNumberOfOnChannelTokens() int { var n int c.Fill() for i := 0; i < len(c.tokens); i++ { t := c.tokens[i] if t.GetChannel() == c.channel { n++ } if t.GetTokenType() == TokenEOF { break } } return n } antlr4-go-antlr-c821258/comparators.go 0000664 0000000 0000000 00000013077 14621155120 0017603 0 ustar 00root root 0000000 0000000 package antlr // Copyright (c) 2012-2022 The ANTLR Project. All rights reserved. // Use of this file is governed by the BSD 3-clause license that // can be found in the LICENSE.txt file in the project root. // This file contains all the implementations of custom comparators used for generic collections when the // Hash() and Equals() funcs supplied by the struct objects themselves need to be overridden. Normally, we would // put the comparators in the source file for the struct themselves, but given the organization of this code is // sorta kinda based upon the Java code, I found it confusing trying to find out which comparator was where and used by // which instantiation of a collection. For instance, an Array2DHashSet in the Java source, when used with ATNConfig // collections requires three different comparators depending on what the collection is being used for. Collecting - pun intended - // all the comparators here, makes it much easier to see which implementation of hash and equals is used by which collection. // It also makes it easy to verify that the Hash() and Equals() functions marry up with the Java implementations. // ObjEqComparator is the equivalent of the Java ObjectEqualityComparator, which is the default instance of // Equality comparator. We do not have inheritance in Go, only interfaces, so we use generics to enforce some // type safety and avoid having to implement this for every type that we want to perform comparison on. // // This comparator works by using the standard Hash() and Equals() methods of the type T that is being compared. Which // allows us to use it in any collection instance that does not require a special hash or equals implementation. type ObjEqComparator[T Collectable[T]] struct{} var ( aStateEqInst = &ObjEqComparator[ATNState]{} aConfEqInst = &ObjEqComparator[*ATNConfig]{} // aConfCompInst is the comparator used for the ATNConfigSet for the configLookup cache aConfCompInst = &ATNConfigComparator[*ATNConfig]{} atnConfCompInst = &BaseATNConfigComparator[*ATNConfig]{} dfaStateEqInst = &ObjEqComparator[*DFAState]{} semctxEqInst = &ObjEqComparator[SemanticContext]{} atnAltCfgEqInst = &ATNAltConfigComparator[*ATNConfig]{} pContextEqInst = &ObjEqComparator[*PredictionContext]{} ) // Equals2 delegates to the Equals() method of type T func (c *ObjEqComparator[T]) Equals2(o1, o2 T) bool { return o1.Equals(o2) } // Hash1 delegates to the Hash() method of type T func (c *ObjEqComparator[T]) Hash1(o T) int { return o.Hash() } type SemCComparator[T Collectable[T]] struct{} // ATNConfigComparator is used as the comparator for the configLookup field of an ATNConfigSet // and has a custom Equals() and Hash() implementation, because equality is not based on the // standard Hash() and Equals() methods of the ATNConfig type. type ATNConfigComparator[T Collectable[T]] struct { } // Equals2 is a custom comparator for ATNConfigs specifically for configLookup func (c *ATNConfigComparator[T]) Equals2(o1, o2 *ATNConfig) bool { // Same pointer, must be equal, even if both nil // if o1 == o2 { return true } // If either are nil, but not both, then the result is false // if o1 == nil || o2 == nil { return false } return o1.GetState().GetStateNumber() == o2.GetState().GetStateNumber() && o1.GetAlt() == o2.GetAlt() && o1.GetSemanticContext().Equals(o2.GetSemanticContext()) } // Hash1 is custom hash implementation for ATNConfigs specifically for configLookup func (c *ATNConfigComparator[T]) Hash1(o *ATNConfig) int { hash := 7 hash = 31*hash + o.GetState().GetStateNumber() hash = 31*hash + o.GetAlt() hash = 31*hash + o.GetSemanticContext().Hash() return hash } // ATNAltConfigComparator is used as the comparator for mapping configs to Alt Bitsets type ATNAltConfigComparator[T Collectable[T]] struct { } // Equals2 is a custom comparator for ATNConfigs specifically for configLookup func (c *ATNAltConfigComparator[T]) Equals2(o1, o2 *ATNConfig) bool { // Same pointer, must be equal, even if both nil // if o1 == o2 { return true } // If either are nil, but not both, then the result is false // if o1 == nil || o2 == nil { return false } return o1.GetState().GetStateNumber() == o2.GetState().GetStateNumber() && o1.GetContext().Equals(o2.GetContext()) } // Hash1 is custom hash implementation for ATNConfigs specifically for configLookup func (c *ATNAltConfigComparator[T]) Hash1(o *ATNConfig) int { h := murmurInit(7) h = murmurUpdate(h, o.GetState().GetStateNumber()) h = murmurUpdate(h, o.GetContext().Hash()) return murmurFinish(h, 2) } // BaseATNConfigComparator is used as the comparator for the configLookup field of a ATNConfigSet // and has a custom Equals() and Hash() implementation, because equality is not based on the // standard Hash() and Equals() methods of the ATNConfig type. type BaseATNConfigComparator[T Collectable[T]] struct { } // Equals2 is a custom comparator for ATNConfigs specifically for baseATNConfigSet func (c *BaseATNConfigComparator[T]) Equals2(o1, o2 *ATNConfig) bool { // Same pointer, must be equal, even if both nil // if o1 == o2 { return true } // If either are nil, but not both, then the result is false // if o1 == nil || o2 == nil { return false } return o1.GetState().GetStateNumber() == o2.GetState().GetStateNumber() && o1.GetAlt() == o2.GetAlt() && o1.GetSemanticContext().Equals(o2.GetSemanticContext()) } // Hash1 is custom hash implementation for ATNConfigs specifically for configLookup, but in fact just // delegates to the standard Hash() method of the ATNConfig type. func (c *BaseATNConfigComparator[T]) Hash1(o *ATNConfig) int { return o.Hash() } antlr4-go-antlr-c821258/configuration.go 0000664 0000000 0000000 00000017730 14621155120 0020120 0 ustar 00root root 0000000 0000000 package antlr type runtimeConfiguration struct { statsTraceStacks bool lexerATNSimulatorDebug bool lexerATNSimulatorDFADebug bool parserATNSimulatorDebug bool parserATNSimulatorTraceATNSim bool parserATNSimulatorDFADebug bool parserATNSimulatorRetryDebug bool lRLoopEntryBranchOpt bool memoryManager bool } // Global runtime configuration var runtimeConfig = runtimeConfiguration{ lRLoopEntryBranchOpt: true, } type runtimeOption func(*runtimeConfiguration) error // ConfigureRuntime allows the runtime to be configured globally setting things like trace and statistics options. // It uses the functional options pattern for go. This is a package global function as it operates on the runtime // configuration regardless of the instantiation of anything higher up such as a parser or lexer. Generally this is // used for debugging/tracing/statistics options, which are usually used by the runtime maintainers (or rather the // only maintainer). However, it is possible that you might want to use this to set a global option concerning the // memory allocation type used by the runtime such as sync.Pool or not. // // The options are applied in the order they are passed in, so the last option will override any previous options. // // For example, if you want to turn on the collection create point stack flag to true, you can do: // // antlr.ConfigureRuntime(antlr.WithStatsTraceStacks(true)) // // If you want to turn it off, you can do: // // antlr.ConfigureRuntime(antlr.WithStatsTraceStacks(false)) func ConfigureRuntime(options ...runtimeOption) error { for _, option := range options { err := option(&runtimeConfig) if err != nil { return err } } return nil } // WithStatsTraceStacks sets the global flag indicating whether to collect stack traces at the create-point of // certain structs, such as collections, or the use point of certain methods such as Put(). // Because this can be expensive, it is turned off by default. However, it // can be useful to track down exactly where memory is being created and used. // // Use: // // antlr.ConfigureRuntime(antlr.WithStatsTraceStacks(true)) // // You can turn it off at any time using: // // antlr.ConfigureRuntime(antlr.WithStatsTraceStacks(false)) func WithStatsTraceStacks(trace bool) runtimeOption { return func(config *runtimeConfiguration) error { config.statsTraceStacks = trace return nil } } // WithLexerATNSimulatorDebug sets the global flag indicating whether to log debug information from the lexer [ATN] // simulator. This is useful for debugging lexer issues by comparing the output with the Java runtime. Only useful // to the runtime maintainers. // // Use: // // antlr.ConfigureRuntime(antlr.WithLexerATNSimulatorDebug(true)) // // You can turn it off at any time using: // // antlr.ConfigureRuntime(antlr.WithLexerATNSimulatorDebug(false)) func WithLexerATNSimulatorDebug(debug bool) runtimeOption { return func(config *runtimeConfiguration) error { config.lexerATNSimulatorDebug = debug return nil } } // WithLexerATNSimulatorDFADebug sets the global flag indicating whether to log debug information from the lexer [ATN] [DFA] // simulator. This is useful for debugging lexer issues by comparing the output with the Java runtime. Only useful // to the runtime maintainers. // // Use: // // antlr.ConfigureRuntime(antlr.WithLexerATNSimulatorDFADebug(true)) // // You can turn it off at any time using: // // antlr.ConfigureRuntime(antlr.WithLexerATNSimulatorDFADebug(false)) func WithLexerATNSimulatorDFADebug(debug bool) runtimeOption { return func(config *runtimeConfiguration) error { config.lexerATNSimulatorDFADebug = debug return nil } } // WithParserATNSimulatorDebug sets the global flag indicating whether to log debug information from the parser [ATN] // simulator. This is useful for debugging parser issues by comparing the output with the Java runtime. Only useful // to the runtime maintainers. // // Use: // // antlr.ConfigureRuntime(antlr.WithParserATNSimulatorDebug(true)) // // You can turn it off at any time using: // // antlr.ConfigureRuntime(antlr.WithParserATNSimulatorDebug(false)) func WithParserATNSimulatorDebug(debug bool) runtimeOption { return func(config *runtimeConfiguration) error { config.parserATNSimulatorDebug = debug return nil } } // WithParserATNSimulatorTraceATNSim sets the global flag indicating whether to log trace information from the parser [ATN] simulator // [DFA]. This is useful for debugging parser issues by comparing the output with the Java runtime. Only useful // to the runtime maintainers. // // Use: // // antlr.ConfigureRuntime(antlr.WithParserATNSimulatorTraceATNSim(true)) // // You can turn it off at any time using: // // antlr.ConfigureRuntime(antlr.WithParserATNSimulatorTraceATNSim(false)) func WithParserATNSimulatorTraceATNSim(trace bool) runtimeOption { return func(config *runtimeConfiguration) error { config.parserATNSimulatorTraceATNSim = trace return nil } } // WithParserATNSimulatorDFADebug sets the global flag indicating whether to log debug information from the parser [ATN] [DFA] // simulator. This is useful for debugging parser issues by comparing the output with the Java runtime. Only useful // to the runtime maintainers. // // Use: // // antlr.ConfigureRuntime(antlr.WithParserATNSimulatorDFADebug(true)) // // You can turn it off at any time using: // // antlr.ConfigureRuntime(antlr.WithParserATNSimulatorDFADebug(false)) func WithParserATNSimulatorDFADebug(debug bool) runtimeOption { return func(config *runtimeConfiguration) error { config.parserATNSimulatorDFADebug = debug return nil } } // WithParserATNSimulatorRetryDebug sets the global flag indicating whether to log debug information from the parser [ATN] [DFA] // simulator when retrying a decision. This is useful for debugging parser issues by comparing the output with the Java runtime. // Only useful to the runtime maintainers. // // Use: // // antlr.ConfigureRuntime(antlr.WithParserATNSimulatorRetryDebug(true)) // // You can turn it off at any time using: // // antlr.ConfigureRuntime(antlr.WithParserATNSimulatorRetryDebug(false)) func WithParserATNSimulatorRetryDebug(debug bool) runtimeOption { return func(config *runtimeConfiguration) error { config.parserATNSimulatorRetryDebug = debug return nil } } // WithLRLoopEntryBranchOpt sets the global flag indicating whether let recursive loop operations should be // optimized or not. This is useful for debugging parser issues by comparing the output with the Java runtime. // It turns off the functionality of [canDropLoopEntryEdgeInLeftRecursiveRule] in [ParserATNSimulator]. // // Note that default is to use this optimization. // // Use: // // antlr.ConfigureRuntime(antlr.WithLRLoopEntryBranchOpt(true)) // // You can turn it off at any time using: // // antlr.ConfigureRuntime(antlr.WithLRLoopEntryBranchOpt(false)) func WithLRLoopEntryBranchOpt(off bool) runtimeOption { return func(config *runtimeConfiguration) error { config.lRLoopEntryBranchOpt = off return nil } } // WithMemoryManager sets the global flag indicating whether to use the memory manager or not. This is useful // for poorly constructed grammars that create a lot of garbage. It turns on the functionality of [memoryManager], which // will intercept garbage collection and cause available memory to be reused. At the end of the day, this is no substitute // for fixing your grammar by ridding yourself of extreme ambiguity. BUt if you are just trying to reuse an opensource // grammar, this may help make it more practical. // // Note that default is to use normal Go memory allocation and not pool memory. // // Use: // // antlr.ConfigureRuntime(antlr.WithMemoryManager(true)) // // Note that if you turn this on, you should probably leave it on. You should use only one memory strategy or the other // and should remember to nil out any references to the parser or lexer when you are done with them. func WithMemoryManager(use bool) runtimeOption { return func(config *runtimeConfiguration) error { config.memoryManager = use return nil } } antlr4-go-antlr-c821258/dfa.go 0000664 0000000 0000000 00000012470 14621155120 0015777 0 ustar 00root root 0000000 0000000 // Copyright (c) 2012-2022 The ANTLR Project. All rights reserved. // Use of this file is governed by the BSD 3-clause license that // can be found in the LICENSE.txt file in the project root. package antlr // DFA represents the Deterministic Finite Automaton used by the recognizer, including all the states it can // reach and the transitions between them. type DFA struct { // atnStartState is the ATN state in which this was created atnStartState DecisionState decision int // states is all the DFA states. Use Map to get the old state back; Set can only // indicate whether it is there. Go maps implement key hash collisions and so on and are very // good, but the DFAState is an object and can't be used directly as the key as it can in say Java // amd C#, whereby if the hashcode is the same for two objects, then Equals() is called against them // to see if they really are the same object. Hence, we have our own map storage. // states *JStore[*DFAState, *ObjEqComparator[*DFAState]] numstates int s0 *DFAState // precedenceDfa is the backing field for isPrecedenceDfa and setPrecedenceDfa. // True if the DFA is for a precedence decision and false otherwise. precedenceDfa bool } func NewDFA(atnStartState DecisionState, decision int) *DFA { dfa := &DFA{ atnStartState: atnStartState, decision: decision, states: nil, // Lazy initialize } if s, ok := atnStartState.(*StarLoopEntryState); ok && s.precedenceRuleDecision { dfa.precedenceDfa = true dfa.s0 = NewDFAState(-1, NewATNConfigSet(false)) dfa.s0.isAcceptState = false dfa.s0.requiresFullContext = false } return dfa } // getPrecedenceStartState gets the start state for the current precedence and // returns the start state corresponding to the specified precedence if a start // state exists for the specified precedence and nil otherwise. d must be a // precedence DFA. See also isPrecedenceDfa. func (d *DFA) getPrecedenceStartState(precedence int) *DFAState { if !d.getPrecedenceDfa() { panic("only precedence DFAs may contain a precedence start state") } // s0.edges is never nil for a precedence DFA if precedence < 0 || precedence >= len(d.getS0().getEdges()) { return nil } return d.getS0().getIthEdge(precedence) } // setPrecedenceStartState sets the start state for the current precedence. d // must be a precedence DFA. See also isPrecedenceDfa. func (d *DFA) setPrecedenceStartState(precedence int, startState *DFAState) { if !d.getPrecedenceDfa() { panic("only precedence DFAs may contain a precedence start state") } if precedence < 0 { return } // Synchronization on s0 here is ok. When the DFA is turned into a // precedence DFA, s0 will be initialized once and not updated again. s0.edges // is never nil for a precedence DFA. s0 := d.getS0() if precedence >= s0.numEdges() { edges := append(s0.getEdges(), make([]*DFAState, precedence+1-s0.numEdges())...) s0.setEdges(edges) d.setS0(s0) } s0.setIthEdge(precedence, startState) } func (d *DFA) getPrecedenceDfa() bool { return d.precedenceDfa } // setPrecedenceDfa sets whether d is a precedence DFA. If precedenceDfa differs // from the current DFA configuration, then d.states is cleared, the initial // state s0 is set to a new DFAState with an empty outgoing DFAState.edges to // store the start states for individual precedence values if precedenceDfa is // true or nil otherwise, and d.precedenceDfa is updated. func (d *DFA) setPrecedenceDfa(precedenceDfa bool) { if d.getPrecedenceDfa() != precedenceDfa { d.states = nil // Lazy initialize d.numstates = 0 if precedenceDfa { precedenceState := NewDFAState(-1, NewATNConfigSet(false)) precedenceState.setEdges(make([]*DFAState, 0)) precedenceState.isAcceptState = false precedenceState.requiresFullContext = false d.setS0(precedenceState) } else { d.setS0(nil) } d.precedenceDfa = precedenceDfa } } // Len returns the number of states in d. We use this instead of accessing states directly so that we can implement lazy // instantiation of the states JMap. func (d *DFA) Len() int { if d.states == nil { return 0 } return d.states.Len() } // Get returns a state that matches s if it is present in the DFA state set. We defer to this // function instead of accessing states directly so that we can implement lazy instantiation of the states JMap. func (d *DFA) Get(s *DFAState) (*DFAState, bool) { if d.states == nil { return nil, false } return d.states.Get(s) } func (d *DFA) Put(s *DFAState) (*DFAState, bool) { if d.states == nil { d.states = NewJStore[*DFAState, *ObjEqComparator[*DFAState]](dfaStateEqInst, DFAStateCollection, "DFA via DFA.Put") } return d.states.Put(s) } func (d *DFA) getS0() *DFAState { return d.s0 } func (d *DFA) setS0(s *DFAState) { d.s0 = s } // sortedStates returns the states in d sorted by their state number, or an empty set if d.states is nil. func (d *DFA) sortedStates() []*DFAState { if d.states == nil { return []*DFAState{} } vs := d.states.SortedSlice(func(i, j *DFAState) bool { return i.stateNumber < j.stateNumber }) return vs } func (d *DFA) String(literalNames []string, symbolicNames []string) string { if d.getS0() == nil { return "" } return NewDFASerializer(d, literalNames, symbolicNames).String() } func (d *DFA) ToLexerString() string { if d.getS0() == nil { return "" } return NewLexerDFASerializer(d).String() } antlr4-go-antlr-c821258/dfa_serializer.go 0000664 0000000 0000000 00000005553 14621155120 0020234 0 ustar 00root root 0000000 0000000 // Copyright (c) 2012-2022 The ANTLR Project. All rights reserved. // Use of this file is governed by the BSD 3-clause license that // can be found in the LICENSE.txt file in the project root. package antlr import ( "fmt" "strconv" "strings" ) // DFASerializer is a DFA walker that knows how to dump the DFA states to serialized // strings. type DFASerializer struct { dfa *DFA literalNames []string symbolicNames []string } func NewDFASerializer(dfa *DFA, literalNames, symbolicNames []string) *DFASerializer { if literalNames == nil { literalNames = make([]string, 0) } if symbolicNames == nil { symbolicNames = make([]string, 0) } return &DFASerializer{ dfa: dfa, literalNames: literalNames, symbolicNames: symbolicNames, } } func (d *DFASerializer) String() string { if d.dfa.getS0() == nil { return "" } buf := "" states := d.dfa.sortedStates() for _, s := range states { if s.edges != nil { n := len(s.edges) for j := 0; j < n; j++ { t := s.edges[j] if t != nil && t.stateNumber != 0x7FFFFFFF { buf += d.GetStateString(s) buf += "-" buf += d.getEdgeLabel(j) buf += "->" buf += d.GetStateString(t) buf += "\n" } } } } if len(buf) == 0 { return "" } return buf } func (d *DFASerializer) getEdgeLabel(i int) string { if i == 0 { return "EOF" } else if d.literalNames != nil && i-1 < len(d.literalNames) { return d.literalNames[i-1] } else if d.symbolicNames != nil && i-1 < len(d.symbolicNames) { return d.symbolicNames[i-1] } return strconv.Itoa(i - 1) } func (d *DFASerializer) GetStateString(s *DFAState) string { var a, b string if s.isAcceptState { a = ":" } if s.requiresFullContext { b = "^" } baseStateStr := a + "s" + strconv.Itoa(s.stateNumber) + b if s.isAcceptState { if s.predicates != nil { return baseStateStr + "=>" + fmt.Sprint(s.predicates) } return baseStateStr + "=>" + fmt.Sprint(s.prediction) } return baseStateStr } type LexerDFASerializer struct { *DFASerializer } func NewLexerDFASerializer(dfa *DFA) *LexerDFASerializer { return &LexerDFASerializer{DFASerializer: NewDFASerializer(dfa, nil, nil)} } func (l *LexerDFASerializer) getEdgeLabel(i int) string { var sb strings.Builder sb.Grow(6) sb.WriteByte('\'') sb.WriteRune(rune(i)) sb.WriteByte('\'') return sb.String() } func (l *LexerDFASerializer) String() string { if l.dfa.getS0() == nil { return "" } buf := "" states := l.dfa.sortedStates() for i := 0; i < len(states); i++ { s := states[i] if s.edges != nil { n := len(s.edges) for j := 0; j < n; j++ { t := s.edges[j] if t != nil && t.stateNumber != 0x7FFFFFFF { buf += l.GetStateString(s) buf += "-" buf += l.getEdgeLabel(j) buf += "->" buf += l.GetStateString(t) buf += "\n" } } } } if len(buf) == 0 { return "" } return buf } antlr4-go-antlr-c821258/dfa_state.go 0000664 0000000 0000000 00000012336 14621155120 0017200 0 ustar 00root root 0000000 0000000 // Copyright (c) 2012-2022 The ANTLR Project. All rights reserved. // Use of this file is governed by the BSD 3-clause license that // can be found in the LICENSE.txt file in the project root. package antlr import ( "fmt" ) // PredPrediction maps a predicate to a predicted alternative. type PredPrediction struct { alt int pred SemanticContext } func NewPredPrediction(pred SemanticContext, alt int) *PredPrediction { return &PredPrediction{alt: alt, pred: pred} } func (p *PredPrediction) String() string { return "(" + fmt.Sprint(p.pred) + ", " + fmt.Sprint(p.alt) + ")" } // DFAState represents a set of possible [ATN] configurations. As Aho, Sethi, // Ullman p. 117 says: "The DFA uses its state to keep track of all possible // states the ATN can be in after reading each input symbol. That is to say, // after reading input a1, a2,..an, the DFA is in a state that represents the // subset T of the states of the ATN that are reachable from the ATN's start // state along some path labeled a1a2..an." // // In conventional NFA-to-DFA conversion, therefore, the subset T would be a bitset representing the set of // states the [ATN] could be in. We need to track the alt predicted by each state // as well, however. More importantly, we need to maintain a stack of states, // tracking the closure operations as they jump from rule to rule, emulating // rule invocations (method calls). I have to add a stack to simulate the proper // lookahead sequences for the underlying LL grammar from which the ATN was // derived. // // I use a set of [ATNConfig] objects, not simple states. An [ATNConfig] is both a // state (ala normal conversion) and a [RuleContext] describing the chain of rules // (if any) followed to arrive at that state. // // A [DFAState] may have multiple references to a particular state, but with // different [ATN] contexts (with same or different alts) meaning that state was // reached via a different set of rule invocations. type DFAState struct { stateNumber int configs *ATNConfigSet // edges elements point to the target of the symbol. Shift up by 1 so (-1) // Token.EOF maps to the first element. edges []*DFAState isAcceptState bool // prediction is the 'ttype' we match or alt we predict if the state is 'accept'. // Set to ATN.INVALID_ALT_NUMBER when predicates != nil or // requiresFullContext. prediction int lexerActionExecutor *LexerActionExecutor // requiresFullContext indicates it was created during an SLL prediction that // discovered a conflict between the configurations in the state. Future // ParserATNSimulator.execATN invocations immediately jump doing // full context prediction if true. requiresFullContext bool // predicates is the predicates associated with the ATN configurations of the // DFA state during SLL parsing. When we have predicates, requiresFullContext // is false, since full context prediction evaluates predicates on-the-fly. If // d is // not nil, then prediction is ATN.INVALID_ALT_NUMBER. // // We only use these for non-requiresFullContext but conflicting states. That // means we know from the context (it's $ or we don't dip into outer context) // that it's an ambiguity not a conflict. // // This list is computed by // ParserATNSimulator.predicateDFAState. predicates []*PredPrediction } func NewDFAState(stateNumber int, configs *ATNConfigSet) *DFAState { if configs == nil { configs = NewATNConfigSet(false) } return &DFAState{configs: configs, stateNumber: stateNumber} } // GetAltSet gets the set of all alts mentioned by all ATN configurations in d. func (d *DFAState) GetAltSet() []int { var alts []int if d.configs != nil { for _, c := range d.configs.configs { alts = append(alts, c.GetAlt()) } } if len(alts) == 0 { return nil } return alts } func (d *DFAState) getEdges() []*DFAState { return d.edges } func (d *DFAState) numEdges() int { return len(d.edges) } func (d *DFAState) getIthEdge(i int) *DFAState { return d.edges[i] } func (d *DFAState) setEdges(newEdges []*DFAState) { d.edges = newEdges } func (d *DFAState) setIthEdge(i int, edge *DFAState) { d.edges[i] = edge } func (d *DFAState) setPrediction(v int) { d.prediction = v } func (d *DFAState) String() string { var s string if d.isAcceptState { if d.predicates != nil { s = "=>" + fmt.Sprint(d.predicates) } else { s = "=>" + fmt.Sprint(d.prediction) } } return fmt.Sprintf("%d:%s%s", d.stateNumber, fmt.Sprint(d.configs), s) } func (d *DFAState) Hash() int { h := murmurInit(7) h = murmurUpdate(h, d.configs.Hash()) return murmurFinish(h, 1) } // Equals returns whether d equals other. Two DFAStates are equal if their ATN // configuration sets are the same. This method is used to see if a state // already exists. // // Because the number of alternatives and number of ATN configurations are // finite, there is a finite number of DFA states that can be processed. This is // necessary to show that the algorithm terminates. // // Cannot test the DFA state numbers here because in // ParserATNSimulator.addDFAState we need to know if any other state exists that // has d exact set of ATN configurations. The stateNumber is irrelevant. func (d *DFAState) Equals(o Collectable[*DFAState]) bool { if d == o { return true } return d.configs.Equals(o.(*DFAState).configs) } antlr4-go-antlr-c821258/diagnostic_error_listener.go 0000664 0000000 0000000 00000007610 14621155120 0022507 0 ustar 00root root 0000000 0000000 // Copyright (c) 2012-2022 The ANTLR Project. All rights reserved. // Use of this file is governed by the BSD 3-clause license that // can be found in the LICENSE.txt file in the project root. package antlr import ( "strconv" ) // // This implementation of {@link ANTLRErrorListener} can be used to identify // certain potential correctness and performance problems in grammars. "reports" // are made by calling {@link Parser//NotifyErrorListeners} with the appropriate // message. // //
The default implementation simply calls {@link //endErrorCondition} to // ensure that the handler is not in error recovery mode.
func (d *DefaultErrorStrategy) reset(recognizer Parser) { d.endErrorCondition(recognizer) } // This method is called to enter error recovery mode when a recognition // exception is Reported. func (d *DefaultErrorStrategy) beginErrorCondition(_ Parser) { d.errorRecoveryMode = true } func (d *DefaultErrorStrategy) InErrorRecoveryMode(_ Parser) bool { return d.errorRecoveryMode } // This method is called to leave error recovery mode after recovering from // a recognition exception. func (d *DefaultErrorStrategy) endErrorCondition(_ Parser) { d.errorRecoveryMode = false d.lastErrorStates = nil d.lastErrorIndex = -1 } // ReportMatch is the default implementation of error matching and simply calls endErrorCondition. func (d *DefaultErrorStrategy) ReportMatch(recognizer Parser) { d.endErrorCondition(recognizer) } // ReportError is the default implementation of error reporting. // It returns immediately if the handler is already // in error recovery mode. Otherwise, it calls [beginErrorCondition] // and dispatches the Reporting task based on the runtime type of e // according to the following table. // // [NoViableAltException] : Dispatches the call to [ReportNoViableAlternative] // [InputMisMatchException] : Dispatches the call to [ReportInputMisMatch] // [FailedPredicateException] : Dispatches the call to [ReportFailedPredicate] // All other types : Calls [NotifyErrorListeners] to Report the exception func (d *DefaultErrorStrategy) ReportError(recognizer Parser, e RecognitionException) { // if we've already Reported an error and have not Matched a token // yet successfully, don't Report any errors. if d.InErrorRecoveryMode(recognizer) { return // don't Report spurious errors } d.beginErrorCondition(recognizer) switch t := e.(type) { default: fmt.Println("unknown recognition error type: " + reflect.TypeOf(e).Name()) // fmt.Println(e.stack) recognizer.NotifyErrorListeners(e.GetMessage(), e.GetOffendingToken(), e) case *NoViableAltException: d.ReportNoViableAlternative(recognizer, t) case *InputMisMatchException: d.ReportInputMisMatch(recognizer, t) case *FailedPredicateException: d.ReportFailedPredicate(recognizer, t) } } // Recover is the default recovery implementation. // It reSynchronizes the parser by consuming tokens until we find one in the reSynchronization set - // loosely the set of tokens that can follow the current rule. func (d *DefaultErrorStrategy) Recover(recognizer Parser, _ RecognitionException) { if d.lastErrorIndex == recognizer.GetInputStream().Index() && d.lastErrorStates != nil && d.lastErrorStates.contains(recognizer.GetState()) { // uh oh, another error at same token index and previously-Visited // state in ATN must be a case where LT(1) is in the recovery // token set so nothing got consumed. Consume a single token // at least to prevent an infinite loop d is a failsafe. recognizer.Consume() } d.lastErrorIndex = recognizer.GetInputStream().Index() if d.lastErrorStates == nil { d.lastErrorStates = NewIntervalSet() } d.lastErrorStates.addOne(recognizer.GetState()) followSet := d.GetErrorRecoverySet(recognizer) d.consumeUntil(recognizer, followSet) } // Sync is the default implementation of error strategy synchronization. // // This Sync makes sure that the current lookahead symbol is consistent with what were expecting // at this point in the [ATN]. You can call this anytime but ANTLR only // generates code to check before sub-rules/loops and each iteration. // // Implements [Jim Idle]'s magic Sync mechanism in closures and optional // sub-rules. E.g.: // // a : Sync ( stuff Sync )* // Sync : {consume to what can follow Sync} // // At the start of a sub-rule upon error, Sync performs single // token deletion, if possible. If it can't do that, it bails on the current // rule and uses the default error recovery, which consumes until the // reSynchronization set of the current rule. // // If the sub-rule is optional // // ({@code (...)?}, {@code (...)*}, // // or a block with an empty alternative), then the expected set includes what follows // the sub-rule. // // During loop iteration, it consumes until it sees a token that can start a // sub-rule or what follows loop. Yes, that is pretty aggressive. We opt to // stay in the loop as long as possible. // // # Origins // // Previous versions of ANTLR did a poor job of their recovery within loops. // A single mismatch token or missing token would force the parser to bail // out of the entire rules surrounding the loop. So, for rule: // // classfunc : 'class' ID '{' member* '}' // // input with an extra token between members would force the parser to // consume until it found the next class definition rather than the next // member definition of the current class. // // This functionality cost a bit of effort because the parser has to // compare the token set at the start of the loop and at each iteration. If for // some reason speed is suffering for you, you can turn off this // functionality by simply overriding this method as empty: // // { } // // [Jim Idle]: https://github.com/jimidle func (d *DefaultErrorStrategy) Sync(recognizer Parser) { // If already recovering, don't try to Sync if d.InErrorRecoveryMode(recognizer) { return } s := recognizer.GetInterpreter().atn.states[recognizer.GetState()] la := recognizer.GetTokenStream().LA(1) // try cheaper subset first might get lucky. seems to shave a wee bit off nextTokens := recognizer.GetATN().NextTokens(s, nil) if nextTokens.contains(TokenEpsilon) || nextTokens.contains(la) { return } switch s.GetStateType() { case ATNStateBlockStart, ATNStateStarBlockStart, ATNStatePlusBlockStart, ATNStateStarLoopEntry: // Report error and recover if possible if d.SingleTokenDeletion(recognizer) != nil { return } recognizer.SetError(NewInputMisMatchException(recognizer)) case ATNStatePlusLoopBack, ATNStateStarLoopBack: d.ReportUnwantedToken(recognizer) expecting := NewIntervalSet() expecting.addSet(recognizer.GetExpectedTokens()) whatFollowsLoopIterationOrRule := expecting.addSet(d.GetErrorRecoverySet(recognizer)) d.consumeUntil(recognizer, whatFollowsLoopIterationOrRule) default: // do nothing if we can't identify the exact kind of ATN state } } // ReportNoViableAlternative is called by [ReportError] when the exception is a [NoViableAltException]. // // See also [ReportError] func (d *DefaultErrorStrategy) ReportNoViableAlternative(recognizer Parser, e *NoViableAltException) { tokens := recognizer.GetTokenStream() var input string if tokens != nil { if e.startToken.GetTokenType() == TokenEOF { input = "If the state number is not known, b method returns -1.
// getExpectedTokens gets the set of input symbols which could potentially follow the // previously Matched symbol at the time this exception was raised. // // If the set of expected tokens is not known and could not be computed, // this method returns nil. // // The func returns the set of token types that could potentially follow the current // state in the {ATN}, or nil if the information is not available. func (b *BaseRecognitionException) getExpectedTokens() *IntervalSet { if b.recognizer != nil { return b.recognizer.GetATN().getExpectedTokens(b.offendingState, b.ctx) } return nil } func (b *BaseRecognitionException) String() string { return b.message } type LexerNoViableAltException struct { *BaseRecognitionException startIndex int deadEndConfigs *ATNConfigSet } func NewLexerNoViableAltException(lexer Lexer, input CharStream, startIndex int, deadEndConfigs *ATNConfigSet) *LexerNoViableAltException { l := new(LexerNoViableAltException) l.BaseRecognitionException = NewBaseRecognitionException("", lexer, input, nil) l.startIndex = startIndex l.deadEndConfigs = deadEndConfigs return l } func (l *LexerNoViableAltException) String() string { symbol := "" if l.startIndex >= 0 && l.startIndex < l.input.Size() { symbol = l.input.(CharStream).GetTextFromInterval(NewInterval(l.startIndex, l.startIndex)) } return "LexerNoViableAltException" + symbol } type NoViableAltException struct { *BaseRecognitionException startToken Token offendingToken Token ctx ParserRuleContext deadEndConfigs *ATNConfigSet } // NewNoViableAltException creates an exception indicating that the parser could not decide which of two or more paths // to take based upon the remaining input. It tracks the starting token // of the offending input and also knows where the parser was // in the various paths when the error. // // Reported by [ReportNoViableAlternative] func NewNoViableAltException(recognizer Parser, input TokenStream, startToken Token, offendingToken Token, deadEndConfigs *ATNConfigSet, ctx ParserRuleContext) *NoViableAltException { if ctx == nil { ctx = recognizer.GetParserRuleContext() } if offendingToken == nil { offendingToken = recognizer.GetCurrentToken() } if startToken == nil { startToken = recognizer.GetCurrentToken() } if input == nil { input = recognizer.GetInputStream().(TokenStream) } n := new(NoViableAltException) n.BaseRecognitionException = NewBaseRecognitionException("", recognizer, input, ctx) // Which configurations did we try at input.Index() that couldn't Match // input.LT(1) n.deadEndConfigs = deadEndConfigs // The token object at the start index the input stream might // not be buffering tokens so get a reference to it. // // At the time the error occurred, of course the stream needs to keep a // buffer of all the tokens, but later we might not have access to those. n.startToken = startToken n.offendingToken = offendingToken return n } type InputMisMatchException struct { *BaseRecognitionException } // NewInputMisMatchException creates an exception that signifies any kind of mismatched input exceptions such as // when the current input does not Match the expected token. func NewInputMisMatchException(recognizer Parser) *InputMisMatchException { i := new(InputMisMatchException) i.BaseRecognitionException = NewBaseRecognitionException("", recognizer, recognizer.GetInputStream(), recognizer.GetParserRuleContext()) i.offendingToken = recognizer.GetCurrentToken() return i } // FailedPredicateException indicates that a semantic predicate failed during validation. Validation of predicates // occurs when normally parsing the alternative just like Matching a token. // Disambiguating predicate evaluation occurs when we test a predicate during // prediction. type FailedPredicateException struct { *BaseRecognitionException ruleIndex int predicateIndex int predicate string } //goland:noinspection GoUnusedExportedFunction func NewFailedPredicateException(recognizer Parser, predicate string, message string) *FailedPredicateException { f := new(FailedPredicateException) f.BaseRecognitionException = NewBaseRecognitionException(f.formatMessage(predicate, message), recognizer, recognizer.GetInputStream(), recognizer.GetParserRuleContext()) s := recognizer.GetInterpreter().atn.states[recognizer.GetState()] trans := s.GetTransitions()[0] if trans2, ok := trans.(*PredicateTransition); ok { f.ruleIndex = trans2.ruleIndex f.predicateIndex = trans2.predIndex } else { f.ruleIndex = 0 f.predicateIndex = 0 } f.predicate = predicate f.offendingToken = recognizer.GetCurrentToken() return f } func (f *FailedPredicateException) formatMessage(predicate, message string) string { if message != "" { return message } return "failed predicate: {" + predicate + "}?" } type ParseCancellationException struct { } func (p ParseCancellationException) GetOffendingToken() Token { //TODO implement me panic("implement me") } func (p ParseCancellationException) GetMessage() string { //TODO implement me panic("implement me") } func (p ParseCancellationException) GetInputStream() IntStream { //TODO implement me panic("implement me") } func NewParseCancellationException() *ParseCancellationException { // Error.call(this) // Error.captureStackTrace(this, ParseCancellationException) return new(ParseCancellationException) } antlr4-go-antlr-c821258/file_stream.go 0000664 0000000 0000000 00000002260 14621155120 0017533 0 ustar 00root root 0000000 0000000 // Copyright (c) 2012-2022 The ANTLR Project. All rights reserved. // Use of this file is governed by the BSD 3-clause license that // can be found in the LICENSE.txt file in the project root. package antlr import ( "bufio" "os" ) // This is an InputStream that is loaded from a file all at once // when you construct the object. type FileStream struct { InputStream filename string } //goland:noinspection GoUnusedExportedFunction func NewFileStream(fileName string) (*FileStream, error) { f, err := os.Open(fileName) if err != nil { return nil, err } defer func(f *os.File) { errF := f.Close() if errF != nil { } }(f) reader := bufio.NewReader(f) fInfo, err := f.Stat() if err != nil { return nil, err } fs := &FileStream{ InputStream: InputStream{ index: 0, name: fileName, }, filename: fileName, } // Pre-build the buffer and read runes efficiently // fs.data = make([]rune, 0, fInfo.Size()) for { r, _, err := reader.ReadRune() if err != nil { break } fs.data = append(fs.data, r) } fs.size = len(fs.data) // Size in runes // All done. // return fs, nil } func (f *FileStream) GetSourceName() string { return f.filename } antlr4-go-antlr-c821258/go.mod 0000664 0000000 0000000 00000000153 14621155120 0016017 0 ustar 00root root 0000000 0000000 module github.com/antlr4-go/antlr/v4 go 1.22 require golang.org/x/exp v0.0.0-20240506185415-9bf2ced13842 antlr4-go-antlr-c821258/go.sum 0000664 0000000 0000000 00000000317 14621155120 0016046 0 ustar 00root root 0000000 0000000 golang.org/x/exp v0.0.0-20240506185415-9bf2ced13842 h1:vr/HnozRka3pE4EsMEg1lgkXJkTFJCVUX+S/ZT6wYzM= golang.org/x/exp v0.0.0-20240506185415-9bf2ced13842/go.mod h1:XtvwrStGgqGPLc4cjQfWqZHG1YFdYs6swckp8vpsjnc= antlr4-go-antlr-c821258/input_stream.go 0000664 0000000 0000000 00000007341 14621155120 0017760 0 ustar 00root root 0000000 0000000 // Copyright (c) 2012-2022 The ANTLR Project. All rights reserved. // Use of this file is governed by the BSD 3-clause license that // can be found in the LICENSE.txt file in the project root. package antlr import ( "bufio" "io" ) type InputStream struct { name string index int data []rune size int } // NewIoStream creates a new input stream from the given io.Reader reader. // Note that the reader is read completely into memory and so it must actually // have a stopping point - you cannot pass in a reader on an open-ended source such // as a socket for instance. func NewIoStream(reader io.Reader) *InputStream { rReader := bufio.NewReader(reader) is := &InputStream{ name: "This action is implemented by calling {@link Lexer//pushMode} with the // value provided by {@link //getMode}.
func (l *LexerPushModeAction) execute(lexer Lexer) { lexer.PushMode(l.mode) } func (l *LexerPushModeAction) Hash() int { h := murmurInit(0) h = murmurUpdate(h, l.actionType) h = murmurUpdate(h, l.mode) return murmurFinish(h, 2) } func (l *LexerPushModeAction) Equals(other LexerAction) bool { if l == other { return true } else if _, ok := other.(*LexerPushModeAction); !ok { return false } else { return l.mode == other.(*LexerPushModeAction).mode } } func (l *LexerPushModeAction) String() string { return "pushMode(" + strconv.Itoa(l.mode) + ")" } // LexerPopModeAction implements the popMode lexer action by calling [Lexer.popMode]. // // The popMode command does not have any parameters, so this action is // implemented as a singleton instance exposed by [LexerPopModeActionINSTANCE] type LexerPopModeAction struct { *BaseLexerAction } func NewLexerPopModeAction() *LexerPopModeAction { l := new(LexerPopModeAction) l.BaseLexerAction = NewBaseLexerAction(LexerActionTypePopMode) return l } var LexerPopModeActionINSTANCE = NewLexerPopModeAction() //This action is implemented by calling {@link Lexer//popMode}.
func (l *LexerPopModeAction) execute(lexer Lexer) { lexer.PopMode() } func (l *LexerPopModeAction) String() string { return "popMode" } // Implements the {@code more} lexer action by calling {@link Lexer//more}. // //The {@code more} command does not have any parameters, so l action is // implemented as a singleton instance exposed by {@link //INSTANCE}.
type LexerMoreAction struct { *BaseLexerAction } func NewLexerMoreAction() *LexerMoreAction { l := new(LexerMoreAction) l.BaseLexerAction = NewBaseLexerAction(LexerActionTypeMore) return l } var LexerMoreActionINSTANCE = NewLexerMoreAction() //This action is implemented by calling {@link Lexer//popMode}.
func (l *LexerMoreAction) execute(lexer Lexer) { lexer.More() } func (l *LexerMoreAction) String() string { return "more" } // LexerModeAction implements the mode lexer action by calling [Lexer.mode] with // the assigned mode. type LexerModeAction struct { *BaseLexerAction mode int } func NewLexerModeAction(mode int) *LexerModeAction { l := new(LexerModeAction) l.BaseLexerAction = NewBaseLexerAction(LexerActionTypeMode) l.mode = mode return l } //This action is implemented by calling {@link Lexer//mode} with the // value provided by {@link //getMode}.
func (l *LexerModeAction) execute(lexer Lexer) { lexer.SetMode(l.mode) } func (l *LexerModeAction) Hash() int { h := murmurInit(0) h = murmurUpdate(h, l.actionType) h = murmurUpdate(h, l.mode) return murmurFinish(h, 2) } func (l *LexerModeAction) Equals(other LexerAction) bool { if l == other { return true } else if _, ok := other.(*LexerModeAction); !ok { return false } else { return l.mode == other.(*LexerModeAction).mode } } func (l *LexerModeAction) String() string { return "mode(" + strconv.Itoa(l.mode) + ")" } // Executes a custom lexer action by calling {@link Recognizer//action} with the // rule and action indexes assigned to the custom action. The implementation of // a custom action is added to the generated code for the lexer in an override // of {@link Recognizer//action} when the grammar is compiled. // //This class may represent embedded actions created with the {...}
// syntax in ANTLR 4, as well as actions created for lexer commands where the
// command argument could not be evaluated when the grammar was compiled.
Custom actions are implemented by calling {@link Lexer//action} with the // appropriate rule and action indexes.
func (l *LexerCustomAction) execute(lexer Lexer) { lexer.Action(nil, l.ruleIndex, l.actionIndex) } func (l *LexerCustomAction) Hash() int { h := murmurInit(0) h = murmurUpdate(h, l.actionType) h = murmurUpdate(h, l.ruleIndex) h = murmurUpdate(h, l.actionIndex) return murmurFinish(h, 3) } func (l *LexerCustomAction) Equals(other LexerAction) bool { if l == other { return true } else if _, ok := other.(*LexerCustomAction); !ok { return false } else { return l.ruleIndex == other.(*LexerCustomAction).ruleIndex && l.actionIndex == other.(*LexerCustomAction).actionIndex } } // LexerChannelAction implements the channel lexer action by calling // [Lexer.setChannel] with the assigned channel. // // Constructs a new channel action with the specified channel value. type LexerChannelAction struct { *BaseLexerAction channel int } // NewLexerChannelAction creates a channel lexer action by calling // [Lexer.setChannel] with the assigned channel. // // Constructs a new channel action with the specified channel value. func NewLexerChannelAction(channel int) *LexerChannelAction { l := new(LexerChannelAction) l.BaseLexerAction = NewBaseLexerAction(LexerActionTypeChannel) l.channel = channel return l } //This action is implemented by calling {@link Lexer//setChannel} with the // value provided by {@link //getChannel}.
func (l *LexerChannelAction) execute(lexer Lexer) { lexer.SetChannel(l.channel) } func (l *LexerChannelAction) Hash() int { h := murmurInit(0) h = murmurUpdate(h, l.actionType) h = murmurUpdate(h, l.channel) return murmurFinish(h, 2) } func (l *LexerChannelAction) Equals(other LexerAction) bool { if l == other { return true } else if _, ok := other.(*LexerChannelAction); !ok { return false } else { return l.channel == other.(*LexerChannelAction).channel } } func (l *LexerChannelAction) String() string { return "channel(" + strconv.Itoa(l.channel) + ")" } // This implementation of {@link LexerAction} is used for tracking input offsets // for position-dependent actions within a {@link LexerActionExecutor}. // //This action is not serialized as part of the ATN, and is only required for // position-dependent lexer actions which appear at a location other than the // end of a rule. For more information about DFA optimizations employed for // lexer actions, see {@link LexerActionExecutor//append} and // {@link LexerActionExecutor//fixOffsetBeforeMatch}.
type LexerIndexedCustomAction struct { *BaseLexerAction offset int lexerAction LexerAction isPositionDependent bool } // NewLexerIndexedCustomAction constructs a new indexed custom action by associating a character offset // with a [LexerAction]. // // Note: This class is only required for lexer actions for which // [LexerAction.isPositionDependent] returns true. // // The offset points into the input [CharStream], relative to // the token start index, at which the specified lexerAction should be // executed. func NewLexerIndexedCustomAction(offset int, lexerAction LexerAction) *LexerIndexedCustomAction { l := new(LexerIndexedCustomAction) l.BaseLexerAction = NewBaseLexerAction(lexerAction.getActionType()) l.offset = offset l.lexerAction = lexerAction l.isPositionDependent = true return l } //This method calls {@link //execute} on the result of {@link //getAction} // using the provided {@code lexer}.
func (l *LexerIndexedCustomAction) execute(lexer Lexer) { // assume the input stream position was properly set by the calling code l.lexerAction.execute(lexer) } func (l *LexerIndexedCustomAction) Hash() int { h := murmurInit(0) h = murmurUpdate(h, l.offset) h = murmurUpdate(h, l.lexerAction.Hash()) return murmurFinish(h, 2) } func (l *LexerIndexedCustomAction) equals(other LexerAction) bool { if l == other { return true } else if _, ok := other.(*LexerIndexedCustomAction); !ok { return false } else { return l.offset == other.(*LexerIndexedCustomAction).offset && l.lexerAction.Equals(other.(*LexerIndexedCustomAction).lexerAction) } } antlr4-go-antlr-c821258/lexer_action_executor.go 0000664 0000000 0000000 00000014000 14621155120 0021626 0 ustar 00root root 0000000 0000000 // Copyright (c) 2012-2022 The ANTLR Project. All rights reserved. // Use of this file is governed by the BSD 3-clause license that // can be found in the LICENSE.txt file in the project root. package antlr import "golang.org/x/exp/slices" // Represents an executor for a sequence of lexer actions which traversed during // the Matching operation of a lexer rule (token). // //The executor tracks position information for position-dependent lexer actions // efficiently, ensuring that actions appearing only at the end of the rule do // not cause bloating of the {@link DFA} created for the lexer.
type LexerActionExecutor struct { lexerActions []LexerAction cachedHash int } func NewLexerActionExecutor(lexerActions []LexerAction) *LexerActionExecutor { if lexerActions == nil { lexerActions = make([]LexerAction, 0) } l := new(LexerActionExecutor) l.lexerActions = lexerActions // Caches the result of {@link //hashCode} since the hash code is an element // of the performance-critical {@link ATNConfig//hashCode} operation. l.cachedHash = murmurInit(0) for _, a := range lexerActions { l.cachedHash = murmurUpdate(l.cachedHash, a.Hash()) } l.cachedHash = murmurFinish(l.cachedHash, len(lexerActions)) return l } // LexerActionExecutorappend creates a [LexerActionExecutor] which executes the actions for // the input [LexerActionExecutor] followed by a specified // [LexerAction]. // TODO: This does not match the Java code func LexerActionExecutorappend(lexerActionExecutor *LexerActionExecutor, lexerAction LexerAction) *LexerActionExecutor { if lexerActionExecutor == nil { return NewLexerActionExecutor([]LexerAction{lexerAction}) } return NewLexerActionExecutor(append(lexerActionExecutor.lexerActions, lexerAction)) } // fixOffsetBeforeMatch creates a [LexerActionExecutor] which encodes the current offset // for position-dependent lexer actions. // // Normally, when the executor encounters lexer actions where // [LexerAction.isPositionDependent] returns true, it calls // [IntStream.Seek] on the input [CharStream] to set the input // position to the end of the current token. This behavior provides // for efficient [DFA] representation of lexer actions which appear at the end // of a lexer rule, even when the lexer rule Matches a variable number of // characters. // // Prior to traversing a Match transition in the [ATN], the current offset // from the token start index is assigned to all position-dependent lexer // actions which have not already been assigned a fixed offset. By storing // the offsets relative to the token start index, the [DFA] representation of // lexer actions which appear in the middle of tokens remains efficient due // to sharing among tokens of the same Length, regardless of their absolute // position in the input stream. // // If the current executor already has offsets assigned to all // position-dependent lexer actions, the method returns this instance. // // The offset is assigned to all position-dependent // lexer actions which do not already have offsets assigned. // // The func returns a [LexerActionExecutor] that stores input stream offsets // for all position-dependent lexer actions. func (l *LexerActionExecutor) fixOffsetBeforeMatch(offset int) *LexerActionExecutor { var updatedLexerActions []LexerAction for i := 0; i < len(l.lexerActions); i++ { _, ok := l.lexerActions[i].(*LexerIndexedCustomAction) if l.lexerActions[i].getIsPositionDependent() && !ok { if updatedLexerActions == nil { updatedLexerActions = make([]LexerAction, 0, len(l.lexerActions)) updatedLexerActions = append(updatedLexerActions, l.lexerActions...) } updatedLexerActions[i] = NewLexerIndexedCustomAction(offset, l.lexerActions[i]) } } if updatedLexerActions == nil { return l } return NewLexerActionExecutor(updatedLexerActions) } // Execute the actions encapsulated by l executor within the context of a // particular {@link Lexer}. // //This method calls {@link IntStream//seek} to set the position of the // {@code input} {@link CharStream} prior to calling // {@link LexerAction//execute} on a position-dependent action. Before the // method returns, the input position will be restored to the same position // it was in when the method was invoked.
// // @param lexer The lexer instance. // @param input The input stream which is the source for the current token. // When l method is called, the current {@link IntStream//index} for // {@code input} should be the start of the following token, i.e. 1 // character past the end of the current token. // @param startIndex The token start index. This value may be passed to // {@link IntStream//seek} to set the {@code input} position to the beginning // of the token. // / func (l *LexerActionExecutor) execute(lexer Lexer, input CharStream, startIndex int) { requiresSeek := false stopIndex := input.Index() defer func() { if requiresSeek { input.Seek(stopIndex) } }() for i := 0; i < len(l.lexerActions); i++ { lexerAction := l.lexerActions[i] if la, ok := lexerAction.(*LexerIndexedCustomAction); ok { offset := la.offset input.Seek(startIndex + offset) lexerAction = la.lexerAction requiresSeek = (startIndex + offset) != stopIndex } else if lexerAction.getIsPositionDependent() { input.Seek(stopIndex) requiresSeek = false } lexerAction.execute(lexer) } } func (l *LexerActionExecutor) Hash() int { if l == nil { // TODO: Why is this here? l should not be nil return 61 } // TODO: This is created from the action itself when the struct is created - will this be an issue at some point? Java uses the runtime assign hashcode return l.cachedHash } func (l *LexerActionExecutor) Equals(other interface{}) bool { if l == other { return true } othert, ok := other.(*LexerActionExecutor) if !ok { return false } if othert == nil { return false } if l.cachedHash != othert.cachedHash { return false } if len(l.lexerActions) != len(othert.lexerActions) { return false } return slices.EqualFunc(l.lexerActions, othert.lexerActions, func(i, j LexerAction) bool { return i.Equals(j) }) } antlr4-go-antlr-c821258/lexer_atn_simulator.go 0000664 0000000 0000000 00000053002 14621155120 0021321 0 ustar 00root root 0000000 0000000 // Copyright (c) 2012-2022 The ANTLR Project. All rights reserved. // Use of this file is governed by the BSD 3-clause license that // can be found in the LICENSE.txt file in the project root. package antlr import ( "fmt" "strconv" "strings" ) //goland:noinspection GoUnusedGlobalVariable var ( LexerATNSimulatorMinDFAEdge = 0 LexerATNSimulatorMaxDFAEdge = 127 // forces unicode to stay in ATN LexerATNSimulatorMatchCalls = 0 ) type ILexerATNSimulator interface { IATNSimulator reset() Match(input CharStream, mode int) int GetCharPositionInLine() int GetLine() int GetText(input CharStream) string Consume(input CharStream) } type LexerATNSimulator struct { BaseATNSimulator recog Lexer predictionMode int mergeCache *JPCMap2 startIndex int Line int CharPositionInLine int mode int prevAccept *SimState MatchCalls int } func NewLexerATNSimulator(recog Lexer, atn *ATN, decisionToDFA []*DFA, sharedContextCache *PredictionContextCache) *LexerATNSimulator { l := &LexerATNSimulator{ BaseATNSimulator: BaseATNSimulator{ atn: atn, sharedContextCache: sharedContextCache, }, } l.decisionToDFA = decisionToDFA l.recog = recog // The current token's starting index into the character stream. // Shared across DFA to ATN simulation in case the ATN fails and the // DFA did not have a previous accept state. In l case, we use the // ATN-generated exception object. l.startIndex = -1 // line number 1..n within the input l.Line = 1 // The index of the character relative to the beginning of the line // 0..n-1 l.CharPositionInLine = 0 l.mode = LexerDefaultMode // Used during DFA/ATN exec to record the most recent accept configuration // info l.prevAccept = NewSimState() return l } func (l *LexerATNSimulator) copyState(simulator *LexerATNSimulator) { l.CharPositionInLine = simulator.CharPositionInLine l.Line = simulator.Line l.mode = simulator.mode l.startIndex = simulator.startIndex } func (l *LexerATNSimulator) Match(input CharStream, mode int) int { l.MatchCalls++ l.mode = mode mark := input.Mark() defer func() { input.Release(mark) }() l.startIndex = input.Index() l.prevAccept.reset() dfa := l.decisionToDFA[mode] var s0 *DFAState l.atn.stateMu.RLock() s0 = dfa.getS0() l.atn.stateMu.RUnlock() if s0 == nil { return l.MatchATN(input) } return l.execATN(input, s0) } func (l *LexerATNSimulator) reset() { l.prevAccept.reset() l.startIndex = -1 l.Line = 1 l.CharPositionInLine = 0 l.mode = LexerDefaultMode } func (l *LexerATNSimulator) MatchATN(input CharStream) int { startState := l.atn.modeToStartState[l.mode] if runtimeConfig.lexerATNSimulatorDebug { fmt.Println("MatchATN mode " + strconv.Itoa(l.mode) + " start: " + startState.String()) } oldMode := l.mode s0Closure := l.computeStartState(input, startState) suppressEdge := s0Closure.hasSemanticContext s0Closure.hasSemanticContext = false next := l.addDFAState(s0Closure, suppressEdge) predict := l.execATN(input, next) if runtimeConfig.lexerATNSimulatorDebug { fmt.Println("DFA after MatchATN: " + l.decisionToDFA[oldMode].ToLexerString()) } return predict } func (l *LexerATNSimulator) execATN(input CharStream, ds0 *DFAState) int { if runtimeConfig.lexerATNSimulatorDebug { fmt.Println("start state closure=" + ds0.configs.String()) } if ds0.isAcceptState { // allow zero-Length tokens l.captureSimState(l.prevAccept, input, ds0) } t := input.LA(1) s := ds0 // s is current/from DFA state for { // while more work if runtimeConfig.lexerATNSimulatorDebug { fmt.Println("execATN loop starting closure: " + s.configs.String()) } // As we move src->trg, src->trg, we keep track of the previous trg to // avoid looking up the DFA state again, which is expensive. // If the previous target was already part of the DFA, we might // be able to avoid doing a reach operation upon t. If s!=nil, // it means that semantic predicates didn't prevent us from // creating a DFA state. Once we know s!=nil, we check to see if // the DFA state has an edge already for t. If so, we can just reuse // it's configuration set there's no point in re-computing it. // This is kind of like doing DFA simulation within the ATN // simulation because DFA simulation is really just a way to avoid // computing reach/closure sets. Technically, once we know that // we have a previously added DFA state, we could jump over to // the DFA simulator. But, that would mean popping back and forth // a lot and making things more complicated algorithmically. // This optimization makes a lot of sense for loops within DFA. // A character will take us back to an existing DFA state // that already has lots of edges out of it. e.g., .* in comments. target := l.getExistingTargetState(s, t) if target == nil { target = l.computeTargetState(input, s, t) // print("Computed:" + str(target)) } if target == ATNSimulatorError { break } // If l is a consumable input element, make sure to consume before // capturing the accept state so the input index, line, and char // position accurately reflect the state of the interpreter at the // end of the token. if t != TokenEOF { l.Consume(input) } if target.isAcceptState { l.captureSimState(l.prevAccept, input, target) if t == TokenEOF { break } } t = input.LA(1) s = target // flip current DFA target becomes new src/from state } return l.failOrAccept(l.prevAccept, input, s.configs, t) } // Get an existing target state for an edge in the DFA. If the target state // for the edge has not yet been computed or is otherwise not available, // l method returns {@code nil}. // // @param s The current DFA state // @param t The next input symbol // @return The existing target DFA state for the given input symbol // {@code t}, or {@code nil} if the target state for l edge is not // already cached func (l *LexerATNSimulator) getExistingTargetState(s *DFAState, t int) *DFAState { if t < LexerATNSimulatorMinDFAEdge || t > LexerATNSimulatorMaxDFAEdge { return nil } l.atn.edgeMu.RLock() defer l.atn.edgeMu.RUnlock() if s.getEdges() == nil { return nil } target := s.getIthEdge(t - LexerATNSimulatorMinDFAEdge) if runtimeConfig.lexerATNSimulatorDebug && target != nil { fmt.Println("reuse state " + strconv.Itoa(s.stateNumber) + " edge to " + strconv.Itoa(target.stateNumber)) } return target } // computeTargetState computes a target state for an edge in the [DFA], and attempt to add the // computed state and corresponding edge to the [DFA]. // // The func returns the computed target [DFA] state for the given input symbol t. // If this does not lead to a valid [DFA] state, this method // returns ATNSimulatorError. func (l *LexerATNSimulator) computeTargetState(input CharStream, s *DFAState, t int) *DFAState { reach := NewOrderedATNConfigSet() // if we don't find an existing DFA state // Fill reach starting from closure, following t transitions l.getReachableConfigSet(input, s.configs, reach, t) if len(reach.configs) == 0 { // we got nowhere on t from s if !reach.hasSemanticContext { // we got nowhere on t, don't panic out l knowledge it'd // cause a fail-over from DFA later. l.addDFAEdge(s, t, ATNSimulatorError, nil) } // stop when we can't Match any more char return ATNSimulatorError } // Add an edge from s to target DFA found/created for reach return l.addDFAEdge(s, t, nil, reach) } func (l *LexerATNSimulator) failOrAccept(prevAccept *SimState, input CharStream, reach *ATNConfigSet, t int) int { if l.prevAccept.dfaState != nil { lexerActionExecutor := prevAccept.dfaState.lexerActionExecutor l.accept(input, lexerActionExecutor, l.startIndex, prevAccept.index, prevAccept.line, prevAccept.column) return prevAccept.dfaState.prediction } // if no accept and EOF is first char, return EOF if t == TokenEOF && input.Index() == l.startIndex { return TokenEOF } panic(NewLexerNoViableAltException(l.recog, input, l.startIndex, reach)) } // getReachableConfigSet when given a starting configuration set, figures out all [ATN] configurations // we can reach upon input t. // // Parameter reach is a return parameter. func (l *LexerATNSimulator) getReachableConfigSet(input CharStream, closure *ATNConfigSet, reach *ATNConfigSet, t int) { // l is used to Skip processing for configs which have a lower priority // than a runtimeConfig that already reached an accept state for the same rule SkipAlt := ATNInvalidAltNumber for _, cfg := range closure.configs { currentAltReachedAcceptState := cfg.GetAlt() == SkipAlt if currentAltReachedAcceptState && cfg.passedThroughNonGreedyDecision { continue } if runtimeConfig.lexerATNSimulatorDebug { fmt.Printf("testing %s at %s\n", l.GetTokenName(t), cfg.String()) } for _, trans := range cfg.GetState().GetTransitions() { target := l.getReachableTarget(trans, t) if target != nil { lexerActionExecutor := cfg.lexerActionExecutor if lexerActionExecutor != nil { lexerActionExecutor = lexerActionExecutor.fixOffsetBeforeMatch(input.Index() - l.startIndex) } treatEOFAsEpsilon := t == TokenEOF config := NewLexerATNConfig3(cfg, target, lexerActionExecutor) if l.closure(input, config, reach, currentAltReachedAcceptState, true, treatEOFAsEpsilon) { // any remaining configs for l alt have a lower priority // than the one that just reached an accept state. SkipAlt = cfg.GetAlt() } } } } } func (l *LexerATNSimulator) accept(input CharStream, lexerActionExecutor *LexerActionExecutor, startIndex, index, line, charPos int) { if runtimeConfig.lexerATNSimulatorDebug { fmt.Printf("ACTION %v\n", lexerActionExecutor) } // seek to after last char in token input.Seek(index) l.Line = line l.CharPositionInLine = charPos if lexerActionExecutor != nil && l.recog != nil { lexerActionExecutor.execute(l.recog, input, startIndex) } } func (l *LexerATNSimulator) getReachableTarget(trans Transition, t int) ATNState { if trans.Matches(t, 0, LexerMaxCharValue) { return trans.getTarget() } return nil } func (l *LexerATNSimulator) computeStartState(input CharStream, p ATNState) *ATNConfigSet { configs := NewOrderedATNConfigSet() for i := 0; i < len(p.GetTransitions()); i++ { target := p.GetTransitions()[i].getTarget() cfg := NewLexerATNConfig6(target, i+1, BasePredictionContextEMPTY) l.closure(input, cfg, configs, false, false, false) } return configs } // closure since the alternatives within any lexer decision are ordered by // preference, this method stops pursuing the closure as soon as an accept // state is reached. After the first accept state is reached by depth-first // search from runtimeConfig, all other (potentially reachable) states for // this rule would have a lower priority. // // The func returns true if an accept state is reached. func (l *LexerATNSimulator) closure(input CharStream, config *ATNConfig, configs *ATNConfigSet, currentAltReachedAcceptState, speculative, treatEOFAsEpsilon bool) bool { if runtimeConfig.lexerATNSimulatorDebug { fmt.Println("closure(" + config.String() + ")") } _, ok := config.state.(*RuleStopState) if ok { if runtimeConfig.lexerATNSimulatorDebug { if l.recog != nil { fmt.Printf("closure at %s rule stop %s\n", l.recog.GetRuleNames()[config.state.GetRuleIndex()], config) } else { fmt.Printf("closure at rule stop %s\n", config) } } if config.context == nil || config.context.hasEmptyPath() { if config.context == nil || config.context.isEmpty() { configs.Add(config, nil) return true } configs.Add(NewLexerATNConfig2(config, config.state, BasePredictionContextEMPTY), nil) currentAltReachedAcceptState = true } if config.context != nil && !config.context.isEmpty() { for i := 0; i < config.context.length(); i++ { if config.context.getReturnState(i) != BasePredictionContextEmptyReturnState { newContext := config.context.GetParent(i) // "pop" return state returnState := l.atn.states[config.context.getReturnState(i)] cfg := NewLexerATNConfig2(config, returnState, newContext) currentAltReachedAcceptState = l.closure(input, cfg, configs, currentAltReachedAcceptState, speculative, treatEOFAsEpsilon) } } } return currentAltReachedAcceptState } // optimization if !config.state.GetEpsilonOnlyTransitions() { if !currentAltReachedAcceptState || !config.passedThroughNonGreedyDecision { configs.Add(config, nil) } } for j := 0; j < len(config.state.GetTransitions()); j++ { trans := config.state.GetTransitions()[j] cfg := l.getEpsilonTarget(input, config, trans, configs, speculative, treatEOFAsEpsilon) if cfg != nil { currentAltReachedAcceptState = l.closure(input, cfg, configs, currentAltReachedAcceptState, speculative, treatEOFAsEpsilon) } } return currentAltReachedAcceptState } // side-effect: can alter configs.hasSemanticContext func (l *LexerATNSimulator) getEpsilonTarget(input CharStream, config *ATNConfig, trans Transition, configs *ATNConfigSet, speculative, treatEOFAsEpsilon bool) *ATNConfig { var cfg *ATNConfig if trans.getSerializationType() == TransitionRULE { rt := trans.(*RuleTransition) newContext := SingletonBasePredictionContextCreate(config.context, rt.followState.GetStateNumber()) cfg = NewLexerATNConfig2(config, trans.getTarget(), newContext) } else if trans.getSerializationType() == TransitionPRECEDENCE { panic("Precedence predicates are not supported in lexers.") } else if trans.getSerializationType() == TransitionPREDICATE { // Track traversing semantic predicates. If we traverse, // we cannot add a DFA state for l "reach" computation // because the DFA would not test the predicate again in the // future. Rather than creating collections of semantic predicates // like v3 and testing them on prediction, v4 will test them on the // fly all the time using the ATN not the DFA. This is slower but // semantically it's not used that often. One of the key elements to // l predicate mechanism is not adding DFA states that see // predicates immediately afterwards in the ATN. For example, // a : ID {p1}? | ID {p2}? // should create the start state for rule 'a' (to save start state // competition), but should not create target of ID state. The // collection of ATN states the following ID references includes // states reached by traversing predicates. Since l is when we // test them, we cannot cash the DFA state target of ID. pt := trans.(*PredicateTransition) if runtimeConfig.lexerATNSimulatorDebug { fmt.Println("EVAL rule " + strconv.Itoa(trans.(*PredicateTransition).ruleIndex) + ":" + strconv.Itoa(pt.predIndex)) } configs.hasSemanticContext = true if l.evaluatePredicate(input, pt.ruleIndex, pt.predIndex, speculative) { cfg = NewLexerATNConfig4(config, trans.getTarget()) } } else if trans.getSerializationType() == TransitionACTION { if config.context == nil || config.context.hasEmptyPath() { // execute actions anywhere in the start rule for a token. // // TODO: if the entry rule is invoked recursively, some // actions may be executed during the recursive call. The // problem can appear when hasEmptyPath() is true but // isEmpty() is false. In this case, the config needs to be // split into two contexts - one with just the empty path // and another with everything but the empty path. // Unfortunately, the current algorithm does not allow // getEpsilonTarget to return two configurations, so // additional modifications are needed before we can support // the split operation. lexerActionExecutor := LexerActionExecutorappend(config.lexerActionExecutor, l.atn.lexerActions[trans.(*ActionTransition).actionIndex]) cfg = NewLexerATNConfig3(config, trans.getTarget(), lexerActionExecutor) } else { // ignore actions in referenced rules cfg = NewLexerATNConfig4(config, trans.getTarget()) } } else if trans.getSerializationType() == TransitionEPSILON { cfg = NewLexerATNConfig4(config, trans.getTarget()) } else if trans.getSerializationType() == TransitionATOM || trans.getSerializationType() == TransitionRANGE || trans.getSerializationType() == TransitionSET { if treatEOFAsEpsilon { if trans.Matches(TokenEOF, 0, LexerMaxCharValue) { cfg = NewLexerATNConfig4(config, trans.getTarget()) } } } return cfg } // evaluatePredicate eEvaluates a predicate specified in the lexer. // // If speculative is true, this method was called before // [consume] for the Matched character. This method should call // [consume] before evaluating the predicate to ensure position // sensitive values, including [GetText], [GetLine], // and [GetColumn], properly reflect the current // lexer state. This method should restore input and the simulator // to the original state before returning, i.e. undo the actions made by the // call to [Consume]. // // The func returns true if the specified predicate evaluates to true. func (l *LexerATNSimulator) evaluatePredicate(input CharStream, ruleIndex, predIndex int, speculative bool) bool { // assume true if no recognizer was provided if l.recog == nil { return true } if !speculative { return l.recog.Sempred(nil, ruleIndex, predIndex) } savedcolumn := l.CharPositionInLine savedLine := l.Line index := input.Index() marker := input.Mark() defer func() { l.CharPositionInLine = savedcolumn l.Line = savedLine input.Seek(index) input.Release(marker) }() l.Consume(input) return l.recog.Sempred(nil, ruleIndex, predIndex) } func (l *LexerATNSimulator) captureSimState(settings *SimState, input CharStream, dfaState *DFAState) { settings.index = input.Index() settings.line = l.Line settings.column = l.CharPositionInLine settings.dfaState = dfaState } func (l *LexerATNSimulator) addDFAEdge(from *DFAState, tk int, to *DFAState, cfgs *ATNConfigSet) *DFAState { if to == nil && cfgs != nil { // leading to l call, ATNConfigSet.hasSemanticContext is used as a // marker indicating dynamic predicate evaluation makes l edge // dependent on the specific input sequence, so the static edge in the // DFA should be omitted. The target DFAState is still created since // execATN has the ability to reSynchronize with the DFA state cache // following the predicate evaluation step. // // TJP notes: next time through the DFA, we see a pred again and eval. // If that gets us to a previously created (but dangling) DFA // state, we can continue in pure DFA mode from there. // suppressEdge := cfgs.hasSemanticContext cfgs.hasSemanticContext = false to = l.addDFAState(cfgs, true) if suppressEdge { return to } } // add the edge if tk < LexerATNSimulatorMinDFAEdge || tk > LexerATNSimulatorMaxDFAEdge { // Only track edges within the DFA bounds return to } if runtimeConfig.lexerATNSimulatorDebug { fmt.Println("EDGE " + from.String() + " -> " + to.String() + " upon " + strconv.Itoa(tk)) } l.atn.edgeMu.Lock() defer l.atn.edgeMu.Unlock() if from.getEdges() == nil { // make room for tokens 1..n and -1 masquerading as index 0 from.setEdges(make([]*DFAState, LexerATNSimulatorMaxDFAEdge-LexerATNSimulatorMinDFAEdge+1)) } from.setIthEdge(tk-LexerATNSimulatorMinDFAEdge, to) // connect return to } // Add a NewDFA state if there isn't one with l set of // configurations already. This method also detects the first // configuration containing an ATN rule stop state. Later, when // traversing the DFA, we will know which rule to accept. func (l *LexerATNSimulator) addDFAState(configs *ATNConfigSet, suppressEdge bool) *DFAState { proposed := NewDFAState(-1, configs) var firstConfigWithRuleStopState *ATNConfig for _, cfg := range configs.configs { _, ok := cfg.GetState().(*RuleStopState) if ok { firstConfigWithRuleStopState = cfg break } } if firstConfigWithRuleStopState != nil { proposed.isAcceptState = true proposed.lexerActionExecutor = firstConfigWithRuleStopState.lexerActionExecutor proposed.setPrediction(l.atn.ruleToTokenType[firstConfigWithRuleStopState.GetState().GetRuleIndex()]) } dfa := l.decisionToDFA[l.mode] l.atn.stateMu.Lock() defer l.atn.stateMu.Unlock() existing, present := dfa.Get(proposed) if present { // This state was already present, so just return it. // proposed = existing } else { // We need to add the new state // proposed.stateNumber = dfa.Len() configs.readOnly = true configs.configLookup = nil // Not needed now proposed.configs = configs dfa.Put(proposed) } if !suppressEdge { dfa.setS0(proposed) } return proposed } func (l *LexerATNSimulator) getDFA(mode int) *DFA { return l.decisionToDFA[mode] } // GetText returns the text [Match]ed so far for the current token. func (l *LexerATNSimulator) GetText(input CharStream) string { // index is first lookahead char, don't include. return input.GetTextFromInterval(NewInterval(l.startIndex, input.Index()-1)) } func (l *LexerATNSimulator) Consume(input CharStream) { curChar := input.LA(1) if curChar == int('\n') { l.Line++ l.CharPositionInLine = 0 } else { l.CharPositionInLine++ } input.Consume() } func (l *LexerATNSimulator) GetCharPositionInLine() int { return l.CharPositionInLine } func (l *LexerATNSimulator) GetLine() int { return l.Line } func (l *LexerATNSimulator) GetTokenName(tt int) string { if tt == -1 { return "EOF" } var sb strings.Builder sb.Grow(6) sb.WriteByte('\'') sb.WriteRune(rune(tt)) sb.WriteByte('\'') return sb.String() } func resetSimState(sim *SimState) { sim.index = -1 sim.line = 0 sim.column = -1 sim.dfaState = nil } type SimState struct { index int line int column int dfaState *DFAState } func NewSimState() *SimState { s := new(SimState) resetSimState(s) return s } func (s *SimState) reset() { resetSimState(s) } antlr4-go-antlr-c821258/ll1_analyzer.go 0000664 0000000 0000000 00000016760 14621155120 0017650 0 ustar 00root root 0000000 0000000 // Copyright (c) 2012-2022 The ANTLR Project. All rights reserved. // Use of this file is governed by the BSD 3-clause license that // can be found in the LICENSE.txt file in the project root. package antlr type LL1Analyzer struct { atn *ATN } func NewLL1Analyzer(atn *ATN) *LL1Analyzer { la := new(LL1Analyzer) la.atn = atn return la } const ( // LL1AnalyzerHitPred is a special value added to the lookahead sets to indicate that we hit // a predicate during analysis if // // seeThruPreds==false LL1AnalyzerHitPred = TokenInvalidType ) // * // Calculates the SLL(1) expected lookahead set for each outgoing transition // of an {@link ATNState}. The returned array has one element for each // outgoing transition in {@code s}. If the closure from transition // i leads to a semantic predicate before Matching a symbol, the // element at index i of the result will be {@code nil}. // // @param s the ATN state // @return the expected symbols for each outgoing transition of {@code s}. func (la *LL1Analyzer) getDecisionLookahead(s ATNState) []*IntervalSet { if s == nil { return nil } count := len(s.GetTransitions()) look := make([]*IntervalSet, count) for alt := 0; alt < count; alt++ { look[alt] = NewIntervalSet() // TODO: This is one of the reasons that ATNConfigs are allocated and freed all the time - fix this tomorrow jim! lookBusy := NewJStore[*ATNConfig, Comparator[*ATNConfig]](aConfEqInst, ClosureBusyCollection, "LL1Analyzer.getDecisionLookahead for lookBusy") la.look1(s.GetTransitions()[alt].getTarget(), nil, BasePredictionContextEMPTY, look[alt], lookBusy, NewBitSet(), false, false) // Wipe out lookahead for la alternative if we found nothing, // or we had a predicate when we !seeThruPreds if look[alt].length() == 0 || look[alt].contains(LL1AnalyzerHitPred) { look[alt] = nil } } return look } // Look computes the set of tokens that can follow s in the [ATN] in the // specified ctx. // // If ctx is nil and the end of the rule containing // s is reached, [EPSILON] is added to the result set. // // If ctx is not nil and the end of the outermost rule is // reached, [EOF] is added to the result set. // // Parameter s the ATN state, and stopState is the ATN state to stop at. This can be a // [BlockEndState] to detect epsilon paths through a closure. // // Parameter ctx is the complete parser context, or nil if the context // should be ignored // // The func returns the set of tokens that can follow s in the [ATN] in the // specified ctx. func (la *LL1Analyzer) Look(s, stopState ATNState, ctx RuleContext) *IntervalSet { r := NewIntervalSet() var lookContext *PredictionContext if ctx != nil { lookContext = predictionContextFromRuleContext(s.GetATN(), ctx) } la.look1(s, stopState, lookContext, r, NewJStore[*ATNConfig, Comparator[*ATNConfig]](aConfEqInst, ClosureBusyCollection, "LL1Analyzer.Look for la.look1()"), NewBitSet(), true, true) return r } //* // Compute set of tokens that can follow {@code s} in the ATN in the // specified {@code ctx}. // //If {@code ctx} is {@code nil} and {@code stopState} or the end of the // rule containing {@code s} is reached, {@link Token//EPSILON} is added to // the result set. If {@code ctx} is not {@code nil} and {@code addEOF} is // {@code true} and {@code stopState} or the end of the outermost rule is // reached, {@link Token//EOF} is added to the result set.
// // @param s the ATN state. // @param stopState the ATN state to stop at. This can be a // {@link BlockEndState} to detect epsilon paths through a closure. // @param ctx The outer context, or {@code nil} if the outer context should // not be used. // @param look The result lookahead set. // @param lookBusy A set used for preventing epsilon closures in the ATN // from causing a stack overflow. Outside code should pass // {@code NewSetIf the symbol type does not Match, // {@link ANTLRErrorStrategy//recoverInline} is called on the current error // strategy to attempt recovery. If {@link //getBuildParseTree} is // {@code true} and the token index of the symbol returned by // {@link ANTLRErrorStrategy//recoverInline} is -1, the symbol is added to // the parse tree by calling {@link ParserRuleContext//addErrorNode}.
// // @param ttype the token type to Match // @return the Matched symbol // @panics RecognitionException if the current input symbol did not Match // {@code ttype} and the error strategy could not recover from the // mismatched symbol func (p *BaseParser) Match(ttype int) Token { t := p.GetCurrentToken() if t.GetTokenType() == ttype { p.errHandler.ReportMatch(p) p.Consume() } else { t = p.errHandler.RecoverInline(p) if p.HasError() { return nil } if p.BuildParseTrees && t.GetTokenIndex() == -1 { // we must have conjured up a new token during single token // insertion if it's not the current symbol p.ctx.AddErrorNode(t) } } return t } // Match current input symbol as a wildcard. If the symbol type Matches // (i.e. has a value greater than 0), {@link ANTLRErrorStrategy//ReportMatch} // and {@link //consume} are called to complete the Match process. // //If the symbol type does not Match, // {@link ANTLRErrorStrategy//recoverInline} is called on the current error // strategy to attempt recovery. If {@link //getBuildParseTree} is // {@code true} and the token index of the symbol returned by // {@link ANTLRErrorStrategy//recoverInline} is -1, the symbol is added to // the parse tree by calling {@link ParserRuleContext//addErrorNode}.
// // @return the Matched symbol // @panics RecognitionException if the current input symbol did not Match // a wildcard and the error strategy could not recover from the mismatched // symbol func (p *BaseParser) MatchWildcard() Token { t := p.GetCurrentToken() if t.GetTokenType() > 0 { p.errHandler.ReportMatch(p) p.Consume() } else { t = p.errHandler.RecoverInline(p) if p.BuildParseTrees && t.GetTokenIndex() == -1 { // we must have conjured up a new token during single token // insertion if it's not the current symbol p.ctx.AddErrorNode(t) } } return t } func (p *BaseParser) GetParserRuleContext() ParserRuleContext { return p.ctx } func (p *BaseParser) SetParserRuleContext(v ParserRuleContext) { p.ctx = v } func (p *BaseParser) GetParseListeners() []ParseTreeListener { if p.parseListeners == nil { return make([]ParseTreeListener, 0) } return p.parseListeners } // AddParseListener registers listener to receive events during the parsing process. // // To support output-preserving grammar transformations (including but not // limited to left-recursion removal, automated left-factoring, and // optimized code generation), calls to listener methods during the parse // may differ substantially from calls made by // [ParseTreeWalker.DEFAULT] used after the parse is complete. In // particular, rule entry and exit events may occur in a different order // during the parse than after the parser. In addition, calls to certain // rule entry methods may be omitted. // // With the following specific exceptions, calls to listener events are // deterministic, i.e. for identical input the calls to listener // methods will be the same. // // - Alterations to the grammar used to generate code may change the // behavior of the listener calls. // - Alterations to the command line options passed to ANTLR 4 when // generating the parser may change the behavior of the listener calls. // - Changing the version of the ANTLR Tool used to generate the parser // may change the behavior of the listener calls. func (p *BaseParser) AddParseListener(listener ParseTreeListener) { if listener == nil { panic("listener") } if p.parseListeners == nil { p.parseListeners = make([]ParseTreeListener, 0) } p.parseListeners = append(p.parseListeners, listener) } // RemoveParseListener removes listener from the list of parse listeners. // // If listener is nil or has not been added as a parse // listener, this func does nothing. func (p *BaseParser) RemoveParseListener(listener ParseTreeListener) { if p.parseListeners != nil { idx := -1 for i, v := range p.parseListeners { if v == listener { idx = i break } } if idx == -1 { return } // remove the listener from the slice p.parseListeners = append(p.parseListeners[0:idx], p.parseListeners[idx+1:]...) if len(p.parseListeners) == 0 { p.parseListeners = nil } } } // Remove all parse listeners. func (p *BaseParser) removeParseListeners() { p.parseListeners = nil } // TriggerEnterRuleEvent notifies all parse listeners of an enter rule event. func (p *BaseParser) TriggerEnterRuleEvent() { if p.parseListeners != nil { ctx := p.ctx for _, listener := range p.parseListeners { listener.EnterEveryRule(ctx) ctx.EnterRule(listener) } } } // TriggerExitRuleEvent notifies any parse listeners of an exit rule event. func (p *BaseParser) TriggerExitRuleEvent() { if p.parseListeners != nil { // reverse order walk of listeners ctx := p.ctx l := len(p.parseListeners) - 1 for i := range p.parseListeners { listener := p.parseListeners[l-i] ctx.ExitRule(listener) listener.ExitEveryRule(ctx) } } } func (p *BaseParser) GetInterpreter() *ParserATNSimulator { return p.Interpreter } func (p *BaseParser) GetATN() *ATN { return p.Interpreter.atn } func (p *BaseParser) GetTokenFactory() TokenFactory { return p.input.GetTokenSource().GetTokenFactory() } // setTokenFactory is used to tell our token source and error strategy about a new way to create tokens. func (p *BaseParser) setTokenFactory(factory TokenFactory) { p.input.GetTokenSource().setTokenFactory(factory) } // GetATNWithBypassAlts - the ATN with bypass alternatives is expensive to create, so we create it // lazily. func (p *BaseParser) GetATNWithBypassAlts() { // TODO - Implement this? panic("Not implemented!") // serializedAtn := p.getSerializedATN() // if (serializedAtn == nil) { // panic("The current parser does not support an ATN with bypass alternatives.") // } // result := p.bypassAltsAtnCache[serializedAtn] // if (result == nil) { // deserializationOptions := NewATNDeserializationOptions(nil) // deserializationOptions.generateRuleBypassTransitions = true // result = NewATNDeserializer(deserializationOptions).deserialize(serializedAtn) // p.bypassAltsAtnCache[serializedAtn] = result // } // return result } // The preferred method of getting a tree pattern. For example, here's a // sample use: // //
// ParseTree t = parser.expr()
// ParseTreePattern p = parser.compileParseTreePattern("<ID>+0",
// MyParser.RULE_expr)
// ParseTreeMatch m = p.Match(t)
// String id = m.Get("ID")
//
//goland:noinspection GoUnusedParameter
func (p *BaseParser) compileParseTreePattern(pattern, patternRuleIndex, lexer Lexer) {
panic("NewParseTreePatternMatcher not implemented!")
//
// if (lexer == nil) {
// if (p.GetTokenStream() != nil) {
// tokenSource := p.GetTokenStream().GetTokenSource()
// if _, ok := tokenSource.(ILexer); ok {
// lexer = tokenSource
// }
// }
// }
// if (lexer == nil) {
// panic("Parser can't discover a lexer to use")
// }
// m := NewParseTreePatternMatcher(lexer, p)
// return m.compile(pattern, patternRuleIndex)
}
func (p *BaseParser) GetInputStream() IntStream {
return p.GetTokenStream()
}
func (p *BaseParser) SetInputStream(input TokenStream) {
p.SetTokenStream(input)
}
func (p *BaseParser) GetTokenStream() TokenStream {
return p.input
}
// SetTokenStream installs input as the token stream and resets the parser.
func (p *BaseParser) SetTokenStream(input TokenStream) {
p.input = nil
p.reset()
p.input = input
}
// GetCurrentToken returns the current token at LT(1).
//
// [Match] needs to return the current input symbol, which gets put
// into the label for the associated token ref e.g., x=ID.
func (p *BaseParser) GetCurrentToken() Token {
return p.input.LT(1)
}
func (p *BaseParser) NotifyErrorListeners(msg string, offendingToken Token, err RecognitionException) {
if offendingToken == nil {
offendingToken = p.GetCurrentToken()
}
p._SyntaxErrors++
line := offendingToken.GetLine()
column := offendingToken.GetColumn()
listener := p.GetErrorListenerDispatch()
listener.SyntaxError(p, offendingToken, line, column, msg, err)
}
func (p *BaseParser) Consume() Token {
o := p.GetCurrentToken()
if o.GetTokenType() != TokenEOF {
p.GetInputStream().Consume()
}
hasListener := p.parseListeners != nil && len(p.parseListeners) > 0
if p.BuildParseTrees || hasListener {
if p.errHandler.InErrorRecoveryMode(p) {
node := p.ctx.AddErrorNode(o)
if p.parseListeners != nil {
for _, l := range p.parseListeners {
l.VisitErrorNode(node)
}
}
} else {
node := p.ctx.AddTokenNode(o)
if p.parseListeners != nil {
for _, l := range p.parseListeners {
l.VisitTerminal(node)
}
}
}
// node.invokingState = p.state
}
return o
}
func (p *BaseParser) addContextToParseTree() {
// add current context to parent if we have a parent
if p.ctx.GetParent() != nil {
p.ctx.GetParent().(ParserRuleContext).AddChild(p.ctx)
}
}
func (p *BaseParser) EnterRule(localctx ParserRuleContext, state, _ int) {
p.SetState(state)
p.ctx = localctx
p.ctx.SetStart(p.input.LT(1))
if p.BuildParseTrees {
p.addContextToParseTree()
}
if p.parseListeners != nil {
p.TriggerEnterRuleEvent()
}
}
func (p *BaseParser) ExitRule() {
p.ctx.SetStop(p.input.LT(-1))
// trigger event on ctx, before it reverts to parent
if p.parseListeners != nil {
p.TriggerExitRuleEvent()
}
p.SetState(p.ctx.GetInvokingState())
if p.ctx.GetParent() != nil {
p.ctx = p.ctx.GetParent().(ParserRuleContext)
} else {
p.ctx = nil
}
}
func (p *BaseParser) EnterOuterAlt(localctx ParserRuleContext, altNum int) {
localctx.SetAltNumber(altNum)
// if we have a new localctx, make sure we replace existing ctx
// that is previous child of parse tree
if p.BuildParseTrees && p.ctx != localctx {
if p.ctx.GetParent() != nil {
p.ctx.GetParent().(ParserRuleContext).RemoveLastChild()
p.ctx.GetParent().(ParserRuleContext).AddChild(localctx)
}
}
p.ctx = localctx
}
// Get the precedence level for the top-most precedence rule.
//
// @return The precedence level for the top-most precedence rule, or -1 if
// the parser context is not nested within a precedence rule.
func (p *BaseParser) GetPrecedence() int {
if len(p.precedenceStack) == 0 {
return -1
}
return p.precedenceStack[len(p.precedenceStack)-1]
}
func (p *BaseParser) EnterRecursionRule(localctx ParserRuleContext, state, _, precedence int) {
p.SetState(state)
p.precedenceStack.Push(precedence)
p.ctx = localctx
p.ctx.SetStart(p.input.LT(1))
if p.parseListeners != nil {
p.TriggerEnterRuleEvent() // simulates rule entry for
// left-recursive rules
}
}
//
// Like {@link //EnterRule} but for recursive rules.
func (p *BaseParser) PushNewRecursionContext(localctx ParserRuleContext, state, _ int) {
previous := p.ctx
previous.SetParent(localctx)
previous.SetInvokingState(state)
previous.SetStop(p.input.LT(-1))
p.ctx = localctx
p.ctx.SetStart(previous.GetStart())
if p.BuildParseTrees {
p.ctx.AddChild(previous)
}
if p.parseListeners != nil {
p.TriggerEnterRuleEvent() // simulates rule entry for
// left-recursive rules
}
}
func (p *BaseParser) UnrollRecursionContexts(parentCtx ParserRuleContext) {
_, _ = p.precedenceStack.Pop()
p.ctx.SetStop(p.input.LT(-1))
retCtx := p.ctx // save current ctx (return value)
// unroll so ctx is as it was before call to recursive method
if p.parseListeners != nil {
for p.ctx != parentCtx {
p.TriggerExitRuleEvent()
p.ctx = p.ctx.GetParent().(ParserRuleContext)
}
} else {
p.ctx = parentCtx
}
// hook into tree
retCtx.SetParent(parentCtx)
if p.BuildParseTrees && parentCtx != nil {
// add return ctx into invoking rule's tree
parentCtx.AddChild(retCtx)
}
}
func (p *BaseParser) GetInvokingContext(ruleIndex int) ParserRuleContext {
ctx := p.ctx
for ctx != nil {
if ctx.GetRuleIndex() == ruleIndex {
return ctx
}
ctx = ctx.GetParent().(ParserRuleContext)
}
return nil
}
func (p *BaseParser) Precpred(_ RuleContext, precedence int) bool {
return precedence >= p.precedenceStack[len(p.precedenceStack)-1]
}
//goland:noinspection GoUnusedParameter
func (p *BaseParser) inContext(context ParserRuleContext) bool {
// TODO: useful in parser?
return false
}
// IsExpectedToken checks whether symbol can follow the current state in the
// {ATN}. The behavior of p.method is equivalent to the following, but is
// implemented such that the complete context-sensitive follow set does not
// need to be explicitly constructed.
//
// return getExpectedTokens().contains(symbol)
func (p *BaseParser) IsExpectedToken(symbol int) bool {
atn := p.Interpreter.atn
ctx := p.ctx
s := atn.states[p.state]
following := atn.NextTokens(s, nil)
if following.contains(symbol) {
return true
}
if !following.contains(TokenEpsilon) {
return false
}
for ctx != nil && ctx.GetInvokingState() >= 0 && following.contains(TokenEpsilon) {
invokingState := atn.states[ctx.GetInvokingState()]
rt := invokingState.GetTransitions()[0]
following = atn.NextTokens(rt.(*RuleTransition).followState, nil)
if following.contains(symbol) {
return true
}
ctx = ctx.GetParent().(ParserRuleContext)
}
if following.contains(TokenEpsilon) && symbol == TokenEOF {
return true
}
return false
}
// GetExpectedTokens and returns the set of input symbols which could follow the current parser
// state and context, as given by [GetState] and [GetContext],
// respectively.
func (p *BaseParser) GetExpectedTokens() *IntervalSet {
return p.Interpreter.atn.getExpectedTokens(p.state, p.ctx)
}
func (p *BaseParser) GetExpectedTokensWithinCurrentRule() *IntervalSet {
atn := p.Interpreter.atn
s := atn.states[p.state]
return atn.NextTokens(s, nil)
}
// GetRuleIndex get a rule's index (i.e., RULE_ruleName field) or -1 if not found.
func (p *BaseParser) GetRuleIndex(ruleName string) int {
var ruleIndex, ok = p.GetRuleIndexMap()[ruleName]
if ok {
return ruleIndex
}
return -1
}
// GetRuleInvocationStack returns a list of the rule names in your parser instance
// leading up to a call to the current rule. You could override if
// you want more details such as the file/line info of where
// in the ATN a rule is invoked.
func (p *BaseParser) GetRuleInvocationStack(c ParserRuleContext) []string {
if c == nil {
c = p.ctx
}
stack := make([]string, 0)
for c != nil {
// compute what follows who invoked us
ruleIndex := c.GetRuleIndex()
if ruleIndex < 0 {
stack = append(stack, "n/a")
} else {
stack = append(stack, p.GetRuleNames()[ruleIndex])
}
vp := c.GetParent()
if vp == nil {
break
}
c = vp.(ParserRuleContext)
}
return stack
}
// GetDFAStrings returns a list of all DFA states used for debugging purposes
func (p *BaseParser) GetDFAStrings() string {
return fmt.Sprint(p.Interpreter.decisionToDFA)
}
// DumpDFA prints the whole of the DFA for debugging
func (p *BaseParser) DumpDFA() {
seenOne := false
for _, dfa := range p.Interpreter.decisionToDFA {
if dfa.Len() > 0 {
if seenOne {
fmt.Println()
}
fmt.Println("Decision " + strconv.Itoa(dfa.decision) + ":")
fmt.Print(dfa.String(p.LiteralNames, p.SymbolicNames))
seenOne = true
}
}
}
func (p *BaseParser) GetSourceName() string {
return p.GrammarFileName
}
// SetTrace installs a trace listener for the parse.
//
// During a parse it is sometimes useful to listen in on the rule entry and exit
// events as well as token Matches. This is for quick and dirty debugging.
func (p *BaseParser) SetTrace(trace *TraceListener) {
if trace == nil {
p.RemoveParseListener(p.tracer)
p.tracer = nil
} else {
if p.tracer != nil {
p.RemoveParseListener(p.tracer)
}
p.tracer = NewTraceListener(p)
p.AddParseListener(p.tracer)
}
}
antlr4-go-antlr-c821258/parser_atn_simulator.go 0000664 0000000 0000000 00000171236 14621155120 0021510 0 ustar 00root root 0000000 0000000 // Copyright (c) 2012-2022 The ANTLR Project. All rights reserved.
// Use of this file is governed by the BSD 3-clause license that
// can be found in the LICENSE.txt file in the project root.
package antlr
import (
"fmt"
"strconv"
"strings"
)
// ClosureBusy is a store of ATNConfigs and is a tiny abstraction layer over
// a standard JStore so that we can use Lazy instantiation of the JStore, mostly
// to avoid polluting the stats module with a ton of JStore instances with nothing in them.
type ClosureBusy struct {
bMap *JStore[*ATNConfig, Comparator[*ATNConfig]]
desc string
}
// NewClosureBusy creates a new ClosureBusy instance used to avoid infinite recursion for right-recursive rules
func NewClosureBusy(desc string) *ClosureBusy {
return &ClosureBusy{
desc: desc,
}
}
func (c *ClosureBusy) Put(config *ATNConfig) (*ATNConfig, bool) {
if c.bMap == nil {
c.bMap = NewJStore[*ATNConfig, Comparator[*ATNConfig]](aConfEqInst, ClosureBusyCollection, c.desc)
}
return c.bMap.Put(config)
}
type ParserATNSimulator struct {
BaseATNSimulator
parser Parser
predictionMode int
input TokenStream
startIndex int
dfa *DFA
mergeCache *JPCMap
outerContext ParserRuleContext
}
//goland:noinspection GoUnusedExportedFunction
func NewParserATNSimulator(parser Parser, atn *ATN, decisionToDFA []*DFA, sharedContextCache *PredictionContextCache) *ParserATNSimulator {
p := &ParserATNSimulator{
BaseATNSimulator: BaseATNSimulator{
atn: atn,
sharedContextCache: sharedContextCache,
},
}
p.parser = parser
p.decisionToDFA = decisionToDFA
// SLL, LL, or LL + exact ambig detection?//
p.predictionMode = PredictionModeLL
// LAME globals to avoid parameters!!!!! I need these down deep in predTransition
p.input = nil
p.startIndex = 0
p.outerContext = nil
p.dfa = nil
// Each prediction operation uses a cache for merge of prediction contexts.
// Don't keep around as it wastes huge amounts of memory. [JPCMap]
// isn't Synchronized, but we're ok since two threads shouldn't reuse same
// parser/atn-simulator object because it can only handle one input at a time.
// This maps graphs a and b to merged result c. (a,b) -> c. We can avoid
// the merge if we ever see a and b again. Note that (b,a) -> c should
// also be examined during cache lookup.
//
p.mergeCache = nil
return p
}
func (p *ParserATNSimulator) GetPredictionMode() int {
return p.predictionMode
}
func (p *ParserATNSimulator) SetPredictionMode(v int) {
p.predictionMode = v
}
func (p *ParserATNSimulator) reset() {
}
//goland:noinspection GoBoolExpressions
func (p *ParserATNSimulator) AdaptivePredict(parser *BaseParser, input TokenStream, decision int, outerContext ParserRuleContext) int {
if runtimeConfig.parserATNSimulatorDebug || runtimeConfig.parserATNSimulatorTraceATNSim {
fmt.Println("adaptivePredict decision " + strconv.Itoa(decision) +
" exec LA(1)==" + p.getLookaheadName(input) +
" line " + strconv.Itoa(input.LT(1).GetLine()) + ":" +
strconv.Itoa(input.LT(1).GetColumn()))
}
p.input = input
p.startIndex = input.Index()
p.outerContext = outerContext
dfa := p.decisionToDFA[decision]
p.dfa = dfa
m := input.Mark()
index := input.Index()
defer func() {
p.dfa = nil
p.mergeCache = nil // whack cache after each prediction
// Do not attempt to run a GC now that we're done with the cache as makes the
// GC overhead terrible for badly formed grammars and has little effect on well formed
// grammars.
// I have made some extra effort to try and reduce memory pressure by reusing allocations when
// possible. However, it can only have a limited effect. The real solution is to encourage grammar
// authors to think more carefully about their grammar and to use the new antlr.stats tag to inspect
// what is happening at runtime, along with using the error listener to report ambiguities.
input.Seek(index)
input.Release(m)
}()
// Now we are certain to have a specific decision's DFA
// But, do we still need an initial state?
var s0 *DFAState
p.atn.stateMu.RLock()
if dfa.getPrecedenceDfa() {
p.atn.edgeMu.RLock()
// the start state for a precedence DFA depends on the current
// parser precedence, and is provided by a DFA method.
s0 = dfa.getPrecedenceStartState(p.parser.GetPrecedence())
p.atn.edgeMu.RUnlock()
} else {
// the start state for a "regular" DFA is just s0
s0 = dfa.getS0()
}
p.atn.stateMu.RUnlock()
if s0 == nil {
if outerContext == nil {
outerContext = ParserRuleContextEmpty
}
if runtimeConfig.parserATNSimulatorDebug {
fmt.Println("predictATN decision " + strconv.Itoa(dfa.decision) +
" exec LA(1)==" + p.getLookaheadName(input) +
", outerContext=" + outerContext.String(p.parser.GetRuleNames(), nil))
}
fullCtx := false
s0Closure := p.computeStartState(dfa.atnStartState, ParserRuleContextEmpty, fullCtx)
p.atn.stateMu.Lock()
if dfa.getPrecedenceDfa() {
// If p is a precedence DFA, we use applyPrecedenceFilter
// to convert the computed start state to a precedence start
// state. We then use DFA.setPrecedenceStartState to set the
// appropriate start state for the precedence level rather
// than simply setting DFA.s0.
//
dfa.s0.configs = s0Closure
s0Closure = p.applyPrecedenceFilter(s0Closure)
s0 = p.addDFAState(dfa, NewDFAState(-1, s0Closure))
p.atn.edgeMu.Lock()
dfa.setPrecedenceStartState(p.parser.GetPrecedence(), s0)
p.atn.edgeMu.Unlock()
} else {
s0 = p.addDFAState(dfa, NewDFAState(-1, s0Closure))
dfa.setS0(s0)
}
p.atn.stateMu.Unlock()
}
alt, re := p.execATN(dfa, s0, input, index, outerContext)
parser.SetError(re)
if runtimeConfig.parserATNSimulatorDebug {
fmt.Println("DFA after predictATN: " + dfa.String(p.parser.GetLiteralNames(), nil))
}
return alt
}
// execATN performs ATN simulation to compute a predicted alternative based
// upon the remaining input, but also updates the DFA cache to avoid
// having to traverse the ATN again for the same input sequence.
//
// There are some key conditions we're looking for after computing a new
// set of ATN configs (proposed DFA state):
//
// - If the set is empty, there is no viable alternative for current symbol
// - Does the state uniquely predict an alternative?
// - Does the state have a conflict that would prevent us from
// putting it on the work list?
//
// We also have some key operations to do:
//
// - Add an edge from previous DFA state to potentially NewDFA state, D,
// - Upon current symbol but only if adding to work list, which means in all
// cases except no viable alternative (and possibly non-greedy decisions?)
// - Collecting predicates and adding semantic context to DFA accept states
// - adding rule context to context-sensitive DFA accept states
// - Consuming an input symbol
// - Reporting a conflict
// - Reporting an ambiguity
// - Reporting a context sensitivity
// - Reporting insufficient predicates
//
// Cover these cases:
//
// - dead end
// - single alt
// - single alt + predicates
// - conflict
// - conflict + predicates
//
//goland:noinspection GoBoolExpressions
func (p *ParserATNSimulator) execATN(dfa *DFA, s0 *DFAState, input TokenStream, startIndex int, outerContext ParserRuleContext) (int, RecognitionException) {
if runtimeConfig.parserATNSimulatorDebug || runtimeConfig.parserATNSimulatorTraceATNSim {
fmt.Println("execATN decision " + strconv.Itoa(dfa.decision) +
", DFA state " + s0.String() +
", LA(1)==" + p.getLookaheadName(input) +
" line " + strconv.Itoa(input.LT(1).GetLine()) + ":" + strconv.Itoa(input.LT(1).GetColumn()))
}
previousD := s0
if runtimeConfig.parserATNSimulatorDebug {
fmt.Println("s0 = " + s0.String())
}
t := input.LA(1)
for { // for more work
D := p.getExistingTargetState(previousD, t)
if D == nil {
D = p.computeTargetState(dfa, previousD, t)
}
if D == ATNSimulatorError {
// if any configs in previous dipped into outer context, that
// means that input up to t actually finished entry rule
// at least for SLL decision. Full LL doesn't dip into outer
// so don't need special case.
// We will get an error no matter what so delay until after
// decision better error message. Also, no reachable target
// ATN states in SLL implies LL will also get nowhere.
// If conflict in states that dip out, choose min since we
// will get error no matter what.
e := p.noViableAlt(input, outerContext, previousD.configs, startIndex)
input.Seek(startIndex)
alt := p.getSynValidOrSemInvalidAltThatFinishedDecisionEntryRule(previousD.configs, outerContext)
if alt != ATNInvalidAltNumber {
return alt, nil
}
p.parser.SetError(e)
return ATNInvalidAltNumber, e
}
if D.requiresFullContext && p.predictionMode != PredictionModeSLL {
// IF PREDS, MIGHT RESOLVE TO SINGLE ALT => SLL (or syntax error)
conflictingAlts := D.configs.conflictingAlts
if D.predicates != nil {
if runtimeConfig.parserATNSimulatorDebug {
fmt.Println("DFA state has preds in DFA sim LL fail-over")
}
conflictIndex := input.Index()
if conflictIndex != startIndex {
input.Seek(startIndex)
}
conflictingAlts = p.evalSemanticContext(D.predicates, outerContext, true)
if conflictingAlts.length() == 1 {
if runtimeConfig.parserATNSimulatorDebug {
fmt.Println("Full LL avoided")
}
return conflictingAlts.minValue(), nil
}
if conflictIndex != startIndex {
// restore the index so Reporting the fallback to full
// context occurs with the index at the correct spot
input.Seek(conflictIndex)
}
}
if runtimeConfig.parserATNSimulatorDFADebug {
fmt.Println("ctx sensitive state " + outerContext.String(nil, nil) + " in " + D.String())
}
fullCtx := true
s0Closure := p.computeStartState(dfa.atnStartState, outerContext, fullCtx)
p.ReportAttemptingFullContext(dfa, conflictingAlts, D.configs, startIndex, input.Index())
alt, re := p.execATNWithFullContext(dfa, D, s0Closure, input, startIndex, outerContext)
return alt, re
}
if D.isAcceptState {
if D.predicates == nil {
return D.prediction, nil
}
stopIndex := input.Index()
input.Seek(startIndex)
alts := p.evalSemanticContext(D.predicates, outerContext, true)
switch alts.length() {
case 0:
return ATNInvalidAltNumber, p.noViableAlt(input, outerContext, D.configs, startIndex)
case 1:
return alts.minValue(), nil
default:
// Report ambiguity after predicate evaluation to make sure the correct set of ambig alts is Reported.
p.ReportAmbiguity(dfa, D, startIndex, stopIndex, false, alts, D.configs)
return alts.minValue(), nil
}
}
previousD = D
if t != TokenEOF {
input.Consume()
t = input.LA(1)
}
}
}
// Get an existing target state for an edge in the DFA. If the target state
// for the edge has not yet been computed or is otherwise not available,
// p method returns {@code nil}.
//
// @param previousD The current DFA state
// @param t The next input symbol
// @return The existing target DFA state for the given input symbol
// {@code t}, or {@code nil} if the target state for p edge is not
// already cached
func (p *ParserATNSimulator) getExistingTargetState(previousD *DFAState, t int) *DFAState {
if t+1 < 0 {
return nil
}
p.atn.edgeMu.RLock()
defer p.atn.edgeMu.RUnlock()
edges := previousD.getEdges()
if edges == nil || t+1 >= len(edges) {
return nil
}
return previousD.getIthEdge(t + 1)
}
// Compute a target state for an edge in the DFA, and attempt to add the
// computed state and corresponding edge to the DFA.
//
// @param dfa The DFA
// @param previousD The current DFA state
// @param t The next input symbol
//
// @return The computed target DFA state for the given input symbol
// {@code t}. If {@code t} does not lead to a valid DFA state, p method
// returns {@link //ERROR}.
//
//goland:noinspection GoBoolExpressions
func (p *ParserATNSimulator) computeTargetState(dfa *DFA, previousD *DFAState, t int) *DFAState {
reach := p.computeReachSet(previousD.configs, t, false)
if reach == nil {
p.addDFAEdge(dfa, previousD, t, ATNSimulatorError)
return ATNSimulatorError
}
// create new target state we'll add to DFA after it's complete
D := NewDFAState(-1, reach)
predictedAlt := p.getUniqueAlt(reach)
if runtimeConfig.parserATNSimulatorDebug {
altSubSets := PredictionModegetConflictingAltSubsets(reach)
fmt.Println("SLL altSubSets=" + fmt.Sprint(altSubSets) +
", previous=" + previousD.configs.String() +
", configs=" + reach.String() +
", predict=" + strconv.Itoa(predictedAlt) +
", allSubsetsConflict=" +
fmt.Sprint(PredictionModeallSubsetsConflict(altSubSets)) +
", conflictingAlts=" + p.getConflictingAlts(reach).String())
}
if predictedAlt != ATNInvalidAltNumber {
// NO CONFLICT, UNIQUELY PREDICTED ALT
D.isAcceptState = true
D.configs.uniqueAlt = predictedAlt
D.setPrediction(predictedAlt)
} else if PredictionModehasSLLConflictTerminatingPrediction(p.predictionMode, reach) {
// MORE THAN ONE VIABLE ALTERNATIVE
D.configs.conflictingAlts = p.getConflictingAlts(reach)
D.requiresFullContext = true
// in SLL-only mode, we will stop at p state and return the minimum alt
D.isAcceptState = true
D.setPrediction(D.configs.conflictingAlts.minValue())
}
if D.isAcceptState && D.configs.hasSemanticContext {
p.predicateDFAState(D, p.atn.getDecisionState(dfa.decision))
if D.predicates != nil {
D.setPrediction(ATNInvalidAltNumber)
}
}
// all adds to dfa are done after we've created full D state
D = p.addDFAEdge(dfa, previousD, t, D)
return D
}
func (p *ParserATNSimulator) predicateDFAState(dfaState *DFAState, decisionState DecisionState) {
// We need to test all predicates, even in DFA states that
// uniquely predict alternative.
nalts := len(decisionState.GetTransitions())
// Update DFA so reach becomes accept state with (predicate,alt)
// pairs if preds found for conflicting alts
altsToCollectPredsFrom := p.getConflictingAltsOrUniqueAlt(dfaState.configs)
altToPred := p.getPredsForAmbigAlts(altsToCollectPredsFrom, dfaState.configs, nalts)
if altToPred != nil {
dfaState.predicates = p.getPredicatePredictions(altsToCollectPredsFrom, altToPred)
dfaState.setPrediction(ATNInvalidAltNumber) // make sure we use preds
} else {
// There are preds in configs but they might go away
// when OR'd together like {p}? || NONE == NONE. If neither
// alt has preds, resolve to min alt
dfaState.setPrediction(altsToCollectPredsFrom.minValue())
}
}
// comes back with reach.uniqueAlt set to a valid alt
//
//goland:noinspection GoBoolExpressions
func (p *ParserATNSimulator) execATNWithFullContext(dfa *DFA, D *DFAState, s0 *ATNConfigSet, input TokenStream, startIndex int, outerContext ParserRuleContext) (int, RecognitionException) {
if runtimeConfig.parserATNSimulatorDebug || runtimeConfig.parserATNSimulatorTraceATNSim {
fmt.Println("execATNWithFullContext " + s0.String())
}
fullCtx := true
foundExactAmbig := false
var reach *ATNConfigSet
previous := s0
input.Seek(startIndex)
t := input.LA(1)
predictedAlt := -1
for { // for more work
reach = p.computeReachSet(previous, t, fullCtx)
if reach == nil {
// if any configs in previous dipped into outer context, that
// means that input up to t actually finished entry rule
// at least for LL decision. Full LL doesn't dip into outer
// so don't need special case.
// We will get an error no matter what so delay until after
// decision better error message. Also, no reachable target
// ATN states in SLL implies LL will also get nowhere.
// If conflict in states that dip out, choose min since we
// will get error no matter what.
input.Seek(startIndex)
alt := p.getSynValidOrSemInvalidAltThatFinishedDecisionEntryRule(previous, outerContext)
if alt != ATNInvalidAltNumber {
return alt, nil
}
return alt, p.noViableAlt(input, outerContext, previous, startIndex)
}
altSubSets := PredictionModegetConflictingAltSubsets(reach)
if runtimeConfig.parserATNSimulatorDebug {
fmt.Println("LL altSubSets=" + fmt.Sprint(altSubSets) + ", predict=" +
strconv.Itoa(PredictionModegetUniqueAlt(altSubSets)) + ", resolvesToJustOneViableAlt=" +
fmt.Sprint(PredictionModeresolvesToJustOneViableAlt(altSubSets)))
}
reach.uniqueAlt = p.getUniqueAlt(reach)
// unique prediction?
if reach.uniqueAlt != ATNInvalidAltNumber {
predictedAlt = reach.uniqueAlt
break
}
if p.predictionMode != PredictionModeLLExactAmbigDetection {
predictedAlt = PredictionModeresolvesToJustOneViableAlt(altSubSets)
if predictedAlt != ATNInvalidAltNumber {
break
}
} else {
// In exact ambiguity mode, we never try to terminate early.
// Just keeps scarfing until we know what the conflict is
if PredictionModeallSubsetsConflict(altSubSets) && PredictionModeallSubsetsEqual(altSubSets) {
foundExactAmbig = true
predictedAlt = PredictionModegetSingleViableAlt(altSubSets)
break
}
// else there are multiple non-conflicting subsets or
// we're not sure what the ambiguity is yet.
// So, keep going.
}
previous = reach
if t != TokenEOF {
input.Consume()
t = input.LA(1)
}
}
// If the configuration set uniquely predicts an alternative,
// without conflict, then we know that it's a full LL decision
// not SLL.
if reach.uniqueAlt != ATNInvalidAltNumber {
p.ReportContextSensitivity(dfa, predictedAlt, reach, startIndex, input.Index())
return predictedAlt, nil
}
// We do not check predicates here because we have checked them
// on-the-fly when doing full context prediction.
//
// In non-exact ambiguity detection mode, we might actually be able to
// detect an exact ambiguity, but I'm not going to spend the cycles
// needed to check. We only emit ambiguity warnings in exact ambiguity
// mode.
//
// For example, we might know that we have conflicting configurations.
// But, that does not mean that there is no way forward without a
// conflict. It's possible to have non-conflicting alt subsets as in:
//
// altSubSets=[{1, 2}, {1, 2}, {1}, {1, 2}]
//
// from
//
// [(17,1,[5 $]), (13,1,[5 10 $]), (21,1,[5 10 $]), (11,1,[$]),
// (13,2,[5 10 $]), (21,2,[5 10 $]), (11,2,[$])]
//
// In p case, (17,1,[5 $]) indicates there is some next sequence that
// would resolve p without conflict to alternative 1. Any other viable
// next sequence, however, is associated with a conflict. We stop
// looking for input because no amount of further lookahead will alter
// the fact that we should predict alternative 1. We just can't say for
// sure that there is an ambiguity without looking further.
p.ReportAmbiguity(dfa, D, startIndex, input.Index(), foundExactAmbig, reach.Alts(), reach)
return predictedAlt, nil
}
//goland:noinspection GoBoolExpressions
func (p *ParserATNSimulator) computeReachSet(closure *ATNConfigSet, t int, fullCtx bool) *ATNConfigSet {
if p.mergeCache == nil {
p.mergeCache = NewJPCMap(ReachSetCollection, "Merge cache for computeReachSet()")
}
intermediate := NewATNConfigSet(fullCtx)
// Configurations already in a rule stop state indicate reaching the end
// of the decision rule (local context) or end of the start rule (full
// context). Once reached, these configurations are never updated by a
// closure operation, so they are handled separately for the performance
// advantage of having a smaller intermediate set when calling closure.
//
// For full-context reach operations, separate handling is required to
// ensure that the alternative Matching the longest overall sequence is
// chosen when multiple such configurations can Match the input.
var skippedStopStates []*ATNConfig
// First figure out where we can reach on input t
for _, c := range closure.configs {
if runtimeConfig.parserATNSimulatorDebug {
fmt.Println("testing " + p.GetTokenName(t) + " at " + c.String())
}
if _, ok := c.GetState().(*RuleStopState); ok {
if fullCtx || t == TokenEOF {
skippedStopStates = append(skippedStopStates, c)
if runtimeConfig.parserATNSimulatorDebug {
fmt.Println("added " + c.String() + " to SkippedStopStates")
}
}
continue
}
for _, trans := range c.GetState().GetTransitions() {
target := p.getReachableTarget(trans, t)
if target != nil {
cfg := NewATNConfig4(c, target)
intermediate.Add(cfg, p.mergeCache)
if runtimeConfig.parserATNSimulatorDebug {
fmt.Println("added " + cfg.String() + " to intermediate")
}
}
}
}
// Now figure out where the reach operation can take us...
var reach *ATNConfigSet
// This block optimizes the reach operation for intermediate sets which
// trivially indicate a termination state for the overall
// AdaptivePredict operation.
//
// The conditions assume that intermediate
// contains all configurations relevant to the reach set, but p
// condition is not true when one or more configurations have been
// withheld in SkippedStopStates, or when the current symbol is EOF.
//
if skippedStopStates == nil && t != TokenEOF {
if len(intermediate.configs) == 1 {
// Don't pursue the closure if there is just one state.
// It can only have one alternative just add to result
// Also don't pursue the closure if there is unique alternative
// among the configurations.
reach = intermediate
} else if p.getUniqueAlt(intermediate) != ATNInvalidAltNumber {
// Also don't pursue the closure if there is unique alternative
// among the configurations.
reach = intermediate
}
}
// If the reach set could not be trivially determined, perform a closure
// operation on the intermediate set to compute its initial value.
//
if reach == nil {
reach = NewATNConfigSet(fullCtx)
closureBusy := NewClosureBusy("ParserATNSimulator.computeReachSet() make a closureBusy")
treatEOFAsEpsilon := t == TokenEOF
amount := len(intermediate.configs)
for k := 0; k < amount; k++ {
p.closure(intermediate.configs[k], reach, closureBusy, false, fullCtx, treatEOFAsEpsilon)
}
}
if t == TokenEOF {
// After consuming EOF no additional input is possible, so we are
// only interested in configurations which reached the end of the
// decision rule (local context) or end of the start rule (full
// context). Update reach to contain only these configurations. This
// handles both explicit EOF transitions in the grammar and implicit
// EOF transitions following the end of the decision or start rule.
//
// When reach==intermediate, no closure operation was performed. In
// p case, removeAllConfigsNotInRuleStopState needs to check for
// reachable rule stop states as well as configurations already in
// a rule stop state.
//
// This is handled before the configurations in SkippedStopStates,
// because any configurations potentially added from that list are
// already guaranteed to meet this condition whether it's
// required.
//
reach = p.removeAllConfigsNotInRuleStopState(reach, reach.Equals(intermediate))
}
// If SkippedStopStates!=nil, then it contains at least one
// configuration. For full-context reach operations, these
// configurations reached the end of the start rule, in which case we
// only add them back to reach if no configuration during the current
// closure operation reached such a state. This ensures AdaptivePredict
// chooses an alternative Matching the longest overall sequence when
// multiple alternatives are viable.
//
if skippedStopStates != nil && ((!fullCtx) || (!PredictionModehasConfigInRuleStopState(reach))) {
for l := 0; l < len(skippedStopStates); l++ {
reach.Add(skippedStopStates[l], p.mergeCache)
}
}
if runtimeConfig.parserATNSimulatorTraceATNSim {
fmt.Println("computeReachSet " + closure.String() + " -> " + reach.String())
}
if len(reach.configs) == 0 {
return nil
}
return reach
}
// removeAllConfigsNotInRuleStopState returns a configuration set containing only the configurations from
// configs which are in a [RuleStopState]. If all
// configurations in configs are already in a rule stop state, this
// method simply returns configs.
//
// When lookToEndOfRule is true, this method uses
// [ATN].[NextTokens] for each configuration in configs which is
// not already in a rule stop state to see if a rule stop state is reachable
// from the configuration via epsilon-only transitions.
//
// When lookToEndOfRule is true, this method checks for rule stop states
// reachable by epsilon-only transitions from each configuration in
// configs.
//
// The func returns configs if all configurations in configs are in a
// rule stop state, otherwise it returns a new configuration set containing only
// the configurations from configs which are in a rule stop state
func (p *ParserATNSimulator) removeAllConfigsNotInRuleStopState(configs *ATNConfigSet, lookToEndOfRule bool) *ATNConfigSet {
if PredictionModeallConfigsInRuleStopStates(configs) {
return configs
}
result := NewATNConfigSet(configs.fullCtx)
for _, config := range configs.configs {
if _, ok := config.GetState().(*RuleStopState); ok {
result.Add(config, p.mergeCache)
continue
}
if lookToEndOfRule && config.GetState().GetEpsilonOnlyTransitions() {
NextTokens := p.atn.NextTokens(config.GetState(), nil)
if NextTokens.contains(TokenEpsilon) {
endOfRuleState := p.atn.ruleToStopState[config.GetState().GetRuleIndex()]
result.Add(NewATNConfig4(config, endOfRuleState), p.mergeCache)
}
}
}
return result
}
//goland:noinspection GoBoolExpressions
func (p *ParserATNSimulator) computeStartState(a ATNState, ctx RuleContext, fullCtx bool) *ATNConfigSet {
// always at least the implicit call to start rule
initialContext := predictionContextFromRuleContext(p.atn, ctx)
configs := NewATNConfigSet(fullCtx)
if runtimeConfig.parserATNSimulatorDebug || runtimeConfig.parserATNSimulatorTraceATNSim {
fmt.Println("computeStartState from ATN state " + a.String() +
" initialContext=" + initialContext.String())
}
for i := 0; i < len(a.GetTransitions()); i++ {
target := a.GetTransitions()[i].getTarget()
c := NewATNConfig6(target, i+1, initialContext)
closureBusy := NewClosureBusy("ParserATNSimulator.computeStartState() make a closureBusy")
p.closure(c, configs, closureBusy, true, fullCtx, false)
}
return configs
}
// applyPrecedenceFilter transforms the start state computed by
// [computeStartState] to the special start state used by a
// precedence [DFA] for a particular precedence value. The transformation
// process applies the following changes to the start state's configuration
// set.
//
// 1. Evaluate the precedence predicates for each configuration using
// [SemanticContext].evalPrecedence.
// 2. Remove all configurations which predict an alternative greater than
// 1, for which another configuration that predicts alternative 1 is in the
// same ATN state with the same prediction context.
//
// Transformation 2 is valid for the following reasons:
//
// - The closure block cannot contain any epsilon transitions which bypass
// the body of the closure, so all states reachable via alternative 1 are
// part of the precedence alternatives of the transformed left-recursive
// rule.
// - The "primary" portion of a left recursive rule cannot contain an
// epsilon transition, so the only way an alternative other than 1 can exist
// in a state that is also reachable via alternative 1 is by nesting calls
// to the left-recursive rule, with the outer calls not being at the
// preferred precedence level.
//
// The prediction context must be considered by this filter to address
// situations like the following:
//
// grammar TA
// prog: statement* EOF
// statement: letterA | statement letterA 'b'
// letterA: 'a'
//
// In the above grammar, the [ATN] state immediately before the token
// reference 'a' in letterA is reachable from the left edge
// of both the primary and closure blocks of the left-recursive rule
// statement. The prediction context associated with each of these
// configurations distinguishes between them, and prevents the alternative
// which stepped out to prog, and then back in to statement
// from being eliminated by the filter.
//
// The func returns the transformed configuration set representing the start state
// for a precedence [DFA] at a particular precedence level (determined by
// calling [Parser].getPrecedence).
func (p *ParserATNSimulator) applyPrecedenceFilter(configs *ATNConfigSet) *ATNConfigSet {
statesFromAlt1 := make(map[int]*PredictionContext)
configSet := NewATNConfigSet(configs.fullCtx)
for _, config := range configs.configs {
// handle alt 1 first
if config.GetAlt() != 1 {
continue
}
updatedContext := config.GetSemanticContext().evalPrecedence(p.parser, p.outerContext)
if updatedContext == nil {
// the configuration was eliminated
continue
}
statesFromAlt1[config.GetState().GetStateNumber()] = config.GetContext()
if updatedContext != config.GetSemanticContext() {
configSet.Add(NewATNConfig2(config, updatedContext), p.mergeCache)
} else {
configSet.Add(config, p.mergeCache)
}
}
for _, config := range configs.configs {
if config.GetAlt() == 1 {
// already handled
continue
}
// In the future, p elimination step could be updated to also
// filter the prediction context for alternatives predicting alt>1
// (basically a graph subtraction algorithm).
if !config.getPrecedenceFilterSuppressed() {
context := statesFromAlt1[config.GetState().GetStateNumber()]
if context != nil && context.Equals(config.GetContext()) {
// eliminated
continue
}
}
configSet.Add(config, p.mergeCache)
}
return configSet
}
func (p *ParserATNSimulator) getReachableTarget(trans Transition, ttype int) ATNState {
if trans.Matches(ttype, 0, p.atn.maxTokenType) {
return trans.getTarget()
}
return nil
}
//goland:noinspection GoBoolExpressions
func (p *ParserATNSimulator) getPredsForAmbigAlts(ambigAlts *BitSet, configs *ATNConfigSet, nalts int) []SemanticContext {
altToPred := make([]SemanticContext, nalts+1)
for _, c := range configs.configs {
if ambigAlts.contains(c.GetAlt()) {
altToPred[c.GetAlt()] = SemanticContextorContext(altToPred[c.GetAlt()], c.GetSemanticContext())
}
}
nPredAlts := 0
for i := 1; i <= nalts; i++ {
pred := altToPred[i]
if pred == nil {
altToPred[i] = SemanticContextNone
} else if pred != SemanticContextNone {
nPredAlts++
}
}
// unambiguous alts are nil in altToPred
if nPredAlts == 0 {
altToPred = nil
}
if runtimeConfig.parserATNSimulatorDebug {
fmt.Println("getPredsForAmbigAlts result " + fmt.Sprint(altToPred))
}
return altToPred
}
func (p *ParserATNSimulator) getPredicatePredictions(ambigAlts *BitSet, altToPred []SemanticContext) []*PredPrediction {
pairs := make([]*PredPrediction, 0)
containsPredicate := false
for i := 1; i < len(altToPred); i++ {
pred := altToPred[i]
// un-predicated is indicated by SemanticContextNONE
if ambigAlts != nil && ambigAlts.contains(i) {
pairs = append(pairs, NewPredPrediction(pred, i))
}
if pred != SemanticContextNone {
containsPredicate = true
}
}
if !containsPredicate {
return nil
}
return pairs
}
// getSynValidOrSemInvalidAltThatFinishedDecisionEntryRule is used to improve the localization of error messages by
// choosing an alternative rather than panic a NoViableAltException in particular prediction scenarios where the
// Error state was reached during [ATN] simulation.
//
// The default implementation of this method uses the following
// algorithm to identify an [ATN] configuration which successfully parsed the
// decision entry rule. Choosing such an alternative ensures that the
// [ParserRuleContext] returned by the calling rule will be complete
// and valid, and the syntax error will be Reported later at a more
// localized location.
//
// - If a syntactically valid path or paths reach the end of the decision rule, and
// they are semantically valid if predicated, return the min associated alt.
// - Else, if a semantically invalid but syntactically valid path exist
// or paths exist, return the minimum associated alt.
// - Otherwise, return [ATNInvalidAltNumber].
//
// In some scenarios, the algorithm described above could predict an
// alternative which will result in a [FailedPredicateException] in
// the parser. Specifically, this could occur if the only configuration
// capable of successfully parsing to the end of the decision rule is
// blocked by a semantic predicate. By choosing this alternative within
// [AdaptivePredict] instead of panic a [NoViableAltException], the resulting
// [FailedPredicateException] in the parser will identify the specific
// predicate which is preventing the parser from successfully parsing the
// decision rule, which helps developers identify and correct logic errors
// in semantic predicates.
//
// pass in the configs holding ATN configurations which were valid immediately before
// the ERROR state was reached, outerContext as the initial parser context from the paper
// or the parser stack at the instant before prediction commences.
//
// The func returns the value to return from [AdaptivePredict], or
// [ATNInvalidAltNumber] if a suitable alternative was not
// identified and [AdaptivePredict] should report an error instead.
func (p *ParserATNSimulator) getSynValidOrSemInvalidAltThatFinishedDecisionEntryRule(configs *ATNConfigSet, outerContext ParserRuleContext) int {
cfgs := p.splitAccordingToSemanticValidity(configs, outerContext)
semValidConfigs := cfgs[0]
semInvalidConfigs := cfgs[1]
alt := p.GetAltThatFinishedDecisionEntryRule(semValidConfigs)
if alt != ATNInvalidAltNumber { // semantically/syntactically viable path exists
return alt
}
// Is there a syntactically valid path with a failed pred?
if len(semInvalidConfigs.configs) > 0 {
alt = p.GetAltThatFinishedDecisionEntryRule(semInvalidConfigs)
if alt != ATNInvalidAltNumber { // syntactically viable path exists
return alt
}
}
return ATNInvalidAltNumber
}
func (p *ParserATNSimulator) GetAltThatFinishedDecisionEntryRule(configs *ATNConfigSet) int {
alts := NewIntervalSet()
for _, c := range configs.configs {
_, ok := c.GetState().(*RuleStopState)
if c.GetReachesIntoOuterContext() > 0 || (ok && c.GetContext().hasEmptyPath()) {
alts.addOne(c.GetAlt())
}
}
if alts.length() == 0 {
return ATNInvalidAltNumber
}
return alts.first()
}
// Walk the list of configurations and split them according to
// those that have preds evaluating to true/false. If no pred, assume
// true pred and include in succeeded set. Returns Pair of sets.
//
// Create a NewSet so as not to alter the incoming parameter.
//
// Assumption: the input stream has been restored to the starting point
// prediction, which is where predicates need to evaluate.
type ATNConfigSetPair struct {
item0, item1 *ATNConfigSet
}
func (p *ParserATNSimulator) splitAccordingToSemanticValidity(configs *ATNConfigSet, outerContext ParserRuleContext) []*ATNConfigSet {
succeeded := NewATNConfigSet(configs.fullCtx)
failed := NewATNConfigSet(configs.fullCtx)
for _, c := range configs.configs {
if c.GetSemanticContext() != SemanticContextNone {
predicateEvaluationResult := c.GetSemanticContext().evaluate(p.parser, outerContext)
if predicateEvaluationResult {
succeeded.Add(c, nil)
} else {
failed.Add(c, nil)
}
} else {
succeeded.Add(c, nil)
}
}
return []*ATNConfigSet{succeeded, failed}
}
// evalSemanticContext looks through a list of predicate/alt pairs, returning alts for the
// pairs that win. A [SemanticContextNone] predicate indicates an alt containing an
// un-predicated runtimeConfig which behaves as "always true." If !complete
// then we stop at the first predicate that evaluates to true. This
// includes pairs with nil predicates.
//
//goland:noinspection GoBoolExpressions
func (p *ParserATNSimulator) evalSemanticContext(predPredictions []*PredPrediction, outerContext ParserRuleContext, complete bool) *BitSet {
predictions := NewBitSet()
for i := 0; i < len(predPredictions); i++ {
pair := predPredictions[i]
if pair.pred == SemanticContextNone {
predictions.add(pair.alt)
if !complete {
break
}
continue
}
predicateEvaluationResult := pair.pred.evaluate(p.parser, outerContext)
if runtimeConfig.parserATNSimulatorDebug || runtimeConfig.parserATNSimulatorDFADebug {
fmt.Println("eval pred " + pair.String() + "=" + fmt.Sprint(predicateEvaluationResult))
}
if predicateEvaluationResult {
if runtimeConfig.parserATNSimulatorDebug || runtimeConfig.parserATNSimulatorDFADebug {
fmt.Println("PREDICT " + fmt.Sprint(pair.alt))
}
predictions.add(pair.alt)
if !complete {
break
}
}
}
return predictions
}
func (p *ParserATNSimulator) closure(config *ATNConfig, configs *ATNConfigSet, closureBusy *ClosureBusy, collectPredicates, fullCtx, treatEOFAsEpsilon bool) {
initialDepth := 0
p.closureCheckingStopState(config, configs, closureBusy, collectPredicates,
fullCtx, initialDepth, treatEOFAsEpsilon)
}
func (p *ParserATNSimulator) closureCheckingStopState(config *ATNConfig, configs *ATNConfigSet, closureBusy *ClosureBusy, collectPredicates, fullCtx bool, depth int, treatEOFAsEpsilon bool) {
if runtimeConfig.parserATNSimulatorTraceATNSim {
fmt.Println("closure(" + config.String() + ")")
}
var stack []*ATNConfig
visited := make(map[*ATNConfig]bool)
stack = append(stack, config)
for len(stack) > 0 {
currConfig := stack[len(stack)-1]
stack = stack[:len(stack)-1]
if _, ok := visited[currConfig]; ok {
continue
}
visited[currConfig] = true
if _, ok := currConfig.GetState().(*RuleStopState); ok {
// We hit rule end. If we have context info, use it
// run thru all possible stack tops in ctx
if !currConfig.GetContext().isEmpty() {
for i := 0; i < currConfig.GetContext().length(); i++ {
if currConfig.GetContext().getReturnState(i) == BasePredictionContextEmptyReturnState {
if fullCtx {
nb := NewATNConfig1(currConfig, currConfig.GetState(), BasePredictionContextEMPTY)
configs.Add(nb, p.mergeCache)
continue
} else {
// we have no context info, just chase follow links (if greedy)
if runtimeConfig.parserATNSimulatorDebug {
fmt.Println("FALLING off rule " + p.getRuleName(currConfig.GetState().GetRuleIndex()))
}
p.closureWork(currConfig, configs, closureBusy, collectPredicates, fullCtx, depth, treatEOFAsEpsilon)
}
continue
}
returnState := p.atn.states[currConfig.GetContext().getReturnState(i)]
newContext := currConfig.GetContext().GetParent(i) // "pop" return state
c := NewATNConfig5(returnState, currConfig.GetAlt(), newContext, currConfig.GetSemanticContext())
// While we have context to pop back from, we may have
// gotten that context AFTER having falling off a rule.
// Make sure we track that we are now out of context.
c.SetReachesIntoOuterContext(currConfig.GetReachesIntoOuterContext())
stack = append(stack, c)
}
continue
} else if fullCtx {
// reached end of start rule
configs.Add(currConfig, p.mergeCache)
continue
} else {
// else if we have no context info, just chase follow links (if greedy)
if runtimeConfig.parserATNSimulatorDebug {
fmt.Println("FALLING off rule " + p.getRuleName(currConfig.GetState().GetRuleIndex()))
}
}
}
p.closureWork(currConfig, configs, closureBusy, collectPredicates, fullCtx, depth, treatEOFAsEpsilon)
}
}
//goland:noinspection GoBoolExpressions
func (p *ParserATNSimulator) closureCheckingStopStateRecursive(config *ATNConfig, configs *ATNConfigSet, closureBusy *ClosureBusy, collectPredicates, fullCtx bool, depth int, treatEOFAsEpsilon bool) {
if runtimeConfig.parserATNSimulatorTraceATNSim {
fmt.Println("closure(" + config.String() + ")")
}
if _, ok := config.GetState().(*RuleStopState); ok {
// We hit rule end. If we have context info, use it
// run thru all possible stack tops in ctx
if !config.GetContext().isEmpty() {
for i := 0; i < config.GetContext().length(); i++ {
if config.GetContext().getReturnState(i) == BasePredictionContextEmptyReturnState {
if fullCtx {
nb := NewATNConfig1(config, config.GetState(), BasePredictionContextEMPTY)
configs.Add(nb, p.mergeCache)
continue
} else {
// we have no context info, just chase follow links (if greedy)
if runtimeConfig.parserATNSimulatorDebug {
fmt.Println("FALLING off rule " + p.getRuleName(config.GetState().GetRuleIndex()))
}
p.closureWork(config, configs, closureBusy, collectPredicates, fullCtx, depth, treatEOFAsEpsilon)
}
continue
}
returnState := p.atn.states[config.GetContext().getReturnState(i)]
newContext := config.GetContext().GetParent(i) // "pop" return state
c := NewATNConfig5(returnState, config.GetAlt(), newContext, config.GetSemanticContext())
// While we have context to pop back from, we may have
// gotten that context AFTER having falling off a rule.
// Make sure we track that we are now out of context.
c.SetReachesIntoOuterContext(config.GetReachesIntoOuterContext())
p.closureCheckingStopState(c, configs, closureBusy, collectPredicates, fullCtx, depth-1, treatEOFAsEpsilon)
}
return
} else if fullCtx {
// reached end of start rule
configs.Add(config, p.mergeCache)
return
} else {
// else if we have no context info, just chase follow links (if greedy)
if runtimeConfig.parserATNSimulatorDebug {
fmt.Println("FALLING off rule " + p.getRuleName(config.GetState().GetRuleIndex()))
}
}
}
p.closureWork(config, configs, closureBusy, collectPredicates, fullCtx, depth, treatEOFAsEpsilon)
}
// Do the actual work of walking epsilon edges
//
//goland:noinspection GoBoolExpressions
func (p *ParserATNSimulator) closureWork(config *ATNConfig, configs *ATNConfigSet, closureBusy *ClosureBusy, collectPredicates, fullCtx bool, depth int, treatEOFAsEpsilon bool) {
state := config.GetState()
// optimization
if !state.GetEpsilonOnlyTransitions() {
configs.Add(config, p.mergeCache)
// make sure to not return here, because EOF transitions can act as
// both epsilon transitions and non-epsilon transitions.
}
for i := 0; i < len(state.GetTransitions()); i++ {
if i == 0 && p.canDropLoopEntryEdgeInLeftRecursiveRule(config) {
continue
}
t := state.GetTransitions()[i]
_, ok := t.(*ActionTransition)
continueCollecting := collectPredicates && !ok
c := p.getEpsilonTarget(config, t, continueCollecting, depth == 0, fullCtx, treatEOFAsEpsilon)
if c != nil {
newDepth := depth
if _, ok := config.GetState().(*RuleStopState); ok {
// target fell off end of rule mark resulting c as having dipped into outer context
// We can't get here if incoming config was rule stop and we had context
// track how far we dip into outer context. Might
// come in handy and we avoid evaluating context dependent
// preds if this is > 0.
if p.dfa != nil && p.dfa.getPrecedenceDfa() {
if t.(*EpsilonTransition).outermostPrecedenceReturn == p.dfa.atnStartState.GetRuleIndex() {
c.setPrecedenceFilterSuppressed(true)
}
}
c.SetReachesIntoOuterContext(c.GetReachesIntoOuterContext() + 1)
_, present := closureBusy.Put(c)
if present {
// avoid infinite recursion for right-recursive rules
continue
}
configs.dipsIntoOuterContext = true // TODO: can remove? only care when we add to set per middle of this method
newDepth--
if runtimeConfig.parserATNSimulatorDebug {
fmt.Println("dips into outer ctx: " + c.String())
}
} else {
if !t.getIsEpsilon() {
_, present := closureBusy.Put(c)
if present {
// avoid infinite recursion for EOF* and EOF+
continue
}
}
if _, ok := t.(*RuleTransition); ok {
// latch when newDepth goes negative - once we step out of the entry context we can't return
if newDepth >= 0 {
newDepth++
}
}
}
p.closureCheckingStopState(c, configs, closureBusy, continueCollecting, fullCtx, newDepth, treatEOFAsEpsilon)
}
}
}
//goland:noinspection GoBoolExpressions
func (p *ParserATNSimulator) canDropLoopEntryEdgeInLeftRecursiveRule(config *ATNConfig) bool {
if !runtimeConfig.lRLoopEntryBranchOpt {
return false
}
_p := config.GetState()
// First check to see if we are in StarLoopEntryState generated during
// left-recursion elimination. For efficiency, also check if
// the context has an empty stack case. If so, it would mean
// global FOLLOW so we can't perform optimization
if _p.GetStateType() != ATNStateStarLoopEntry {
return false
}
startLoop, ok := _p.(*StarLoopEntryState)
if !ok {
return false
}
if !startLoop.precedenceRuleDecision ||
config.GetContext().isEmpty() ||
config.GetContext().hasEmptyPath() {
return false
}
// Require all return states to return back to the same rule
// that p is in.
numCtxs := config.GetContext().length()
for i := 0; i < numCtxs; i++ {
returnState := p.atn.states[config.GetContext().getReturnState(i)]
if returnState.GetRuleIndex() != _p.GetRuleIndex() {
return false
}
}
x := _p.GetTransitions()[0].getTarget()
decisionStartState := x.(BlockStartState)
blockEndStateNum := decisionStartState.getEndState().stateNumber
blockEndState := p.atn.states[blockEndStateNum].(*BlockEndState)
// Verify that the top of each stack context leads to loop entry/exit
// state through epsilon edges and w/o leaving rule.
for i := 0; i < numCtxs; i++ { // for each stack context
returnStateNumber := config.GetContext().getReturnState(i)
returnState := p.atn.states[returnStateNumber]
// all states must have single outgoing epsilon edge
if len(returnState.GetTransitions()) != 1 || !returnState.GetTransitions()[0].getIsEpsilon() {
return false
}
// Look for prefix op case like 'not expr', (' type ')' expr
returnStateTarget := returnState.GetTransitions()[0].getTarget()
if returnState.GetStateType() == ATNStateBlockEnd && returnStateTarget == _p {
continue
}
// Look for 'expr op expr' or case where expr's return state is block end
// of (...)* internal block; the block end points to loop back
// which points to p but we don't need to check that
if returnState == blockEndState {
continue
}
// Look for ternary expr ? expr : expr. The return state points at block end,
// which points at loop entry state
if returnStateTarget == blockEndState {
continue
}
// Look for complex prefix 'between expr and expr' case where 2nd expr's
// return state points at block end state of (...)* internal block
if returnStateTarget.GetStateType() == ATNStateBlockEnd &&
len(returnStateTarget.GetTransitions()) == 1 &&
returnStateTarget.GetTransitions()[0].getIsEpsilon() &&
returnStateTarget.GetTransitions()[0].getTarget() == _p {
continue
}
// anything else ain't conforming
return false
}
return true
}
func (p *ParserATNSimulator) getRuleName(index int) string {
if p.parser != nil && index >= 0 {
return p.parser.GetRuleNames()[index]
}
var sb strings.Builder
sb.Grow(32)
sb.WriteString("If {@code to} is {@code nil}, p method returns {@code nil}. // Otherwise, p method returns the {@link DFAState} returned by calling // {@link //addDFAState} for the {@code to} state.
// // @param dfa The DFA // @param from The source state for the edge // @param t The input symbol // @param to The target state for the edge // // @return If {@code to} is {@code nil}, p method returns {@code nil} // otherwise p method returns the result of calling {@link //addDFAState} // on {@code to} // //goland:noinspection GoBoolExpressions func (p *ParserATNSimulator) addDFAEdge(dfa *DFA, from *DFAState, t int, to *DFAState) *DFAState { if runtimeConfig.parserATNSimulatorDebug { fmt.Println("EDGE " + from.String() + " -> " + to.String() + " upon " + p.GetTokenName(t)) } if to == nil { return nil } p.atn.stateMu.Lock() to = p.addDFAState(dfa, to) // used existing if possible not incoming p.atn.stateMu.Unlock() if from == nil || t < -1 || t > p.atn.maxTokenType { return to } p.atn.edgeMu.Lock() if from.getEdges() == nil { from.setEdges(make([]*DFAState, p.atn.maxTokenType+1+1)) } from.setIthEdge(t+1, to) // connect p.atn.edgeMu.Unlock() if runtimeConfig.parserATNSimulatorDebug { var names []string if p.parser != nil { names = p.parser.GetLiteralNames() } fmt.Println("DFA=\n" + dfa.String(names, nil)) } return to } // addDFAState adds state D to the [DFA] if it is not already present, and returns // the actual instance stored in the [DFA]. If a state equivalent to D // is already in the [DFA], the existing state is returned. Otherwise, this // method returns D after adding it to the [DFA]. // // If D is [ATNSimulatorError], this method returns [ATNSimulatorError] and // does not change the DFA. // //goland:noinspection GoBoolExpressions func (p *ParserATNSimulator) addDFAState(dfa *DFA, d *DFAState) *DFAState { if d == ATNSimulatorError { return d } existing, present := dfa.Get(d) if present { if runtimeConfig.parserATNSimulatorTraceATNSim { fmt.Print("addDFAState " + d.String() + " exists") } return existing } // The state will be added if not already there or we will be given back the existing state struct // if it is present. // d.stateNumber = dfa.Len() if !d.configs.readOnly { d.configs.OptimizeConfigs(&p.BaseATNSimulator) d.configs.readOnly = true d.configs.configLookup = nil } dfa.Put(d) if runtimeConfig.parserATNSimulatorTraceATNSim { fmt.Println("addDFAState new " + d.String()) } return d } //goland:noinspection GoBoolExpressions func (p *ParserATNSimulator) ReportAttemptingFullContext(dfa *DFA, conflictingAlts *BitSet, configs *ATNConfigSet, startIndex, stopIndex int) { if runtimeConfig.parserATNSimulatorDebug || runtimeConfig.parserATNSimulatorRetryDebug { interval := NewInterval(startIndex, stopIndex+1) fmt.Println("ReportAttemptingFullContext decision=" + strconv.Itoa(dfa.decision) + ":" + configs.String() + ", input=" + p.parser.GetTokenStream().GetTextFromInterval(interval)) } if p.parser != nil { p.parser.GetErrorListenerDispatch().ReportAttemptingFullContext(p.parser, dfa, startIndex, stopIndex, conflictingAlts, configs) } } //goland:noinspection GoBoolExpressions func (p *ParserATNSimulator) ReportContextSensitivity(dfa *DFA, prediction int, configs *ATNConfigSet, startIndex, stopIndex int) { if runtimeConfig.parserATNSimulatorDebug || runtimeConfig.parserATNSimulatorRetryDebug { interval := NewInterval(startIndex, stopIndex+1) fmt.Println("ReportContextSensitivity decision=" + strconv.Itoa(dfa.decision) + ":" + configs.String() + ", input=" + p.parser.GetTokenStream().GetTextFromInterval(interval)) } if p.parser != nil { p.parser.GetErrorListenerDispatch().ReportContextSensitivity(p.parser, dfa, startIndex, stopIndex, prediction, configs) } } // ReportAmbiguity reports and ambiguity in the parse, which shows that the parser will explore a different route. // // If context-sensitive parsing, we know it's an ambiguity not a conflict or error, but we can report it to the developer // so that they can see that this is happening and can take action if they want to. // //goland:noinspection GoBoolExpressions func (p *ParserATNSimulator) ReportAmbiguity(dfa *DFA, _ *DFAState, startIndex, stopIndex int, exact bool, ambigAlts *BitSet, configs *ATNConfigSet) { if runtimeConfig.parserATNSimulatorDebug || runtimeConfig.parserATNSimulatorRetryDebug { interval := NewInterval(startIndex, stopIndex+1) fmt.Println("ReportAmbiguity " + ambigAlts.String() + ":" + configs.String() + ", input=" + p.parser.GetTokenStream().GetTextFromInterval(interval)) } if p.parser != nil { p.parser.GetErrorListenerDispatch().ReportAmbiguity(p.parser, dfa, startIndex, stopIndex, exact, ambigAlts, configs) } } antlr4-go-antlr-c821258/parser_rule_context.go 0000664 0000000 0000000 00000023430 14621155120 0021332 0 ustar 00root root 0000000 0000000 // Copyright (c) 2012-2022 The ANTLR Project. All rights reserved. // Use of this file is governed by the BSD 3-clause license that // can be found in the LICENSE.txt file in the project root. package antlr import ( "reflect" "strconv" ) type ParserRuleContext interface { RuleContext SetException(RecognitionException) AddTokenNode(token Token) *TerminalNodeImpl AddErrorNode(badToken Token) *ErrorNodeImpl EnterRule(listener ParseTreeListener) ExitRule(listener ParseTreeListener) SetStart(Token) GetStart() Token SetStop(Token) GetStop() Token AddChild(child RuleContext) RuleContext RemoveLastChild() } type BaseParserRuleContext struct { parentCtx RuleContext invokingState int RuleIndex int start, stop Token exception RecognitionException children []Tree } func NewBaseParserRuleContext(parent ParserRuleContext, invokingStateNumber int) *BaseParserRuleContext { prc := new(BaseParserRuleContext) InitBaseParserRuleContext(prc, parent, invokingStateNumber) return prc } func InitBaseParserRuleContext(prc *BaseParserRuleContext, parent ParserRuleContext, invokingStateNumber int) { // What context invoked b rule? prc.parentCtx = parent // What state invoked the rule associated with b context? // The "return address" is the followState of invokingState // If parent is nil, b should be -1. if parent == nil { prc.invokingState = -1 } else { prc.invokingState = invokingStateNumber } prc.RuleIndex = -1 // * If we are debugging or building a parse tree for a Visitor, // we need to track all of the tokens and rule invocations associated // with prc rule's context. This is empty for parsing w/o tree constr. // operation because we don't the need to track the details about // how we parse prc rule. // / prc.children = nil prc.start = nil prc.stop = nil // The exception that forced prc rule to return. If the rule successfully // completed, prc is {@code nil}. prc.exception = nil } func (prc *BaseParserRuleContext) SetException(e RecognitionException) { prc.exception = e } func (prc *BaseParserRuleContext) GetChildren() []Tree { return prc.children } func (prc *BaseParserRuleContext) CopyFrom(ctx *BaseParserRuleContext) { // from RuleContext prc.parentCtx = ctx.parentCtx prc.invokingState = ctx.invokingState prc.children = nil prc.start = ctx.start prc.stop = ctx.stop } func (prc *BaseParserRuleContext) GetText() string { if prc.GetChildCount() == 0 { return "" } var s string for _, child := range prc.children { s += child.(ParseTree).GetText() } return s } // EnterRule is called when any rule is entered. func (prc *BaseParserRuleContext) EnterRule(_ ParseTreeListener) { } // ExitRule is called when any rule is exited. func (prc *BaseParserRuleContext) ExitRule(_ ParseTreeListener) { } // * Does not set parent link other add methods do that func (prc *BaseParserRuleContext) addTerminalNodeChild(child TerminalNode) TerminalNode { if prc.children == nil { prc.children = make([]Tree, 0) } if child == nil { panic("Child may not be null") } prc.children = append(prc.children, child) return child } func (prc *BaseParserRuleContext) AddChild(child RuleContext) RuleContext { if prc.children == nil { prc.children = make([]Tree, 0) } if child == nil { panic("Child may not be null") } prc.children = append(prc.children, child) return child } // RemoveLastChild is used by [EnterOuterAlt] to toss out a [RuleContext] previously added as // we entered a rule. If we have a label, we will need to remove // the generic ruleContext object. func (prc *BaseParserRuleContext) RemoveLastChild() { if prc.children != nil && len(prc.children) > 0 { prc.children = prc.children[0 : len(prc.children)-1] } } func (prc *BaseParserRuleContext) AddTokenNode(token Token) *TerminalNodeImpl { node := NewTerminalNodeImpl(token) prc.addTerminalNodeChild(node) node.parentCtx = prc return node } func (prc *BaseParserRuleContext) AddErrorNode(badToken Token) *ErrorNodeImpl { node := NewErrorNodeImpl(badToken) prc.addTerminalNodeChild(node) node.parentCtx = prc return node } func (prc *BaseParserRuleContext) GetChild(i int) Tree { if prc.children != nil && len(prc.children) >= i { return prc.children[i] } return nil } func (prc *BaseParserRuleContext) GetChildOfType(i int, childType reflect.Type) RuleContext { if childType == nil { return prc.GetChild(i).(RuleContext) } for j := 0; j < len(prc.children); j++ { child := prc.children[j] if reflect.TypeOf(child) == childType { if i == 0 { return child.(RuleContext) } i-- } } return nil } func (prc *BaseParserRuleContext) ToStringTree(ruleNames []string, recog Recognizer) string { return TreesStringTree(prc, ruleNames, recog) } func (prc *BaseParserRuleContext) GetRuleContext() RuleContext { return prc } func (prc *BaseParserRuleContext) Accept(visitor ParseTreeVisitor) interface{} { return visitor.VisitChildren(prc) } func (prc *BaseParserRuleContext) SetStart(t Token) { prc.start = t } func (prc *BaseParserRuleContext) GetStart() Token { return prc.start } func (prc *BaseParserRuleContext) SetStop(t Token) { prc.stop = t } func (prc *BaseParserRuleContext) GetStop() Token { return prc.stop } func (prc *BaseParserRuleContext) GetToken(ttype int, i int) TerminalNode { for j := 0; j < len(prc.children); j++ { child := prc.children[j] if c2, ok := child.(TerminalNode); ok { if c2.GetSymbol().GetTokenType() == ttype { if i == 0 { return c2 } i-- } } } return nil } func (prc *BaseParserRuleContext) GetTokens(ttype int) []TerminalNode { if prc.children == nil { return make([]TerminalNode, 0) } tokens := make([]TerminalNode, 0) for j := 0; j < len(prc.children); j++ { child := prc.children[j] if tchild, ok := child.(TerminalNode); ok { if tchild.GetSymbol().GetTokenType() == ttype { tokens = append(tokens, tchild) } } } return tokens } func (prc *BaseParserRuleContext) GetPayload() interface{} { return prc } func (prc *BaseParserRuleContext) getChild(ctxType reflect.Type, i int) RuleContext { if prc.children == nil || i < 0 || i >= len(prc.children) { return nil } j := -1 // what element have we found with ctxType? for _, o := range prc.children { childType := reflect.TypeOf(o) if childType.Implements(ctxType) { j++ if j == i { return o.(RuleContext) } } } return nil } // Go lacks generics, so it's not possible for us to return the child with the correct type, but we do // check for convertibility func (prc *BaseParserRuleContext) GetTypedRuleContext(ctxType reflect.Type, i int) RuleContext { return prc.getChild(ctxType, i) } func (prc *BaseParserRuleContext) GetTypedRuleContexts(ctxType reflect.Type) []RuleContext { if prc.children == nil { return make([]RuleContext, 0) } contexts := make([]RuleContext, 0) for _, child := range prc.children { childType := reflect.TypeOf(child) if childType.ConvertibleTo(ctxType) { contexts = append(contexts, child.(RuleContext)) } } return contexts } func (prc *BaseParserRuleContext) GetChildCount() int { if prc.children == nil { return 0 } return len(prc.children) } func (prc *BaseParserRuleContext) GetSourceInterval() Interval { if prc.start == nil || prc.stop == nil { return TreeInvalidInterval } return NewInterval(prc.start.GetTokenIndex(), prc.stop.GetTokenIndex()) } //need to manage circular dependencies, so export now // Print out a whole tree, not just a node, in LISP format // (root child1 .. childN). Print just a node if b is a leaf. // func (prc *BaseParserRuleContext) String(ruleNames []string, stop RuleContext) string { var p ParserRuleContext = prc s := "[" for p != nil && p != stop { if ruleNames == nil { if !p.IsEmpty() { s += strconv.Itoa(p.GetInvokingState()) } } else { ri := p.GetRuleIndex() var ruleName string if ri >= 0 && ri < len(ruleNames) { ruleName = ruleNames[ri] } else { ruleName = strconv.Itoa(ri) } s += ruleName } if p.GetParent() != nil && (ruleNames != nil || !p.GetParent().(ParserRuleContext).IsEmpty()) { s += " " } pi := p.GetParent() if pi != nil { p = pi.(ParserRuleContext) } else { p = nil } } s += "]" return s } func (prc *BaseParserRuleContext) SetParent(v Tree) { if v == nil { prc.parentCtx = nil } else { prc.parentCtx = v.(RuleContext) } } func (prc *BaseParserRuleContext) GetInvokingState() int { return prc.invokingState } func (prc *BaseParserRuleContext) SetInvokingState(t int) { prc.invokingState = t } func (prc *BaseParserRuleContext) GetRuleIndex() int { return prc.RuleIndex } func (prc *BaseParserRuleContext) GetAltNumber() int { return ATNInvalidAltNumber } func (prc *BaseParserRuleContext) SetAltNumber(_ int) {} // IsEmpty returns true if the context of b is empty. // // A context is empty if there is no invoking state, meaning nobody calls // current context. func (prc *BaseParserRuleContext) IsEmpty() bool { return prc.invokingState == -1 } // GetParent returns the combined text of all child nodes. This method only considers // tokens which have been added to the parse tree. // // Since tokens on hidden channels (e.g. whitespace or comments) are not // added to the parse trees, they will not appear in the output of this // method. func (prc *BaseParserRuleContext) GetParent() Tree { return prc.parentCtx } var ParserRuleContextEmpty = NewBaseParserRuleContext(nil, -1) type InterpreterRuleContext interface { ParserRuleContext } type BaseInterpreterRuleContext struct { *BaseParserRuleContext } //goland:noinspection GoUnusedExportedFunction func NewBaseInterpreterRuleContext(parent BaseInterpreterRuleContext, invokingStateNumber, ruleIndex int) *BaseInterpreterRuleContext { prc := new(BaseInterpreterRuleContext) prc.BaseParserRuleContext = NewBaseParserRuleContext(parent, invokingStateNumber) prc.RuleIndex = ruleIndex return prc } antlr4-go-antlr-c821258/prediction_context.go 0000664 0000000 0000000 00000052601 14621155120 0021151 0 ustar 00root root 0000000 0000000 // Copyright (c) 2012-2022 The ANTLR Project. All rights reserved. // Use of this file is governed by the BSD 3-clause license that // can be found in the LICENSE.txt file in the project root. package antlr import ( "fmt" "strconv" ) var _emptyPredictionContextHash int func init() { _emptyPredictionContextHash = murmurInit(1) _emptyPredictionContextHash = murmurFinish(_emptyPredictionContextHash, 0) } func calculateEmptyHash() int { return _emptyPredictionContextHash } const ( // BasePredictionContextEmptyReturnState represents {@code $} in an array in full context mode, $ // doesn't mean wildcard: // // $ + x = [$,x] // // Here, // // $ = EmptyReturnState BasePredictionContextEmptyReturnState = 0x7FFFFFFF ) // TODO: JI These are meant to be atomics - this does not seem to match the Java runtime here // //goland:noinspection GoUnusedGlobalVariable var ( BasePredictionContextglobalNodeCount = 1 BasePredictionContextid = BasePredictionContextglobalNodeCount ) const ( PredictionContextEmpty = iota PredictionContextSingleton PredictionContextArray ) // PredictionContext is a go idiomatic implementation of PredictionContext that does not rty to // emulate inheritance from Java, and can be used without an interface definition. An interface // is not required because no user code will ever need to implement this interface. type PredictionContext struct { cachedHash int pcType int parentCtx *PredictionContext returnState int parents []*PredictionContext returnStates []int } func NewEmptyPredictionContext() *PredictionContext { nep := &PredictionContext{} nep.cachedHash = calculateEmptyHash() nep.pcType = PredictionContextEmpty nep.returnState = BasePredictionContextEmptyReturnState return nep } func NewBaseSingletonPredictionContext(parent *PredictionContext, returnState int) *PredictionContext { pc := &PredictionContext{} pc.pcType = PredictionContextSingleton pc.returnState = returnState pc.parentCtx = parent if parent != nil { pc.cachedHash = calculateHash(parent, returnState) } else { pc.cachedHash = calculateEmptyHash() } return pc } func SingletonBasePredictionContextCreate(parent *PredictionContext, returnState int) *PredictionContext { if returnState == BasePredictionContextEmptyReturnState && parent == nil { // someone can pass in the bits of an array ctx that mean $ return BasePredictionContextEMPTY } return NewBaseSingletonPredictionContext(parent, returnState) } func NewArrayPredictionContext(parents []*PredictionContext, returnStates []int) *PredictionContext { // Parent can be nil only if full ctx mode and we make an array // from {@link //EMPTY} and non-empty. We merge {@link //EMPTY} by using // nil parent and // returnState == {@link //EmptyReturnState}. hash := murmurInit(1) for _, parent := range parents { hash = murmurUpdate(hash, parent.Hash()) } for _, returnState := range returnStates { hash = murmurUpdate(hash, returnState) } hash = murmurFinish(hash, len(parents)<<1) nec := &PredictionContext{} nec.cachedHash = hash nec.pcType = PredictionContextArray nec.parents = parents nec.returnStates = returnStates return nec } func (p *PredictionContext) Hash() int { return p.cachedHash } func (p *PredictionContext) Equals(other Collectable[*PredictionContext]) bool { if p == other { return true } switch p.pcType { case PredictionContextEmpty: otherP := other.(*PredictionContext) return other == nil || otherP == nil || otherP.isEmpty() case PredictionContextSingleton: return p.SingletonEquals(other) case PredictionContextArray: return p.ArrayEquals(other) } return false } func (p *PredictionContext) ArrayEquals(o Collectable[*PredictionContext]) bool { if o == nil { return false } other := o.(*PredictionContext) if other == nil || other.pcType != PredictionContextArray { return false } if p.cachedHash != other.Hash() { return false // can't be same if hash is different } // Must compare the actual array elements and not just the array address // return intSlicesEqual(p.returnStates, other.returnStates) && pcSliceEqual(p.parents, other.parents) } func (p *PredictionContext) SingletonEquals(other Collectable[*PredictionContext]) bool { if other == nil { return false } otherP := other.(*PredictionContext) if otherP == nil || otherP.pcType != PredictionContextSingleton { return false } if p.cachedHash != otherP.Hash() { return false // Can't be same if hash is different } if p.returnState != otherP.getReturnState(0) { return false } // Both parents must be nil if one is if p.parentCtx == nil { return otherP.parentCtx == nil } return p.parentCtx.Equals(otherP.parentCtx) } func (p *PredictionContext) GetParent(i int) *PredictionContext { switch p.pcType { case PredictionContextEmpty: return nil case PredictionContextSingleton: return p.parentCtx case PredictionContextArray: return p.parents[i] } return nil } func (p *PredictionContext) getReturnState(i int) int { switch p.pcType { case PredictionContextArray: return p.returnStates[i] default: return p.returnState } } func (p *PredictionContext) GetReturnStates() []int { switch p.pcType { case PredictionContextArray: return p.returnStates default: return []int{p.returnState} } } func (p *PredictionContext) length() int { switch p.pcType { case PredictionContextArray: return len(p.returnStates) default: return 1 } } func (p *PredictionContext) hasEmptyPath() bool { switch p.pcType { case PredictionContextSingleton: return p.returnState == BasePredictionContextEmptyReturnState } return p.getReturnState(p.length()-1) == BasePredictionContextEmptyReturnState } func (p *PredictionContext) String() string { switch p.pcType { case PredictionContextEmpty: return "$" case PredictionContextSingleton: var up string if p.parentCtx == nil { up = "" } else { up = p.parentCtx.String() } if len(up) == 0 { if p.returnState == BasePredictionContextEmptyReturnState { return "$" } return strconv.Itoa(p.returnState) } return strconv.Itoa(p.returnState) + " " + up case PredictionContextArray: if p.isEmpty() { return "[]" } s := "[" for i := 0; i < len(p.returnStates); i++ { if i > 0 { s = s + ", " } if p.returnStates[i] == BasePredictionContextEmptyReturnState { s = s + "$" continue } s = s + strconv.Itoa(p.returnStates[i]) if !p.parents[i].isEmpty() { s = s + " " + p.parents[i].String() } else { s = s + "nil" } } return s + "]" default: return "unknown" } } func (p *PredictionContext) isEmpty() bool { switch p.pcType { case PredictionContextEmpty: return true case PredictionContextArray: // since EmptyReturnState can only appear in the last position, we // don't need to verify that size==1 return p.returnStates[0] == BasePredictionContextEmptyReturnState default: return false } } func (p *PredictionContext) Type() int { return p.pcType } func calculateHash(parent *PredictionContext, returnState int) int { h := murmurInit(1) h = murmurUpdate(h, parent.Hash()) h = murmurUpdate(h, returnState) return murmurFinish(h, 2) } // Convert a {@link RuleContext} tree to a {@link BasePredictionContext} graph. // Return {@link //EMPTY} if {@code outerContext} is empty or nil. // / func predictionContextFromRuleContext(a *ATN, outerContext RuleContext) *PredictionContext { if outerContext == nil { outerContext = ParserRuleContextEmpty } // if we are in RuleContext of start rule, s, then BasePredictionContext // is EMPTY. Nobody called us. (if we are empty, return empty) if outerContext.GetParent() == nil || outerContext == ParserRuleContextEmpty { return BasePredictionContextEMPTY } // If we have a parent, convert it to a BasePredictionContext graph parent := predictionContextFromRuleContext(a, outerContext.GetParent().(RuleContext)) state := a.states[outerContext.GetInvokingState()] transition := state.GetTransitions()[0] return SingletonBasePredictionContextCreate(parent, transition.(*RuleTransition).followState.GetStateNumber()) } func merge(a, b *PredictionContext, rootIsWildcard bool, mergeCache *JPCMap) *PredictionContext { // Share same graph if both same // if a == b || a.Equals(b) { return a } if a.pcType == PredictionContextSingleton && b.pcType == PredictionContextSingleton { return mergeSingletons(a, b, rootIsWildcard, mergeCache) } // At least one of a or b is array // If one is $ and rootIsWildcard, return $ as wildcard if rootIsWildcard { if a.isEmpty() { return a } if b.isEmpty() { return b } } // Convert either Singleton or Empty to arrays, so that we can merge them // ara := convertToArray(a) arb := convertToArray(b) return mergeArrays(ara, arb, rootIsWildcard, mergeCache) } func convertToArray(pc *PredictionContext) *PredictionContext { switch pc.Type() { case PredictionContextEmpty: return NewArrayPredictionContext([]*PredictionContext{}, []int{}) case PredictionContextSingleton: return NewArrayPredictionContext([]*PredictionContext{pc.GetParent(0)}, []int{pc.getReturnState(0)}) default: // Already an array } return pc } // mergeSingletons merges two Singleton [PredictionContext] instances. // // Stack tops equal, parents merge is same return left graph. // // //Same stack top, parents differ merge parents giving array node, then
// remainders of those graphs. A new root node is created to point to the
// merged parents.
//
Different stack tops pointing to same parent. Make array node for the
// root where both element in the root point to the same (original)
// parent.
//
Different stack tops pointing to different parents. Make array node for
// the root where each element points to the corresponding original
// parent.
//
These local-context merge operations are used when {@code rootIsWildcard} // is true.
// //{@link //EMPTY} is superset of any graph return {@link //EMPTY}.
//
{@link //EMPTY} and anything is {@code //EMPTY}, so merged parent is
// {@code //EMPTY} return left graph.
//
Special case of last merge if local context.
//
These full-context merge operations are used when {@code rootIsWildcard} // is false.
// // // //Must keep all contexts {@link //EMPTY} in array is a special value (and
// nil parent).
//
Different tops, different parents.
//
Shared top, same parents.
//
Shared top, different parents.
//
Shared top, all shared parents.
//
Equal tops, merge parents and reduce top to
// {@link SingletonBasePredictionContext}.
//
// The evaluation of predicates by a context is short-circuiting, but // unordered.
func (a *AND) evaluate(parser Recognizer, outerContext RuleContext) bool { for i := 0; i < len(a.opnds); i++ { if !a.opnds[i].evaluate(parser, outerContext) { return false } } return true } func (a *AND) evalPrecedence(parser Recognizer, outerContext RuleContext) SemanticContext { differs := false operands := make([]SemanticContext, 0) for i := 0; i < len(a.opnds); i++ { context := a.opnds[i] evaluated := context.evalPrecedence(parser, outerContext) differs = differs || (evaluated != context) if evaluated == nil { // The AND context is false if any element is false return nil } else if evaluated != SemanticContextNone { // Reduce the result by Skipping true elements operands = append(operands, evaluated) } } if !differs { return a } if len(operands) == 0 { // all elements were true, so the AND context is true return SemanticContextNone } var result SemanticContext for _, o := range operands { if result == nil { result = o } else { result = SemanticContextandContext(result, o) } } return result } func (a *AND) Hash() int { h := murmurInit(37) // Init with a value different from OR for _, op := range a.opnds { h = murmurUpdate(h, op.Hash()) } return murmurFinish(h, len(a.opnds)) } func (o *OR) Hash() int { h := murmurInit(41) // Init with o value different from AND for _, op := range o.opnds { h = murmurUpdate(h, op.Hash()) } return murmurFinish(h, len(o.opnds)) } func (a *AND) String() string { s := "" for _, o := range a.opnds { s += "&& " + fmt.Sprint(o) } if len(s) > 3 { return s[0:3] } return s } // // A semantic context which is true whenever at least one of the contained // contexts is true. // type OR struct { opnds []SemanticContext } func NewOR(a, b SemanticContext) *OR { operands := NewJStore[SemanticContext, Comparator[SemanticContext]](semctxEqInst, SemanticContextCollection, "NewOR() operands") if aa, ok := a.(*OR); ok { for _, o := range aa.opnds { operands.Put(o) } } else { operands.Put(a) } if ba, ok := b.(*OR); ok { for _, o := range ba.opnds { operands.Put(o) } } else { operands.Put(b) } precedencePredicates := PrecedencePredicatefilterPrecedencePredicates(operands) if len(precedencePredicates) > 0 { // interested in the transition with the lowest precedence var reduced *PrecedencePredicate for _, p := range precedencePredicates { if reduced == nil || p.precedence > reduced.precedence { reduced = p } } operands.Put(reduced) } vs := operands.Values() opnds := make([]SemanticContext, len(vs)) copy(opnds, vs) o := new(OR) o.opnds = opnds return o } func (o *OR) Equals(other Collectable[SemanticContext]) bool { if o == other { return true } else if _, ok := other.(*OR); !ok { return false } else { for i, v := range other.(*OR).opnds { if !o.opnds[i].Equals(v) { return false } } return true } } //// The evaluation of predicates by o context is short-circuiting, but // unordered.
func (o *OR) evaluate(parser Recognizer, outerContext RuleContext) bool { for i := 0; i < len(o.opnds); i++ { if o.opnds[i].evaluate(parser, outerContext) { return true } } return false } func (o *OR) evalPrecedence(parser Recognizer, outerContext RuleContext) SemanticContext { differs := false operands := make([]SemanticContext, 0) for i := 0; i < len(o.opnds); i++ { context := o.opnds[i] evaluated := context.evalPrecedence(parser, outerContext) differs = differs || (evaluated != context) if evaluated == SemanticContextNone { // The OR context is true if any element is true return SemanticContextNone } else if evaluated != nil { // Reduce the result by Skipping false elements operands = append(operands, evaluated) } } if !differs { return o } if len(operands) == 0 { // all elements were false, so the OR context is false return nil } var result SemanticContext for _, o := range operands { if result == nil { result = o } else { result = SemanticContextorContext(result, o) } } return result } func (o *OR) String() string { s := "" for _, o := range o.opnds { s += "|| " + fmt.Sprint(o) } if len(s) > 3 { return s[0:3] } return s } antlr4-go-antlr-c821258/statistics.go 0000664 0000000 0000000 00000023512 14621155120 0017436 0 ustar 00root root 0000000 0000000 //go:build antlr.stats package antlr import ( "fmt" "log" "os" "path/filepath" "sort" "strconv" ) // This file allows the user to collect statistics about the runtime of the ANTLR runtime. It is not enabled by default // and so incurs no time penalty. To enable it, you must build the runtime with the antlr.stats build tag. // // Tells various components to collect statistics - because it is only true when this file is included, it will // allow the compiler to completely eliminate all the code that is only used when collecting statistics. const collectStats = true // goRunStats is a collection of all the various data the ANTLR runtime has collected about a particular run. // It is exported so that it can be used by others to look for things that are not already looked for in the // runtime statistics. type goRunStats struct { // jStats is a slice of all the [JStatRec] records that have been created, which is one for EVERY collection created // during a run. It is exported so that it can be used by others to look for things that are not already looked for // within this package. // jStats []*JStatRec jStatsLock RWMutex topN int topNByMax []*JStatRec topNByUsed []*JStatRec unusedCollections map[CollectionSource]int counts map[CollectionSource]int } const ( collectionsFile = "collections" ) var ( Statistics = &goRunStats{ topN: 10, } ) type statsOption func(*goRunStats) error // Configure allows the statistics system to be configured as the user wants and override the defaults func (s *goRunStats) Configure(options ...statsOption) error { for _, option := range options { err := option(s) if err != nil { return err } } return nil } // WithTopN sets the number of things to list in the report when we are concerned with the top N things. // // For example, if you want to see the top 20 collections by size, you can do: // // antlr.Statistics.Configure(antlr.WithTopN(20)) func WithTopN(topN int) statsOption { return func(s *goRunStats) error { s.topN = topN return nil } } // Analyze looks through all the statistical records and computes all the outputs that might be useful to the user. // // The function gathers and analyzes a number of statistics about any particular run of // an ANTLR generated recognizer. In the vast majority of cases, the statistics are only // useful to maintainers of ANTLR itself, but they can be useful to users as well. They may be // especially useful in tracking down bugs or performance problems when an ANTLR user could // supply the output from this package, but cannot supply the grammar file(s) they are using, even // privately to the maintainers. // // The statistics are gathered by the runtime itself, and are not gathered by the parser or lexer, but the user // must call this function their selves to analyze the statistics. This is because none of the infrastructure is // extant unless the calling program is built with the antlr.stats tag like so: // // go build -tags antlr.stats . // // When a program is built with the antlr.stats tag, the Statistics object is created and available outside // the package. The user can then call the [Statistics.Analyze] function to analyze the statistics and then call the // [Statistics.Report] function to report the statistics. // // Please forward any questions about this package to the ANTLR discussion groups on GitHub or send to them to // me [Jim Idle] directly at jimi@idle.ws // // [Jim Idle]: https:://github.com/jim-idle func (s *goRunStats) Analyze() { // Look for anything that looks strange and record it in our local maps etc for the report to present it // s.CollectionAnomalies() s.TopNCollections() } // TopNCollections looks through all the statistical records and gathers the top ten collections by size. func (s *goRunStats) TopNCollections() { // Let's sort the stat records by MaxSize // sort.Slice(s.jStats, func(i, j int) bool { return s.jStats[i].MaxSize > s.jStats[j].MaxSize }) for i := 0; i < len(s.jStats) && i < s.topN; i++ { s.topNByMax = append(s.topNByMax, s.jStats[i]) } // Sort by the number of times used // sort.Slice(s.jStats, func(i, j int) bool { return s.jStats[i].Gets+s.jStats[i].Puts > s.jStats[j].Gets+s.jStats[j].Puts }) for i := 0; i < len(s.jStats) && i < s.topN; i++ { s.topNByUsed = append(s.topNByUsed, s.jStats[i]) } } // Report dumps a markdown formatted report of all the statistics collected during a run to the given dir output // path, which should represent a directory. Generated files will be prefixed with the given prefix and will be // given a type name such as `anomalies` and a time stamp such as `2021-09-01T12:34:56` and a .md suffix. func (s *goRunStats) Report(dir string, prefix string) error { isDir, err := isDirectory(dir) switch { case err != nil: return err case !isDir: return fmt.Errorf("output directory `%s` is not a directory", dir) } s.reportCollections(dir, prefix) // Clean out any old data in case the user forgets // s.Reset() return nil } func (s *goRunStats) Reset() { s.jStats = nil s.topNByUsed = nil s.topNByMax = nil } func (s *goRunStats) reportCollections(dir, prefix string) { cname := filepath.Join(dir, ".asciidoctor") // If the file doesn't exist, create it, or append to the file f, err := os.OpenFile(cname, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644) if err != nil { log.Fatal(err) } _, _ = f.WriteString(`// .asciidoctorconfig ++++ ++++`) _ = f.Close() fname := filepath.Join(dir, prefix+"_"+"_"+collectionsFile+"_"+".adoc") // If the file doesn't exist, create it, or append to the file f, err = os.OpenFile(fname, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644) if err != nil { log.Fatal(err) } defer func(f *os.File) { err := f.Close() if err != nil { log.Fatal(err) } }(f) _, _ = f.WriteString("= Collections for " + prefix + "\n\n") _, _ = f.WriteString("== Summary\n") if s.unusedCollections != nil { _, _ = f.WriteString("=== Unused Collections\n") _, _ = f.WriteString("Unused collections incur a penalty for allocation that makes them a candidate for either\n") _, _ = f.WriteString(" removal or optimization. If you are using a collection that is not used, you should\n") _, _ = f.WriteString(" consider removing it. If you are using a collection that is used, but not very often,\n") _, _ = f.WriteString(" you should consider using lazy initialization to defer the allocation until it is\n") _, _ = f.WriteString(" actually needed.\n\n") _, _ = f.WriteString("\n.Unused collections\n") _, _ = f.WriteString(`[cols="<3,>1"]` + "\n\n") _, _ = f.WriteString("|===\n") _, _ = f.WriteString("| Type | Count\n") for k, v := range s.unusedCollections { _, _ = f.WriteString("| " + CollectionDescriptors[k].SybolicName + " | " + strconv.Itoa(v) + "\n") } f.WriteString("|===\n\n") } _, _ = f.WriteString("\n.Summary of Collections\n") _, _ = f.WriteString(`[cols="<3,>1"]` + "\n\n") _, _ = f.WriteString("|===\n") _, _ = f.WriteString("| Type | Count\n") for k, v := range s.counts { _, _ = f.WriteString("| " + CollectionDescriptors[k].SybolicName + " | " + strconv.Itoa(v) + "\n") } _, _ = f.WriteString("| Total | " + strconv.Itoa(len(s.jStats)) + "\n") _, _ = f.WriteString("|===\n\n") _, _ = f.WriteString("\n.Summary of Top " + strconv.Itoa(s.topN) + " Collections by MaxSize\n") _, _ = f.WriteString(`[cols="<1,<3,>1,>1,>1,>1"]` + "\n\n") _, _ = f.WriteString("|===\n") _, _ = f.WriteString("| Source | Description | MaxSize | EndSize | Puts | Gets\n") for _, c := range s.topNByMax { _, _ = f.WriteString("| " + CollectionDescriptors[c.Source].SybolicName + "\n") _, _ = f.WriteString("| " + c.Description + "\n") _, _ = f.WriteString("| " + strconv.Itoa(c.MaxSize) + "\n") _, _ = f.WriteString("| " + strconv.Itoa(c.CurSize) + "\n") _, _ = f.WriteString("| " + strconv.Itoa(c.Puts) + "\n") _, _ = f.WriteString("| " + strconv.Itoa(c.Gets) + "\n") _, _ = f.WriteString("\n") } _, _ = f.WriteString("|===\n\n") _, _ = f.WriteString("\n.Summary of Top " + strconv.Itoa(s.topN) + " Collections by Access\n") _, _ = f.WriteString(`[cols="<1,<3,>1,>1,>1,>1,>1"]` + "\n\n") _, _ = f.WriteString("|===\n") _, _ = f.WriteString("| Source | Description | MaxSize | EndSize | Puts | Gets | P+G\n") for _, c := range s.topNByUsed { _, _ = f.WriteString("| " + CollectionDescriptors[c.Source].SybolicName + "\n") _, _ = f.WriteString("| " + c.Description + "\n") _, _ = f.WriteString("| " + strconv.Itoa(c.MaxSize) + "\n") _, _ = f.WriteString("| " + strconv.Itoa(c.CurSize) + "\n") _, _ = f.WriteString("| " + strconv.Itoa(c.Puts) + "\n") _, _ = f.WriteString("| " + strconv.Itoa(c.Gets) + "\n") _, _ = f.WriteString("| " + strconv.Itoa(c.Gets+c.Puts) + "\n") _, _ = f.WriteString("\n") } _, _ = f.WriteString("|===\n\n") } // AddJStatRec adds a [JStatRec] record to the [goRunStats] collection when build runtimeConfig antlr.stats is enabled. func (s *goRunStats) AddJStatRec(rec *JStatRec) { s.jStatsLock.Lock() defer s.jStatsLock.Unlock() s.jStats = append(s.jStats, rec) } // CollectionAnomalies looks through all the statistical records and gathers any anomalies that have been found. func (s *goRunStats) CollectionAnomalies() { s.jStatsLock.RLock() defer s.jStatsLock.RUnlock() s.counts = make(map[CollectionSource]int, len(s.jStats)) for _, c := range s.jStats { // Accumlate raw counts // s.counts[c.Source]++ // Look for allocated but unused collections and count them if c.MaxSize == 0 && c.Puts == 0 { if s.unusedCollections == nil { s.unusedCollections = make(map[CollectionSource]int) } s.unusedCollections[c.Source]++ } if c.MaxSize > 6000 { fmt.Println("Collection ", c.Description, "accumulated a max size of ", c.MaxSize, " - this is probably too large and indicates a poorly formed grammar") } } } antlr4-go-antlr-c821258/stats_data.go 0000664 0000000 0000000 00000001452 14621155120 0017372 0 ustar 00root root 0000000 0000000 package antlr // A JStatRec is a record of a particular use of a [JStore], [JMap] or JPCMap] collection. Typically, it will be // used to look for unused collections that wre allocated anyway, problems with hash bucket clashes, and anomalies // such as huge numbers of Gets with no entries found GetNoEnt. You can refer to the CollectionAnomalies() function // for ideas on what can be gleaned from these statistics about collections. type JStatRec struct { Source CollectionSource MaxSize int CurSize int Gets int GetHits int GetMisses int GetHashConflicts int GetNoEnt int Puts int PutHits int PutMisses int PutHashConflicts int MaxSlotSize int Description string CreateStack []byte } antlr4-go-antlr-c821258/token.go 0000664 0000000 0000000 00000012415 14621155120 0016364 0 ustar 00root root 0000000 0000000 // Copyright (c) 2012-2022 The ANTLR Project. All rights reserved. // Use of this file is governed by the BSD 3-clause license that // can be found in the LICENSE.txt file in the project root. package antlr import ( "strconv" "strings" ) type TokenSourceCharStreamPair struct { tokenSource TokenSource charStream CharStream } // A token has properties: text, type, line, character position in the line // (so we can ignore tabs), token channel, index, and source from which // we obtained this token. type Token interface { GetSource() *TokenSourceCharStreamPair GetTokenType() int GetChannel() int GetStart() int GetStop() int GetLine() int GetColumn() int GetText() string SetText(s string) GetTokenIndex() int SetTokenIndex(v int) GetTokenSource() TokenSource GetInputStream() CharStream String() string } type BaseToken struct { source *TokenSourceCharStreamPair tokenType int // token type of the token channel int // The parser ignores everything not on DEFAULT_CHANNEL start int // optional return -1 if not implemented. stop int // optional return -1 if not implemented. tokenIndex int // from 0..n-1 of the token object in the input stream line int // line=1..n of the 1st character column int // beginning of the line at which it occurs, 0..n-1 text string // text of the token. readOnly bool } const ( TokenInvalidType = 0 // TokenEpsilon - during lookahead operations, this "token" signifies we hit the rule end [ATN] state // and did not follow it despite needing to. TokenEpsilon = -2 TokenMinUserTokenType = 1 TokenEOF = -1 // TokenDefaultChannel is the default channel upon which tokens are sent to the parser. // // All tokens go to the parser (unless [Skip] is called in the lexer rule) // on a particular "channel". The parser tunes to a particular channel // so that whitespace etc... can go to the parser on a "hidden" channel. TokenDefaultChannel = 0 // TokenHiddenChannel defines the normal hidden channel - the parser wil not see tokens that are not on [TokenDefaultChannel]. // // Anything on a different channel than TokenDefaultChannel is not parsed by parser. TokenHiddenChannel = 1 ) func (b *BaseToken) GetChannel() int { return b.channel } func (b *BaseToken) GetStart() int { return b.start } func (b *BaseToken) GetStop() int { return b.stop } func (b *BaseToken) GetLine() int { return b.line } func (b *BaseToken) GetColumn() int { return b.column } func (b *BaseToken) GetTokenType() int { return b.tokenType } func (b *BaseToken) GetSource() *TokenSourceCharStreamPair { return b.source } func (b *BaseToken) GetText() string { if b.text != "" { return b.text } input := b.GetInputStream() if input == nil { return "" } n := input.Size() if b.GetStart() < n && b.GetStop() < n { return input.GetTextFromInterval(NewInterval(b.GetStart(), b.GetStop())) } return "// If {@code oldToken} is also a {@link CommonToken} instance, the newly // constructed token will share a reference to the {@link //text} field and // the {@link Pair} stored in {@link //source}. Otherwise, {@link //text} will // be assigned the result of calling {@link //GetText}, and {@link //source} // will be constructed from the result of {@link Token//GetTokenSource} and // {@link Token//GetInputStream}.
// // @param oldToken The token to copy. func (c *CommonToken) clone() *CommonToken { t := NewCommonToken(c.source, c.tokenType, c.channel, c.start, c.stop) t.tokenIndex = c.GetTokenIndex() t.line = c.GetLine() t.column = c.GetColumn() t.text = c.GetText() return t } antlr4-go-antlr-c821258/token_source.go 0000664 0000000 0000000 00000000673 14621155120 0017747 0 ustar 00root root 0000000 0000000 // Copyright (c) 2012-2022 The ANTLR Project. All rights reserved. // Use of this file is governed by the BSD 3-clause license that // can be found in the LICENSE.txt file in the project root. package antlr type TokenSource interface { NextToken() Token Skip() More() GetLine() int GetCharPositionInLine() int GetInputStream() CharStream GetSourceName() string setTokenFactory(factory TokenFactory) GetTokenFactory() TokenFactory } antlr4-go-antlr-c821258/token_stream.go 0000664 0000000 0000000 00000000770 14621155120 0017740 0 ustar 00root root 0000000 0000000 // Copyright (c) 2012-2022 The ANTLR Project. All rights reserved. // Use of this file is governed by the BSD 3-clause license that // can be found in the LICENSE.txt file in the project root. package antlr type TokenStream interface { IntStream LT(k int) Token Reset() Get(index int) Token GetTokenSource() TokenSource SetTokenSource(TokenSource) GetAllText() string GetTextFromInterval(Interval) string GetTextFromRuleContext(RuleContext) string GetTextFromTokens(Token, Token) string } antlr4-go-antlr-c821258/tokenstream_rewriter.go 0000664 0000000 0000000 00000047423 14621155120 0021532 0 ustar 00root root 0000000 0000000 // Copyright (c) 2012-2022 The ANTLR Project. All rights reserved. // Use of this file is governed by the BSD 3-clause license that // can be found in the LICENSE.txt file in the project root. package antlr import ( "bytes" "fmt" ) // // Useful for rewriting out a buffered input token stream after doing some // augmentation or other manipulations on it. //// You can insert stuff, replace, and delete chunks. Note that the operations // are done lazily--only if you convert the buffer to a {@link String} with // {@link TokenStream#getText()}. This is very efficient because you are not // moving data around all the time. As the buffer of tokens is converted to // strings, the {@link #getText()} method(s) scan the input token stream and // check to see if there is an operation at the current index. If so, the // operation is done and then normal {@link String} rendering continues on the // buffer. This is like having multiple Turing machine instruction streams // (programs) operating on a single input tape. :)
//// This rewriter makes no modifications to the token stream. It does not ask the // stream to fill itself up nor does it advance the input cursor. The token // stream {@link TokenStream#index()} will return the same value before and // after any {@link #getText()} call.
//// The rewriter only works on tokens that you have in the buffer and ignores the // current input cursor. If you are buffering tokens on-demand, calling // {@link #getText()} halfway through the input will only do rewrites for those // tokens in the first half of the file.
//// Since the operations are done lazily at {@link #getText}-time, operations do // not screw up the token index values. That is, an insert operation at token // index {@code i} does not change the index values for tokens // {@code i}+1..n-1.
//// Because operations never actually alter the buffer, you may always get the // original token stream back without undoing anything. Since the instructions // are queued up, you can easily simulate transactions and roll back any changes // if there is an error just by removing instructions. For example,
//
// CharStream input = new ANTLRFileStream("input");
// TLexer lex = new TLexer(input);
// CommonTokenStream tokens = new CommonTokenStream(lex);
// T parser = new T(tokens);
// TokenStreamRewriter rewriter = new TokenStreamRewriter(tokens);
// parser.startRule();
//
// // Then in the rules, you can execute (assuming rewriter is visible):
//// Token t,u; // ... // rewriter.insertAfter(t, "text to put after t");} // rewriter.insertAfter(u, "text after u");} // System.out.println(rewriter.getText()); ////
// You can also have multiple "instruction streams" and get multiple rewrites // from a single pass over the input. Just name the instruction streams and use // that name again when printing the buffer. This could be useful for generating // a C file and also its header file--all from the same buffer:
//
// rewriter.insertAfter("pass1", t, "text to put after t");}
// rewriter.insertAfter("pass2", u, "text after u");}
// System.out.println(rewriter.getText("pass1"));
// System.out.println(rewriter.getText("pass2"));
//
// // If you don't use named rewrite streams, a "default" stream is used as the // first example shows.
const ( DefaultProgramName = "default" ProgramInitSize = 100 MinTokenIndex = 0 ) // Define the rewrite operation hierarchy type RewriteOperation interface { // Execute the rewrite operation by possibly adding to the buffer. // Return the index of the next token to operate on. Execute(buffer *bytes.Buffer) int String() string GetInstructionIndex() int GetIndex() int GetText() string GetOpName() string GetTokens() TokenStream SetInstructionIndex(val int) SetIndex(int) SetText(string) SetOpName(string) SetTokens(TokenStream) } type BaseRewriteOperation struct { //Current index of rewrites list instructionIndex int //Token buffer index index int //Substitution text text string //Actual operation name opName string //Pointer to token steam tokens TokenStream } func (op *BaseRewriteOperation) GetInstructionIndex() int { return op.instructionIndex } func (op *BaseRewriteOperation) GetIndex() int { return op.index } func (op *BaseRewriteOperation) GetText() string { return op.text } func (op *BaseRewriteOperation) GetOpName() string { return op.opName } func (op *BaseRewriteOperation) GetTokens() TokenStream { return op.tokens } func (op *BaseRewriteOperation) SetInstructionIndex(val int) { op.instructionIndex = val } func (op *BaseRewriteOperation) SetIndex(val int) { op.index = val } func (op *BaseRewriteOperation) SetText(val string) { op.text = val } func (op *BaseRewriteOperation) SetOpName(val string) { op.opName = val } func (op *BaseRewriteOperation) SetTokens(val TokenStream) { op.tokens = val } func (op *BaseRewriteOperation) Execute(_ *bytes.Buffer) int { return op.index } func (op *BaseRewriteOperation) String() string { return fmt.Sprintf("<%s@%d:\"%s\">", op.opName, op.tokens.Get(op.GetIndex()), op.text, ) } type InsertBeforeOp struct { BaseRewriteOperation } func NewInsertBeforeOp(index int, text string, stream TokenStream) *InsertBeforeOp { return &InsertBeforeOp{BaseRewriteOperation: BaseRewriteOperation{ index: index, text: text, opName: "InsertBeforeOp", tokens: stream, }} } func (op *InsertBeforeOp) Execute(buffer *bytes.Buffer) int { buffer.WriteString(op.text) if op.tokens.Get(op.index).GetTokenType() != TokenEOF { buffer.WriteString(op.tokens.Get(op.index).GetText()) } return op.index + 1 } func (op *InsertBeforeOp) String() string { return op.BaseRewriteOperation.String() } // InsertAfterOp distinguishes between insert after/before to do the "insert after" instructions // first and then the "insert before" instructions at same index. Implementation // of "insert after" is "insert before index+1". type InsertAfterOp struct { BaseRewriteOperation } func NewInsertAfterOp(index int, text string, stream TokenStream) *InsertAfterOp { return &InsertAfterOp{ BaseRewriteOperation: BaseRewriteOperation{ index: index + 1, text: text, tokens: stream, }, } } func (op *InsertAfterOp) Execute(buffer *bytes.Buffer) int { buffer.WriteString(op.text) if op.tokens.Get(op.index).GetTokenType() != TokenEOF { buffer.WriteString(op.tokens.Get(op.index).GetText()) } return op.index + 1 } func (op *InsertAfterOp) String() string { return op.BaseRewriteOperation.String() } // ReplaceOp tries to replace range from x..y with (y-x)+1 ReplaceOp // instructions. type ReplaceOp struct { BaseRewriteOperation LastIndex int } func NewReplaceOp(from, to int, text string, stream TokenStream) *ReplaceOp { return &ReplaceOp{ BaseRewriteOperation: BaseRewriteOperation{ index: from, text: text, opName: "ReplaceOp", tokens: stream, }, LastIndex: to, } } func (op *ReplaceOp) Execute(buffer *bytes.Buffer) int { if op.text != "" { buffer.WriteString(op.text) } return op.LastIndex + 1 } func (op *ReplaceOp) String() string { if op.text == "" { return fmt.Sprintf("This is a one way link. It emanates from a state (usually via a list of // transitions) and has a target state.
// //Since we never have to change the ATN transitions once we construct it, // the states. We'll use the term Edge for the DFA to distinguish them from // ATN transitions.
type Transition interface { getTarget() ATNState setTarget(ATNState) getIsEpsilon() bool getLabel() *IntervalSet getSerializationType() int Matches(int, int, int) bool } type BaseTransition struct { target ATNState isEpsilon bool label int intervalSet *IntervalSet serializationType int } func NewBaseTransition(target ATNState) *BaseTransition { if target == nil { panic("target cannot be nil.") } t := new(BaseTransition) t.target = target // Are we epsilon, action, sempred? t.isEpsilon = false t.intervalSet = nil return t } func (t *BaseTransition) getTarget() ATNState { return t.target } func (t *BaseTransition) setTarget(s ATNState) { t.target = s } func (t *BaseTransition) getIsEpsilon() bool { return t.isEpsilon } func (t *BaseTransition) getLabel() *IntervalSet { return t.intervalSet } func (t *BaseTransition) getSerializationType() int { return t.serializationType } func (t *BaseTransition) Matches(_, _, _ int) bool { panic("Not implemented") } const ( TransitionEPSILON = 1 TransitionRANGE = 2 TransitionRULE = 3 TransitionPREDICATE = 4 // e.g., {isType(input.LT(1))}? TransitionATOM = 5 TransitionACTION = 6 TransitionSET = 7 // ~(A|B) or ~atom, wildcard, which convert to next 2 TransitionNOTSET = 8 TransitionWILDCARD = 9 TransitionPRECEDENCE = 10 ) //goland:noinspection GoUnusedGlobalVariable var TransitionserializationNames = []string{ "INVALID", "EPSILON", "RANGE", "RULE", "PREDICATE", "ATOM", "ACTION", "SET", "NOT_SET", "WILDCARD", "PRECEDENCE", } //var TransitionserializationTypes struct { // EpsilonTransition int // RangeTransition int // RuleTransition int // PredicateTransition int // AtomTransition int // ActionTransition int // SetTransition int // NotSetTransition int // WildcardTransition int // PrecedencePredicateTransition int //}{ // TransitionEPSILON, // TransitionRANGE, // TransitionRULE, // TransitionPREDICATE, // TransitionATOM, // TransitionACTION, // TransitionSET, // TransitionNOTSET, // TransitionWILDCARD, // TransitionPRECEDENCE //} // AtomTransition // TODO: make all transitions sets? no, should remove set edges type AtomTransition struct { BaseTransition } func NewAtomTransition(target ATNState, intervalSet int) *AtomTransition { t := &AtomTransition{ BaseTransition: BaseTransition{ target: target, serializationType: TransitionATOM, label: intervalSet, isEpsilon: false, }, } t.intervalSet = t.makeLabel() return t } func (t *AtomTransition) makeLabel() *IntervalSet { s := NewIntervalSet() s.addOne(t.label) return s } func (t *AtomTransition) Matches(symbol, _, _ int) bool { return t.label == symbol } func (t *AtomTransition) String() string { return strconv.Itoa(t.label) } type RuleTransition struct { BaseTransition followState ATNState ruleIndex, precedence int } func NewRuleTransition(ruleStart ATNState, ruleIndex, precedence int, followState ATNState) *RuleTransition { return &RuleTransition{ BaseTransition: BaseTransition{ target: ruleStart, isEpsilon: true, serializationType: TransitionRULE, }, ruleIndex: ruleIndex, precedence: precedence, followState: followState, } } func (t *RuleTransition) Matches(_, _, _ int) bool { return false } type EpsilonTransition struct { BaseTransition outermostPrecedenceReturn int } func NewEpsilonTransition(target ATNState, outermostPrecedenceReturn int) *EpsilonTransition { return &EpsilonTransition{ BaseTransition: BaseTransition{ target: target, serializationType: TransitionEPSILON, isEpsilon: true, }, outermostPrecedenceReturn: outermostPrecedenceReturn, } } func (t *EpsilonTransition) Matches(_, _, _ int) bool { return false } func (t *EpsilonTransition) String() string { return "epsilon" } type RangeTransition struct { BaseTransition start, stop int } func NewRangeTransition(target ATNState, start, stop int) *RangeTransition { t := &RangeTransition{ BaseTransition: BaseTransition{ target: target, serializationType: TransitionRANGE, isEpsilon: false, }, start: start, stop: stop, } t.intervalSet = t.makeLabel() return t } func (t *RangeTransition) makeLabel() *IntervalSet { s := NewIntervalSet() s.addRange(t.start, t.stop) return s } func (t *RangeTransition) Matches(symbol, _, _ int) bool { return symbol >= t.start && symbol <= t.stop } func (t *RangeTransition) String() string { var sb strings.Builder sb.WriteByte('\'') sb.WriteRune(rune(t.start)) sb.WriteString("'..'") sb.WriteRune(rune(t.stop)) sb.WriteByte('\'') return sb.String() } type AbstractPredicateTransition interface { Transition IAbstractPredicateTransitionFoo() } type BaseAbstractPredicateTransition struct { BaseTransition } func NewBasePredicateTransition(target ATNState) *BaseAbstractPredicateTransition { return &BaseAbstractPredicateTransition{ BaseTransition: BaseTransition{ target: target, }, } } func (a *BaseAbstractPredicateTransition) IAbstractPredicateTransitionFoo() {} type PredicateTransition struct { BaseAbstractPredicateTransition isCtxDependent bool ruleIndex, predIndex int } func NewPredicateTransition(target ATNState, ruleIndex, predIndex int, isCtxDependent bool) *PredicateTransition { return &PredicateTransition{ BaseAbstractPredicateTransition: BaseAbstractPredicateTransition{ BaseTransition: BaseTransition{ target: target, serializationType: TransitionPREDICATE, isEpsilon: true, }, }, isCtxDependent: isCtxDependent, ruleIndex: ruleIndex, predIndex: predIndex, } } func (t *PredicateTransition) Matches(_, _, _ int) bool { return false } func (t *PredicateTransition) getPredicate() *Predicate { return NewPredicate(t.ruleIndex, t.predIndex, t.isCtxDependent) } func (t *PredicateTransition) String() string { return "pred_" + strconv.Itoa(t.ruleIndex) + ":" + strconv.Itoa(t.predIndex) } type ActionTransition struct { BaseTransition isCtxDependent bool ruleIndex, actionIndex, predIndex int } func NewActionTransition(target ATNState, ruleIndex, actionIndex int, isCtxDependent bool) *ActionTransition { return &ActionTransition{ BaseTransition: BaseTransition{ target: target, serializationType: TransitionACTION, isEpsilon: true, }, isCtxDependent: isCtxDependent, ruleIndex: ruleIndex, actionIndex: actionIndex, } } func (t *ActionTransition) Matches(_, _, _ int) bool { return false } func (t *ActionTransition) String() string { return "action_" + strconv.Itoa(t.ruleIndex) + ":" + strconv.Itoa(t.actionIndex) } type SetTransition struct { BaseTransition } func NewSetTransition(target ATNState, set *IntervalSet) *SetTransition { t := &SetTransition{ BaseTransition: BaseTransition{ target: target, serializationType: TransitionSET, }, } if set != nil { t.intervalSet = set } else { t.intervalSet = NewIntervalSet() t.intervalSet.addOne(TokenInvalidType) } return t } func (t *SetTransition) Matches(symbol, _, _ int) bool { return t.intervalSet.contains(symbol) } func (t *SetTransition) String() string { return t.intervalSet.String() } type NotSetTransition struct { SetTransition } func NewNotSetTransition(target ATNState, set *IntervalSet) *NotSetTransition { t := &NotSetTransition{ SetTransition: SetTransition{ BaseTransition: BaseTransition{ target: target, serializationType: TransitionNOTSET, }, }, } if set != nil { t.intervalSet = set } else { t.intervalSet = NewIntervalSet() t.intervalSet.addOne(TokenInvalidType) } return t } func (t *NotSetTransition) Matches(symbol, minVocabSymbol, maxVocabSymbol int) bool { return symbol >= minVocabSymbol && symbol <= maxVocabSymbol && !t.intervalSet.contains(symbol) } func (t *NotSetTransition) String() string { return "~" + t.intervalSet.String() } type WildcardTransition struct { BaseTransition } func NewWildcardTransition(target ATNState) *WildcardTransition { return &WildcardTransition{ BaseTransition: BaseTransition{ target: target, serializationType: TransitionWILDCARD, }, } } func (t *WildcardTransition) Matches(symbol, minVocabSymbol, maxVocabSymbol int) bool { return symbol >= minVocabSymbol && symbol <= maxVocabSymbol } func (t *WildcardTransition) String() string { return "." } type PrecedencePredicateTransition struct { BaseAbstractPredicateTransition precedence int } func NewPrecedencePredicateTransition(target ATNState, precedence int) *PrecedencePredicateTransition { return &PrecedencePredicateTransition{ BaseAbstractPredicateTransition: BaseAbstractPredicateTransition{ BaseTransition: BaseTransition{ target: target, serializationType: TransitionPRECEDENCE, isEpsilon: true, }, }, precedence: precedence, } } func (t *PrecedencePredicateTransition) Matches(_, _, _ int) bool { return false } func (t *PrecedencePredicateTransition) getPredicate() *PrecedencePredicate { return NewPrecedencePredicate(t.precedence) } func (t *PrecedencePredicateTransition) String() string { return fmt.Sprint(t.precedence) + " >= _p" } antlr4-go-antlr-c821258/tree.go 0000664 0000000 0000000 00000017244 14621155120 0016210 0 ustar 00root root 0000000 0000000 // Copyright (c) 2012-2022 The ANTLR Project. All rights reserved. // Use of this file is governed by the BSD 3-clause license that // can be found in the LICENSE.txt file in the project root. package antlr // The basic notion of a tree has a parent, a payload, and a list of children. // It is the most abstract interface for all the trees used by ANTLR. /// var TreeInvalidInterval = NewInterval(-1, -2) type Tree interface { GetParent() Tree SetParent(Tree) GetPayload() interface{} GetChild(i int) Tree GetChildCount() int GetChildren() []Tree } type SyntaxTree interface { Tree GetSourceInterval() Interval } type ParseTree interface { SyntaxTree Accept(Visitor ParseTreeVisitor) interface{} GetText() string ToStringTree([]string, Recognizer) string } type RuleNode interface { ParseTree GetRuleContext() RuleContext } type TerminalNode interface { ParseTree GetSymbol() Token } type ErrorNode interface { TerminalNode errorNode() } type ParseTreeVisitor interface { Visit(tree ParseTree) interface{} VisitChildren(node RuleNode) interface{} VisitTerminal(node TerminalNode) interface{} VisitErrorNode(node ErrorNode) interface{} } type BaseParseTreeVisitor struct{} var _ ParseTreeVisitor = &BaseParseTreeVisitor{} func (v *BaseParseTreeVisitor) Visit(tree ParseTree) interface{} { return tree.Accept(v) } func (v *BaseParseTreeVisitor) VisitChildren(_ RuleNode) interface{} { return nil } func (v *BaseParseTreeVisitor) VisitTerminal(_ TerminalNode) interface{} { return nil } func (v *BaseParseTreeVisitor) VisitErrorNode(_ ErrorNode) interface{} { return nil } // TODO: Implement this? //func (this ParseTreeVisitor) Visit(ctx) { // if (Utils.isArray(ctx)) { // self := this // return ctx.map(function(child) { return VisitAtom(self, child)}) // } else { // return VisitAtom(this, ctx) // } //} // //func VisitAtom(Visitor, ctx) { // if (ctx.parser == nil) { //is terminal // return // } // // name := ctx.parser.ruleNames[ctx.ruleIndex] // funcName := "Visit" + Utils.titleCase(name) // // return Visitor[funcName](ctx) //} type ParseTreeListener interface { VisitTerminal(node TerminalNode) VisitErrorNode(node ErrorNode) EnterEveryRule(ctx ParserRuleContext) ExitEveryRule(ctx ParserRuleContext) } type BaseParseTreeListener struct{} var _ ParseTreeListener = &BaseParseTreeListener{} func (l *BaseParseTreeListener) VisitTerminal(_ TerminalNode) {} func (l *BaseParseTreeListener) VisitErrorNode(_ ErrorNode) {} func (l *BaseParseTreeListener) EnterEveryRule(_ ParserRuleContext) {} func (l *BaseParseTreeListener) ExitEveryRule(_ ParserRuleContext) {} type TerminalNodeImpl struct { parentCtx RuleContext symbol Token } var _ TerminalNode = &TerminalNodeImpl{} func NewTerminalNodeImpl(symbol Token) *TerminalNodeImpl { tn := new(TerminalNodeImpl) tn.parentCtx = nil tn.symbol = symbol return tn } func (t *TerminalNodeImpl) GetChild(_ int) Tree { return nil } func (t *TerminalNodeImpl) GetChildren() []Tree { return nil } func (t *TerminalNodeImpl) SetChildren(_ []Tree) { panic("Cannot set children on terminal node") } func (t *TerminalNodeImpl) GetSymbol() Token { return t.symbol } func (t *TerminalNodeImpl) GetParent() Tree { return t.parentCtx } func (t *TerminalNodeImpl) SetParent(tree Tree) { t.parentCtx = tree.(RuleContext) } func (t *TerminalNodeImpl) GetPayload() interface{} { return t.symbol } func (t *TerminalNodeImpl) GetSourceInterval() Interval { if t.symbol == nil { return TreeInvalidInterval } tokenIndex := t.symbol.GetTokenIndex() return NewInterval(tokenIndex, tokenIndex) } func (t *TerminalNodeImpl) GetChildCount() int { return 0 } func (t *TerminalNodeImpl) Accept(v ParseTreeVisitor) interface{} { return v.VisitTerminal(t) } func (t *TerminalNodeImpl) GetText() string { return t.symbol.GetText() } func (t *TerminalNodeImpl) String() string { if t.symbol.GetTokenType() == TokenEOF { return "