refactored LexerAction.js to use es 6 classes

use jsdoc
use module.exports
This commit is contained in:
Camilo Roca 2020-03-06 19:29:23 +01:00
parent 44331c067e
commit d61e4d5049
1 changed files with 327 additions and 309 deletions

View File

@ -1,366 +1,384 @@
//
/* Copyright (c) 2012-2017 The ANTLR Project. All rights reserved.
* Use of this file is governed by the BSD 3-clause license that
* can be found in the LICENSE.txt file in the project root.
*/
//
function LexerActionType() {
const LexerActionType = {
// The type of a {@link LexerChannelAction} action.
CHANNEL: 0,
// The type of a {@link LexerCustomAction} action
CUSTOM: 1,
// The type of a {@link LexerModeAction} action.
MODE: 2,
//The type of a {@link LexerMoreAction} action.
MORE: 3,
//The type of a {@link LexerPopModeAction} action.
POP_MODE: 4,
//The type of a {@link LexerPushModeAction} action.
PUSH_MODE: 5,
//The type of a {@link LexerSkipAction} action.
SKIP: 6,
//The type of a {@link LexerTypeAction} action.
TYPE: 7
}
LexerActionType.CHANNEL = 0; //The type of a {@link LexerChannelAction} action.
LexerActionType.CUSTOM = 1; //The type of a {@link LexerCustomAction} action.
LexerActionType.MODE = 2; //The type of a {@link LexerModeAction} action.
LexerActionType.MORE = 3; //The type of a {@link LexerMoreAction} action.
LexerActionType.POP_MODE = 4; //The type of a {@link LexerPopModeAction} action.
LexerActionType.PUSH_MODE = 5; //The type of a {@link LexerPushModeAction} action.
LexerActionType.SKIP = 6; //The type of a {@link LexerSkipAction} action.
LexerActionType.TYPE = 7; //The type of a {@link LexerTypeAction} action.
class LexerAction {
constructor(action) {
this.actionType = action;
this.isPositionDependent = false;
}
function LexerAction(action) {
this.actionType = action;
this.isPositionDependent = false;
return this;
hashCode() {
const hash = new Hash();
this.updateHashCode(hash);
return hash.finish()
}
updateHashCode(hash) {
hash.update(this.actionType);
}
equals(other) {
return this === other;
}
}
LexerAction.prototype.hashCode = function() {
var hash = new Hash();
this.updateHashCode(hash);
return hash.finish()
};
LexerAction.prototype.updateHashCode = function(hash) {
hash.update(this.actionType);
};
/**
* Implements the {@code skip} lexer action by calling {@link Lexer//skip}.
*
* <p>The {@code skip} command does not have any parameters, so this action is
* implemented as a singleton instance exposed by {@link //INSTANCE}.</p>
*/
class LexerSkipAction extends LexerAction {
constructor() {
super(LexerActionType.SKIP);
}
LexerAction.prototype.equals = function(other) {
return this === other;
};
execute(lexer) {
lexer.skip();
}
//
// Implements the {@code skip} lexer action by calling {@link Lexer//skip}.
//
// <p>The {@code skip} command does not have any parameters, so this action is
// implemented as a singleton instance exposed by {@link //INSTANCE}.</p>
function LexerSkipAction() {
LexerAction.call(this, LexerActionType.SKIP);
return this;
toString() {
return "skip";
}
}
LexerSkipAction.prototype = Object.create(LexerAction.prototype);
LexerSkipAction.prototype.constructor = LexerSkipAction;
// Provides a singleton instance of this parameterless lexer action.
LexerSkipAction.INSTANCE = new LexerSkipAction();
LexerSkipAction.prototype.execute = function(lexer) {
lexer.skip();
};
LexerSkipAction.prototype.toString = function() {
return "skip";
};
// Implements the {@code type} lexer action by calling {@link Lexer//setType}
// with the assigned type.
function LexerTypeAction(type) {
LexerAction.call(this, LexerActionType.TYPE);
this.type = type;
return this;
}
LexerTypeAction.prototype = Object.create(LexerAction.prototype);
LexerTypeAction.prototype.constructor = LexerTypeAction;
LexerTypeAction.prototype.execute = function(lexer) {
lexer.type = this.type;
};
LexerTypeAction.prototype.updateHashCode = function(hash) {
hash.update(this.actionType, this.type);
};
LexerTypeAction.prototype.equals = function(other) {
if(this === other) {
return true;
} else if (! (other instanceof LexerTypeAction)) {
return false;
} else {
return this.type === other.type;
/**
* Implements the {@code type} lexer action by calling {@link Lexer//setType}
* with the assigned type
*/
class LexerTypeAction extends LexerAction {
constructor(type) {
super(LexerActionType.TYPE);
this.type = type;
}
};
LexerTypeAction.prototype.toString = function() {
return "type(" + this.type + ")";
};
// Implements the {@code pushMode} lexer action by calling
// {@link Lexer//pushMode} with the assigned mode.
function LexerPushModeAction(mode) {
LexerAction.call(this, LexerActionType.PUSH_MODE);
this.mode = mode;
return this;
}
LexerPushModeAction.prototype = Object.create(LexerAction.prototype);
LexerPushModeAction.prototype.constructor = LexerPushModeAction;
// <p>This action is implemented by calling {@link Lexer//pushMode} with the
// value provided by {@link //getMode}.</p>
LexerPushModeAction.prototype.execute = function(lexer) {
lexer.pushMode(this.mode);
};
LexerPushModeAction.prototype.updateHashCode = function(hash) {
hash.update(this.actionType, this.mode);
};
LexerPushModeAction.prototype.equals = function(other) {
if (this === other) {
return true;
} else if (! (other instanceof LexerPushModeAction)) {
return false;
} else {
return this.mode === other.mode;
execute(lexer) {
lexer.type = this.type;
}
};
LexerPushModeAction.prototype.toString = function() {
return "pushMode(" + this.mode + ")";
};
updateHashCode(hash) {
hash.update(this.actionType, this.type);
}
equals(other) {
if(this === other) {
return true;
} else if (! (other instanceof LexerTypeAction)) {
return false;
} else {
return this.type === other.type;
}
}
// Implements the {@code popMode} lexer action by calling {@link Lexer//popMode}.
//
// <p>The {@code popMode} command does not have any parameters, so this action is
// implemented as a singleton instance exposed by {@link //INSTANCE}.</p>
function LexerPopModeAction() {
LexerAction.call(this,LexerActionType.POP_MODE);
return this;
toString() {
return "type(" + this.type + ")";
}
}
LexerPopModeAction.prototype = Object.create(LexerAction.prototype);
LexerPopModeAction.prototype.constructor = LexerPopModeAction;
/**
* Implements the {@code pushMode} lexer action by calling
* {@link Lexer//pushMode} with the assigned mode
*/
class LexerPushModeAction extends LexerAction {
constructor(mode) {
super(LexerActionType.PUSH_MODE);
this.mode = mode;
}
/**
* <p>This action is implemented by calling {@link Lexer//pushMode} with the
* value provided by {@link //getMode}.</p>
*/
execute(lexer) {
lexer.pushMode(this.mode);
}
updateHashCode(hash) {
hash.update(this.actionType, this.mode);
}
equals(other) {
if (this === other) {
return true;
} else if (! (other instanceof LexerPushModeAction)) {
return false;
} else {
return this.mode === other.mode;
}
}
toString() {
return "pushMode(" + this.mode + ")";
}
}
/**
* Implements the {@code popMode} lexer action by calling {@link Lexer//popMode}.
*
* <p>The {@code popMode} command does not have any parameters, so this action is
* implemented as a singleton instance exposed by {@link //INSTANCE}.</p>
*/
class LexerPopModeAction extends LexerAction {
constructor() {
super(LexerActionType.POP_MODE);
}
/**
* <p>This action is implemented by calling {@link Lexer//popMode}.</p>
*/
execute(lexer) {
lexer.popMode();
}
toString() {
return "popMode";
}
}
LexerPopModeAction.INSTANCE = new LexerPopModeAction();
// <p>This action is implemented by calling {@link Lexer//popMode}.</p>
LexerPopModeAction.prototype.execute = function(lexer) {
lexer.popMode();
};
/**
* Implements the {@code more} lexer action by calling {@link Lexer//more}.
*
* <p>The {@code more} command does not have any parameters, so this action is
* implemented as a singleton instance exposed by {@link //INSTANCE}.</p>
*/
class LexerMoreAction extends LexerAction {
constructor() {
super(LexerActionType.MORE);
}
LexerPopModeAction.prototype.toString = function() {
return "popMode";
};
/**
* <p>This action is implemented by calling {@link Lexer//popMode}.</p>
*/
execute(lexer) {
lexer.more();
}
// Implements the {@code more} lexer action by calling {@link Lexer//more}.
//
// <p>The {@code more} command does not have any parameters, so this action is
// implemented as a singleton instance exposed by {@link //INSTANCE}.</p>
function LexerMoreAction() {
LexerAction.call(this, LexerActionType.MORE);
return this;
toString() {
return "more";
}
}
LexerMoreAction.prototype = Object.create(LexerAction.prototype);
LexerMoreAction.prototype.constructor = LexerMoreAction;
LexerMoreAction.INSTANCE = new LexerMoreAction();
// <p>This action is implemented by calling {@link Lexer//popMode}.</p>
LexerMoreAction.prototype.execute = function(lexer) {
lexer.more();
};
LexerMoreAction.prototype.toString = function() {
return "more";
};
/**
* Implements the {@code mode} lexer action by calling {@link Lexer//mode} with
* the assigned mode
*/
class LexerModeAction extends LexerAction {
constructor(mode) {
super(LexerActionType.MODE);
this.mode = mode;
}
/**
* <p>This action is implemented by calling {@link Lexer//mode} with the
* value provided by {@link //getMode}.</p>
*/
execute(lexer) {
lexer.mode(this.mode);
}
// Implements the {@code mode} lexer action by calling {@link Lexer//mode} with
// the assigned mode.
function LexerModeAction(mode) {
LexerAction.call(this, LexerActionType.MODE);
this.mode = mode;
return this;
updateHashCode(hash) {
hash.update(this.actionType, this.mode);
}
equals(other) {
if (this === other) {
return true;
} else if (! (other instanceof LexerModeAction)) {
return false;
} else {
return this.mode === other.mode;
}
}
toString() {
return "mode(" + this.mode + ")";
}
}
LexerModeAction.prototype = Object.create(LexerAction.prototype);
LexerModeAction.prototype.constructor = LexerModeAction;
// <p>This action is implemented by calling {@link Lexer//mode} with the
// value provided by {@link //getMode}.</p>
LexerModeAction.prototype.execute = function(lexer) {
lexer.mode(this.mode);
};
LexerModeAction.prototype.updateHashCode = function(hash) {
hash.update(this.actionType, this.mode);
};
LexerModeAction.prototype.equals = function(other) {
if (this === other) {
return true;
} else if (! (other instanceof LexerModeAction)) {
return false;
} else {
return this.mode === other.mode;
/**
* Executes a custom lexer action by calling {@link Recognizer//action} with the
* rule and action indexes assigned to the custom action. The implementation of
* a custom action is added to the generated code for the lexer in an override
* of {@link Recognizer//action} when the grammar is compiled.
*
* <p>This class may represent embedded actions created with the <code>{...}</code>
* syntax in ANTLR 4, as well as actions created for lexer commands where the
* command argument could not be evaluated when the grammar was compiled.</p>
*/
class LexerCustomAction extends LexerAction {
/**
* Constructs a custom lexer action with the specified rule and action
* indexes.
*
* @param ruleIndex The rule index to use for calls to
* {@link Recognizer//action}.
* @param actionIndex The action index to use for calls to
* {@link Recognizer//action}.
*/
constructor(ruleIndex, actionIndex) {
super(LexerActionType.CUSTOM);
this.ruleIndex = ruleIndex;
this.actionIndex = actionIndex;
this.isPositionDependent = true;
}
};
LexerModeAction.prototype.toString = function() {
return "mode(" + this.mode + ")";
};
/**
* <p>Custom actions are implemented by calling {@link Lexer//action} with the
* appropriate rule and action indexes.</p>
*/
execute(lexer) {
lexer.action(null, this.ruleIndex, this.actionIndex);
}
// Executes a custom lexer action by calling {@link Recognizer//action} with the
// rule and action indexes assigned to the custom action. The implementation of
// a custom action is added to the generated code for the lexer in an override
// of {@link Recognizer//action} when the grammar is compiled.
//
// <p>This class may represent embedded actions created with the <code>{...}</code>
// syntax in ANTLR 4, as well as actions created for lexer commands where the
// command argument could not be evaluated when the grammar was compiled.</p>
updateHashCode(hash) {
hash.update(this.actionType, this.ruleIndex, this.actionIndex);
}
// Constructs a custom lexer action with the specified rule and action
// indexes.
//
// @param ruleIndex The rule index to use for calls to
// {@link Recognizer//action}.
// @param actionIndex The action index to use for calls to
// {@link Recognizer//action}.
function LexerCustomAction(ruleIndex, actionIndex) {
LexerAction.call(this, LexerActionType.CUSTOM);
this.ruleIndex = ruleIndex;
this.actionIndex = actionIndex;
this.isPositionDependent = true;
return this;
equals(other) {
if (this === other) {
return true;
} else if (! (other instanceof LexerCustomAction)) {
return false;
} else {
return this.ruleIndex === other.ruleIndex && this.actionIndex === other.actionIndex;
}
}
}
LexerCustomAction.prototype = Object.create(LexerAction.prototype);
LexerCustomAction.prototype.constructor = LexerCustomAction;
// <p>Custom actions are implemented by calling {@link Lexer//action} with the
// appropriate rule and action indexes.</p>
LexerCustomAction.prototype.execute = function(lexer) {
lexer.action(null, this.ruleIndex, this.actionIndex);
};
LexerCustomAction.prototype.updateHashCode = function(hash) {
hash.update(this.actionType, this.ruleIndex, this.actionIndex);
};
LexerCustomAction.prototype.equals = function(other) {
if (this === other) {
return true;
} else if (! (other instanceof LexerCustomAction)) {
return false;
} else {
return this.ruleIndex === other.ruleIndex && this.actionIndex === other.actionIndex;
/**
* Implements the {@code channel} lexer action by calling
* {@link Lexer//setChannel} with the assigned channel.
* Constructs a new {@code channel} action with the specified channel value.
* @param channel The channel value to pass to {@link Lexer//setChannel}
*/
class LexerChannelAction extends LexerAction {
constructor(channel) {
super(LexerActionType.CHANNEL);
this.channel = channel;
}
};
// Implements the {@code channel} lexer action by calling
// {@link Lexer//setChannel} with the assigned channel.
// Constructs a new {@code channel} action with the specified channel value.
// @param channel The channel value to pass to {@link Lexer//setChannel}.
function LexerChannelAction(channel) {
LexerAction.call(this, LexerActionType.CHANNEL);
this.channel = channel;
return this;
/**
* <p>This action is implemented by calling {@link Lexer//setChannel} with the
* value provided by {@link //getChannel}.</p>
*/
execute(lexer) {
lexer._channel = this.channel;
}
updateHashCode(hash) {
hash.update(this.actionType, this.channel);
}
equals(other) {
if (this === other) {
return true;
} else if (! (other instanceof LexerChannelAction)) {
return false;
} else {
return this.channel === other.channel;
}
}
toString() {
return "channel(" + this.channel + ")";
}
}
LexerChannelAction.prototype = Object.create(LexerAction.prototype);
LexerChannelAction.prototype.constructor = LexerChannelAction;
// <p>This action is implemented by calling {@link Lexer//setChannel} with the
// value provided by {@link //getChannel}.</p>
LexerChannelAction.prototype.execute = function(lexer) {
lexer._channel = this.channel;
};
LexerChannelAction.prototype.updateHashCode = function(hash) {
hash.update(this.actionType, this.channel);
};
LexerChannelAction.prototype.equals = function(other) {
if (this === other) {
return true;
} else if (! (other instanceof LexerChannelAction)) {
return false;
} else {
return this.channel === other.channel;
/**
* This implementation of {@link LexerAction} is used for tracking input offsets
* for position-dependent actions within a {@link LexerActionExecutor}.
*
* <p>This action is not serialized as part of the ATN, and is only required for
* position-dependent lexer actions which appear at a location other than the
* end of a rule. For more information about DFA optimizations employed for
* lexer actions, see {@link LexerActionExecutor//append} and
* {@link LexerActionExecutor//fixOffsetBeforeMatch}.</p>
*
* Constructs a new indexed custom action by associating a character offset
* with a {@link LexerAction}.
*
* <p>Note: This class is only required for lexer actions for which
* {@link LexerAction//isPositionDependent} returns {@code true}.</p>
*
* @param offset The offset into the input {@link CharStream}, relative to
* the token start index, at which the specified lexer action should be
* executed.
* @param action The lexer action to execute at a particular offset in the
* input {@link CharStream}.
*/
class LexerIndexedCustomAction extends LexerAction {
constructor(offset, action) {
super(action.actionType);
this.offset = offset;
this.action = action;
this.isPositionDependent = true;
}
};
LexerChannelAction.prototype.toString = function() {
return "channel(" + this.channel + ")";
};
/**
* <p>This method calls {@link //execute} on the result of {@link //getAction}
* using the provided {@code lexer}.</p>
*/
execute(lexer) {
// assume the input stream position was properly set by the calling code
this.action.execute(lexer);
}
// This implementation of {@link LexerAction} is used for tracking input offsets
// for position-dependent actions within a {@link LexerActionExecutor}.
//
// <p>This action is not serialized as part of the ATN, and is only required for
// position-dependent lexer actions which appear at a location other than the
// end of a rule. For more information about DFA optimizations employed for
// lexer actions, see {@link LexerActionExecutor//append} and
// {@link LexerActionExecutor//fixOffsetBeforeMatch}.</p>
updateHashCode(hash) {
hash.update(this.actionType, this.offset, this.action);
}
// Constructs a new indexed custom action by associating a character offset
// with a {@link LexerAction}.
//
// <p>Note: This class is only required for lexer actions for which
// {@link LexerAction//isPositionDependent} returns {@code true}.</p>
//
// @param offset The offset into the input {@link CharStream}, relative to
// the token start index, at which the specified lexer action should be
// executed.
// @param action The lexer action to execute at a particular offset in the
// input {@link CharStream}.
function LexerIndexedCustomAction(offset, action) {
LexerAction.call(this, action.actionType);
this.offset = offset;
this.action = action;
this.isPositionDependent = true;
return this;
equals(other) {
if (this === other) {
return true;
} else if (! (other instanceof LexerIndexedCustomAction)) {
return false;
} else {
return this.offset === other.offset && this.action === other.action;
}
}
}
LexerIndexedCustomAction.prototype = Object.create(LexerAction.prototype);
LexerIndexedCustomAction.prototype.constructor = LexerIndexedCustomAction;
// <p>This method calls {@link //execute} on the result of {@link //getAction}
// using the provided {@code lexer}.</p>
LexerIndexedCustomAction.prototype.execute = function(lexer) {
// assume the input stream position was properly set by the calling code
this.action.execute(lexer);
};
LexerIndexedCustomAction.prototype.updateHashCode = function(hash) {
hash.update(this.actionType, this.offset, this.action);
};
LexerIndexedCustomAction.prototype.equals = function(other) {
if (this === other) {
return true;
} else if (! (other instanceof LexerIndexedCustomAction)) {
return false;
} else {
return this.offset === other.offset && this.action === other.action;
}
};
exports.LexerActionType = LexerActionType;
exports.LexerSkipAction = LexerSkipAction;
exports.LexerChannelAction = LexerChannelAction;
exports.LexerCustomAction = LexerCustomAction;
exports.LexerIndexedCustomAction = LexerIndexedCustomAction;
exports.LexerMoreAction = LexerMoreAction;
exports.LexerTypeAction = LexerTypeAction;
exports.LexerPushModeAction = LexerPushModeAction;
exports.LexerPopModeAction = LexerPopModeAction;
exports.LexerModeAction = LexerModeAction;
module.exports = {
LexerActionType,
LexerSkipAction,
LexerChannelAction,
LexerCustomAction,
LexerIndexedCustomAction,
LexerMoreAction,
LexerTypeAction,
LexerPushModeAction,
LexerPopModeAction,
LexerModeAction
}