Skip to content

Commit

Permalink
Fixed human-readable parser when identifier begins with valid type pr…
Browse files Browse the repository at this point in the history
…efix (#3728).
  • Loading branch information
ricmoo committed Feb 4, 2023
1 parent 803e8f9 commit 522fd16
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 9 deletions.
File renamed without changes.
21 changes: 12 additions & 9 deletions src.ts/abi/fragments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand All @@ -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";
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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 });
Expand Down

0 comments on commit 522fd16

Please sign in to comment.