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 // An ATN configuration is equal to another if both have
// the same state, they predict the same alternative, and // the same state, they predict the same alternative, and
// syntactic/semantic contexts are the same. // syntactic/semantic contexts are the same.
///
ATNConfig.prototype.equals = function(other) { ATNConfig.prototype.equals = function(other) {
if (this === other) { if (this === other) {
return true; 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; return "" + this.state.stateNumber + "/" + this.alt + "/" + this.semanticContext;
}; };
ATNConfig.prototype.hashString = function() { ATNConfig.prototype.equalsForConfigSet = function(other) {
return "" + this.state.stateNumber + "/" + this.alt + "/" + if (this === other) {
(this.context===null ? "" : this.context.hashString()) + return true;
"/" + this.semanticContext.hashString(); } 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() { ATNConfig.prototype.toString = function() {
return "(" + this.state + "," + this.alt + return "(" + this.state + "," + this.alt +
(this.context!==null ? ",[" + this.context.toString() + "]" : "") + (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) { LexerATNConfig.prototype.checkNonGreedyDecision = function(source, target) {
return source.passedThroughNonGreedyDecision || return source.passedThroughNonGreedyDecision ||
(target instanceof DecisionState) && target.nonGreedy; (target instanceof DecisionState) && target.nonGreedy;

View File

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