Archived
1
0

Added my email to license, renamed parser.lua to tokenizer.lua, changed all instances of "parser" to "tokenizer", improved tokenizer:getToken() function

This commit is contained in:
Benjamin Moir 2015-06-06 14:17:23 +09:30
parent 8f637feac8
commit 1d64ae4e33
4 changed files with 137 additions and 130 deletions

View File

@ -1,4 +1,4 @@
Copyright (c) 2015 Benjamin Moir Copyright (c) 2015 Benjamin Moir <bennyboy.private@hotmail.com.au>
Copyright (c) 2015 Marrub <marrub@greyserv.net> Copyright (c) 2015 Marrub <marrub@greyserv.net>
Permission is hereby granted, free of charge, to any person obtaining a copy Permission is hereby granted, free of charge, to any person obtaining a copy

View File

@ -1,127 +1,134 @@
--[[ --[[
Copyright (c) 2015 Benjamin Moir Copyright (c) 2015 Benjamin Moir <bennyboy.private@hotmail.com.au>
Copyright (c) 2015 Marrub <marrub@greyserv.net> Copyright (c) 2015 Marrub <marrub@greyserv.net>
Permission is hereby granted, free of charge, to any person obtaining a copy Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions: furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software. all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE. THE SOFTWARE.
--]] --]]
local ffi = require("ffi") local ffi = require("ffi")
local parser = {} local tokenizer = {}
local loveToken = ffi.load("LoveToken") local loveToken = ffi.load("LoveToken")
ffi.cdef([[ ffi.cdef([[
typedef struct typedef struct
{ {
bool escapeChars; bool escapeChars;
bool stripInvalid; bool stripInvalid;
bool doConvert; bool doConvert;
const char *fromCode; const char *fromCode;
const char *toCode; const char *toCode;
} LT_InitInfo; } LT_InitInfo;
typedef struct typedef struct
{ {
char *token; char *token;
char *string; char *string;
int pos; int pos;
} LT_Token; } LT_Token;
typedef struct typedef struct
{ {
bool failure; bool failure;
const char *str; const char *str;
} LT_AssertInfo; } LT_AssertInfo;
void LT_Init(LT_InitInfo initInfo); void LT_Init(LT_InitInfo initInfo);
void LT_Quit(); void LT_Quit();
bool LT_Assert(bool assertion, const char *str); bool LT_Assert(bool assertion, const char *str);
LT_AssertInfo LT_CheckAssert(); LT_AssertInfo LT_CheckAssert();
bool LT_OpenFile(const char *filePath); bool LT_OpenFile(const char *filePath);
void LT_CloseFile(); void LT_CloseFile();
char *LT_ReadNumber(); char *LT_ReadNumber();
char *LT_ReadString(char term); char *LT_ReadString(char term);
char *LT_Escaper(char *str, size_t pos, char escape); char *LT_Escaper(char *str, size_t pos, char escape);
LT_Token LT_GetToken(); LT_Token LT_GetToken();
]]) ]])
local pReturn local pReturn
function parser:init(initInfo, filePath) function tokenizer:init(initInfo, filePath)
loveToken.LT_Init(initInfo) loveToken.LT_Init(initInfo)
loveToken.LT_OpenFile(filePath) loveToken.LT_OpenFile(filePath)
end end
function parser:assert(assertion, str) function tokenizer:assert(assertion, str)
return loveToken.LT_Assert(assertion, str) return loveToken.LT_Assert(assertion, str)
end end
function parser:checkError() function tokenizer:checkError()
ltAssertion = loveToken.LT_CheckAssert() ltAssertion = loveToken.LT_CheckAssert()
if (ltAssertion.str == nil) then if (ltAssertion.str == nil) then
assert(not ltAssertion.failure, "unknown assertion") assert(not ltAssertion.failure, "unknown assertion")
else else
assert(not ltAssertion.failure, ffi.string(ltAssertion.str)) assert(not ltAssertion.failure, ffi.string(ltAssertion.str))
end end
end end
function parser:openFile(filePath) function tokenizer:openFile(filePath)
pReturn = loveToken.LT_OpenFile(filePath) pReturn = loveToken.LT_OpenFile(filePath)
parser:checkError() tokenizer:checkError()
return pReturn return pReturn
end end
function parser:closeFile() function tokenizer:closeFile()
loveToken.LT_CloseFile() loveToken.LT_CloseFile()
end end
function parser:quit() function tokenizer:quit()
loveToken.LT_CloseFile() loveToken.LT_CloseFile()
loveToken.LT_Quit() loveToken.LT_Quit()
end end
function parser:readNumber() function tokenizer:readNumber()
pReturn = loveToken.LT_ReadNumber() pReturn = loveToken.LT_ReadNumber()
parser:checkError() tokenizer:checkError()
return ffi.string(pReturn) return ffi.string(pReturn)
end end
function parser:readString(term) function tokenizer:readString(term)
pReturn = loveToken.LT_ReadString(term) pReturn = loveToken.LT_ReadString(term)
parser:checkError() tokenizer:checkError()
return ffi.string(pReturn) return ffi.string(pReturn)
end end
function parser:escaper(str, pos, escape) function tokenizer:escaper(str, pos, escape)
pReturn = loveToken.LT_Escaper(str, pos, escape) pReturn = loveToken.LT_Escaper(str, pos, escape)
parser:checkError() tokenizer:checkError()
return ffi.string(pReturn) return ffi.string(pReturn)
end end
function parser:getToken() function tokenizer:getToken()
pReturn = loveToken.LT_GetToken() pReturn = loveToken.LT_GetToken()
parser:checkError() tokenizer:checkError()
return pReturn local lt = {}
end lt.token = ffi.string(pReturn.token)
lt.string = pReturn.string
return parser lt.pos = pReturn.pos
if (pReturn.string ~= nil) then
lt.string = ffi.string(pReturn.string)
end
return lt
end
return tokenizer

View File

@ -1,5 +1,5 @@
/* /*
Copyright (c) 2015 Benjamin Moir Copyright (c) 2015 Benjamin Moir <bennyboy.private@hotmail.com.au>
Copyright (c) 2015 Marrub <marrub@greyserv.net> Copyright (c) 2015 Marrub <marrub@greyserv.net>
Permission is hereby granted, free of charge, to any person obtaining a copy Permission is hereby granted, free of charge, to any person obtaining a copy

View File

@ -1,5 +1,5 @@
/* /*
Copyright (c) 2015 Benjamin Moir Copyright (c) 2015 Benjamin Moir <bennyboy.private@hotmail.com.au>
Copyright (c) 2015 Marrub <marrub@greyserv.net> Copyright (c) 2015 Marrub <marrub@greyserv.net>
Permission is hereby granted, free of charge, to any person obtaining a copy Permission is hereby granted, free of charge, to any person obtaining a copy