0.2.0 - Mid migration

This commit is contained in:
Daniel Mason 2022-04-25 14:47:15 +12:00
parent 139e6a915e
commit 7e38fdbd7d
42393 changed files with 5358157 additions and 62 deletions

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,35 @@
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
var _exportNames = {
parse: true
};
exports.parse = parse;
var parser = _interopRequireWildcard(require("./grammar"));
var _tokenizer = require("./tokenizer");
var _numberLiterals = require("./number-literals");
Object.keys(_numberLiterals).forEach(function (key) {
if (key === "default" || key === "__esModule") return;
if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
Object.defineProperty(exports, key, {
enumerable: true,
get: function get() {
return _numberLiterals[key];
}
});
});
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {}; if (desc.get || desc.set) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj.default = obj; return newObj; } }
function parse(source) {
var tokens = (0, _tokenizer.tokenize)(source); // We pass the source here to show code frames
var ast = parser.parse(tokens, source);
return ast;
}

View file

@ -0,0 +1,116 @@
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.parse32F = parse32F;
exports.parse64F = parse64F;
exports.parse32I = parse32I;
exports.parseU32 = parseU32;
exports.parse64I = parse64I;
exports.isInfLiteral = isInfLiteral;
exports.isNanLiteral = isNanLiteral;
var _long = _interopRequireDefault(require("@xtuc/long"));
var _floatingPointHexParser = _interopRequireDefault(require("@webassemblyjs/floating-point-hex-parser"));
var _helperApiError = require("@webassemblyjs/helper-api-error");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function parse32F(sourceString) {
if (isHexLiteral(sourceString)) {
return (0, _floatingPointHexParser.default)(sourceString);
}
if (isInfLiteral(sourceString)) {
return sourceString[0] === "-" ? -1 : 1;
}
if (isNanLiteral(sourceString)) {
return (sourceString[0] === "-" ? -1 : 1) * (sourceString.includes(":") ? parseInt(sourceString.substring(sourceString.indexOf(":") + 1), 16) : 0x400000);
}
return parseFloat(sourceString);
}
function parse64F(sourceString) {
if (isHexLiteral(sourceString)) {
return (0, _floatingPointHexParser.default)(sourceString);
}
if (isInfLiteral(sourceString)) {
return sourceString[0] === "-" ? -1 : 1;
}
if (isNanLiteral(sourceString)) {
return (sourceString[0] === "-" ? -1 : 1) * (sourceString.includes(":") ? parseInt(sourceString.substring(sourceString.indexOf(":") + 1), 16) : 0x8000000000000);
}
if (isHexLiteral(sourceString)) {
return (0, _floatingPointHexParser.default)(sourceString);
}
return parseFloat(sourceString);
}
function parse32I(sourceString) {
var value = 0;
if (isHexLiteral(sourceString)) {
value = ~~parseInt(sourceString, 16);
} else if (isDecimalExponentLiteral(sourceString)) {
throw new Error("This number literal format is yet to be implemented.");
} else {
value = parseInt(sourceString, 10);
}
return value;
}
function parseU32(sourceString) {
var value = parse32I(sourceString);
if (value < 0) {
throw new _helperApiError.CompileError("Illegal value for u32: " + sourceString);
}
return value;
}
function parse64I(sourceString) {
var long;
if (isHexLiteral(sourceString)) {
long = _long.default.fromString(sourceString, false, 16);
} else if (isDecimalExponentLiteral(sourceString)) {
throw new Error("This number literal format is yet to be implemented.");
} else {
long = _long.default.fromString(sourceString);
}
return {
high: long.high,
low: long.low
};
}
var NAN_WORD = /^\+?-?nan/;
var INF_WORD = /^\+?-?inf/;
function isInfLiteral(sourceString) {
return INF_WORD.test(sourceString.toLowerCase());
}
function isNanLiteral(sourceString) {
return NAN_WORD.test(sourceString.toLowerCase());
}
function isDecimalExponentLiteral(sourceString) {
return !isHexLiteral(sourceString) && sourceString.toUpperCase().includes("E");
}
function isHexLiteral(sourceString) {
return sourceString.substring(0, 2).toUpperCase() === "0X" || sourceString.substring(0, 3).toUpperCase() === "-0X";
}

View file

