Skip to content

Commit

Permalink
Add tokenIndex to SymbolNode on parse
Browse files Browse the repository at this point in the history
  • Loading branch information
matthew-canestraro committed Sep 28, 2022
1 parent 98f83c6 commit 9c24329
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
3 changes: 2 additions & 1 deletion src/expression/node/SymbolNode.js
Expand Up @@ -28,14 +28,15 @@ export const createSymbolNode = /* #__PURE__ */ factory(name, dependencies, ({ m
* @param {string} name
* @extends {Node}
*/
constructor (name) {
constructor (name, index) {
super()
// validate input
if (typeof name !== 'string') {
throw new TypeError('String expected for parameter "name"')
}

this.name = name
this.tokenIndex = index;
}

get type () { return 'SymbolNode' }
Expand Down
7 changes: 5 additions & 2 deletions src/expression/parse.js
Expand Up @@ -258,6 +258,7 @@ export const createParse = /* #__PURE__ */ factory(name, dependencies, ({
state.tokenType = TOKENTYPE.NULL
state.token = ''
state.comment = ''
state.tokenIndex = state.index;

// skip over ignored characters:
while (true) {
Expand Down Expand Up @@ -668,9 +669,10 @@ export const createParse = /* #__PURE__ */ factory(name, dependencies, ({
if (isSymbolNode(node)) {
// parse a variable assignment like 'a = 2/3'
name = node.name
const symbolIndex = state.tokenIndex - name.length;
getTokenSkipNewline(state)
value = parseAssignment(state)
return new AssignmentNode(new SymbolNode(name), value)
return new AssignmentNode(new SymbolNode(name, symbolIndex), value)
} else if (isAccessorNode(node)) {
// parse a matrix subset assignment like 'A[1,2] = 4'
getTokenSkipNewline(state)
Expand Down Expand Up @@ -1331,7 +1333,8 @@ export const createParse = /* #__PURE__ */ factory(name, dependencies, ({
} else if (NUMERIC_CONSTANTS.indexOf(name) !== -1) { // NaN, Infinity
node = new ConstantNode(numeric(name, 'number'))
} else {
node = new SymbolNode(name)
const symbolIndex = state.tokenIndex - name.length;
node = new SymbolNode(name, symbolIndex)
}

// parse function parameters and matrix index
Expand Down

0 comments on commit 9c24329

Please sign in to comment.