Fixes to Go runtime, tool

This commit is contained in:
Peter Boyer 2016-01-21 19:31:30 -05:00
parent 92b8f9f891
commit 89f1192cb6
17 changed files with 145 additions and 62 deletions

View File

@ -72,7 +72,7 @@ func (this *ATN) NextTokensInContext(s ATNState, ctx RuleContext) *IntervalSet {
func (this *ATN) NextTokensNoContext(s ATNState) *IntervalSet {
if s.GetNextTokenWithinRule() != nil {
if PortDebug {
fmt.Println("DEBUG 1")
fmt.Println("DEBUG A")
}
return s.GetNextTokenWithinRule()
}

View File

@ -2,7 +2,7 @@ package antlr4
import (
"fmt"
"reflect"
// "reflect"
"strconv"
)
@ -14,8 +14,13 @@ import (
// an ATN state.
//
type Comparable interface {
equals(other interface{}) bool
}
type ATNConfig interface {
Hasher
Comparable
getPrecedenceFilterSuppressed() bool
setPrecedenceFilterSuppressed(bool)
@ -144,14 +149,30 @@ func (this *BaseATNConfig) SetReachesIntoOuterContext(v int) {
// the same state, they predict the same alternative, and
// syntactic/semantic contexts are the same.
///
func (this *BaseATNConfig) equals(other interface{}) bool {
if this == other {
func (this *BaseATNConfig) equals(o interface{}) bool {
if this == o {
return true
} else if _, ok := other.(*BaseATNConfig); !ok {
return false
} else {
return reflect.DeepEqual(this, other)
}
other, ok := o.(*BaseATNConfig)
if !ok {
return false
}
var b bool
if this.context==nil {
b = other.context==nil
} else {
b = this.context.equals(other.context)
}
return this.state.GetStateNumber() == other.state.GetStateNumber() &&
this.alt==other.alt &&
this.semanticContext.equals(other.semanticContext) &&
this.precedenceFilterSuppressed==other.precedenceFilterSuppressed &&
b;
}
func (this *BaseATNConfig) shortHash() string {
@ -295,8 +316,7 @@ func (this *LexerATNConfig) equals(other interface{}) bool {
if b {
return false
} else {
panic("Not implemented")
// return ATNConfig.prototype.equals.call(this, other)
return this.BaseATNConfig.equals(othert.BaseATNConfig)
}
}

View File

@ -660,6 +660,10 @@ func (this *ParserATNSimulator) removeAllConfigsNotInRuleStopState(configs ATNCo
}
func (this *ParserATNSimulator) computeStartState(p ATNState, ctx RuleContext, fullCtx bool) ATNConfigSet {
if PortDebug {
fmt.Println("computeStartState")
}
// always at least the implicit call to start rule
var initialContext = predictionContextFromRuleContext(this.atn, ctx)
var configs = NewBaseATNConfigSet(fullCtx)
@ -954,7 +958,7 @@ func (this *ParserATNSimulator) evalSemanticContext(predPredictions []*PredPredi
}
continue
}
fmt.Println(predPredictions)
var predicateEvaluationResult = pair.pred.evaluate(this.parser, outerContext)
if ParserATNSimulatorDebug || ParserATNSimulatorDFADebug {
fmt.Println("eval pred " + pair.String() + "=" + fmt.Sprint(predicateEvaluationResult))
@ -1002,7 +1006,7 @@ func (this *ParserATNSimulator) closureCheckingStopState(config ATNConfig, confi
// we have no context info, just chase follow links (if greedy)
if ParserATNSimulatorDebug {
if PortDebug {
fmt.Println("DEBUG 1")
fmt.Println("DEBUG B")
}
fmt.Println("FALLING off rule " + this.getRuleName(config.GetState().GetRuleIndex()))
}
@ -1057,7 +1061,7 @@ func (this *ParserATNSimulator) closure_(config ATNConfig, configs ATNConfigSet,
var c = this.getEpsilonTarget(config, t, continueCollecting, depth == 0, fullCtx, treatEofAsEpsilon)
if c != nil {
if PortDebug {
fmt.Println("DEBUG 1")
fmt.Println("DEBUG 1 ok")
}
if !t.getIsEpsilon() && closureBusy.add(c) != c {
// avoid infinite recursion for EOF* and EOF+
@ -1069,6 +1073,7 @@ func (this *ParserATNSimulator) closure_(config ATNConfig, configs ATNConfigSet,
if PortDebug {
fmt.Println("DEBUG 2")
fmt.Println(closureBusy)
}
// 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
@ -1094,7 +1099,7 @@ func (this *ParserATNSimulator) closure_(config ATNConfig, configs ATNConfigSet,
fmt.Println("DEBUG 4")
}
if t.(*EpsilonTransition).outermostPrecedenceReturn == this._dfa.atnStartState.GetRuleIndex() {
c.precedenceFilterSuppressed = true
c.setPrecedenceFilterSuppressed(true)
}
}
@ -1123,7 +1128,7 @@ func (this *ParserATNSimulator) getRuleName(index int) string {
}
}
func (this *ParserATNSimulator) getEpsilonTarget(config ATNConfig, t Transition, collectPredicates, inContext, fullCtx, treatEofAsEpsilon bool) *BaseATNConfig {
func (this *ParserATNSimulator) getEpsilonTarget(config ATNConfig, t Transition, collectPredicates, inContext, fullCtx, treatEofAsEpsilon bool) ATNConfig {
switch t.getSerializationType() {
case TransitionRULE:

View File

@ -14,9 +14,10 @@ import (
//
type SemanticContext interface {
Comparable
evaluate(parser Recognizer, outerContext RuleContext) bool
evalPrecedence(parser Recognizer, outerContext RuleContext) SemanticContext
equals(interface{}) bool
String() string
}
@ -382,7 +383,6 @@ func (this *OR) Hash() string {
// unordered.</p>
//
func (this *OR) evaluate(parser Recognizer, outerContext RuleContext) bool {
fmt.Println("HI")
for i := 0; i < len(this.opnds); i++ {
if this.opnds[i].evaluate(parser, outerContext) {
return true

View File

@ -72,6 +72,18 @@ func NewSet(hashFunction func(interface{}) string, equalsFunction func(interface
return s
}
func standardEqualsFunction(a interface{}, b interface{}) bool {
ac, oka := a.(Comparable)
bc, okb := b.(Comparable)
if !oka || !okb {
panic("Not Comparable")
}
return ac.equals(bc)
}
func standardHashFunction(a interface{}) string {
h, ok := a.(Hasher)
@ -79,7 +91,7 @@ func standardHashFunction(a interface{}) string {
return h.Hash()
}
return fmt.Sprint(a)
panic("Not Hasher")
}
//func getBytes(key interface{}) ([]byte, error) {
@ -102,9 +114,6 @@ func hashCode(s string) string {
return fmt.Sprint(h.Sum32())
}
func standardEqualsFunction(a interface{}, b interface{}) bool {
return standardHashFunction(a) == standardHashFunction(b)
}
func (this *Set) length() int {
return len(this.data)
@ -141,7 +150,6 @@ func (this *Set) contains(value interface{}) bool {
values := this.data[key]
if this.data[key] != nil {
for i := 0; i < len(values); i++ {
if this.equalsFunction(value, values[i]) {
return true

View File

@ -0,0 +1,32 @@
PORT_DEBUG = false
var antlr4 = require("./antlr4/index"),
tree = antlr4.tree
JSONLexer = require("./JSONLexer").JSONLexer,
JSONParser = require("./JSONParser").JSONParser,
JSONListener = require("./JSONListener").JSONListener;
var a = new antlr4.FileStream("foo.txt");
var l = new JSONLexer(a);
var s = new antlr4.CommonTokenStream(l, 0);
var p = new JSONParser(s);
p.buildParseTrees = true;
KeyPrinter = function() {
JSONListener.call(this); // inherit default listener
return this;
};
// inherit default listener
KeyPrinter.prototype = Object.create(JSONListener.prototype);
KeyPrinter.prototype.constructor = KeyPrinter;
// override default listener behavior
KeyPrinter.prototype.enterValue = function(ctx) {
console.log(ctx.start.text);
};
var tree = p.json();
var printer = new KeyPrinter();
antlr4.tree.ParseTreeWalker.DEFAULT.walk(printer, tree);

View File

@ -76,7 +76,7 @@ ATN.prototype.nextTokensInContext = function(s, ctx) {
ATN.prototype.nextTokensNoContext = function(s) {
if (s.nextTokenWithinRule !== null ) {
if (PORT_DEBUG) {
console.log("DEBUG 1")
console.log("DEBUG A")
}
return s.nextTokenWithinRule;
}

View File

@ -169,10 +169,6 @@ LexerATNSimulator.prototype.matchATN = function(input) {
var suppressEdge = s0_closure.hasSemanticContext;
s0_closure.hasSemanticContext = false;
if (PORT_DEBUG) {
console.log(s0_closure.toString())
}
var next = this.addDFAState(s0_closure);
if (!suppressEdge) {
this.decisionToDFA[this.mode].s0 = next;
@ -224,6 +220,7 @@ LexerATNSimulator.prototype.execATN = function(input, ds0) {
// if (PORT_DEBUG) {
// console.log(target)
// }
if (target === null) {
target = this.computeTargetState(input, s, t);
// print("Computed:" + str(target))
@ -316,7 +313,7 @@ LexerATNSimulator.prototype.failOrAccept = function(prevAccept, input, reach, t)
prevAccept.index, prevAccept.line, prevAccept.column);
if (PORT_DEBUG) {
console.log("Prevaccept", prevAccept.dfaState.prediction)
console.log(prevAccept.dfaState.prediction)
}
return prevAccept.dfaState.prediction;

View File

@ -318,7 +318,6 @@ ParserATNSimulator.prototype.debug_list_atn_decisions = false;
ParserATNSimulator.prototype.dfa_debug = false;
ParserATNSimulator.prototype.retry_debug = false;
ParserATNSimulator.prototype.reset = function() {
};
@ -903,6 +902,11 @@ ParserATNSimulator.prototype.removeAllConfigsNotInRuleStopState = function(confi
};
ParserATNSimulator.prototype.computeStartState = function(p, ctx, fullCtx) {
if (PORT_DEBUG){
console.log("computeStartState")
}
// always at least the implicit call to start rule
var initialContext = predictionContextFromRuleContext(this.atn, ctx);
var configs = new ATNConfigSet(fullCtx);
@ -1257,7 +1261,7 @@ ParserATNSimulator.prototype.closureCheckingStopState = function(config, configs
// we have no context info, just chase follow links (if greedy)
if (this.debug) {
if (PORT_DEBUG) {
console.log("DEBUG 1")
console.log("DEBUG B")
}
console.log("FALLING off rule " + this.getRuleName(config.state.ruleIndex));
}
@ -1312,7 +1316,7 @@ ParserATNSimulator.prototype.closure_ = function(config, configs, closureBusy, c
var c = this.getEpsilonTarget(config, t, continueCollecting, depth === 0, fullCtx, treatEofAsEpsilon);
if (c!==null) {
if (PORT_DEBUG) {
console.log("DEBUG 1")
console.log("DEBUG 1 ok")
}
if (!t.isEpsilon && closureBusy.add(c)!==c){
// avoid infinite recursion for EOF* and EOF+

View File

@ -0,0 +1 @@
a=b+1;

View File

@ -1,32 +1,43 @@
PORT_DEBUG = false
var antlr4 = require("./antlr4/index"),
tree = antlr4.tree
JSONLexer = require("./JSONLexer").JSONLexer,
JSONParser = require("./JSONParser").JSONParser,
JSONListener = require("./JSONListener").JSONListener;
var antlr4 = require('./antlr4/index');
var TLexer = require('./TLexer');
var TParser = require('./TParser');
var TListener = require('./TListener').TListener;
// var TVisitor = require('./parser/TVisitor').TVisitor;
var a = new antlr4.FileStream("foo.txt");
var l = new JSONLexer(a);
var s = new antlr4.CommonTokenStream(l, 0);
var p = new JSONParser(s);
p.buildParseTrees = true;
KeyPrinter = function() {
JSONListener.call(this); // inherit default listener
function TreeShapeListener() {
antlr4.tree.ParseTreeListener.call(this);
return this;
}
TreeShapeListener.prototype = Object.create(antlr4.tree.ParseTreeListener.prototype);
TreeShapeListener.prototype.constructor = TreeShapeListener;
TreeShapeListener.prototype.enterEveryRule = function(ctx) {
for(var i=0;i<ctx.getChildCount; i++) {
var child = ctx.getChild(i);
var parent = child.parentCtx;
if(parent.getRuleContext() !== ctx || !(parent instanceof antlr4.tree.RuleNode)) {
throw "Invalid parse tree shape detected.";
}
}
};
// inherit default listener
KeyPrinter.prototype = Object.create(JSONListener.prototype);
KeyPrinter.prototype.constructor = KeyPrinter;
function main(argv) {
var input = new antlr4.FileStream(argv[2]);
var lexer = new TLexer.TLexer(input);
var stream = new antlr4.CommonTokenStream(lexer);
var parser = new TParser.TParser(stream);
parser.buildParseTrees = true;
printer = function() {
this.println = function(s) { console.log(s); }
this.print = function(s) { process.stdout.write(s); }
return this;
};
parser.printer = new printer();
var tree = parser.program();
antlr4.tree.ParseTreeWalker.DEFAULT.walk(new TreeShapeListener(), tree);
}
// override default listener behavior
KeyPrinter.prototype.enterValue = function(ctx) {
console.log(ctx.start.text);
};
var tree = p.json();
var printer = new KeyPrinter();
antlr4.tree.ParseTreeWalker.DEFAULT.walk(printer, tree);
main(process.argv);

View File

@ -194,7 +194,8 @@ func (l *<lexer.name>) Action(localctx antlr4.RuleContext, ruleIndex, actionInde
l.<f.name>_Action(t, actionIndex)
<else>
l.<f.name>_Action(localctx, actionIndex)
<endif>}; separator="\n">
<endif>
break}; separator="\n">
default:
panic("No registered action for:" + fmt.Sprint(ruleIndex))
}
@ -233,7 +234,7 @@ func (l *<lexer.name>) <r.name; format="cap">_Action(localctx <if(r.isRuleContex
<actions:{index|
case <index>:
<actions.(index)>
}; separator="\n">
break}; separator="\n">
default:
panic("No registered action for:" + fmt.Sprint(actionIndex))
}
@ -361,7 +362,7 @@ p.SetState(<choice.stateNumber>)
switch p.GetTokenStream().LA(1) {
<choice.altLook,alts:{look,alt| <cases(ttypes=look)>
<alt>
}; separator="\n">
break }; separator="\n">
default:
<error>
}
@ -372,7 +373,7 @@ p.SetState(<choice.stateNumber>)
switch p.GetTokenStream().LA(1) {
<choice.altLook,alts:{look,alt| <cases(ttypes=look)>
<alt>
}; separator="\n">
break }; separator="\n">
default:
<error>
}
@ -423,6 +424,7 @@ switch la_ {
<alts:{alt |
case <i>:
<alt>
break
}; separator="\n">
}
>>
@ -463,7 +465,7 @@ for ok := true; ok; ok = _alt!=<choice.exitAlt> && _alt!= antlr4.ATNInvalidAltNu
<alts:{alt|
case <i><if(!choice.ast.greedy)>+1<endif>:
<alt>
//}; separator="\n">
break }; separator="\n">
default:
<error>
}
@ -510,7 +512,7 @@ bitsetInlineComparison(s, bits) ::= <%
%>
cases(ttypes) ::= <<
<ttypes:{t | case <parser.name><t>:}; separator="\n">
<ttypes:{t | case <parser.name><t>:}; separator="fallthrough \n">
>>
InvokeRule(r, argExprsChunks) ::= <<
@ -709,6 +711,7 @@ type I<struct.name> interface {
antlr4.ParserRuleContext
GetParser() antlr4.Parser
get<struct.name>() // to differentiate from other interfaces
<struct.tokenDecls:{a | Get<a.name; format="cap">() <TokenLabelType()> }; separator="\n">
<struct.tokenDecls:{a | Set<a.name; format="cap">(<TokenLabelType()>) }; separator="\n">
@ -738,6 +741,8 @@ func NewEmpty<struct.name>() *<struct.name> {
return p
}
func (*<struct.name>) get<struct.name>() {}
func New<struct.name>(parser antlr4.Parser, parent antlr4.ParserRuleContext, invokingState int<struct.ctorAttrs:{a | , <a.name> <a.type;format="lower">}>) *<struct.name> {
var p = new(<struct.name>)