Skip to content

Commit 6c66317

Browse files
committed
Remove the unnecessary "longestElement" variable in the Trie creation/traversing mechanism
1 parent ad7c897 commit 6c66317

File tree

2 files changed

+6
-12
lines changed

2 files changed

+6
-12
lines changed

src/Helpers/Helpers.lua

+1-7
Original file line numberDiff line numberDiff line change
@@ -54,16 +54,10 @@ end
5454
--- Creates a trie from the given operators, it's used to support 2+ character (potentional) operators.
5555
-- @param <Table> table The operators to create the trie from.
5656
-- @return <Table> trieTable The trie table.
57-
-- @return <Number> longestElement The length of the longest operator.
5857
function Helpers.makeTrie(table)
5958
local trieTable = {}
60-
local longestElement = 0
6159

6260
for _, op in ipairs(table) do
63-
if #op > longestElement then
64-
longestElement = #op
65-
end
66-
6761
local node = trieTable
6862
for index = 1, #op do
6963
local character = op:sub(index, index)
@@ -72,7 +66,7 @@ function Helpers.makeTrie(table)
7266
end
7367
node.value = op
7468
end
75-
return trieTable, longestElement
69+
return trieTable
7670
end
7771

7872
return Helpers

src/Lexer/Lexer.lua

+5-5
Original file line numberDiff line numberDiff line change
@@ -219,20 +219,21 @@ end
219219
-- @return <Table> operatorToken The next operator token.
220220
function LexerMethods:consumeOperator()
221221
local node = self.operatorsTrie
222-
local longestOperator = self.longestOperator
223222
local charStream = self.charStream
224223
local curCharPos = self.curCharPos
225224
local operator
226225

227226
-- Trie walker
228-
for index = 0, longestOperator - 1 do
227+
local index = 0
228+
while true do
229229
-- Use raw charStream instead of methods for optimization
230230
local character = charStream[curCharPos + index]
231231
node = node[character] -- Advance to the deeper node
232232
if not node then break end
233233
if node.value then
234234
operator = node.value
235235
end
236+
index = index + 1
236237
end
237238
if operator then
238239
self:consume(#operator - 1)
@@ -299,7 +300,7 @@ function LexerMethods:resetToInitialState(charStream, operators)
299300
self.curCharPos = 1
300301

301302
self.operators = operators or DEFAULT_OPERATORS
302-
self.operatorsTrie, self.longestOperator = makeTrie(self.operators)
303+
self.operatorsTrie = makeTrie(self.operators)
303304
end
304305

305306
--- Runs the lexer.
@@ -331,11 +332,10 @@ function Lexer:new(expression, operators, charPos)
331332
end
332333
if operators then
333334
LexerInstance.operators = operators
334-
LexerInstance.operatorsTrie, LexerInstance.longestOperator = makeTrie(operators)
335+
LexerInstance.operatorsTrie = makeTrie(operators)
335336
else
336337
LexerInstance.operators = DEFAULT_OPERATORS
337338
LexerInstance.operatorsTrie = DEFAULT_OPERATORS_TRIE
338-
LexerInstance.longestOperator = DEFAULT_LONGEST_OPERATOR
339339
end
340340

341341
local function inheritModule(moduleName, moduleTable)

0 commit comments

Comments
 (0)