@ -0,0 +1,94 @@
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.parseString = parseString;
// string literal characters cannot contain control codes
var CONTROL_CODES = [0, // null
7, // bell
8, // backspace
9, // horizontal
10, // line feed
11, // vertical tab
12, // form feed
13, // carriage return
26, // Control-Z
27, // escape
127 // delete
]; // escaped sequences can either be a two character hex value, or one of the
// following single character codes
function decodeControlCharacter(char) {
switch (char) {
case "t":
return 0x09;
case "n":
return 0x0a;
case "r":
return 0x0d;
case '"':
return 0x22;
case "":
return 0x27;
case "\\":
return 0x5c;
}
return -1;
}
var ESCAPE_CHAR = 92; // backslash
var QUOTE_CHAR = 34; // backslash
// parse string as per the spec:
// https://webassembly.github.io/spec/core/multipage/text/values.html#text-string
function parseString(value) {
var byteArray = [];
var index = 0;
while (index < value.length) {
var charCode = value.charCodeAt(index);
if (CONTROL_CODES.indexOf(charCode) !== -1) {
throw new Error("ASCII control characters are not permitted within string literals");
}
if (charCode === QUOTE_CHAR) {
throw new Error("quotes are not permitted within string literals");
}
if (charCode === ESCAPE_CHAR) {
var firstChar = value.substr(index + 1, 1);
var decodedControlChar = decodeControlCharacter(firstChar);
if (decodedControlChar !== -1) {
// single character escaped values, e.g. \r
byteArray.push(decodedControlChar);
index += 2;
} else {
// hex escaped values, e.g. \2a
var hexValue = value.substr(index + 1, 2);
if (!/^[0-9A-F]{2}$/i.test(hexValue)) {
throw new Error("invalid character encoding");
}
byteArray.push(parseInt(hexValue, 16));
index += 3;
}
} else {
// ASCII encoded values
byteArray.push(charCode);
index++;
}
}
return byteArray;
}

View file

