-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpexp.lua
More file actions
135 lines (119 loc) · 4.09 KB
/
pexp.lua
File metadata and controls
135 lines (119 loc) · 4.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
-- =============================================================================
-- Pascal Expert
-- =============================================================================
local LPascalExpert = {}
local LComments, LLitterals, LDirectives = {}, {}, {} -- Tables
function LPascalExpert.AnalyzePascalText(AText)
--[[
This is the main function. It
- searches comments, litterals and directives;
- copies them in tables;
- replaces them with numerotated marks (as you can see in debug1.txt).
--]]
local LText = AText
local i, j, k = 1, 0, 0 -- Indexes
local c, l, d = false, false, false -- Flags (comment, litteral, directive)
local s = nil -- Character
while i <= #LText do
s = string.sub(LText, i, i)
if c then
if ((c == '{') and (s == '}'))
or ((c == '(') and (s == ')') and (string.sub(LText, i - 1, i - 1) == '*'))
or ((c == '/') and (string.sub(LText, i + 1, i + 1) == '\n')) then
k = i
table.insert(LComments, string.sub(LText, j, k))
LText = string.sub(LText, 1, j - 1) .. '_comment_' .. #LComments .. '_' .. string.sub(LText, k + 1)
c = false
i = j - 1
end
else
if l then
if (s == "'") then
l = l + 1
if (l % 2 == 0) and (string.sub(LText, i + 1, i + 1) ~= "'") then
k = i
table.insert(LLitterals, string.sub(LText, j, k))
LText = string.sub(LText, 1, j - 1) .. '_litteral_' .. #LLitterals .. '_' .. string.sub(LText, k + 1)
l = false
i = j - 1
end
end
else
if d then
if (s == '}') then
k = i
table.insert(LDirectives, string.sub(LText, j, k))
LText = string.sub(LText, 1, j - 1) .. '_directive_' .. #LDirectives .. '_' .. string.sub(LText, k + 1)
d = false
i = j - 1
end
else
if ((s == '{') and (string.sub(LText, i + 1, i + 1) ~= '$'))
or ((s == '(') and (string.sub(LText, i + 1, i + 1) == '*'))
or ((s == '/') and (string.sub(LText, i + 1, i + 1) == '/')) then
j = i
c = s
else
if (s == "'") then
j = i
l = 1
else
if ((s == '{') and (string.sub(LText, i + 1, i + 1) == '$')) then
j = i
d = true
end
end
end
end
end
end
i = i + 1
end
return LText
end
function LPascalExpert.RestoreComments(AText)
local LText = AText
local i, j, k
j, k = string.find(LText, '_comment_[%d]+_')
while j do
i = string.match(string.sub(LText, j, k), '[%d]+')
i = tonumber(i)
LText = string.sub(LText, 1, j - 1) .. LComments[i] .. string.sub(LText, k + 1)
j, k = string.find(LText, '_comment_[%d]+_', j)
end
return LText
end
function LPascalExpert.RestoreLitterals(AText)
local LText = AText
local i, j, k
j, k = string.find(LText, '_litteral_[%d]+_')
while j do
i = string.match(string.sub(LText, j, k), '[%d]+')
i = tonumber(i)
LText = string.sub(LText, 1, j - 1) .. LLitterals[i] .. string.sub(LText, k + 1)
j, k = string.find(LText, '_litteral_[%d]+_', j)
end
return LText
end
function LPascalExpert.RestoreDirectives(AText)
local LText = AText
local i, j, k
j, k = string.find(LText, '_directive_[%d]+_')
while j do
i = string.match(string.sub(LText, j, k), '[%d]+')
i = tonumber(i)
LText = string.sub(LText, 1, j - 1) .. LDirectives[i] .. string.sub(LText, k + 1)
j, k = string.find(LText, '_directive_[%d]+_', j)
end
return LText
end
function LPascalExpert.CreateDebugFile(AFileName)
if AFileName ~= nil then
LFile = io.open(AFileName, 'w')
for i = 1, #LComments do LFile:write('COMMENT[' .. i .. ']=<<' .. LComments[i] .. '>>\n') end
for i = 1, #LLitterals do LFile:write('LITTERAL[' .. i .. ']=<<' .. LLitterals[i] .. '>>\n') end
for i = 1, #LDirectives do LFile:write('DIRECTIVE[' .. i .. ']=<<' .. LDirectives[i] .. '>>\n') end
LFile:close()
end
end
return LPascalExpert