forked from jasder/antlr
DFASerializer, DiagnosticErrorListener
This commit is contained in:
parent
54206cd853
commit
2a4de9bbf9
|
@ -129,7 +129,7 @@ func (this *ATNConfigSet) add(config *ATNConfig, mergeCache DoubleDict) bool {
|
|||
}
|
||||
|
||||
func (this *ATNConfigSet) getStates() {
|
||||
var states = NewSet()
|
||||
var states = NewSet(nil,nil)
|
||||
for i := 0; i < len(this.configs); i++ {
|
||||
states.add(this.configs[i].state)
|
||||
}
|
||||
|
|
|
@ -1,80 +1,117 @@
|
|||
package antlr4
|
||||
import "fmt"
|
||||
|
||||
// A DFA walker that knows how to dump them to serialized strings.#/
|
||||
|
||||
|
||||
type DFASerializer struct {
|
||||
|
||||
dfa *DFA
|
||||
literalNames, symbolicNames []string
|
||||
}
|
||||
|
||||
func DFASerializer(dfa, literalNames, symbolicNames) {
|
||||
func NewDFASerializer(dfa *DFA, literalNames, symbolicNames []string) *DFASerializer {
|
||||
|
||||
if (literalNames == nil){
|
||||
literalNames = make([]string)
|
||||
}
|
||||
|
||||
if (symbolicNames == nil){
|
||||
symbolicNames = make([]string)
|
||||
}
|
||||
|
||||
this := new(DFASerializer)
|
||||
|
||||
this.initDFASerializer(dfa, literalNames, symbolicNames)
|
||||
|
||||
this.dfa = dfa
|
||||
this.literalNames = literalNames || []
|
||||
this.symbolicNames = symbolicNames || []
|
||||
return this
|
||||
}
|
||||
|
||||
func (this *DFASerializer) initDFASerializer(dfa *DFA, literalNames, symbolicNames []string) {
|
||||
this.dfa = dfa
|
||||
this.literalNames = literalNames
|
||||
this.symbolicNames = symbolicNames
|
||||
}
|
||||
|
||||
func (this *DFASerializer) toString() string {
|
||||
|
||||
if(this.dfa.s0 == nil) {
|
||||
return nil
|
||||
}
|
||||
var buf = ""
|
||||
var states = this.dfa.sortedStates()
|
||||
for(var i=0i<states.lengthi++) {
|
||||
for i := 0; i<len(states); i++ {
|
||||
var s = states[i]
|
||||
if(s.edges!=nil) {
|
||||
var n = s.edges.length
|
||||
for(var j=0j<nj++) {
|
||||
var t = s.edges[j] || nil
|
||||
var n = len(s.edges)
|
||||
for j :=0; j<n; j++ {
|
||||
var t = s.edges[j]
|
||||
if(t!=nil && t.stateNumber != 0x7FFFFFFF) {
|
||||
buf = buf.concat(this.getStateString(s))
|
||||
buf = buf.concat("-")
|
||||
buf = buf.concat(this.getEdgeLabel(j))
|
||||
buf = buf.concat("->")
|
||||
buf = buf.concat(this.getStateString(t))
|
||||
buf = buf.concat('\n')
|
||||
buf += this.getStateString(s)
|
||||
buf += "-"
|
||||
buf += this.getEdgeLabel(j)
|
||||
buf += "->"
|
||||
buf += this.getStateString(t)
|
||||
buf += '\n'
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return buf.length==0 ? nil : buf
|
||||
if len(buf) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
return buf
|
||||
}
|
||||
|
||||
func (this *DFASerializer) getEdgeLabel(i) {
|
||||
func (this *DFASerializer) getEdgeLabel(i int) string {
|
||||
if (i==0) {
|
||||
return "EOF"
|
||||
} else if(this.literalNames !=nil || this.symbolicNames!=nil) {
|
||||
return this.literalNames[i-1] || this.symbolicNames[i-1]
|
||||
} else {
|
||||
return String.fromCharCode(i-1)
|
||||
return string(i-1)
|
||||
}
|
||||
}
|
||||
|
||||
func (this *DFASerializer) getStateString(s) {
|
||||
var baseStateStr = ( s.isAcceptState ? ":" : "") + "s" + s.stateNumber + ( s.requiresFullContext ? "^" : "")
|
||||
func (this *DFASerializer) getStateString(s *DFAState) string {
|
||||
|
||||
var a,b string
|
||||
|
||||
if (s.isAcceptState){
|
||||
a = ":"
|
||||
}
|
||||
|
||||
if (s.requiresFullContext){
|
||||
b = "^"
|
||||
}
|
||||
|
||||
var baseStateStr = a + "s" + s.stateNumber + b
|
||||
if(s.isAcceptState) {
|
||||
if (s.predicates != nil) {
|
||||
return baseStateStr + "=>" + s.predicates.toString()
|
||||
return baseStateStr + "=>" + fmt.Sprint(s.predicates)
|
||||
} else {
|
||||
return baseStateStr + "=>" + s.prediction.toString()
|
||||
return baseStateStr + "=>" + fmt.Sprint(s.prediction)
|
||||
}
|
||||
} else {
|
||||
return baseStateStr
|
||||
}
|
||||
}
|
||||
|
||||
func LexerDFASerializer(dfa) {
|
||||
DFASerializer.call(this, dfa, nil)
|
||||
type LexerDFASerializer struct {
|
||||
DFASerializer
|
||||
}
|
||||
|
||||
func NewLexerDFASerializer(dfa *DFA) *LexerDFASerializer {
|
||||
|
||||
this := new(DFASerializer)
|
||||
|
||||
this.initDFASerializer(dfa, nil, nil)
|
||||
|
||||
return this
|
||||
}
|
||||
|
||||
//LexerDFASerializer.prototype = Object.create(DFASerializer.prototype)
|
||||
//LexerDFASerializer.prototype.constructor = LexerDFASerializer
|
||||
|
||||
func (this *LexerDFASerializer) getEdgeLabel(i) {
|
||||
return "'" + String.fromCharCode(i) + "'"
|
||||
func (this *LexerDFASerializer) getEdgeLabel(i int) {
|
||||
return "'" + string(i) + "'"
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
package antlr4
|
||||
import "strings"
|
||||
|
||||
//
|
||||
// This implementation of {@link ANTLRErrorListener} can be used to identify
|
||||
|
@ -19,16 +20,13 @@ package antlr4
|
|||
// this situation occurs.</li>
|
||||
// </ul>
|
||||
|
||||
//var BitSet = require('./../Utils').BitSet
|
||||
//var ErrorListener = require('./ErrorListener').ErrorListener
|
||||
//var Interval = require('./../IntervalSet').Interval
|
||||
|
||||
type DiagnosticErrorListener struct {
|
||||
ErrorListener
|
||||
|
||||
exactOnly bool
|
||||
}
|
||||
|
||||
func DiagnosticErrorListener(exactOnly bool) {
|
||||
func NewDiagnosticErrorListener(exactOnly bool) *DiagnosticErrorListener {
|
||||
|
||||
n := new(DiagnosticErrorListener)
|
||||
|
||||
|
@ -37,9 +35,6 @@ func DiagnosticErrorListener(exactOnly bool) {
|
|||
return n
|
||||
}
|
||||
|
||||
//DiagnosticErrorListener.prototype = Object.create(ErrorListener.prototype)
|
||||
//DiagnosticErrorListener.prototype.constructor = DiagnosticErrorListener
|
||||
|
||||
func (this *DiagnosticErrorListener) reportAmbiguity(recognizer *Parser, dfa *DFA, startIndex, stopIndex int, exact bool, ambigAlts *BitSet, configs *ATNConfigSet) {
|
||||
if (this.exactOnly && !exact) {
|
||||
return
|
||||
|
@ -49,8 +44,8 @@ func (this *DiagnosticErrorListener) reportAmbiguity(recognizer *Parser, dfa *DF
|
|||
": ambigAlts=" +
|
||||
this.getConflictingAlts(ambigAlts, configs) +
|
||||
", input='" +
|
||||
recognizer.getTokenStream().getText(NewInterval(startIndex, stopIndex)) + "'"
|
||||
recognizer.notifyErrorListeners(msg)
|
||||
recognizer.getTokenStream().getTextFromInterval(NewInterval(startIndex, stopIndex)) + "'"
|
||||
recognizer.notifyErrorListeners(msg, nil, nil)
|
||||
}
|
||||
|
||||
func (this *DiagnosticErrorListener) reportAttemptingFullContext(recognizer *Parser, dfa *DFA, startIndex, stopIndex int, conflictingAlts *BitSet, configs *ATNConfigSet) {
|
||||
|
@ -58,28 +53,28 @@ func (this *DiagnosticErrorListener) reportAttemptingFullContext(recognizer *Par
|
|||
var msg = "reportAttemptingFullContext d=" +
|
||||
this.getDecisionDescription(recognizer, dfa) +
|
||||
", input='" +
|
||||
recognizer.getTokenStream().getText(NewInterval(startIndex, stopIndex)) + "'"
|
||||
recognizer.notifyErrorListeners(msg)
|
||||
recognizer.getTokenStream().getTextFromInterval(NewInterval(startIndex, stopIndex)) + "'"
|
||||
recognizer.notifyErrorListeners(msg, nil, nil)
|
||||
}
|
||||
|
||||
func (this *DiagnosticErrorListener) reportContextSensitivity(recognizer *Parser, dfa *DFA, startIndex, stopIndex, prediction int, configs *ATNConfigSet) {
|
||||
var msg = "reportContextSensitivity d=" +
|
||||
this.getDecisionDescription(recognizer, dfa) +
|
||||
", input='" +
|
||||
recognizer.getTokenStream().getText(NewInterval(startIndex, stopIndex)) + "'"
|
||||
recognizer.notifyErrorListeners(msg)
|
||||
recognizer.getTokenStream().getTextFromInterval(NewInterval(startIndex, stopIndex)) + "'"
|
||||
recognizer.notifyErrorListeners(msg, nil, nil)
|
||||
}
|
||||
|
||||
func (this *DiagnosticErrorListener) getDecisionDescription(recognizer *Parser, dfa *DFA) {
|
||||
var decision = dfa.decision
|
||||
var ruleIndex = dfa.atnStartState.ruleIndex
|
||||
|
||||
var ruleNames = recognizer.ruleNames
|
||||
if (ruleIndex < 0 || ruleIndex >= ruleNames.length) {
|
||||
var ruleNames = recognizer.getRuleNames()
|
||||
if (ruleIndex < 0 || ruleIndex >= len(ruleNames)) {
|
||||
return "" + decision
|
||||
}
|
||||
var ruleName = ruleNames[ruleIndex] || nil
|
||||
if (ruleName == nil || ruleName.length == 0) {
|
||||
if (ruleName == nil || len(ruleName) == 0) {
|
||||
return "" + decision
|
||||
}
|
||||
return "" + decision + " (" + ruleName + ")"
|
||||
|
@ -96,13 +91,13 @@ func (this *DiagnosticErrorListener) getDecisionDescription(recognizer *Parser,
|
|||
// @return Returns {@code reportedAlts} if it is not {@code nil}, otherwise
|
||||
// returns the set of alternatives represented in {@code configs}.
|
||||
//
|
||||
func (this *DiagnosticErrorListener) getConflictingAlts(reportedAlts, configs) {
|
||||
func (this *DiagnosticErrorListener) getConflictingAlts(reportedAlts *BitSet, set *ATNConfigSet) *BitSet {
|
||||
if (reportedAlts != nil) {
|
||||
return reportedAlts
|
||||
}
|
||||
var result = NewBitSet()
|
||||
for i := 0; i < len(configs.items); i++ {
|
||||
result.add(configs.items[i].alt)
|
||||
for i := 0; i < len(set.configs); i++ {
|
||||
result.add(set.configs[i].alt)
|
||||
}
|
||||
return "{" + result.values().join(", ") + "}"
|
||||
return "{" + strings.Join(result.values(), ", ") + "}"
|
||||
}
|
Loading…
Reference in New Issue