DFASerializer, DiagnosticErrorListener

This commit is contained in:
Peter Boyer 2015-12-18 11:00:46 -05:00
parent 54206cd853
commit 2a4de9bbf9
3 changed files with 83 additions and 51 deletions

View File

@ -129,7 +129,7 @@ func (this *ATNConfigSet) add(config *ATNConfig, mergeCache DoubleDict) bool {
} }
func (this *ATNConfigSet) getStates() { func (this *ATNConfigSet) getStates() {
var states = NewSet() var states = NewSet(nil,nil)
for i := 0; i < len(this.configs); i++ { for i := 0; i < len(this.configs); i++ {
states.add(this.configs[i].state) states.add(this.configs[i].state)
} }

View File

@ -1,80 +1,117 @@
package antlr4 package antlr4
import "fmt"
// A DFA walker that knows how to dump them to serialized strings.#/ // A DFA walker that knows how to dump them to serialized strings.#/
type DFASerializer struct { 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 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 { func (this *DFASerializer) toString() string {
if(this.dfa.s0 == nil) { if(this.dfa.s0 == nil) {
return nil return nil
} }
var buf = "" var buf = ""
var states = this.dfa.sortedStates() var states = this.dfa.sortedStates()
for(var i=0i<states.lengthi++) { for i := 0; i<len(states); i++ {
var s = states[i] var s = states[i]
if(s.edges!=nil) { if(s.edges!=nil) {
var n = s.edges.length var n = len(s.edges)
for(var j=0j<nj++) { for j :=0; j<n; j++ {
var t = s.edges[j] || nil var t = s.edges[j]
if(t!=nil && t.stateNumber != 0x7FFFFFFF) { if(t!=nil && t.stateNumber != 0x7FFFFFFF) {
buf = buf.concat(this.getStateString(s)) buf += this.getStateString(s)
buf = buf.concat("-") buf += "-"
buf = buf.concat(this.getEdgeLabel(j)) buf += this.getEdgeLabel(j)
buf = buf.concat("->") buf += "->"
buf = buf.concat(this.getStateString(t)) buf += this.getStateString(t)
buf = buf.concat('\n') 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) { if (i==0) {
return "EOF" return "EOF"
} else if(this.literalNames !=nil || this.symbolicNames!=nil) { } else if(this.literalNames !=nil || this.symbolicNames!=nil) {
return this.literalNames[i-1] || this.symbolicNames[i-1] return this.literalNames[i-1] || this.symbolicNames[i-1]
} else { } else {
return String.fromCharCode(i-1) return string(i-1)
} }
} }
func (this *DFASerializer) getStateString(s) { func (this *DFASerializer) getStateString(s *DFAState) string {
var baseStateStr = ( s.isAcceptState ? ":" : "") + "s" + s.stateNumber + ( s.requiresFullContext ? "^" : "")
var a,b string
if (s.isAcceptState){
a = ":"
}
if (s.requiresFullContext){
b = "^"
}
var baseStateStr = a + "s" + s.stateNumber + b
if(s.isAcceptState) { if(s.isAcceptState) {
if (s.predicates != nil) { if (s.predicates != nil) {
return baseStateStr + "=>" + s.predicates.toString() return baseStateStr + "=>" + fmt.Sprint(s.predicates)
} else { } else {
return baseStateStr + "=>" + s.prediction.toString() return baseStateStr + "=>" + fmt.Sprint(s.prediction)
} }
} else { } else {
return baseStateStr return baseStateStr
} }
} }
func LexerDFASerializer(dfa) { type LexerDFASerializer struct {
DFASerializer.call(this, dfa, nil) DFASerializer
}
func NewLexerDFASerializer(dfa *DFA) *LexerDFASerializer {
this := new(DFASerializer)
this.initDFASerializer(dfa, nil, nil)
return this return this
} }
//LexerDFASerializer.prototype = Object.create(DFASerializer.prototype) func (this *LexerDFASerializer) getEdgeLabel(i int) {
//LexerDFASerializer.prototype.constructor = LexerDFASerializer return "'" + string(i) + "'"
func (this *LexerDFASerializer) getEdgeLabel(i) {
return "'" + String.fromCharCode(i) + "'"
} }

View File

@ -1,4 +1,5 @@
package antlr4 package antlr4
import "strings"
// //
// This implementation of {@link ANTLRErrorListener} can be used to identify // This implementation of {@link ANTLRErrorListener} can be used to identify
@ -19,16 +20,13 @@ package antlr4
// this situation occurs.</li> // this situation occurs.</li>
// </ul> // </ul>
//var BitSet = require('./../Utils').BitSet
//var ErrorListener = require('./ErrorListener').ErrorListener
//var Interval = require('./../IntervalSet').Interval
type DiagnosticErrorListener struct { type DiagnosticErrorListener struct {
ErrorListener ErrorListener
exactOnly bool exactOnly bool
} }
func DiagnosticErrorListener(exactOnly bool) { func NewDiagnosticErrorListener(exactOnly bool) *DiagnosticErrorListener {
n := new(DiagnosticErrorListener) n := new(DiagnosticErrorListener)
@ -37,9 +35,6 @@ func DiagnosticErrorListener(exactOnly bool) {
return n 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) { func (this *DiagnosticErrorListener) reportAmbiguity(recognizer *Parser, dfa *DFA, startIndex, stopIndex int, exact bool, ambigAlts *BitSet, configs *ATNConfigSet) {
if (this.exactOnly && !exact) { if (this.exactOnly && !exact) {
return return
@ -49,8 +44,8 @@ func (this *DiagnosticErrorListener) reportAmbiguity(recognizer *Parser, dfa *DF
": ambigAlts=" + ": ambigAlts=" +
this.getConflictingAlts(ambigAlts, configs) + this.getConflictingAlts(ambigAlts, configs) +
", input='" + ", input='" +
recognizer.getTokenStream().getText(NewInterval(startIndex, stopIndex)) + "'" recognizer.getTokenStream().getTextFromInterval(NewInterval(startIndex, stopIndex)) + "'"
recognizer.notifyErrorListeners(msg) recognizer.notifyErrorListeners(msg, nil, nil)
} }
func (this *DiagnosticErrorListener) reportAttemptingFullContext(recognizer *Parser, dfa *DFA, startIndex, stopIndex int, conflictingAlts *BitSet, configs *ATNConfigSet) { 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=" + var msg = "reportAttemptingFullContext d=" +
this.getDecisionDescription(recognizer, dfa) + this.getDecisionDescription(recognizer, dfa) +
", input='" + ", input='" +
recognizer.getTokenStream().getText(NewInterval(startIndex, stopIndex)) + "'" recognizer.getTokenStream().getTextFromInterval(NewInterval(startIndex, stopIndex)) + "'"
recognizer.notifyErrorListeners(msg) recognizer.notifyErrorListeners(msg, nil, nil)
} }
func (this *DiagnosticErrorListener) reportContextSensitivity(recognizer *Parser, dfa *DFA, startIndex, stopIndex, prediction int, configs *ATNConfigSet) { func (this *DiagnosticErrorListener) reportContextSensitivity(recognizer *Parser, dfa *DFA, startIndex, stopIndex, prediction int, configs *ATNConfigSet) {
var msg = "reportContextSensitivity d=" + var msg = "reportContextSensitivity d=" +
this.getDecisionDescription(recognizer, dfa) + this.getDecisionDescription(recognizer, dfa) +
", input='" + ", input='" +
recognizer.getTokenStream().getText(NewInterval(startIndex, stopIndex)) + "'" recognizer.getTokenStream().getTextFromInterval(NewInterval(startIndex, stopIndex)) + "'"
recognizer.notifyErrorListeners(msg) recognizer.notifyErrorListeners(msg, nil, nil)
} }
func (this *DiagnosticErrorListener) getDecisionDescription(recognizer *Parser, dfa *DFA) { func (this *DiagnosticErrorListener) getDecisionDescription(recognizer *Parser, dfa *DFA) {
var decision = dfa.decision var decision = dfa.decision
var ruleIndex = dfa.atnStartState.ruleIndex var ruleIndex = dfa.atnStartState.ruleIndex
var ruleNames = recognizer.ruleNames var ruleNames = recognizer.getRuleNames()
if (ruleIndex < 0 || ruleIndex >= ruleNames.length) { if (ruleIndex < 0 || ruleIndex >= len(ruleNames)) {
return "" + decision return "" + decision
} }
var ruleName = ruleNames[ruleIndex] || nil var ruleName = ruleNames[ruleIndex] || nil
if (ruleName == nil || ruleName.length == 0) { if (ruleName == nil || len(ruleName) == 0) {
return "" + decision return "" + decision
} }
return "" + decision + " (" + ruleName + ")" return "" + decision + " (" + ruleName + ")"
@ -96,13 +91,13 @@ func (this *DiagnosticErrorListener) getDecisionDescription(recognizer *Parser,
// @return Returns {@code reportedAlts} if it is not {@code nil}, otherwise // @return Returns {@code reportedAlts} if it is not {@code nil}, otherwise
// returns the set of alternatives represented in {@code configs}. // 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) { if (reportedAlts != nil) {
return reportedAlts return reportedAlts
} }
var result = NewBitSet() var result = NewBitSet()
for i := 0; i < len(configs.items); i++ { for i := 0; i < len(set.configs); i++ {
result.add(configs.items[i].alt) result.add(set.configs[i].alt)
} }
return "{" + result.values().join(", ") + "}" return "{" + strings.Join(result.values(), ", ") + "}"
} }