@ -0,0 +1,447 @@
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.tokenize = tokenize;
exports.tokens = exports.keywords = void 0;
var _helperFsm = require("@webassemblyjs/helper-fsm");
var _helperCodeFrame = require("@webassemblyjs/helper-code-frame");
// eslint-disable-next-line
function getCodeFrame(source, line, column) {
var loc = {
start: {
line: line,
column: column
}
};
return "\n" + (0, _helperCodeFrame.codeFrameFromSource)(source, loc) + "\n";
}
var WHITESPACE = /\s/;
var PARENS = /\(|\)/;
var LETTERS = /[a-z0-9_/]/i;
var idchar = /[a-z0-9!#$%&*+./:<=>?@\\[\]^_`|~-]/i;
var valtypes = ["i32", "i64", "f32", "f64"];
var NUMBERS = /[0-9|.|_]/;
var NUMBER_KEYWORDS = /nan|inf/;
function isNewLine(char) {
return char.charCodeAt(0) === 10 || char.charCodeAt(0) === 13;
}
function Token(type, value, start, end) {
var opts = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : {};
var token = {
type: type,
value: value,
loc: {
start: start,
end: end
}
};
if (Object.keys(opts).length > 0) {
// $FlowIgnore
token["opts"] = opts;
}
return token;
}
var tokenTypes = {
openParen: "openParen",
closeParen: "closeParen",
number: "number",
string: "string",
name: "name",
identifier: "identifier",
valtype: "valtype",
dot: "dot",
comment: "comment",
equal: "equal",
keyword: "keyword"
};
var keywords = {
module: "module",
func: "func",
param: "param",
result: "result",
export: "export",
loop: "loop",
block: "block",
if: "if",
then: "then",
else: "else",
call: "call",
call_indirect: "call_indirect",
import: "import",
memory: "memory",
table: "table",
global: "global",
anyfunc: "anyfunc",
mut: "mut",
data: "data",
type: "type",
elem: "elem",
start: "start",
offset: "offset"
};
exports.keywords = keywords;
var NUMERIC_SEPARATOR = "_";
/**
* Build the FSM for number literals
*/
var numberLiteralFSM = new _helperFsm.FSM({
START: [(0, _helperFsm.makeTransition)(/-|\+/, "AFTER_SIGN"), (0, _helperFsm.makeTransition)(/nan:0x/, "NAN_HEX", {
n: 6
}), (0, _helperFsm.makeTransition)(/nan|inf/, "STOP", {
n: 3
}), (0, _helperFsm.makeTransition)(/0x/, "HEX", {
n: 2
}), (0, _helperFsm.makeTransition)(/[0-9]/, "DEC"), (0, _helperFsm.makeTransition)(/\./, "DEC_FRAC")],
AFTER_SIGN: [(0, _helperFsm.makeTransition)(/nan:0x/, "NAN_HEX", {
n: 6
}), (0, _helperFsm.makeTransition)(/nan|inf/, "STOP", {
n: 3
}), (0, _helperFsm.makeTransition)(/0x/, "HEX", {
n: 2
}), (0, _helperFsm.makeTransition)(/[0-9]/, "DEC"), (0, _helperFsm.makeTransition)(/\./, "DEC_FRAC")],
DEC_FRAC: [(0, _helperFsm.makeTransition)(/[0-9]/, "DEC_FRAC", {
allowedSeparator: NUMERIC_SEPARATOR
}), (0, _helperFsm.makeTransition)(/e|E/, "DEC_SIGNED_EXP")],
DEC: [(0, _helperFsm.makeTransition)(/[0-9]/, "DEC", {
allowedSeparator: NUMERIC_SEPARATOR
}), (0, _helperFsm.makeTransition)(/\./, "DEC_FRAC"), (0, _helperFsm.makeTransition)(/e|E/, "DEC_SIGNED_EXP")],
DEC_SIGNED_EXP: [(0, _helperFsm.makeTransition)(/\+|-/, "DEC_EXP"), (0, _helperFsm.makeTransition)(/[0-9]/, "DEC_EXP")],
DEC_EXP: [(0, _helperFsm.makeTransition)(/[0-9]/, "DEC_EXP", {
allowedSeparator: NUMERIC_SEPARATOR
})],
HEX: [(0, _helperFsm.makeTransition)(/[0-9|A-F|a-f]/, "HEX", {
allowedSeparator: NUMERIC_SEPARATOR
}), (0, _helperFsm.makeTransition)(/\./, "HEX_FRAC"), (0, _helperFsm.makeTransition)(/p|P/, "HEX_SIGNED_EXP")],
HEX_FRAC: [(0, _helperFsm.makeTransition)(/[0-9|A-F|a-f]/, "HEX_FRAC", {
allowedSeparator: NUMERIC_SEPARATOR
}), (0, _helperFsm.makeTransition)(/p|P|/, "HEX_SIGNED_EXP")],
HEX_SIGNED_EXP: [(0, _helperFsm.makeTransition)(/[0-9|+|-]/, "HEX_EXP")],
HEX_EXP: [(0, _helperFsm.makeTransition)(/[0-9]/, "HEX_EXP", {
allowedSeparator: NUMERIC_SEPARATOR
})],
NAN_HEX: [(0, _helperFsm.makeTransition)(/[0-9|A-F|a-f]/, "NAN_HEX", {
allowedSeparator: NUMERIC_SEPARATOR
})],
STOP: []
}, "START", "STOP");
function tokenize(input) {
var current = 0;
var char = input[current]; // Used by SourceLocation
var column = 1;
var line = 1;
var tokens = [];
/**
* Creates a pushToken function for a given type
*/
function pushToken(type) {
return function (v) {
var opts = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
var startColumn = opts.startColumn || column - String(v).length;
delete opts.startColumn;
var endColumn = opts.endColumn || startColumn + String(v).length - 1;
delete opts.endColumn;
var start = {
line: line,
column: startColumn
};
var end = {
line: line,
column: endColumn
};
tokens.push(Token(type, v, start, end, opts));
};
}
/**
* Functions to save newly encountered tokens
*/
var pushCloseParenToken = pushToken(tokenTypes.closeParen);
var pushOpenParenToken = pushToken(tokenTypes.openParen);
var pushNumberToken = pushToken(tokenTypes.number);
var pushValtypeToken = pushToken(tokenTypes.valtype);
var pushNameToken = pushToken(tokenTypes.name);
var pushIdentifierToken = pushToken(tokenTypes.identifier);
var pushKeywordToken = pushToken(tokenTypes.keyword);
var pushDotToken = pushToken(tokenTypes.dot);
var pushStringToken = pushToken(tokenTypes.string);
var pushCommentToken = pushToken(tokenTypes.comment);
var pushEqualToken = pushToken(tokenTypes.equal);
/**
* Can be used to look at the next character(s).
*
* The default behavior `lookahead()` simply returns the next character without consuming it.
* Letters are always returned in lowercase.
*
* @param {number} length How many characters to query. Default = 1
* @param {number} offset How many characters to skip forward from current one. Default = 1
*
*/
function lookahead() {
var length = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 1;
var offset = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 1;
return input.substring(current + offset, current + offset + length).toLowerCase();
}
/**
* Advances the cursor in the input by a certain amount
*
* @param {number} amount How many characters to consume. Default = 1
*/
function eatCharacter() {
var amount = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 1;
column += amount;
current += amount;
char = input[current];
}
while (current < input.length) {
// ;;
if (char === ";" && lookahead() === ";") {
var startColumn = column;
eatCharacter(2);
var text = "";
while (!isNewLine(char)) {
text += char;
eatCharacter();
if (char === undefined) {
break;
}
}
var endColumn = column;
pushCommentToken(text, {
type: "leading",
startColumn: startColumn,
endColumn: endColumn
});
continue;
} // (;
if (char === "(" && lookahead() === ";") {
var _startColumn = column;
eatCharacter(2);
var _text = ""; // ;)
while (true) {
char = input[current];
if (char === ";" && lookahead() === ")") {
eatCharacter(2);
break;
}
_text += char;
eatCharacter();
if (isNewLine(char)) {
line++;
column = 0;
}
}
var _endColumn = column;
pushCommentToken(_text, {
type: "block",
startColumn: _startColumn,
endColumn: _endColumn
});
continue;
}
if (char === "(") {
pushOpenParenToken(char);
eatCharacter();
continue;
}
if (char === "=") {
pushEqualToken(char);
eatCharacter();
continue;
}
if (char === ")") {
pushCloseParenToken(char);
eatCharacter();
continue;
}
if (isNewLine(char)) {
line++;
eatCharacter();
column = 0;
continue;
}
if (WHITESPACE.test(char)) {
eatCharacter();
continue;
}
if (char === "$") {
var _startColumn2 = column;
eatCharacter();
var value = "";
while (idchar.test(char)) {
value += char;
eatCharacter();
}
var _endColumn2 = column;
pushIdentifierToken(value, {
startColumn: _startColumn2,
endColumn: _endColumn2
});
continue;
}
if (NUMBERS.test(char) || NUMBER_KEYWORDS.test(lookahead(3, 0)) || char === "-" || char === "+") {
var _startColumn3 = column;
var _value = numberLiteralFSM.run(input.slice(current));
if (_value === "") {
throw new Error(getCodeFrame(input, line, column) + "Unexpected character " + JSON.stringify(char));
}
pushNumberToken(_value, {
startColumn: _startColumn3
});
eatCharacter(_value.length);
if (char && !PARENS.test(char) && !WHITESPACE.test(char)) {
throw new Error(getCodeFrame(input, line, column) + "Unexpected character " + JSON.stringify(char));
}
continue;
}
if (char === '"') {
var _startColumn4 = column;
var _value2 = "";
eatCharacter(); // "
while (char !== '"') {
if (isNewLine(char)) {
throw new Error(getCodeFrame(input, line, column) + "Unexpected character " + JSON.stringify(char));
}
_value2 += char;
eatCharacter(); // char
}
eatCharacter(); // "
var _endColumn3 = column;
pushStringToken(_value2, {
startColumn: _startColumn4,
endColumn: _endColumn3
});
continue;
}
if (LETTERS.test(char)) {
var _value3 = "";
var _startColumn5 = column;
while (char && LETTERS.test(char)) {
_value3 += char;
eatCharacter();
}
/*
* Handle MemberAccess
*/
if (char === ".") {
var dotStartColumn = column;
if (valtypes.indexOf(_value3) !== -1) {
pushValtypeToken(_value3, {
startColumn: _startColumn5
});
} else {
pushNameToken(_value3);
}
eatCharacter();
_value3 = "";
var nameStartColumn = column;
while (LETTERS.test(char)) {
_value3 += char;
eatCharacter();
}
pushDotToken(".", {
startColumn: dotStartColumn
});
pushNameToken(_value3, {
startColumn: nameStartColumn
});
continue;
}
/*
* Handle keywords
*/
// $FlowIgnore
if (typeof keywords[_value3] === "string") {
pushKeywordToken(_value3, {
startColumn: _startColumn5
});
continue;
}
/*
* Handle types
*/
if (valtypes.indexOf(_value3) !== -1) {
pushValtypeToken(_value3, {
startColumn: _startColumn5
});
continue;
}
/*
* Handle literals
*/
pushNameToken(_value3, {
startColumn: _startColumn5
});
continue;
}
throw new Error(getCodeFrame(input, line, column) + "Unexpected character " + JSON.stringify(char));
}
return tokens;
}
var tokens = tokenTypes;
exports.tokens = tokens;