Page 1 of 1

Navigator Helper for Bison/Flexx

Posted: Thu Sep 18, 2008 4:49 am
by jussij
For more details refer to the comments section of the macro code shown below.

Code: Select all

--
--        Name: Output Navigation Helper Macro
--
--      Author: Jussi Jumppanen
--
--    Language: Lua Macro
--
-- Description: This macro script tries to help with the navigation
--              from an import file to a corresponding line of a
--              matching output file.
--
--              To use the macro place the line of code in the input
--              file and the macro will try to locate the corresponding
--              line of code in the output file.
--
--              The output file is assumed to be tagged with input line
--              markers that use the [b]#line 10 "filename.l"[/b] format.
--
--        NOTE: The macro assumes the input files are bison (.y)
--              or flexx (.l) and the output files are C++ (.cpp).
--

function find_string(pattern)

    local line_current = 1
    local line_total = get_line_count()

    -- search the current file for the 'pattern'
    while line_current < line_total do
        -- get the line text
        local line_text = get_line_text(line_current)

        local len = string.len(line_text)

        if len > 0 then
            -- look for all todo lines
            if string.find(line_text, pattern) ~= nil then
                return line_current
            end
        end

        -- next line
        line_current = line_current + 1
    end

    return -1
end


function key_macro()
  -- macro only works for documents
  local document = is_document()

  if document > 0 then
    -- get the current file extension
    local extension = macro_tag("$ext")

    -- make sure it is a bison or flex file
    if (string.lower(extension) == ".y") or (string.lower(extension) == ".l") then
        -- get the current file name
        local file_name = macro_tag("$f")

        -- get the current line position
        local line_current = get_line_pos()

        -- this is the matching output .cpp file
        local output_file_name = macro_tag("$fdd$fb"..".cpp")

        -- try to open the file
        local result = file_open(output_file_name)

        if (result == 1) then
            -- build up a pattern
            local pattern = "#line " .. line_current .. " \"" .. file_name .. "\""

            -- look for an exact line match
            local line_found = find_string(pattern)

            if line_found == -1 then
                -- patten for the previous line
                line_current = line_current - 1
                pattern = "#line " .. line_current .. " \"" .. file_name .. "\""
                line_current = line_current + 1

                -- look for an the previous line
                line_found = find_string(pattern)
            end

            if line_found == -1 then
                -- patten for the next line
                line_current = line_current + 1
                pattern = "#line " .. line_current .. " \"" .. file_name .. "\""
                line_current = line_current - 1

                -- look for an the next line
                line_found = find_string(pattern)
            end

--            message_box(1, pattern .. " " .. line_found, "Debug Information")

            if (line_found ~= -1) then
                -- move the cursor to the line found
                set_line_pos(line_found)

                -- some feedback
                message("Search Pattern '" .. pattern .. "' not found.")
            else
                -- some feedback
                message("Search Pattern '" .. pattern .. "' not found.")
                beep()
            end
        else
            message("Error opening: '" .. output_file_name .. "' file.")
            beep()
        end
    else
        message("This macro only works for document bison or flex files!")
        beep()
    end
  else
    message("This macro only works for document files!")
    beep()
  end
end

key_macro() -- run the macro