docs: encourage adding comments; fix nested IF/ELSIF indentation
- Update AGENTS.md to encourage comments instead of forbidding them - Add docstrings to all public functions in src/ modules - Add module headers and function comments to lua/scl/ modules - Fix formatter to properly indent multi-line IF/ELSIF conditions - Fix FB call detection to not match IF statements with parentheses
This commit is contained in:
@@ -1,5 +1,11 @@
|
||||
-- JSON Encoder/Decoder for LSP server
|
||||
-- Minimal implementation supporting basic JSON parsing and encoding
|
||||
|
||||
local json = {}
|
||||
|
||||
--- Check if a table is an array (sequential numeric keys starting from 1)
|
||||
--- @param t table Table to check
|
||||
--- @return boolean True if table is an array
|
||||
local function is_array(t)
|
||||
local i = 0
|
||||
for _ in pairs(t) do
|
||||
@@ -11,6 +17,9 @@ local function is_array(t)
|
||||
return true
|
||||
end
|
||||
|
||||
--- Escape special characters in string for JSON
|
||||
--- @param s string String to escape
|
||||
--- @return string Escaped string
|
||||
local function escape(s)
|
||||
s = s:gsub("\\", "\\\\")
|
||||
s = s:gsub('"', '\\"')
|
||||
@@ -20,6 +29,9 @@ local function escape(s)
|
||||
return s
|
||||
end
|
||||
|
||||
--- Encode Lua value to JSON string
|
||||
--- @param data any Value to encode
|
||||
--- @return string JSON string
|
||||
function json.encode(data)
|
||||
local t = type(data)
|
||||
if t == "nil" then
|
||||
@@ -48,6 +60,10 @@ function json.encode(data)
|
||||
end
|
||||
end
|
||||
|
||||
--- Skip whitespace in string
|
||||
--- @param s string JSON string
|
||||
--- @param i number Current position
|
||||
--- @return number New position after whitespace
|
||||
local function skip_whitespace(s, i)
|
||||
while i <= #s and s:sub(i, i):match("%s") do
|
||||
i = i + 1
|
||||
@@ -55,6 +71,11 @@ local function skip_whitespace(s, i)
|
||||
return i
|
||||
end
|
||||
|
||||
--- Parse JSON string literal
|
||||
--- @param s string JSON string
|
||||
--- @param i number Current position (at opening quote)
|
||||
--- @return string|nil Parsed string
|
||||
--- @return number New position
|
||||
local function parse_string(s, i)
|
||||
local result = {}
|
||||
i = i + 1
|
||||
@@ -86,6 +107,11 @@ local function parse_string(s, i)
|
||||
return nil, i
|
||||
end
|
||||
|
||||
--- Parse JSON number
|
||||
--- @param s string JSON string
|
||||
--- @param i number Current position
|
||||
--- @return number Parsed number
|
||||
--- @return number New position
|
||||
local function parse_number(s, i)
|
||||
local start = i
|
||||
if s:sub(i, i) == "-" then
|
||||
@@ -114,6 +140,11 @@ end
|
||||
|
||||
local parse_value
|
||||
|
||||
--- Parse JSON array
|
||||
--- @param s string JSON string
|
||||
--- @param i number Current position (at opening bracket)
|
||||
--- @return table|nil Parsed array
|
||||
--- @return number New position
|
||||
local function parse_array(s, i)
|
||||
local arr = {}
|
||||
i = i + 1
|
||||
@@ -136,6 +167,11 @@ local function parse_array(s, i)
|
||||
return nil, i
|
||||
end
|
||||
|
||||
--- Parse JSON object
|
||||
--- @param s string JSON string
|
||||
--- @param i number Current position (at opening brace)
|
||||
--- @return table|nil Parsed object
|
||||
--- @return number New position
|
||||
local function parse_object(s, i)
|
||||
local obj = {}
|
||||
i = i + 1
|
||||
@@ -205,6 +241,10 @@ parse_value = function(s, i)
|
||||
end
|
||||
end
|
||||
|
||||
--- Decode JSON string to Lua value
|
||||
--- @param s string JSON string
|
||||
--- @return any|nil Parsed value
|
||||
--- @return number|nil Position after parsing or error
|
||||
function json.decode(s)
|
||||
if type(s) ~= "string" then
|
||||
return nil, "invalid input"
|
||||
|
||||
Reference in New Issue
Block a user