Merge pull request #1221 from ericvergnaud/javascript-consistency

Following #1218, ensure consistency of ATNConfigSet hashing strategy …
This commit is contained in:
Terence Parr 2016-10-13 14:05:36 -07:00 committed by GitHub
commit 8d0ae8599e
2 changed files with 29 additions and 13 deletions

View File

@ -96,10 +96,16 @@ ATNConfig.prototype.checkContext = function(params, config) {
}
};
ATNConfig.prototype.hashString = function() {
return "" + this.state.stateNumber + "/" + this.alt + "/" +
(this.context===null ? "" : this.context.hashString()) +
"/" + this.semanticContext.hashString();
};
// An ATN configuration is equal to another if both have
// the same state, they predict the same alternative, and
// syntactic/semantic contexts are the same.
///
ATNConfig.prototype.equals = function(other) {
if (this === other) {
return true;
@ -114,16 +120,23 @@ ATNConfig.prototype.equals = function(other) {
}
};
ATNConfig.prototype.shortHashString = function() {
ATNConfig.prototype.hashStringForConfigSet = function() {
return "" + this.state.stateNumber + "/" + this.alt + "/" + this.semanticContext;
};
ATNConfig.prototype.hashString = function() {
return "" + this.state.stateNumber + "/" + this.alt + "/" +
(this.context===null ? "" : this.context.hashString()) +
"/" + this.semanticContext.hashString();
ATNConfig.prototype.equalsForConfigSet = function(other) {
if (this === other) {
return true;
} else if (! (other instanceof ATNConfig)) {
return false;
} else {
return this.state.stateNumber===other.state.stateNumber &&
this.alt===other.alt &&
this.semanticContext.equals(other.semanticContext);
}
};
ATNConfig.prototype.toString = function() {
return "(" + this.state + "," + this.alt +
(this.context!==null ? ",[" + this.context.toString() + "]" : "") +
@ -171,6 +184,11 @@ LexerATNConfig.prototype.equals = function(other) {
}
};
LexerATNConfig.prototype.hashStringForConfigSet = LexerATNConfig.prototype.hashString;
LexerATNConfig.prototype.equalsForConfigSet = LexerATNConfig.prototype.eqials;
LexerATNConfig.prototype.checkNonGreedyDecision = function(source, target) {
return source.passedThroughNonGreedyDecision ||
(target instanceof DecisionState) && target.nonGreedy;

View File

@ -41,19 +41,17 @@ var SemanticContext = require('./SemanticContext').SemanticContext;
var merge = require('./../PredictionContext').merge;
function hashATNConfig(c) {
return c.shortHashString();
return c.hashStringForConfigSet();
}
function equalATNConfigs(a, b) {
if ( a===b ) {
return true;
}
if ( a===null || b===null ) {
} else if ( a===null || b===null ) {
return false;
}
return a.state.stateNumber===b.state.stateNumber &&
a.alt===b.alt && a.semanticContext.equals(b.semanticContext);
}
} else
return a.equalsForConfigSet(b);
}
function ATNConfigSet(fullCtx) {