More debugging

This commit is contained in:
Peter Boyer 2015-12-29 12:34:26 -06:00
parent c08a19233c
commit 56e6e6c26f
16 changed files with 81 additions and 28 deletions

View File

@ -13,6 +13,7 @@ package antlr4
import (
"strconv"
"fmt"
)
// bt is just to keep meaningful parameter types to Parser
@ -105,10 +106,13 @@ func (bt *BufferedTokenStream) Consume() {
// not yet initialized
skipEofCheck = false
}
fmt.Println("Consume 1")
if !skipEofCheck && bt.LA(1) == TokenEOF {
panic("cannot consume EOF")
}
if bt.Sync(bt.index + 1) {
fmt.Println("Consume 2")
bt.index = bt.adjustSeekIndex(bt.index + 1)
}
}
@ -123,6 +127,7 @@ func (bt *BufferedTokenStream) Sync(i int) bool {
var n = i - len(bt.tokens) + 1 // how many more elements we need?
if n > 0 {
var fetched = bt.fetch(n)
fmt.Println("Sync done")
return fetched >= n
}
return true
@ -139,6 +144,7 @@ func (bt *BufferedTokenStream) fetch(n int) int {
for i := 0; i < n; i++ {
var t IToken = bt.tokenSource.nextToken()
fmt.Println("fetch loop")
t.SetTokenIndex( len(bt.tokens) )
bt.tokens = append(bt.tokens, t)
if t.GetTokenType() == TokenEOF {
@ -146,6 +152,8 @@ func (bt *BufferedTokenStream) fetch(n int) int {
return i + 1
}
}
fmt.Println("fetch done")
return n
}

View File

@ -58,7 +58,8 @@ func (this *CommonTokenFactory) Create(source *TokenSourceCharStreamPair, ttype
} else if this.copyText && source.charStream != nil {
t.SetText(source.charStream.GetTextFromInterval(NewInterval(start, stop)))
}
return t.Token
return t
}
@ -68,5 +69,5 @@ func (this *CommonTokenFactory) createThin(ttype int, text string) IToken {
var t = NewCommonToken(nil, ttype, TokenDefaultChannel, -1, -1)
t.SetText(text)
return t.Token
return t
}

View File

@ -19,7 +19,7 @@ type IErrorListener interface {
type DefaultErrorListener struct {
}
func NewErrorListener() *DefaultErrorListener {
func NewDefaultErrorListener() *DefaultErrorListener {
return new(DefaultErrorListener)
}

View File

@ -544,6 +544,8 @@ func (this *DefaultErrorStrategy) getMissingSymbol(recognizer IParser) IToken {
}
tf := recognizer.GetTokenFactory()
fmt.Println("Missing symbol error")
return tf.Create( current.GetSource(), expectedTokenType, tokenText, TokenDefaultChannel, -1, -1, current.GetLine(), current.GetColumn())
}

View File

@ -1,4 +1,5 @@
package antlr4
import "fmt"
type InputStream struct {
name string
@ -66,6 +67,7 @@ func (is *InputStream) Mark() int {
}
func (is *InputStream) Release(marker int) {
fmt.Println("RELEASING")
}
func (is *InputStream) Seek(index int) {

View File

@ -211,7 +211,10 @@ func (l *Lexer) nextToken() IToken {
if l._type != LexerMore {
break
}
fmt.Println("lex inner loop")
}
fmt.Println("lex loop")
if continueOuter {
continue
}
@ -290,6 +293,7 @@ func (l *Lexer) emitToken(token IToken) {
// custom Token objects or provide a Newfactory.
// /
func (l *Lexer) emit() IToken {
fmt.Println("emit")
var t = l._factory.Create(l._tokenFactorySourcePair, l._type, l._text, l._channel, l._tokenStartCharIndex, l.getCharIndex()-1, l._tokenStartLine, l._tokenStartColumn)
l.emitToken(t)
return t
@ -298,6 +302,7 @@ func (l *Lexer) emit() IToken {
func (l *Lexer) emitEOF() IToken {
cpos := l.getCharPositionInLine()
lpos := l.getLine()
fmt.Println("emitEOF")
var eof = l._factory.Create(l._tokenFactorySourcePair, TokenEOF, "", TokenDefaultChannel, l._input.Index(), l._input.Index()-1, lpos, cpos)
l.emitToken(eof)
return eof

View File

@ -107,21 +107,25 @@ func (this *LexerATNSimulator) Match(input CharStream, mode int) int {
fmt.Println("Match")
this.Match_calls += 1
this.mode = mode
var mark = input.Mark()
defer func() {
fmt.Println("FINALLY")
input.Release(mark)
}()
this.startIndex = input.Index()
this.prevAccept.reset()
var dfa = this.decisionToDFA[mode]
if dfa.s0 == nil {
fmt.Println("MatchATN")
return this.MatchATN(input)
} else {
fmt.Println("execATN")
return this.execATN(input, dfa.s0)
}
}
@ -159,16 +163,8 @@ func (this *LexerATNSimulator) MatchATN(input CharStream) int {
return predict
}
var countA = 0
func (this *LexerATNSimulator) execATN(input CharStream, ds0 *DFAState) int {
countA += 1
if (countA == 2) {
panic("GAH")
}
if LexerATNSimulatorDebug {
fmt.Println("start state closure=" + ds0.configs.String())
}
@ -228,7 +224,7 @@ func (this *LexerATNSimulator) execATN(input CharStream, ds0 *DFAState) int {
s = target // flip current DFA target becomes Newsrc/from state
}
fmt.Println("OUT")
fmt.Println("DONE WITH execATN loop")
return this.failOrAccept(this.prevAccept, input, s.configs, t)
}

View File

@ -125,6 +125,8 @@ func (p *Parser) GetParseListeners() []ParseTreeListener {
// misMatched symbol
func (p *Parser) Match(ttype int) IToken {
fmt.Println("get current token")
var t = p.getCurrentToken()
fmt.Println("TOKEN IS " + t.GetText())
@ -141,6 +143,9 @@ func (p *Parser) Match(ttype int) IToken {
p._ctx.addErrorNode(t)
}
}
fmt.Println("match done")
return t
}
@ -406,7 +411,9 @@ func (p *Parser) NotifyErrorListeners(msg string, offendingToken IToken, err IRe
func (p *Parser) Consume() IToken {
var o = p.getCurrentToken()
if o.GetTokenType() != TokenEOF {
fmt.Println("Consuming")
p.GetInputStream().Consume()
fmt.Println("Done consuming")
}
var hasListener = p._parseListeners != nil && len(p._parseListeners) > 0
if p.BuildParseTrees || hasListener {

View File

@ -98,15 +98,6 @@ func (this *Token) GetSource() *TokenSourceCharStreamPair{
return this.source
}
func (this *Token) GetText() string {
return this._text
}
func (this *Token) SetText(s string) {
this._text = s
}
func (this *Token) GetTokenIndex() int {
return this.tokenIndex
}

View File

@ -818,6 +818,7 @@ ArithmeticParser.prototype.number = function() {
do {
this.state = 61;
this.match(ArithmeticParser.DIGIT);
console.log("Done with match")
this.state = 64;
this._errHandler.sync(this);
_la = this._input.LA(1);

View File

@ -97,6 +97,7 @@ BufferedTokenStream.prototype.mark = function() {
};
BufferedTokenStream.prototype.release = function(marker) {
console.log("Nothing to release")
// no resources to release
};
@ -129,10 +130,12 @@ BufferedTokenStream.prototype.consume = function() {
// not yet initialized
skipEofCheck = false;
}
console.log("consume 1")
if (!skipEofCheck && this.LA(1) === Token.EOF) {
throw "cannot consume EOF";
}
if (this.sync(this.index + 1)) {
console.log("consume 2")
this.index = this.adjustSeekIndex(this.index + 1);
}
};
@ -147,6 +150,10 @@ BufferedTokenStream.prototype.sync = function(i) {
var n = i - this.tokens.length + 1; // how many more elements we need?
if (n > 0) {
var fetched = this.fetch(n);
var e = new Error();
console.log("sync done")
console.log(e.stack)
return fetched >= n;
}
return true;
@ -162,6 +169,7 @@ BufferedTokenStream.prototype.fetch = function(n) {
}
for (var i = 0; i < n; i++) {
var t = this.tokenSource.nextToken();
console.log("fetch loop")
t.tokenIndex = this.tokens.length;
this.tokens.push(t);
if (t.type === Token.EOF) {
@ -169,6 +177,8 @@ BufferedTokenStream.prototype.fetch = function(n) {
return i + 1;
}
}
console.log("fetch done")
return n;
};

View File

@ -101,6 +101,7 @@ InputStream.prototype.mark = function() {
};
InputStream.prototype.release = function(marker) {
console.log("RELEASING")
};
// consume() ahead until p==_index; can't just set p=_index as we must

View File

@ -168,7 +168,12 @@ Lexer.prototype.nextToken = function() {
if (this._type !== Lexer.MORE) {
break;
}
console.log("lex inner loop")
}
console.log("lex loop")
if (continueOuter) {
continue;
}
@ -257,6 +262,7 @@ Lexer.prototype.emitToken = function(token) {
// custom Token objects or provide a new factory.
// /
Lexer.prototype.emit = function() {
console.log("emit")
var t = this._factory.create(this._tokenFactorySourcePair, this._type,
this._text, this._channel, this._tokenStartCharIndex, this
.getCharIndex() - 1, this._tokenStartLine,
@ -266,6 +272,7 @@ Lexer.prototype.emit = function() {
};
Lexer.prototype.emitEOF = function() {
console.log("emitEOF")
var cpos = this.column;
var lpos = this.line;
var eof = this._factory.create(this._tokenFactorySourcePair, Token.EOF,

View File

@ -133,12 +133,15 @@ Parser.prototype.reset = function() {
// mismatched symbol
Parser.prototype.match = function(ttype) {
console.log("get current token")
var t = this.getCurrentToken();
console.log("TOKEN IS " + t.text)
if (t.type === ttype) {
this._errHandler.reportMatch(this);
this.consume();
console.log("consume done")
} else {
t = this._errHandler.recoverInline(this);
if (this.buildParseTrees && t.tokenIndex === -1) {
@ -148,6 +151,9 @@ Parser.prototype.match = function(ttype) {
this._ctx.addErrorNode(t);
}
}
console.log("Match done")
return t;
};
// Match current input symbol as a wildcard. If the symbol type matches
@ -400,8 +406,11 @@ Parser.prototype.notifyErrorListeners = function(msg, offendingToken, err) {
Parser.prototype.consume = function() {
var o = this.getCurrentToken();
if (o.type !== Token.EOF) {
console.log("Consuming")
this.getInputStream().consume();
console.log("done consuming")
}
var hasListener = this._parseListeners !== null && this._parseListeners.length > 0;
if (this.buildParseTrees || hasListener) {
var node;

View File

@ -128,11 +128,16 @@ LexerATNSimulator.prototype.match = function(input, mode) {
this.prevAccept.reset();
var dfa = this.decisionToDFA[mode];
if (dfa.s0 === null) {
console.log("matchATN")
return this.matchATN(input);
} else {
return this.execATN(input, dfa.s0);
console.log("execATN")
console.log((new Error()).stack)
var res = this.execATN(input, dfa.s0);
return res;
}
} finally {
console.log("FINALLY")
input.release(mark);
}
};
@ -229,8 +234,10 @@ LexerATNSimulator.prototype.execATN = function(input, ds0) {
s = target; // flip; current DFA target becomes new src/from state
}
console.log("OUT")
return this.failOrAccept(this.prevAccept, input, s.configs, t);
console.log("Done with execATN loop")
var res = this.failOrAccept(this.prevAccept, input, s.configs, t);
console.log("Done with failOrAccept", res)
return res;
};
// Get an existing target state for an edge in the DFA. If the target state
@ -287,11 +294,15 @@ LexerATNSimulator.prototype.computeTargetState = function(input, s, t) {
};
LexerATNSimulator.prototype.failOrAccept = function(prevAccept, input, reach, t) {
if (this.prevAccept.dfaState !== null) {
var lexerActionExecutor = prevAccept.dfaState.lexerActionExecutor;
this.accept(input, lexerActionExecutor, this.startIndex,
prevAccept.index, prevAccept.line, prevAccept.column);
console.log(prevAccept.dfaState.prediction)
console.log("Prevaccept", prevAccept.dfaState.prediction)
return prevAccept.dfaState.prediction;
} else {
// if no accept and EOF is first char, return EOF
@ -341,8 +352,7 @@ LexerATNSimulator.prototype.getReachableConfigSet = function(input, closure,
}
};
LexerATNSimulator.prototype.accept = function(input, lexerActionExecutor,
startIndex, index, line, charPos) {
LexerATNSimulator.prototype.accept = function(input, lexerActionExecutor, startIndex, index, line, charPos) {
if (this.debug) {
console.log("ACTION %s", lexerActionExecutor);
}

View File

@ -573,6 +573,9 @@ DefaultErrorStrategy.prototype.getMissingSymbol = function(recognizer) {
if (current.type===Token.EOF && lookback !== null) {
current = lookback;
}
fmt.Println("Missing symbol error")
return recognizer.getTokenFactory().create(current.source,
expectedTokenType, tokenText, Token.DEFAULT_CHANNEL,
-1, -1, current.line, current.column);