diff options
-rw-r--r-- | script/core/guide.lua | 39 | ||||
-rw-r--r-- | test/basic/linker.txt | 56 |
2 files changed, 66 insertions, 29 deletions
diff --git a/script/core/guide.lua b/script/core/guide.lua index 3f61e610..0ae4454e 100644 --- a/script/core/guide.lua +++ b/script/core/guide.lua @@ -4,6 +4,11 @@ local guide = require 'parser.guide' local osClock = os.clock local pairs = pairs +local SEARCH_FLAG = { + ['forward'] = 1 << 0, + ['backward'] = 1 << 1, +} + local m = {} -- TODO: compatible @@ -101,29 +106,30 @@ function m.searchRefs(status, source, mode) local search - local function seachSource(obj, expect) + local function seachSource(obj, expect, flag) local link = linker.getLink(obj) if not link then return end - search(link.id, expect or link.id) + search(link.id, expect or link.id, flag) end - local function checkForward(link, expect) + local function checkForward(link, expect, flag) if not link.forward then return end + local forwardID = linker.getForwardID(root, expect) for _, forwardSources in ipairs(link.forward) do - seachSource(forwardSources, expect) + seachSource(forwardSources, expect, flag) end end - local function checkBackward(link, expect) + local function checkBackward(link, expect, flag) if not link.backward then return end for _, backSources in ipairs(link.backward) do - seachSource(backSources, expect) + seachSource(backSources, expect, flag) end end @@ -139,24 +145,23 @@ function m.searchRefs(status, source, mode) end local stackCount = 0 - search = function (id, expect) - local links = linker.getLinksByID(root, id) - if not links then - return - end - if stackCount > 10 then + search = function (expect, flag) + local links = linker.getLinksByID(root, expect) + if links then return end stackCount = stackCount + 1 + flag = flag or 0 for _, eachLink in ipairs(links) do if expect == eachLink.id or expect == '' then m.pushResult(status, mode, eachLink.source) - checkBackward(eachLink, '') - checkForward(eachLink, '') - else - checkBackward(eachLink, expect) - checkForward(eachLink, expect) + end + if flag & SEARCH_FLAG.backward == 0 then + checkForward(eachLink, expect, SEARCH_FLAG.forward) + end + if flag & SEARCH_FLAG.forward == 0 then + checkBackward(eachLink, expect, SEARCH_FLAG.backward) end end --checkLastID(id, expect) diff --git a/test/basic/linker.txt b/test/basic/linker.txt index 18176369..49f27234 100644 --- a/test/basic/linker.txt +++ b/test/basic/linker.txt @@ -1,16 +1,48 @@ ast -> linkers = { - globals = { - ['"X"|"Y"|"Z"'] = {src1, src2, src3}, - ['"X"|"Y"'] = {src4, src5, src6}, - ['"X"'] = {src7, src8, src9}, - }, - locals = { - ['7'] = {src10}, - ['7|"x"'] = {src11}, - }, - tfield = { - ['11|"k"'] = {src12}, - }, + ['g|"X"|"Y"|"Z"'] = {src1, src2, src3}, + ['g|"X"|"Y"'] = {src4, src5, src6}, + ['g|"X"'] = {src7, src8, src9}, + ['l|7'] = {src10}, + ['l|7|"x"'] = {src11}, + ['l|11|"k"'] = {src12}, } +```lua +x.y.<?z?> = <!f!> +<?g?> = x.y.z + +t.<!z!> = 1 +x.y = t + +x = { + y = { + <!z!> = 1 + } +} +``` + +expect: 'l|x|y|z' +forward: 'l|x|y|z' -> f +backward: 'l|x|y|z' -> g +last: 'l|x|y' + 'z' + +expect: 'l|x|y' + '|z' +forward: 'l|t' + '|z' -> 'l|t|z' -> t.z +backward: nil +last: 'l|x' + '|y|z' + +expect: 'l|x' + '|y|z' +forward: 'l|0' + '|y|z' -> 'l|0|y|z' +backward: nil +last: nil + +expect: 'l|0|y|z' +forward: nil +backward: nil +last: 'l|0|y' + '|z' + +expect: 'l|0|y' + '|z' +forward: 'l|1'+ '|z' -> 'l|1|z' -> field z +backward: nil +last: 'l|0' + '|y|z' |