This commit is contained in:
Peter Boyer 2015-12-18 10:44:02 -05:00
parent 40890f2122
commit 54206cd853
8 changed files with 185 additions and 112 deletions

View File

@ -1,8 +1,5 @@
package antlr4
import (
)
type ATNSimulator struct {
atn *ATN
sharedContextCache *PredictionContextCache

View File

@ -38,11 +38,11 @@ func NewCommonTokenStream(lexer *Lexer, channel int) {
}
func (ts *CommonTokenStream) adjustSeekIndex(i int) {
func (ts *CommonTokenStream) adjustSeekIndex(i int) int {
return ts.nextTokenOnChannel(i, ts.channel)
}
func (ts *CommonTokenStream) LB(k int) {
func (ts *CommonTokenStream) LB(k int) Token {
if (k==0 || ts.index-k<0) {
return nil
}
@ -60,7 +60,7 @@ func (ts *CommonTokenStream) LB(k int) {
return ts.tokens[i]
}
func (ts *CommonTokenStream) LT(k int) {
func (ts *CommonTokenStream) LT(k int) Token {
ts.lazyInit()
if (k == 0) {
return nil
@ -82,7 +82,7 @@ func (ts *CommonTokenStream) LT(k int) {
}
// Count EOF just once.///
func (ts *CommonTokenStream) getNumberOfOnChannelTokens() {
func (ts *CommonTokenStream) getNumberOfOnChannelTokens() int {
var n = 0
ts.fill()
for i := 0; i < len(ts.tokens); i++ {

View File

@ -110,7 +110,7 @@ func (this *DFA) setPrecedenceDfa(precedenceDfa bool) {
this._states = NewDFAStatesSet()
if (precedenceDfa) {
var precedenceState = NewDFAState(NewATNConfigSet(false))
precedenceState.edges = []
precedenceState.edges = make([]*DFAState)
precedenceState.isAcceptState = false
precedenceState.requiresFullContext = false
this.s0 = precedenceState

View File

@ -9,47 +9,57 @@ import (
// in the input, where it is in the ATN, the rule invocation stack,
// and what kind of problem occurred.
//var PredicateTransition = require('./../atn/Transition').PredicateTransition
type RecognitionException struct {
message string
recognizer Recognizer
recognizer *Recognizer
offendingToken *Token
offendingState int
ctx *RuleContext
input *InputStream
}
func RecognitionException(params) {
Error.call(this)
func NewRecognitionException(message string, recognizer *Recognizer, input *InputStream, ctx *RuleContext) *RecognitionException {
if (!!Error.captureStackTrace) {
Error.captureStackTrace(this, RecognitionException)
} else {
var stack = NewError().stack
// todo
// Error.call(this)
//
// if (!!Error.captureStackTrace) {
// Error.captureStackTrace(this, RecognitionException)
// } else {
// var stack = NewError().stack
// }
// TODO may be able to use - "runtime" func Stack(buf []byte, all bool) int
t := new(RecognitionException)
t.initRecognitionException(message, recognizer, input, ctx)
return t
}
func (t *RecognitionException) initRecognitionException(message string, recognizer *Recognizer, input *InputStream, ctx *RuleContext){
t.message = message
t.recognizer = recognizer
t.input = input
t.ctx = ctx
// The current {@link Token} when an error occurred. Since not all streams
// support accessing symbols by index, we have to track the {@link Token}
// instance itself.
t.offendingToken = nil
// Get the ATN state number the parser was in at the time the error
// occurred. For {@link NoViableAltException} and
// {@link LexerNoViableAltException} exceptions, this is the
// {@link DecisionState} number. For others, it is the state whose outgoing
// edge we couldn't match.
t.offendingState = -1
if (t.recognizer!=nil) {
t.offendingState = t.recognizer.state
}
this.message = params.message
this.recognizer = params.recognizer
this.input = params.input
this.ctx = params.ctx
// The current {@link Token} when an error occurred. Since not all streams
// support accessing symbols by index, we have to track the {@link Token}
// instance itself.
this.offendingToken = nil
// Get the ATN state number the parser was in at the time the error
// occurred. For {@link NoViableAltException} and
// {@link LexerNoViableAltException} exceptions, this is the
// {@link DecisionState} number. For others, it is the state whose outgoing
// edge we couldn't match.
this.offendingState = -1
if (this.recognizer!=nil) {
this.offendingState = this.recognizer.state
}
return this
}
//RecognitionException.prototype = Object.create(Error.prototype)
//RecognitionException.prototype.constructor = RecognitionException
// <p>If the state number is not known, this method returns -1.</p>
//
@ -64,7 +74,7 @@ func RecognitionException(params) {
// /
func (this *RecognitionException) getExpectedTokens() {
if (this.recognizer!=nil) {
return this.recognizer.atn.getExpectedTokens(this.offendingState, this.ctx)
return this.recognizer.getATN().getExpectedTokens(this.offendingState, this.ctx)
} else {
return nil
}
@ -74,35 +84,76 @@ func (this *RecognitionException) toString() string {
return this.message
}
func LexerNoViableAltException(lexer, input, startIndex, deadEndConfigs) {
RecognitionException.call(this, {message:"", recognizer:lexer, input:input, ctx:nil})
this.startIndex = startIndex
this.deadEndConfigs = deadEndConfigs
return this
type LexerNoViableAltException struct {
RecognitionException
startIndex int
deadEndConfigs *ATNConfigSet
}
//LexerNoViableAltException.prototype = Object.create(RecognitionException.prototype)
//LexerNoViableAltException.prototype.constructor = LexerNoViableAltException
func NewLexerNoViableAltException(lexer *Lexer, input *InputStream, startIndex int,
deadEndConfigs *ATNConfigSet) *LexerNoViableAltException {
this := new (LexerNoViableAltException)
this.initRecognitionException("", lexer, input, nil)
this.startIndex = startIndex
this.deadEndConfigs = deadEndConfigs
return this
}
func (this *LexerNoViableAltException) toString() string {
var symbol = ""
if (this.startIndex >= 0 && this.startIndex < this.input.size) {
symbol = this.input.getText((this.startIndex,this.startIndex))
symbol = this.input.getText(this.startIndex,this.startIndex)
}
return "LexerNoViableAltException" + symbol
}
type NoViableAltException struct {
RecognitionException
startToken *Token
offendingToken *Token
ctx *ParserRuleContext
deadEndConfigs *ATNConfigSet
}
// Indicates 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 NoViableAltException(recognizer, input, startToken, offendingToken, deadEndConfigs, ctx) {
ctx = ctx || recognizer._ctx
offendingToken = offendingToken || recognizer.getCurrentToken()
startToken = startToken || recognizer.getCurrentToken()
input = input || recognizer.getInputStream()
RecognitionException.call(this, {message:"", recognizer:recognizer, input:input, ctx:ctx})
func NoViableAltException(recognizer *Parser, input *InputStream, startToken *Token,
offendingToken *Token, deadEndConfigs *ATNConfigSet, ctx *ParserRuleContext) *NoViableAltException {
if (ctx == nil){
ctx = recognizer._ctx
}
if (offendingToken == nil){
offendingToken = recognizer.getCurrentToken()
}
if (startToken == nil){
startToken = recognizer.getCurrentToken()
}
if (input == nil){
input = recognizer.getInputStream()
}
this := new(NoViableAltException)
this.initRecognitionException("", recognizer, input, ctx)
// Which configurations did we try at input.index() that couldn't match
// input.LT(1)?//
this.deadEndConfigs = deadEndConfigs
@ -112,48 +163,67 @@ func NoViableAltException(recognizer, input, startToken, offendingToken, deadEnd
// buffer all of the tokens but later we might not have access to those.)
this.startToken = startToken
this.offendingToken = offendingToken
return this
}
//NoViableAltException.prototype = Object.create(RecognitionException.prototype)
//NoViableAltException.prototype.constructor = NoViableAltException
type InputMismatchException struct {
RecognitionException
}
// This signifies any kind of mismatched input exceptions such as
// when the current input does not match the expected token.
//
func InputMismatchException(recognizer) {
RecognitionException.call(this, {message:"", recognizer:recognizer, input:recognizer.getInputStream(), ctx:recognizer._ctx})
this.offendingToken = recognizer.getCurrentToken()
}
func NewInputMismatchException(recognizer *Parser) *InputMismatchException {
//InputMismatchException.prototype = Object.create(RecognitionException.prototype)
//InputMismatchException.prototype.constructor = InputMismatchException
this := new(InputMismatchException)
this.initRecognitionException("", recognizer, recognizer.getInputStream(), recognizer._ctx)
this.offendingToken = recognizer.getCurrentToken()
return this
}
// 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.
func FailedPredicateException(recognizer, predicate, message) {
RecognitionException.call(this, {message:this.formatMessage(predicate,message || nil), recognizer:recognizer,
input:recognizer.getInputStream(), ctx:recognizer._ctx})
type FailedPredicateException struct {
RecognitionException
ruleIndex int
predicateIndex int
predicate string
}
func NewFailedPredicateException(recognizer *Parser, predicate string, message string) *FailedPredicateException {
this := new(FailedPredicateException)
this.initRecognitionException(this.formatMessage(predicate, message), recognizer, recognizer.getInputStream(), recognizer._ctx)
var s = recognizer._interp.atn.states[recognizer.state]
var trans = s.transitions[0]
if _, ok := trans.(PredicateTransition); ok {
this.ruleIndex = trans.ruleIndex
this.predicateIndex = trans.predIndex
if trans2, ok := trans.(PredicateTransition); ok {
this.ruleIndex = trans2.ruleIndex
this.predicateIndex = trans2.predIndex
} else {
this.ruleIndex = 0
this.predicateIndex = 0
}
this.predicate = predicate
this.offendingToken = recognizer.getCurrentToken()
return this
}
//FailedPredicateException.prototype = Object.create(RecognitionException.prototype)
//FailedPredicateException.prototype.constructor = FailedPredicateException
func (this *FailedPredicateException) formatMessage(predicate, message) {
func (this *FailedPredicateException) formatMessage(predicate, message string) string {
if (message !=nil) {
return message
} else {
@ -162,16 +232,14 @@ func (this *FailedPredicateException) formatMessage(predicate, message) {
}
type ParseCancellationException struct {
Error.call(this)
Error.captureStackTrace(this, ParseCancellationException)
return this
}
//ParseCancellationException.prototype = Object.create(Error.prototype)
//ParseCancellationException.prototype.constructor = ParseCancellationException
func NewParseCancellationException() *ParseCancellationException {
// Error.call(this)
// Error.captureStackTrace(this, ParseCancellationException)
return new(ParseCancellationException)
}

View File

@ -267,6 +267,10 @@ func (p *Parser) triggerExitRuleEvent() {
}
}
func (this *Parser) getATN() *ATN {
return this.atn
}
func (p *Parser) getTokenFactory() {
return p._input.getTokenSource()._factory
}

View File

@ -49,6 +49,10 @@ func (this *Recognizer) getTokenNames() []string {
return nil
}
func (this *Recognizer) getATN() *ATN {
return this._interp.atn
}
//func (this *Recognizer) getTokenTypeMap() {
// var tokenNames = this.getTokenNames()
// if (tokenNames==nil) {

View File

@ -102,7 +102,7 @@ LexerNoViableAltException.prototype.constructor = LexerNoViableAltException;
LexerNoViableAltException.prototype.toString = function() {
var symbol = "";
if (this.startIndex >= 0 && this.startIndex < this.input.size) {
symbol = this.input.getText((this.startIndex,this.startIndex));
symbol = this.input.getText(this.startIndex,this.startIndex);
}
return "LexerNoViableAltException" + symbol;
};

View File

@ -40,16 +40,16 @@ import(
// This class defines a complete listener for a parse tree produced by <file.parserName>.
type <file.grammarName>Listener struct {
tree.ParseTreeListener
ParseTreeListener
}
<file.listenerNames:{lname |
// Enter a parse tree produced by <file.parserName>#<lname>.
func (l *<file.grammarName>Listener) enter<lname; format="cap">(ctx *antlr4.ParserRuleContext) {
func (l *<file.grammarName>Listener) enter<lname; format="cap">(ctx *ParserRuleContext) {
\}
// Exit a parse tree produced by <file.parserName>#<lname>.
func (l *<file.grammarName>Listener) exit<lname; format="cap">(ctx *antlr4.ParserRuleContext) {
func (l *<file.grammarName>Listener) exit<lname; format="cap">(ctx *ParserRuleContext) {
\}
}; separator="\n">
@ -70,12 +70,12 @@ import(
// This class defines a complete generic visitor for a parse tree produced by <file.parserName>.
type <file.grammarName>Visitor struct {
tree.ParseTreeVisitor
ParseTreeVisitor
}
<file.visitorNames:{lname |
// Visit a parse tree produced by <file.parserName>#<lname>.
func (l <file.grammarName>Visitor) visit<lname; format="cap">(ctx *antlr4.ParserRuleContext) {
func (l <file.grammarName>Visitor) visit<lname; format="cap">(ctx *ParserRuleContext) {
\}
}; separator="\n">
@ -92,8 +92,8 @@ var <superClass> = require('./<superClass>').<superClass> // TODO
<atn>
type <parser.name> struct {
<superClass; null="antlr4.Parser">
_interp *atn.ParserATNSimulator
<superClass; null="Parser">
_interp *ParserATNSimulator
ruleNames []string
literalNames []string
symbolicNames []string
@ -104,7 +104,7 @@ func New<parser.name>(input) <parser.name> {
// TODO could be package level variable
var deserializer = atn.NewATNDeserializer()
var deserializer = NewATNDeserializer()
var deserializedAtn = deserializer.deserialize(serializedATN)
var decisionToDFA = make([]dfa.DFA,len(deserializedAtn.decisionToState))
@ -112,7 +112,7 @@ func New<parser.name>(input) <parser.name> {
decisionToDFA[index] = dfa.NewDFA(ds, index)
}
var sharedContextCache = antlr4.NewPredictionContextCache()
var sharedContextCache = NewPredictionContextCache()
var literalNames = [...]string{ <parser.literalNames:{t | <t>}; null="nil", separator=", ", wrap, anchor> }
var symbolicNames = [...]string{ <parser.symbolicNames:{t | <t>}; null="nil", separator=", ", wrap, anchor> }
@ -121,7 +121,7 @@ func New<parser.name>(input) <parser.name> {
// init the parser
parser := new(<parser.name>)
parser._interp = atn.NewParserATNSimulator(parser, atn, decisionToDFA, sharedContextCache)
parser._interp = NewParserATNSimulator(parser, atn, decisionToDFA, sharedContextCache)
parser.ruleNames = ruleNames
parser.literalNames = literalNames
parser.symbolicNames = symbolicNames
@ -132,7 +132,7 @@ func New<parser.name>(input) <parser.name> {
}
const(
<parser.name>EOF = antlr4.TokenEOF
<parser.name>EOF = TokenEOF
<if(parser.tokens)>
<parser.tokens:{k | <parser.name><k> = <parser.tokens.(k)>}; separator="\n", wrap, anchor>
<endif>
@ -407,7 +407,7 @@ StarBlock(choice, alts, sync, iteration) ::= <<
p.state = <choice.stateNumber>
p._errHandler.sync(p)
_alt := p._interp.adaptivePredict(p._input,<choice.decision>,p._ctx)
for _alt!=<choice.exitAlt> && _alt!= atn.ATN.INVALID_ALT_NUMBER {
for _alt!=<choice.exitAlt> && _alt!= ATNINVALID_ALT_NUMBER {
if(_alt==1<if(!choice.ast.greedy)>+1<endif>) {
<iteration>
<alts> <! should only be one !>
@ -423,7 +423,7 @@ PlusBlock(choice, alts, error) ::= <<
p.state = <choice.blockStartStateNumber>; <! alt block decision !>
p._errHandler.sync(p)
_alt := 1<if(!choice.ast.greedy)>+1<endif>
for ok := true; ok; ok = _alt!=<choice.exitAlt> && _alt!= atn.ATN.INVALID_ALT_NUMBER {
for ok := true; ok; ok = _alt!=<choice.exitAlt> && _alt!= ATNINVALID_ALT_NUMBER {
switch _alt) {
<alts:{alt|
case <i><if(!choice.ast.greedy)>+1<endif>:
@ -563,13 +563,13 @@ TokenPropertyRef_int(t) ::= "(<ctx(t)>.<t.label> == null ? 0 : parseInt(<ctx(t)>
RulePropertyRef_start(r) ::= "(<ctx(r)>.<r.label>==null ? null : <ctx(r)>.<r.label>.start)"
RulePropertyRef_stop(r) ::= "(<ctx(r)>.<r.label>==null ? null : <ctx(r)>.<r.label>.stop)"
RulePropertyRef_text(r) ::= "(<ctx(r)>.<r.label>==null ? null : p._input.getText(new antlr4.Interval(<ctx(r)>.<r.label>.start,<ctx(r)>.<r.label>.stop)))"
RulePropertyRef_text(r) ::= "(<ctx(r)>.<r.label>==null ? null : p._input.getText(new Interval(<ctx(r)>.<r.label>.start,<ctx(r)>.<r.label>.stop)))"
RulePropertyRef_ctx(r) ::= "<ctx(r)>.<r.label>"
RulePropertyRef_parser(r) ::= "this"
ThisRulePropertyRef_start(r) ::= "localctx.start"
ThisRulePropertyRef_stop(r) ::= "localctx.stop"
ThisRulePropertyRef_text(r) ::= "p._input.getText(new antlr4.Interval(localctx.start, p._input.LT(-1)))"
ThisRulePropertyRef_text(r) ::= "p._input.getText(new Interval(localctx.start, p._input.LT(-1)))"
ThisRulePropertyRef_ctx(r) ::= "localctx"
ThisRulePropertyRef_parser(r) ::= "p"
@ -649,16 +649,16 @@ CaptureNextToken(d) ::= "<d.varName> = p._input.LT(1)"
CaptureNextTokenType(d) ::= "<d.varName> = p._input.LA(1);"
StructDecl(struct,ctorAttrs,attrs,getters,dispatchMethods,interfaces,extensionMembers,
superClass={antlr4.ParserRuleContext}) ::= <<
superClass={ParserRuleContext}) ::= <<
type <struct.name> struct {
antlr4.ParserRuleContext
parent *antlr4.ParserRuleContext
parser *antlr4.Parser
ParserRuleContext
parent *ParserRuleContext
parser *Parser
ruleIndex
}
func New<struct.name>(parser *antlr4.Parser, parent *antlr4.ParserRuleContext, invokingState int<struct.ctorAttrs:{a | , <a.name>}>) <struct.name> {
func New<struct.name>(parser *Parser, parent *ParserRuleContext, invokingState int<struct.ctorAttrs:{a | , <a.name>}>) <struct.name> {
var p = new(<struct.name>)
p.initParserRuleContext( parent, invokingState )
@ -686,12 +686,12 @@ func (s *<struct.name>) copyFrom(ctx <struct.name>) {
AltLabelStructDecl(struct,attrs,getters,dispatchMethods) ::= <<
type <struct.name> struct {
parent *antlr4.ParserRuleContext
parser *antlr4.Parser
parent *ParserRuleContext
parser *Parser
ruleIndex int
}
func New<struct.name>(parser *antlr4.Parser, ctx *antlr4.ParserRuleContext) <struct.name> {
func New<struct.name>(parser *Parser, ctx *ParserRuleContext) <struct.name> {
var p = new(<struct.name>)
@ -711,7 +711,7 @@ func New<struct.name>(parser *antlr4.Parser, ctx *antlr4.ParserRuleContext) <str
ListenerDispatchMethod(method) ::= <<
func (s *<struct.name>) <if(method.isEnter)>enter<else>exit<endif>Rule(listener *tree.ParseTreeListener) {
func (s *<struct.name>) <if(method.isEnter)>enter<else>exit<endif>Rule(listener *ParseTreeListener) {
// TODO
switch t := listener.(type) {
case *<parser.grammarName>Listener:
@ -723,7 +723,7 @@ func (s *<struct.name>) <if(method.isEnter)>enter<else>exit<endif>Rule(listener
VisitorDispatchMethod(method) ::= <<
func (s *<struct.name>) accept(visitor *tree.ParseTreeVisitor) {
func (s *<struct.name>) accept(visitor *ParseTreeVisitor) {
switch t := listener.(type) {
case *<parser.grammarName>Listener:
@ -803,8 +803,8 @@ Lexer(lexer, atn, actionFuncs, sempredFuncs, superClass) ::= <<
<atn>
type <lexer.name> struct {
<if(superClass)><superClass><else>antlr4.Lexer<endif>
_interp *atn.LexerATNSimulator
<if(superClass)><superClass><else>Lexer<endif>
_interp *LexerATNSimulator
modeNames []string
literalNames []string
symbolicNames []string
@ -813,11 +813,11 @@ type <lexer.name> struct {
EOF string
}
func New<lexer.name>(input *antlr4.TokenStream) *<lexer.name> {
func New<lexer.name>(input *TokenStream) *<lexer.name> {
// TODO could be package level variables
var deserializer = atn.NewATNDeserializer()
var deserializer = NewATNDeserializer()
var deserializedAtn = deserializer.deserialize(serializedATN)
var decisionToDFA = make([]dfa.DFA,len(deserializedAtn.decisionToState))
@ -828,15 +828,15 @@ func New<lexer.name>(input *antlr4.TokenStream) *<lexer.name> {
lex := new(<lexer.name>)
antlr4.initLexer(lex, input);
initLexer(lex, input);
lex._interp = atn.NewLexerATNSimulator(lex, atn, decisionToDFA, antlr4.NewPredictionContextCache())
lex._interp = NewLexerATNSimulator(lex, atn, decisionToDFA, NewPredictionContextCache())
lex.modeNames = [...]string{ <lexer.modes:{m| "<m>"}; separator=", ", wrap, anchor> }
lex.literalNames = [...]string{ <lexer.literalNames:{t | <t>}; null="nil", separator=", ", wrap, anchor> }
lex.symbolicNames = [...]string{ <lexer.symbolicNames:{t | <t>}; null="nil", separator=", ", wrap, anchor> }
lex.ruleNames = [...]string{ <lexer.ruleNames:{r | "<r>"}; separator=", ", wrap, anchor> }
lex.grammarFileName = "<lexer.grammarFileName>"
lex.EOF = antlr4.TokenEOF
lex.EOF = TokenEOF
return lex
}