Skip to content

Commit a25c8bb

Browse files
committed
refactoring
1 parent 3e0b53a commit a25c8bb

File tree

1 file changed

+43
-56
lines changed

1 file changed

+43
-56
lines changed

nattlua/c_declarations/preprocessor.lua

Lines changed: 43 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -43,39 +43,19 @@ do
4343
return false
4444
end
4545

46-
local function tostring_tokens(tokens, pos)
47-
if not tokens then return "" end
48-
49-
local str = ""
50-
local str_point = ""
51-
52-
for i, tk in ipairs(tokens) do
53-
str = str .. " " .. tk.value
54-
str_point = str_point .. " " .. (i == pos and "^" or (" "):rep(#tk.value))
55-
end
56-
57-
return str .. "\n" .. str_point
58-
end
59-
60-
local function is_stringinfy(self)
61-
return self:GetToken(-1).value == "#" and self:GetToken(-2).value ~= "#"
62-
end
63-
6446
do -- for normal define, can be overriden
6547
function META:Define(identifier, args, tokens)
6648
local new_tokens = {}
6749

6850
for i, v in ipairs(tokens) do
69-
new_tokens[i] = {
70-
value = v.value,
71-
type = v.type,
72-
whitespace = {
73-
{value = " ", type = "space"},
74-
},
51+
local tk = self:NewToken(v.type, v.value)
52+
tk.whitespace = {
53+
{value = " ", type = "space"},
7554
}
55+
new_tokens[i] = tk
7656
end
7757

78-
self.defines[identifier] = {args = args, tokens = new_tokens}
58+
self.defines[identifier] = {args = args, tokens = new_tokens, identifier = identifier}
7959
end
8060

8161
function META:Undefine(identifier)
@@ -88,17 +68,15 @@ do
8868
local new_tokens = {}
8969

9070
for i, v in ipairs(tokens) do
91-
new_tokens[i] = {
92-
value = v.value,
93-
type = v.type,
94-
whitespace = {
95-
{value = " ", type = "space"},
96-
},
71+
local tk = self:NewToken(v.type, v.value)
72+
tk.whitespace = {
73+
{value = " ", type = "space"},
9774
}
75+
new_tokens[i] = tk
9876
end
9977

10078
self.define_stack[identifier] = self.define_stack[identifier] or {}
101-
table.insert(self.define_stack[identifier], 1, {args = args, tokens = new_tokens})
79+
table.insert(self.define_stack[identifier], 1, {args = args, tokens = new_tokens, identifier = identifier})
10280
end
10381

10482
function META:PushUndefine(identifier)
@@ -111,7 +89,17 @@ do
11189
end
11290
end
11391

114-
function META:GetDefinition(identifier)
92+
function META:GetDefinition(identifier, offset)
93+
if not identifier then
94+
local tk = self:GetToken(offset)
95+
96+
if not tk then return false end
97+
98+
if tk.type ~= "letter" then return false end
99+
100+
identifier = tk.value
101+
end
102+
115103
if self.define_stack[identifier] then
116104
return self.define_stack[identifier][1]
117105
end
@@ -205,8 +193,21 @@ do
205193
return args
206194
end
207195

208-
function META:PrintState()
209-
print("\n" .. tostring_tokens(self.tokens, self:GetPosition()))
196+
function META:PrintState(tokens, pos)
197+
pos = pos or self:GetPosition()
198+
199+
if not tokens then return "" end
200+
201+
local str = ""
202+
local str_point = ""
203+
204+
for i, tk in ipairs(tokens) do
205+
str = str .. " " .. tk.value
206+
str_point = str_point .. " " .. (i == pos and "^" or (" "):rep(#tk.value))
207+
end
208+
209+
str = str .. "\n" .. str_point
210+
print("\n" .. str)
210211
end
211212

212213
function META:ReadDefine()
@@ -255,11 +256,7 @@ do
255256
end
256257

257258
function META:ExpandMacroCall()
258-
local tk = self:GetToken()
259-
260-
if not tk then return false end
261-
262-
local def = self:GetDefinition(tk.value)
259+
local def = self:GetDefinition()
263260

264261
if not (def and self:IsTokenValue("(", 1)) then return false end
265262

@@ -274,7 +271,7 @@ do
274271

275272
self.current_token_index = start
276273

277-
if tk.value == "__VA_OPT__" then
274+
if def.identifier == "__VA_OPT__" then
278275
local va = self:GetDefinition("__VA_ARGS__")
279276

280277
if not va or #va.tokens == 0 or va.tokens[1].value == "" then
@@ -368,13 +365,11 @@ do
368365
return false
369366
end
370367

371-
local tk_left = self:GetToken()
372-
local def_left = self:GetDefinition(tk_left.value)
368+
local def_left = self:GetDefinition()
373369

374370
if not def_left then return false end
375371

376-
local tk_right = self:GetToken(3)
377-
local def_right = self:GetDefinition(tk_right.value)
372+
local def_right = self:GetDefinition(nil, 3)
378373

379374
if not def_right then return false end
380375

@@ -396,11 +391,7 @@ do
396391
function META:ExpandMacroString()
397392
if not self:IsTokenValue("#") then return false end
398393

399-
local tk = self:GetToken(1)
400-
401-
if not tk then return false end
402-
403-
local def = self:GetDefinition(tk.value)
394+
local def = self:GetDefinition(nil, 1)
404395

405396
if not def then return false end
406397

@@ -411,7 +402,7 @@ do
411402
output = output .. tk.value
412403
end
413404

414-
tk = self:NewToken("string", "\"" .. output .. "\"")
405+
local tk = self:NewToken("string", "\"" .. output .. "\"")
415406
tk.whitespace = {
416407
{value = " ", type = "space"},
417408
}
@@ -422,11 +413,7 @@ do
422413
end
423414

424415
function META:ExpandMacro()
425-
local tk = self:GetToken()
426-
427-
if not tk then return false end
428-
429-
local def = self:GetDefinition(tk.value)
416+
local def = self:GetDefinition()
430417

431418
if not def then return false end
432419

0 commit comments

Comments
 (0)