Skip to content

Add SMCParser.ParseString#1817

Merged
peace-maker merged 1 commit intoalliedmodders:masterfrom
sirdigbot:smcparser-parsestring
Dec 2, 2022
Merged

Add SMCParser.ParseString#1817
peace-maker merged 1 commit intoalliedmodders:masterfrom
sirdigbot:smcparser-parsestring

Conversation

@sirdigbot
Copy link
Contributor

@sirdigbot sirdigbot commented Aug 11, 2022

Resolves #1686

Tested with the code below.
(TL;DR - Exact same output as ParseFile)

sourcemod/configs/parsetest.cfg

"Root"
{
    "key1" "value1"
    "key2" "value2"

    "section1"
    {
        "key3" "value3"
        "section2"
        {
            "key4" "value4"
        }
    }
}

parsetest.sp

#include <sourcemod>

char NO_NEWLINES[] = ""
... "\"Root\""
... "{"
... "    \"key1\" \"value1\""
... "    \"key2\" \"value2\""
... ""
... "    \"section1\""
... "    {"
... "        \"key3\" \"value3\""
... "        \"section2\""
... "        {"
... "            \"key4\" \"value4\""
... "        }"
... "    }"
... "}";


char WITH_NEWLINES[] = ""
... "\"Root\"\n"
... "{\n"
... "    \"key1\" \"value1\"\n"
... "    \"key2\" \"value2\"\n"
... "\n"
... "    \"section1\"\n"
... "    {\n"
... "        \"key3\" \"value3\"\n"
... "        \"section2\"\n"
... "        {\n"
... "            \"key4\" \"value4\"\n"
... "        }\n"
... "    }\n"
... "}"; // Doesn't end in newline!


public void OnPluginStart()
{
    RegAdminCmd("sm_parsetest", Command_Test, ADMFLAG_ROOT);
}


public Action Command_Test(int client, int args)
{
    SMCParser smc = new SMCParser();
    
    smc.OnEnd = OnEndCB;
    smc.OnEnterSection = OnEnterSectionCB;
    smc.OnKeyValue = OnKeyValueCB;
    smc.OnLeaveSection = OnLeaveSectionCB;
    smc.OnRawLine = OnRawLineCB;
    smc.OnStart = OnStartCB;
    
    int line, col;
    
    PrintToServer("-------------------------------------------------");
    
    PrintToServer("ParseString (no newlines)");
    SMCError err = smc.ParseString(NO_NEWLINES, line, col);
    PrintToServer("Returned: err:%i, line:%i, col:%i", err, line, col);
    
    PrintToServer("-------------------------------------------------");
    
    PrintToServer("ParseString (with newlines)");
    err = smc.ParseString(WITH_NEWLINES, line, col);
    PrintToServer("Returned: err:%i, line:%i, col:%i", err, line, col);
    
    PrintToServer("-------------------------------------------------");
    
    char path[PLATFORM_MAX_PATH];
    BuildPath(Path_SM, path, sizeof(path), "configs/parsetest.cfg");
    PrintToServer("ParseFile");
    err = smc.ParseFile(path, line, col);
    PrintToServer("Returned: err:%i, line:%i, col:%i", err, line, col);
    
    PrintToServer("-------------------------------------------------");
    
    return Plugin_Handled;
}



void OnEndCB(SMCParser smc, bool halted, bool failed)
{
    PrintToServer("End: halted:%i, failed:%i", halted, failed);
}

SMCResult OnEnterSectionCB(SMCParser smc, const char[] name, bool opt_quotes)
{
    PrintToServer("EnterSection: name:'%s', opt_quotes:%i", name, opt_quotes);
    return SMCParse_Continue;
}

SMCResult OnKeyValueCB(SMCParser smc, const char[] key, const char[] value, bool key_quotes, bool value_quotes)
{
    PrintToServer("KeyValue: key:'%s', value:'%s', key_quotes:%i, value_quote:%i", key, value, key_quotes, value_quotes);
    return SMCParse_Continue;
}

SMCResult OnLeaveSectionCB(SMCParser smc)
{
    PrintToServer("LeaveSection");
    return SMCParse_Continue;
}

SMCResult OnRawLineCB(SMCParser smc, const char[] line, int lineno)
{
    PrintToServer("RawLine: line:'%s', lineno:%i", line, lineno);
    return SMCParse_Continue;
}

void OnStartCB(SMCParser smc)
{
    PrintToServer("Start");
}

Output (TL;DR - both the ParseString + newlines and ParseFile sections are identical as expected)

-------------------------------------------------
ParseString (no newlines)
Start
EnterSection: name:'Root', opt_quotes:1
End: halted:1, failed:0
Returned: err:9, line:1, col:35
-------------------------------------------------
ParseString (with newlines)
Start
RawLine: line:'"Root"', lineno:1
EnterSection: name:'Root', opt_quotes:1
RawLine: line:'{', lineno:2
RawLine: line:'    "key1" "value1"', lineno:3
KeyValue: key:'key1', value:'value1', key_quotes:1, value_quote:1
RawLine: line:'    "key2" "value2"', lineno:4
KeyValue: key:'key2', value:'value2', key_quotes:1, value_quote:1
RawLine: line:'', lineno:5
RawLine: line:'    "section1"', lineno:6
EnterSection: name:'section1', opt_quotes:1
RawLine: line:'    {', lineno:7
RawLine: line:'        "key3" "value3"', lineno:8
KeyValue: key:'key3', value:'value3', key_quotes:1, value_quote:1
RawLine: line:'        "section2"', lineno:9
EnterSection: name:'section2', opt_quotes:1
RawLine: line:'        {', lineno:10
RawLine: line:'            "key4" "value4"', lineno:11
KeyValue: key:'key4', value:'value4', key_quotes:1, value_quote:1
LeaveSection
RawLine: line:'        }', lineno:12
LeaveSection
RawLine: line:'    }', lineno:13
LeaveSection
End: halted:0, failed:0
Returned: err:0, line:14, col:2
-------------------------------------------------
ParseFile
Start
RawLine: line:'"Root"', lineno:1
EnterSection: name:'Root', opt_quotes:1
RawLine: line:'{', lineno:2
RawLine: line:'    "key1" "value1"', lineno:3
KeyValue: key:'key1', value:'value1', key_quotes:1, value_quote:1
RawLine: line:'    "key2" "value2"', lineno:4
KeyValue: key:'key2', value:'value2', key_quotes:1, value_quote:1
RawLine: line:'', lineno:5
RawLine: line:'    "section1"', lineno:6
EnterSection: name:'section1', opt_quotes:1
RawLine: line:'    {', lineno:7
RawLine: line:'        "key3" "value3"', lineno:8
KeyValue: key:'key3', value:'value3', key_quotes:1, value_quote:1
RawLine: line:'        "section2"', lineno:9
EnterSection: name:'section2', opt_quotes:1
RawLine: line:'        {', lineno:10
RawLine: line:'            "key4" "value4"', lineno:11
KeyValue: key:'key4', value:'value4', key_quotes:1, value_quote:1
LeaveSection
RawLine: line:'        }', lineno:12
LeaveSection
RawLine: line:'    }', lineno:13
LeaveSection
End: halted:0, failed:0
Returned: err:0, line:14, col:2
-------------------------------------------------

Copy link
Member

@peace-maker peace-maker left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a good addition analog to the KeyValues string parsing native we have! Thank you.

@peace-maker peace-maker merged commit 5d391fd into alliedmodders:master Dec 2, 2022
@sirdigbot sirdigbot deleted the smcparser-parsestring branch December 12, 2022 04:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Implement SMCParser.ParseString native

2 participants