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