Skip to content

Commit 5f879b1

Browse files
committed
Special handling for commas inside functions
1 parent 15d55d7 commit 5f879b1

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

src/parser.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,16 @@ export function tokenize(str: string) {
5555
const tokens = [];
5656
let buffer = '';
5757
let type = TokenType.UNKNOWN;
58+
let parenDepth = 0;
5859

5960
for (const s of str) {
6061

62+
if (['(', '[', '{'].includes(s)) {
63+
parenDepth++;
64+
} else if ([')', ']', '}'].includes(s)) {
65+
parenDepth--;
66+
}
67+
6168
// Handle Strings
6269
if (s === '"') {
6370
const newType: TokenType = (((type as TokenType) === TokenType.STR) ? TokenType.UNKNOWN : TokenType.STR);
@@ -71,6 +78,22 @@ export function tokenize(str: string) {
7178
continue;
7279
}
7380

81+
// Special handling for commas
82+
if (s === ',') {
83+
// If we're in a number and not inside parenthesis include the comma
84+
if (type === TokenType.NUM && parenDepth === 0) {
85+
buffer += s;
86+
continue;
87+
}
88+
// Otherwise treat it as an operator
89+
const token = createToken(buffer, type);
90+
if (token) tokens.push(token);
91+
tokens.push(new ExprOperator(','));
92+
buffer = '';
93+
type = TokenType.UNKNOWN;
94+
continue;
95+
}
96+
7497
const sType = s.match(/[0-9.]/) ? TokenType.NUM :
7598
IDENTIFIER_SYMBOLS.includes(s) ? TokenType.VAR :
7699
OPERATOR_SYMBOLS.includes(s) ? TokenType.OP :

0 commit comments

Comments
 (0)