hdl-js
Version:
Hardware definition language (HDL) and Hardware simulator
727 lines (619 loc) • 21.5 kB
JavaScript
/**
* LR parser generated by the Syntax tool.
*
* https://www.npmjs.com/package/syntax-cli
*
* npm install -g syntax-cli
*
* syntax-cli --help
*
* To regenerate run:
*
* syntax-cli \
* --grammar ~/path-to-grammar-file \
* --mode <parsing-mode> \
* --output ~/path-to-output-parser-file.js
*/
'use strict';
/**
* Matched token text.
*/
var _slicedToArray = function () { function sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"]) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } return function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return sliceIterator(arr, i); } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } }; }();
function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } }
var yytext = void 0;
/**
* Length of the matched token text.
*/
var yyleng = void 0;
/**
* Storage object.
*/
var yy = {};
/**
* Result of semantic action.
*/
var __ = void 0;
/**
* Result location object.
*/
var __loc = void 0;
function yyloc(start, end) {
if (!yy.options.captureLocations) {
return null;
}
// Epsilon doesn't produce location.
if (!start || !end) {
return start || end;
}
return {
startOffset: start.startOffset,
endOffset: end.endOffset,
startLine: start.startLine,
endLine: end.endLine,
startColumn: start.startColumn,
endColumn: end.endColumn
};
}
var EOF = '$';
/**
* List of productions (generated by Syntax tool).
*/
var productions = [[-1, 1, function (_1, _1loc) {
__loc = yyloc(_1loc, _1loc);__ = _1;
}], [0, 1, function (_1, _1loc) {
__loc = yyloc(_1loc, _1loc);
__ = {
type: 'Script',
commands: _1
};
}], [1, 1, function (_1, _1loc) {
__loc = yyloc(_1loc, _1loc);__ = [_1];
}], [1, 2, function (_1, _2, _1loc, _2loc) {
__loc = yyloc(_1loc, _2loc);_1.push(_2);__ = _1;
}], [2, 1, function (_1, _1loc) {
__loc = yyloc(_1loc, _1loc);__ = _1;
}], [2, 1, function (_1, _1loc) {
__loc = yyloc(_1loc, _1loc);__ = _1;
}], [2, 1, function (_1, _1loc) {
__loc = yyloc(_1loc, _1loc);__ = _1;
}], [3, 3, function (_1, _2, _3, _1loc, _2loc, _3loc) {
__loc = yyloc(_1loc, _3loc);
if (controllerCommands.has(_1)) {
__ = {
type: 'ControllerCommand',
name: _1,
arguments: parseControllerCommandArgs(_2),
terminator: _3
};
} else {
// Simulator command:
if (_1 === 'set') {
_2[0] = parseName(_2[0]);
_2[1] = parseValue(_2[1]);
}
__ = {
type: 'SimulatorCommand',
name: _1,
arguments: _2,
terminator: _3
};
}
}], [4, 1, function (_1, _1loc) {
__loc = yyloc(_1loc, _1loc);__ = _1;
}], [4, 0, function () {
__loc = null;__ = [];
}], [5, 1, function (_1, _1loc) {
__loc = yyloc(_1loc, _1loc);__ = [_1];
}], [5, 2, function (_1, _2, _1loc, _2loc) {
__loc = yyloc(_1loc, _2loc);_1.push(_2);__ = _1;
}], [6, 1, function (_1, _1loc) {
__loc = yyloc(_1loc, _1loc);__ = _1;
}], [6, 1, function (_1, _1loc) {
__loc = yyloc(_1loc, _1loc);__ = _1;
}], [6, 1, function (_1, _1loc) {
__loc = yyloc(_1loc, _1loc);__ = _1;
}], [6, 1, function (_1, _1loc) {
__loc = yyloc(_1loc, _1loc);__ = _1;
}], [7, 3, function (_1, _2, _3, _1loc, _2loc, _3loc) {
__loc = yyloc(_1loc, _3loc);
__ = {
type: 'ControllerCommand',
name: _1,
times: parseValue(_2),
commands: _3
};
}], [8, 3, function (_1, _2, _3, _1loc, _2loc, _3loc) {
__loc = yyloc(_1loc, _3loc);
__ = {
type: 'ControllerCommand',
name: _1,
condition: _2,
commands: _3
};
}], [9, 3, function (_1, _2, _3, _1loc, _2loc, _3loc) {
__loc = yyloc(_1loc, _3loc);
__ = {
type: 'RelationalExpression',
operator: _2,
left: parseName(_1),
right: parseValue(_3)
};
}], [10, 1, function (_1, _1loc) {
__loc = yyloc(_1loc, _1loc);__ = _1;
}], [10, 1, function (_1, _1loc) {
__loc = yyloc(_1loc, _1loc);__ = _1;
}], [10, 1, function (_1, _1loc) {
__loc = yyloc(_1loc, _1loc);__ = _1;
}], [10, 1, function (_1, _1loc) {
__loc = yyloc(_1loc, _1loc);__ = _1;
}], [10, 1, function (_1, _1loc) {
__loc = yyloc(_1loc, _1loc);__ = _1;
}], [10, 1, function (_1, _1loc) {
__loc = yyloc(_1loc, _1loc);__ = _1;
}], [11, 3, function (_1, _2, _3, _1loc, _2loc, _3loc) {
__loc = yyloc(_1loc, _3loc);
__ = _2;
}]];
/**
* Encoded tokens map.
*/
var tokens = { "REF_VAL": "12", "COMMAND_TERMINATOR": "13", "NUMBER": "14", "FORMATTED_NUMBER": "15", "STRING": "16", "REPEAT": "17", "WHILE": "18", "EQUAL": "19", "NOT_EQUAL": "20", "LESS": "21", "GREATER": "22", "LESS_EQUAL": "23", "GREATER_EQUAL": "24", "'{'": "25", "'}'": "26", "$": "27" };
/**
* Parsing table (generated by Syntax tool).
*/
var table = [{ "0": 1, "1": 2, "2": 3, "3": 4, "7": 5, "8": 6, "12": "s7", "17": "s8", "18": "s9" }, { "27": "acc" }, { "2": 10, "3": 4, "7": 5, "8": 6, "12": "s7", "17": "s8", "18": "s9", "27": "r1" }, { "12": "r2", "17": "r2", "18": "r2", "26": "r2", "27": "r2" }, { "12": "r4", "17": "r4", "18": "r4", "26": "r4", "27": "r4" }, { "12": "r5", "17": "r5", "18": "r5", "26": "r5", "27": "r5" }, { "12": "r6", "17": "r6", "18": "r6", "26": "r6", "27": "r6" }, { "4": 11, "5": 12, "6": 13, "12": "s16", "13": "r9", "14": "s14", "15": "s15", "16": "s17" }, { "6": 20, "12": "s16", "14": "s14", "15": "s15", "16": "s17" }, { "9": 25, "12": "s26" }, { "12": "r3", "17": "r3", "18": "r3", "26": "r3", "27": "r3" }, { "13": "s18" }, { "6": 19, "12": "s16", "13": "r8", "14": "s14", "15": "s15", "16": "s17" }, { "12": "r10", "13": "r10", "14": "r10", "15": "r10", "16": "r10" }, { "12": "r12", "13": "r12", "14": "r12", "15": "r12", "16": "r12", "25": "r12" }, { "12": "r13", "13": "r13", "14": "r13", "15": "r13", "16": "r13", "25": "r13" }, { "12": "r14", "13": "r14", "14": "r14", "15": "r14", "16": "r14", "25": "r14" }, { "12": "r15", "13": "r15", "14": "r15", "15": "r15", "16": "r15", "25": "r15" }, { "12": "r7", "17": "r7", "18": "r7", "26": "r7", "27": "r7" }, { "12": "r11", "13": "r11", "14": "r11", "15": "r11", "16": "r11" }, { "11": 21, "25": "s22" }, { "12": "r16", "17": "r16", "18": "r16", "26": "r16", "27": "r16" }, { "1": 23, "2": 3, "3": 4, "7": 5, "8": 6, "12": "s7", "17": "s8", "18": "s9" }, { "2": 10, "3": 4, "7": 5, "8": 6, "12": "s7", "17": "s8", "18": "s9", "26": "s24" }, { "12": "r25", "17": "r25", "18": "r25", "26": "r25", "27": "r25" }, { "11": 27, "25": "s22" }, { "10": 28, "19": "s29", "20": "s30", "21": "s31", "22": "s32", "23": "s33", "24": "s34" }, { "12": "r17", "17": "r17", "18": "r17", "26": "r17", "27": "r17" }, { "6": 35, "12": "s16", "14": "s14", "15": "s15", "16": "s17" }, { "12": "r19", "14": "r19", "15": "r19", "16": "r19" }, { "12": "r20", "14": "r20", "15": "r20", "16": "r20" }, { "12": "r21", "14": "r21", "15": "r21", "16": "r21" }, { "12": "r22", "14": "r22", "15": "r22", "16": "r22" }, { "12": "r23", "14": "r23", "15": "r23", "16": "r23" }, { "12": "r24", "14": "r24", "15": "r24", "16": "r24" }, { "25": "r18" }];
/**
* Parsing stack.
*/
var stack = [];
/**
* Tokenizer instance.
*/
var tokenizer = void 0;
/**
* Generic tokenizer used by the parser in the Syntax tool.
*
* https://www.npmjs.com/package/syntax-cli
*
* See `--custom-tokinzer` to skip this generation, and use a custom one.
*/
var lexRules = [[/^\/\/.*/, function () {/* skip comments */}], [/^\/\*(.|\s)*?\*\//, function () {/* skip comments */}], [/^\s+/, function () {/* skip whitespace */}], [/^"[^\"]*"/, function () {
yytext = yytext.slice(1, -1);return 'STRING';
}], [/^'[^\']*'/, function () {
yytext = yytext.slice(1, -1);return 'STRING';
}], [/^(;|,|!)/, function () {
return 'COMMAND_TERMINATOR';
}], [/^<>/, function () {
return 'NOT_EQUAL';
}], [/^<=/, function () {
return 'LESS_EQUAL';
}], [/^>=/, function () {
return 'GREATER_EQUAL';
}], [/^</, function () {
return 'LESS';
}], [/^>/, function () {
return 'GREATER';
}], [/^=/, function () {
return 'EQUAL';
}], [/^repeat/, function () {
return 'REPEAT';
}], [/^while/, function () {
return 'WHILE';
}], [/^\b(B|X|D)\d+/, function () {
return 'FORMATTED_NUMBER';
}], [/^(-?)\d+/, function () {
return 'NUMBER';
}], [/^[\w\.%\[\]\-$]+/, function () {
return 'REF_VAL';
}], [/^\{/, function () {
return "'{'";
}], [/^\}/, function () {
return "'}'";
}]];
var lexRulesByConditions = { "INITIAL": [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18] };
var EOF_TOKEN = {
type: EOF,
value: ''
};
tokenizer = {
initString: function initString(string) {
this._string = string;
this._cursor = 0;
this._states = ['INITIAL'];
this._tokensQueue = [];
this._currentLine = 1;
this._currentColumn = 0;
this._currentLineBeginOffset = 0;
/**
* Matched token location data.
*/
this._tokenStartOffset = 0;
this._tokenEndOffset = 0;
this._tokenStartLine = 1;
this._tokenEndLine = 1;
this._tokenStartColumn = 0;
this._tokenEndColumn = 0;
return this;
},
/**
* Returns tokenizer states.
*/
getStates: function getStates() {
return this._states;
},
getCurrentState: function getCurrentState() {
return this._states[this._states.length - 1];
},
pushState: function pushState(state) {
this._states.push(state);
},
begin: function begin(state) {
this.pushState(state);
},
popState: function popState() {
if (this._states.length > 1) {
return this._states.pop();
}
return this._states[0];
},
getNextToken: function getNextToken() {
// Something was queued, return it.
if (this._tokensQueue.length > 0) {
return this._toToken(this._tokensQueue.shift());
}
if (!this.hasMoreTokens()) {
return EOF_TOKEN;
}
var string = this._string.slice(this._cursor);
var lexRulesForState = lexRulesByConditions[this.getCurrentState()];
for (var i = 0; i < lexRulesForState.length; i++) {
var lexRuleIndex = lexRulesForState[i];
var lexRule = lexRules[lexRuleIndex];
var matched = this._match(string, lexRule[0]);
// Manual handling of EOF token (the end of string). Return it
// as `EOF` symbol.
if (string === '' && matched === '') {
this._cursor++;
}
if (matched !== null) {
yytext = matched;
yyleng = yytext.length;
var token = lexRule[1].call(this);
if (!token) {
return this.getNextToken();
}
// If multiple tokens are returned, save them to return
// on next `getNextToken` call.
if (Array.isArray(token)) {
var tokensToQueue = token.slice(1);
token = token[0];
if (tokensToQueue.length > 0) {
var _tokensQueue;
(_tokensQueue = this._tokensQueue).unshift.apply(_tokensQueue, _toConsumableArray(tokensToQueue));
}
}
return this._toToken(token, yytext);
}
}
if (this.isEOF()) {
this._cursor++;
return EOF_TOKEN;
}
this.throwUnexpectedToken(string[0], this._currentLine, this._currentColumn);
},
/**
* Throws default "Unexpected token" exception, showing the actual
* line from the source, pointing with the ^ marker to the bad token.
* In addition, shows `line:column` location.
*/
throwUnexpectedToken: function throwUnexpectedToken(symbol, line, column) {
var lineSource = this._string.split('\n')[line - 1];
var lineData = '';
if (lineSource) {
var pad = ' '.repeat(column);
lineData = '\n\n' + lineSource + '\n' + pad + '^\n';
}
throw new SyntaxError(lineData + 'Unexpected token: "' + symbol + '" ' + ('at ' + line + ':' + column + '.'));
},
getCursor: function getCursor() {
return this._cursor;
},
getCurrentLine: function getCurrentLine() {
return this._currentLine;
},
getCurrentColumn: function getCurrentColumn() {
return this._currentColumn;
},
_captureLocation: function _captureLocation(matched) {
var nlRe = /\n/g;
// Absolute offsets.
this._tokenStartOffset = this._cursor;
// Line-based locations, start.
this._tokenStartLine = this._currentLine;
this._tokenStartColumn = this._tokenStartOffset - this._currentLineBeginOffset;
// Extract `\n` in the matched token.
var nlMatch = void 0;
while ((nlMatch = nlRe.exec(matched)) !== null) {
this._currentLine++;
this._currentLineBeginOffset = this._tokenStartOffset + nlMatch.index + 1;
}
this._tokenEndOffset = this._cursor + matched.length;
// Line-based locations, end.
this._tokenEndLine = this._currentLine;
this._tokenEndColumn = this._currentColumn = this._tokenEndOffset - this._currentLineBeginOffset;
},
_toToken: function _toToken(tokenType) {
var yytext = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '';
return {
// Basic data.
type: tokenType,
value: yytext,
// Location data.
startOffset: this._tokenStartOffset,
endOffset: this._tokenEndOffset,
startLine: this._tokenStartLine,
endLine: this._tokenEndLine,
startColumn: this._tokenStartColumn,
endColumn: this._tokenEndColumn
};
},
isEOF: function isEOF() {
return this._cursor === this._string.length;
},
hasMoreTokens: function hasMoreTokens() {
return this._cursor <= this._string.length;
},
_match: function _match(string, regexp) {
var matched = string.match(regexp);
if (matched) {
// Handle `\n` in the matched token to track line numbers.
this._captureLocation(matched[0]);
this._cursor += matched[0].length;
return matched[0];
}
return null;
}
};
/**
* Expose tokenizer so it can be accessed in semantic actions.
*/
yy.lexer = tokenizer;
yy.tokenizer = tokenizer;
/**
* Global parsing options. Some options can be shadowed per
* each `parse` call, if the optations are passed.
*
* Initalized to the `captureLocations` which is passed
* from the generator. Other options can be added at runtime.
*/
yy.options = {
captureLocations: true
};
/**
* Parsing module.
*/
var yyparse = {
/**
* Sets global parsing options.
*/
setOptions: function setOptions(options) {
yy.options = options;
return this;
},
/**
* Returns parsing options.
*/
getOptions: function getOptions() {
return yy.options;
},
/**
* Parses a string.
*/
parse: function parse(string, parseOptions) {
if (!tokenizer) {
throw new Error('Tokenizer instance wasn\'t specified.');
}
tokenizer.initString(string);
/**
* If parse options are passed, override global parse options for
* this call, and later restore global options.
*/
var globalOptions = yy.options;
if (parseOptions) {
yy.options = Object.assign({}, yy.options, parseOptions);
}
/**
* Allow callers to do setup work based on the
* parsing string, and passed options.
*/
yyparse.onParseBegin(string, tokenizer, yy.options);
stack.length = 0;
stack.push(0);
var token = tokenizer.getNextToken();
var shiftedToken = null;
do {
if (!token) {
// Restore options.
yy.options = globalOptions;
unexpectedEndOfInput();
}
var state = stack[stack.length - 1];
var column = tokens[token.type];
if (!table[state].hasOwnProperty(column)) {
yy.options = globalOptions;
unexpectedToken(token);
}
var entry = table[state][column];
// Shift action.
if (entry[0] === 's') {
var loc = null;
if (yy.options.captureLocations) {
loc = {
startOffset: token.startOffset,
endOffset: token.endOffset,
startLine: token.startLine,
endLine: token.endLine,
startColumn: token.startColumn,
endColumn: token.endColumn
};
}
stack.push({ symbol: tokens[token.type], semanticValue: token.value, loc: loc }, Number(entry.slice(1)));
shiftedToken = token;
token = tokenizer.getNextToken();
}
// Reduce action.
else if (entry[0] === 'r') {
var productionNumber = entry.slice(1);
var production = productions[productionNumber];
var hasSemanticAction = typeof production[2] === 'function';
var semanticValueArgs = hasSemanticAction ? [] : null;
var locationArgs = hasSemanticAction && yy.options.captureLocations ? [] : null;
if (production[1] !== 0) {
var rhsLength = production[1];
while (rhsLength-- > 0) {
stack.pop();
var stackEntry = stack.pop();
if (hasSemanticAction) {
semanticValueArgs.unshift(stackEntry.semanticValue);
if (locationArgs) {
locationArgs.unshift(stackEntry.loc);
}
}
}
}
var reduceStackEntry = { symbol: production[0] };
if (hasSemanticAction) {
yytext = shiftedToken ? shiftedToken.value : null;
yyleng = shiftedToken ? shiftedToken.value.length : null;
var semanticActionArgs = locationArgs !== null ? semanticValueArgs.concat(locationArgs) : semanticValueArgs;
production[2].apply(production, _toConsumableArray(semanticActionArgs));
reduceStackEntry.semanticValue = __;
if (locationArgs) {
reduceStackEntry.loc = __loc;
}
}
var nextState = stack[stack.length - 1];
var symbolToReduceWith = production[0];
stack.push(reduceStackEntry, table[nextState][symbolToReduceWith]);
}
// Accept.
else if (entry === 'acc') {
stack.pop();
var parsed = stack.pop();
if (stack.length !== 1 || stack[0] !== 0 || tokenizer.hasMoreTokens()) {
// Restore options.
yy.options = globalOptions;
unexpectedToken(token);
}
if (parsed.hasOwnProperty('semanticValue')) {
yy.options = globalOptions;
yyparse.onParseEnd(parsed.semanticValue);
return parsed.semanticValue;
}
yyparse.onParseEnd();
// Restore options.
yy.options = globalOptions;
return true;
}
} while (tokenizer.hasMoreTokens() || stack.length > 1);
},
setTokenizer: function setTokenizer(customTokenizer) {
tokenizer = customTokenizer;
return yyparse;
},
getTokenizer: function getTokenizer() {
return tokenizer;
},
onParseBegin: function onParseBegin(string, tokenizer, options) {},
onParseEnd: function onParseEnd(parsed) {}
};
/**
* Extracts name, and an optional subscript.
*/
function parseName(rawName) {
var subIdx = rawName.indexOf('[');
// Simple name: `a`
if (subIdx === -1) {
return {
type: 'Name',
value: rawName
};
}
// Name with an index: `a[1]`
return {
type: 'Name',
value: rawName.slice(0, subIdx),
index: Number(rawName.match(/\[(\d+)\]/)[1])
};
}
var formatRadix = {
B: 2,
X: 16,
D: 10
};
function parseValue(rawValue) {
// Simple decimal value: 15
if (rawValue[0] !== '%') {
return {
type: 'Value',
value: Number(rawValue)
};
}
// Formatted value: %B0101, %XFF, %D15
var radix = formatRadix[rawValue[1]];
return {
type: 'Value',
value: Number.parseInt(rawValue.slice(2), radix),
format: rawValue[1],
raw: rawValue
};
}
/**
* Controller commands.
*/
var controllerCommands = new Set(['load', 'output-file', 'compare-to', 'output-list', 'echo', 'clear-echo', 'breakpoint', 'clear-breakpoints', 'repeat', 'while', 'output']);
/**
* Parses controller command arguments.
*/
function parseControllerCommandArgs(rawArguments) {
if (!rawArguments) {
return [];
}
return rawArguments.map(function (arg) {
if (!arg.includes('%')) {
return arg;
}
// Example: a%B3.1.3
var _arg$split = arg.split('%'),
_arg$split2 = _slicedToArray(_arg$split, 2),
column = _arg$split2[0],
parts = _arg$split2[1];
var format = parts[0];
// Offsets
var _parts$slice$split$ma = parts.slice(1).split('.').map(Number),
_parts$slice$split$ma2 = _slicedToArray(_parts$slice$split$ma, 3),
left = _parts$slice$split$ma2[0],
middle = _parts$slice$split$ma2[1],
right = _parts$slice$split$ma2[2];
return {
column: column,
format: format,
left: left,
middle: middle,
right: right
};
});
}
function unexpectedToken(token) {
if (token.type === EOF) {
unexpectedEndOfInput();
}
tokenizer.throwUnexpectedToken(token.value, token.startLine, token.startColumn);
}
function unexpectedEndOfInput() {
parseError('Unexpected end of input.');
}
function parseError(message) {
throw new SyntaxError(message);
}
module.exports = yyparse;