Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Removing comments from a script?

Asked by
Imperialy 149
4 years ago
Edited 4 years ago

How would you go about removing comments from this

local scr = [[
local t = "hello world" -- cool
local f = "world hello" -- removing comments
]]
print(scr) --outcome (supposed to be) local t = "hello world"local f = "worldhello"
instead of //--\\ local t = "hello world" -- cool
local f = "world hello" -- removing comments

I tried using string.gsub but also failed, also couldn't find any articles to help me

people are saying this is a solution, but I'm trying to make this

comment_symbols = "--"

s1 = [[
"cool" -- lol
"man"   
]]

print ( string.match( s1, "[^"..comment_symbols.."]+" ) ) -- outputs cool

im trying to make this output "cool man" instead of just "cool"

0
Are you talking about the comments highlighted in green or variables? DesignerOfGames -7 — 4y
0
green Imperialy 149 — 4y
0
I think ctrl + f can find and replace stuff in a script. So just put in -- or [[/]] and replace it with nothing Joshument 110 — 4y
0
I think .gsub is too weak for this, regex would work perfectly fine but regex itself is bigger than Lua itself. Either you can implement 4000 lines of regex yourself, or ignore them. Fifkee 2017 — 4y

3 answers

Log in to vote
0
Answered by 3 years ago

Hello everyone, I know this reply is a year late but this is what I came up with:

local astring  = [[
local a = {
    "foo", -- fa fa fa fa fa
    "bar"
}
]]

print(astring:gsub("--(-.-)\n","\n"))

This prints out:

    local a = {
        "foo", 
        "bar"
    }
0
I don't care if you're a year late MiAiHsIs1226 189 — 3y
Ad
Log in to vote
0
Answered by 3 years ago

I'm a year late, yay.

We need it to ignore string formats because it will mess up the code, right?

This is the multi-lined version

local function GetMutliLinedComments(code)
    local commentfound = false -- if the comments found then it will turn true
    local start_ = 0 -- the start of the comment
    local end_ = 0 -- the end of the comment
    local sub = 0 -- its use is to get the next letter(or word)
    local ignore = false -- if the function can ignore the comment inside a format
    local comments = {}
    code:gsub(".",function(c) -- usin gsub( well it's kinda bad cuz it has a limit
        sub = sub + 1 -- what
        if c == '"'  then -- if it is a string format then
            if ignore then 
                ignore = false
            else
                ignore = true
                return -- not gonna do anything
            end
        end

        if c == "'"  then-- if it is a string format then
            if ignore then
                ignore = false
            else
                ignore = true
                return
            end
        end
        if code:sub(sub,sub+1) == "[[" and code:sub(sub-2,sub+1) ~= "--[["  then-- if it is a string format then
            if ignore then
                ignore = false
            else
                print(code:sub(sub-2,sub+1))
                ignore = true
                return
            end
        end
        if not ignore then -- here comes the fun part
            if code:sub(sub,sub+3) == "--[[" and commentfound == false then -- finds a comment format and no comments are found then
                commentfound = true -- a comment is found!
                start_ = sub
            end
            if code:sub(sub,sub+1) == "]]" and commentfound == true then -- finds teh end of a comment format and a comment format is found then
                commentfound = false -- if it's the end of the format no comments are found
                end_ = sub+1 -- just a little bug
                table.insert(comments,code:sub(start_,end_)) -- inserts the comment format into a table
            end
        end
    end)
    return (comments) -- returns the table
end
Log in to vote
-2
Answered by 4 years ago

String.match will work just fine here, just implement it correctly.

http://lua-users.org/wiki/PatternsTutorial https://www.rosettacode.org/wiki/Strip_comments_from_a_string

0
im trying to remove the whole line of comment and nothing else Imperialy 149 — 4y
1
Have you even checked the second link? Ashley_Phantom 372 — 4y
0
HAVE YOU EVEN CHECKED THE SECOND LINK?! Fifkee 2017 — 4y
0
second link doesnt show anything Imperialy 149 — 4y

Answer this question