This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
LewisTehMinerz's profile picture

"LuaPlus.lua:18: vm error: java.lang.ArrayIndexOutOfBoundsException: -1" Help

Started by LewisTehMinerz, 21 January 2015 - 04:13 PM
LewisTehMinerz #1
Posted 21 January 2015 - 05:13 PM

local tBlacklist = {
["{"] = true,
["}"] = true,
}
local function stripwords(str, words)
	local w = {};
	return str:gsub("([^%s.,!?]+)%s*", function(word)
		if words[word] then return "" end
		w[#w+1] = word
	end), w;
end
function replacetext(source, find, replace, wholeword)
  if wholeword then
	find = '%f[%a]'..find..'%f[%A]'
  end
  return (source:gsub(find,replace))
end
function parse( _S )
if fs.exists( _S ) then
  _F = fs.open( _S, "r" )
  local _CONTENTS = _F.readAll()
  _TMP = fs.open( "tmp.LPT", "w" )
  local _returnedFile = stripwords( _CONTENTS, tBlacklist )
  local s = _returnedFile
  _returnedFile = replacetext( s, "super", "printError", true )
  _returnedFile = replacetext( s, "constructor", "--[[ Constructor not used... Sorry! ]]--", true )
  _returnedFile = replacetext( s, "void", "function", true )
  _returnedFile = replacetext( s, "class", "--[[ LuaPlus Class ]]--", true )
  _TMP.write( returnedFile )
  _TMP.close()
  _F.close()
  --shell.run( "tmp.LPT" )
  print( _returnedFile )
  fs.delete( "tmp.LPT" )
end
end
function openEditor( _FILEFOREDIT )
shell.run( "/.luaplus/Editor.lua " .. _FILEFOREDIT)
end
-- , "{" = true, "}" = true,

I don't get why line 16 errors. Can someone help? EDIT: Copy-Paste didn't copy linebreaks.
Edited on 21 January 2015 - 04:40 PM
GopherAtl #2
Posted 21 January 2015 - 05:23 PM
because %f isn't a thing, and gsub fails gracelessly.

Also, please, for the love of god people, paste complete, unaltered code. The "18" is a line number, and you've apparently removed lines in the pasting that made what was line 18 into line 16. Even if all you removed were comments, this sort of thing makes a barrier to people helping you, which you ought to want to avoid.
Edited on 21 January 2015 - 04:25 PM
LewisTehMinerz #3
Posted 21 January 2015 - 05:28 PM
This is the full code… Screenshot here. Line 44 is the end. I believe it's the _S
Edited on 21 January 2015 - 04:30 PM
GopherAtl #4
Posted 21 January 2015 - 05:37 PM
something in your copy-paste process removed the blank lines between functions, as the 16th line here is indeed numbered 18 in that screenshot.

%f in a pattern to any of lua's string pattern functions definitely throws a "java.lang.ArrayIndexOutOfBoundsException" error. If "wholeword" is true, as it is on lines 28-31, then %fs are passed to gsub on line 18, hence, error.

What is it you thought %f would do exactly?
Edited on 21 January 2015 - 04:39 PM
Lyqyd #5
Posted 21 January 2015 - 05:38 PM
But it's been altered. See how there are blank lines between things in your screenshot, but not on the forums? That means that line 18 is two different lines depending on where you look, which makes it difficult to track down the error.
LewisTehMinerz #6
Posted 21 January 2015 - 05:43 PM
Yeah, looks like Copy-Paste removed my linebreaks, I'll update it now.

I didn't write the replacetext function. I found it and I was seeing if it worked. Apparently, no… Can be found here.
Edited on 21 January 2015 - 04:44 PM
Exerro #7
Posted 21 January 2015 - 05:55 PM
The forums remove blank lines in code blocks, it's not his fault.

I'm guessing if wholeword is true you want to check if it's a whole word? You may want to use "%s" which looks for a space character, or "%W" which looks for anything but an alphanumeric character (including symbols and spaces).
GopherAtl #8
Posted 21 January 2015 - 05:58 PM
The forums remove blank lines in code blocks, it's not his fault.
Wat?


This

is

a

test

No… it doesn't…O_o


I'm guessing if wholeword is true you want to check if it's a whole word? You may want to use "%s" which looks for a space character, or "%W" which looks for anything but an alphanumeric character (including symbols and spaces).

Not quite as simple as that, as a look at the discussions on the page he linked, and the page linked from there about the frontier pattern, as %f is apparentyl called, make clear. This is one of those areas where lua patterns are weak compared to their buff cousins regular expressions.

:edit: Actually… in this specific case… something as simple as "%w*"..find.."%w*" might reliably match the whole word containing find…
Edited on 21 January 2015 - 05:03 PM
LewisTehMinerz #9
Posted 21 January 2015 - 06:23 PM
Right, it's now erroring "Java exception thrown"… Help
Exerro #10
Posted 21 January 2015 - 06:31 PM
The forums remove blank lines in code blocks, it's not his fault.
Wat?


This

is

a

test

No… it doesn't…O_o


I'm guessing if wholeword is true you want to check if it's a whole word? You may want to use "%s" which looks for a space character, or "%W" which looks for anything but an alphanumeric character (including symbols and spaces).

Not quite as simple as that, as a look at the discussions on the page he linked, and the page linked from there about the frontier pattern, as %f is apparentyl called, make clear. This is one of those areas where lua patterns are weak compared to their buff cousins regular expressions.

:edit: Actually… in this specific case… something as simple as "%w*"..find.."%w*" might reliably match the whole word containing find…

Are you sure you didn't do anything special to get the lines left in. There have been multiple cases where I've been annoyed at how "in one lump" the code I put on here is due to it removing lines. Might be something I'm doing wrong (although as lewislovesgames had the same problem, it's probably something you've changed).

Frontier pattern
I had also never heard of this, but seems to be a thing. Tested in the Lua prompt (not CC) and it didn't throw an error, although it also didn't work as expected.
LewisTehMinerz #11
Posted 21 January 2015 - 06:33 PM
Fixed Java Exception Thrown. Looks like everything is working now.
Lyqyd #12
Posted 21 January 2015 - 06:54 PM
The forums remove blank lines in code blocks, it's not his fault.

No, they don't. I've only ever heard of this happening if you paste code into the little pop-up thing when you click the code button on the fancy editor. Don't do that, just hit okay and paste it between the tags it adds.
LewisTehMinerz #13
Posted 21 January 2015 - 06:58 PM
UPDATE: Not writing to the file correctly. I've stopped using stripwords because it removes the linebreaks…

It replaces only the class with
--[[ LuaPlus Class ]]--
which is correct. It doesn't replace anything else like void, super or constructor, only class. Help!


local tBlacklist = {
["{"] = true,
["}"] = true,
}

local function stripwords(str, words)
    local w = {};
    return str:gsub("([^%s.,!?]+)%s*", function(word)
        if words[word] then return "" end
        w[#w+1] = word
    end), w;
end

function replacetext(source, find, replace, wholeword)
  if wholeword then
    find = '%w[%a]'..find..'%w[%A]'
  end
  return (source:gsub(find,replace))
end

function parse( _S )
if fs.exists( _S ) then
_F = fs.open( _S, "r" )
local _CONTENTS = _F.readAll()
print( _CONTENTS )
_TMP = fs.open( "tmp.LPT", "w" )
--local returnedFile = ( _CONTENTS, tBlacklist )
--print( returnedFile )
local returnedFile
returnedFile = replacetext( _CONTENTS, "{", "", true )
returnedFile = replacetext( _CONTENTS, "}", "", false )
----  local s = returnedFile
---- print( s )
returnedFile = replacetext( _CONTENTS, "super", "printError", true )
returnedFile = replacetext( _CONTENTS, "constructor", "--[[ Constructor not used... Sorry! ]]--", false )
returnedFile = replacetext( _CONTENTS, "void", "function", true )
returnedFile = replacetext( _CONTENTS, "class", "--[[ LuaPlus Class ]]--", false )
print( returnedFile )
_TMP.write( returnedFile )
_TMP.close()
_F.close()
shell.run( "tmp.LPT" )
--fs.delete( "tmp.LPT" )
else
error( "LuaPlus Parse Failed: File is not existent.", 0 )
end
end

function openEditor( _FILEFOREDIT )
shell.run( "/.luaplus/Editor.lua " .. _FILEFOREDIT)
end
-- , "{" = true, "}" = true,


BTW, its stopped replacing "{" and "}" because I removed stripwords.
GopherAtl #14
Posted 21 January 2015 - 07:00 PM
The forums remove blank lines in code blocks, it's not his fault.

No, they don't. I've only ever heard of this happening if you paste code into the little pop-up thing when you click the code button on the fancy editor. Don't do that, just hit okay and paste it between the tags it adds.
aah, that makes sense then. I've never used the "code" button, I just type the code tags myself, lol.

Frontier pattern
I had also never heard of this, but seems to be a thing. Tested in the Lua prompt (not CC) and it didn't throw an error, although it also didn't work as expected.

I had already tested in cc and confirmed it generated the original error message, so.. yeah. Just potentially confusing things by saying it works in normal lua 5.1+.


:edit: Back on topic!
Every time you call replacetext on the original text in _CONTENTS and store the result to returnedFile. So naturally, you're only seeing the results of the last replace in returnedFile. You want to keep applying each substitution to the output of the previous substitution.
Edited on 21 January 2015 - 06:01 PM
LewisTehMinerz #15
Posted 22 January 2015 - 07:15 AM
Ah. what would be the fix?
Bomb Bloke #16
Posted 22 January 2015 - 07:45 AM
Ah. what would be the fix?

You want to keep applying each substitution to the output of the previous substitution.

Eg:

local returnedFile
returnedFile = replacetext( _CONTENTS, "{", "", true )      -- Use _CONTENTS as the "source" once,
returnedFile = replacetext( returnedFile, "}", "", false )  -- ... then use "returnedFile" each time after that.
.
.
.
LewisTehMinerz #17
Posted 22 January 2015 - 08:02 AM
got it.