Fixed human-readable parser when identifier begins with valid type prefix (#3728).

This commit is contained in:
Richard Moore 2023-02-03 21:50:30 -05:00
parent 803e8f9821
commit 522fd16f68
2 changed files with 12 additions and 9 deletions

View File

@ -137,10 +137,13 @@ const SimpleTokens: Record<string, string> = {
};
// Parser regexes to consume the next token
const regexWhitespace = new RegExp("^(\\s*)");
const regexNumber = new RegExp("^([0-9]+)");
const regexIdentifier = new RegExp("^([a-zA-Z$_][a-zA-Z0-9$_]*)");
const regexType = new RegExp("^(address|bool|bytes([0-9]*)|string|u?int([0-9]*))");
const regexWhitespacePrefix = new RegExp("^(\\s*)");
const regexNumberPrefix = new RegExp("^([0-9]+)");
const regexIdPrefix = new RegExp("^([a-zA-Z$_][a-zA-Z0-9$_]*)");
// Parser regexs to check validity
const regexId = new RegExp("^([a-zA-Z$_][a-zA-Z0-9$_]*)$");
const regexType = new RegExp("^(address|bool|bytes([0-9]*)|string|u?int([0-9]*))$");
/**
* @ignore:
@ -293,7 +296,7 @@ function lex(text: string): TokenString {
// Strip off any leading whitespace
let cur = text.substring(offset);
let match = cur.match(regexWhitespace);
let match = cur.match(regexWhitespacePrefix);
if (match) {
offset += match[1].length;
cur = text.substring(offset);
@ -347,7 +350,7 @@ function lex(text: string): TokenString {
continue;
}
match = cur.match(regexIdentifier);
match = cur.match(regexIdPrefix);
if (match) {
token.text = match[1];
offset += token.text.length;
@ -366,7 +369,7 @@ function lex(text: string): TokenString {
continue;
}
match = cur.match(regexNumber);
match = cur.match(regexNumberPrefix);
if (match) {
token.text = match[1];
token.type = "NUMBER";
@ -841,7 +844,7 @@ export class ParamType {
}
const name = obj.name;
assertArgument(!name || (typeof(name) === "string" && name.match(regexIdentifier)),
assertArgument(!name || (typeof(name) === "string" && name.match(regexId)),
"invalid name", "obj.name", name);
let indexed = obj.indexed;
@ -1019,7 +1022,7 @@ export abstract class NamedFragment extends Fragment {
*/
constructor(guard: any, type: FragmentType, name: string, inputs: ReadonlyArray<ParamType>) {
super(guard, type, inputs);
assertArgument(typeof(name) === "string" && name.match(regexIdentifier),
assertArgument(typeof(name) === "string" && name.match(regexId),
"invalid identifier", "name", name);
inputs = Object.freeze(inputs.slice());
defineProperties<NamedFragment>(this, { name });