diff options
author | Benoit Giannangeli <giann@users.noreply.github.com> | 2017-03-01 11:54:57 +0100 |
---|---|---|
committer | Benoit Giannangeli <benoit.giannangeli@boursorama.fr> | 2017-03-01 12:19:22 +0100 |
commit | 166b951b9d05eff654ef664124f17c8a37f418a6 (patch) | |
tree | a783d5f05cd640d06065c514b87bc7cf74f5e506 /tests/llex.js | |
parent | 94a301a27a8a75c4684790a99a898262b8354f68 (diff) | |
parent | 444182dbbb18f44cf7cafc378f092c28006be365 (diff) | |
download | fengari-166b951b9d05eff654ef664124f17c8a37f418a6.tar.gz fengari-166b951b9d05eff654ef664124f17c8a37f418a6.tar.bz2 fengari-166b951b9d05eff654ef664124f17c8a37f418a6.zip |
Merge pull request #2 from giann/feature/lex-parse
Lexing & Parsing
Diffstat (limited to 'tests/llex.js')
-rw-r--r-- | tests/llex.js | 97 |
1 files changed, 97 insertions, 0 deletions
diff --git a/tests/llex.js b/tests/llex.js new file mode 100644 index 0000000..8b69acc --- /dev/null +++ b/tests/llex.js @@ -0,0 +1,97 @@ +"use strict"; + +const test = require('tape'); +const beautify = require('js-beautify').js_beautify; + +const tests = require("./tests.js"); + +const lapi = require("../src/lapi.js"); +const lauxlib = require("../src/lauxlib.js"); +const llex = require("../src/llex.js"); +const lua = require('../src/lua.js'); +const R = llex.RESERVED; + + +test('basic lexing: TK_RETURN, TK_STRING', function (t) { + let luaCode = ` + return "hello lex !" + `, L; + + t.plan(2); + + let readTokens = []; + + t.doesNotThrow(function () { + + L = lauxlib.luaL_newstate(); + + let ls = new llex.LexState(); + llex.luaX_setinput(L, ls, new llex.MBuffer(luaCode), luaCode, luaCode.charAt(0)); + + llex.luaX_next(ls); + + while (ls.t.token !== R.TK_EOS) { + // console.log(llex.luaX_tokens[ls.t.token - llex.FIRST_RESERVED]); + + readTokens.push(ls.t.token); + llex.luaX_next(ls); + } + + + }, "JS Lua program ran without error"); + + t.deepEqual( + readTokens, + [R.TK_RETURN, R.TK_STRING], + "Correct tokens found" + ); + +}); + + +test('TK_LOCAL, TK_NAME, TK_INT', function (t) { + let luaCode = ` + local f = testing(aBunch) + return "of" + end + + return f("things") + 12 + `, L; + + t.plan(2); + + let readTokens = []; + + t.doesNotThrow(function () { + + L = lauxlib.luaL_newstate(); + + let ls = new llex.LexState(); + llex.luaX_setinput(L, ls, new llex.MBuffer(luaCode), luaCode, luaCode.charAt(0)); + + llex.luaX_next(ls); + + while (ls.t.token !== R.TK_EOS) { + // console.log(ls.t.token >= llex.FIRST_RESERVED ? + // llex.luaX_tokens[ls.t.token - llex.FIRST_RESERVED] + // : ls.t.token); + + readTokens.push(ls.t.token); + llex.luaX_next(ls); + } + + + }, "JS Lua program ran without error"); + + t.deepEqual( + readTokens, + [ + R.TK_LOCAL, R.TK_NAME, '=', R.TK_NAME, '(', R.TK_NAME, ')', + R.TK_RETURN, R.TK_STRING, + R.TK_END, + R.TK_RETURN, R.TK_NAME, '(', R.TK_STRING, ')', '+', R.TK_INT + ], + "Correct tokens found" + ); + +});
\ No newline at end of file |