Skip to content

Commit 31c4bc4

Browse files
committed
feat(lua): Allow for multiline matching of datastructures
1 parent 73b71d2 commit 31c4bc4

File tree

5 files changed

+73
-34
lines changed

5 files changed

+73
-34
lines changed

source/DataStructures/Lua/Accessor.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,12 @@ public override void Process(FileParser fileParser)
5353
if (local)
5454
Ignore = true;
5555

56-
string line = fileParser.Lines[fileParser.CurrentLineCount];
56+
string line = "";
57+
58+
for (int j = 0; j < fileParser.CurrentMatchedLines; j++)
59+
{
60+
line = line + fileParser.Lines[fileParser.CurrentLineCount + j];
61+
}
5762

5863
if (typs == null)
5964
NeoDoc.WriteErrors("Missing essential param", new List<string>{

source/DataStructures/Lua/CreateConVar.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public class CreateConVar : DataStructure
1515

1616
public override Regex GetRegex()
1717
{
18-
return new Regex(@"\s*CreateConVar\s*\(");
18+
return new Regex(@"\s*CreateConVar\s*\(.*\)");
1919
}
2020

2121
public override bool CheckMatch(string line)
@@ -25,8 +25,6 @@ public override bool CheckMatch(string line)
2525

2626
public override void Process(FileParser fileParser)
2727
{
28-
Line = fileParser.Lines[fileParser.CurrentLineCount];
29-
3028
string name = null;
3129

3230
if (ParamsList != null && ParamsList.Count > 0)
@@ -47,6 +45,11 @@ public override void Process(FileParser fileParser)
4745
ParamsList = null;
4846
}
4947

48+
for (int j = 0; j < fileParser.CurrentMatchedLines; j++)
49+
{
50+
Line = Line + fileParser.Lines[fileParser.CurrentLineCount + j];
51+
}
52+
5053
Match splitMatch = GetRegex().Match(Line);
5154
string result = Line.Substring(splitMatch.Index, Line.Length - splitMatch.Index);
5255

source/DataStructures/Lua/Function.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ public class Function : DataStructure
1818

1919
public override Regex GetRegex()
2020
{
21-
return new Regex(@"(^\s*(local\s+)?\s*|@)function\s*\w+((\.|\:)\w+)*\s*\((\w+\s*(,\s*\w+\s*)*)?\)"); // RegEx matches "@function opt.name(param, opt)" or "local function opt:name()"
21+
// RegEx matches "@function opt.name(param, opt)" or "local function opt:name()"
22+
return new Regex(@"(^\s*(local\s+)?\s*|@)function\s*\w+((\.|\:)\w+)*\s*\((\w+\s*(,\s*\w+\s*)*)?\)");
2223
}
2324

2425
public override bool CheckMatch(string line)
@@ -44,7 +45,10 @@ public override void Process(FileParser fileParser)
4445
}
4546
}
4647

47-
Line = fileParser.Lines[fileParser.CurrentLineCount];
48+
for (int j = 0; j < fileParser.CurrentMatchedLines; j++)
49+
{
50+
Line = Line + fileParser.Lines[fileParser.CurrentLineCount + j];
51+
}
4852

4953
if (!Local)
5054
{

source/DataStructures/Lua/Hook.cs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public class Hook : DataStructure
2727

2828
public override Regex GetRegex()
2929
{
30-
return new Regex(@"\s*hook\.(Run|Call)\s*\("); // RegEx matches "hook.Run(" or "hook.Call("
30+
return new Regex(@"\s*hook\.(Run|Call)\s*\(.*\)"); // RegEx matches "hook.Run(" or "hook.Call("
3131
}
3232

3333
public override bool CheckMatch(string line)
@@ -37,7 +37,6 @@ public override bool CheckMatch(string line)
3737

3838
public override void Process(FileParser fileParser)
3939
{
40-
Line = fileParser.Lines[fileParser.CurrentLineCount];
4140

4241
string name = null;
4342

@@ -59,20 +58,26 @@ public override void Process(FileParser fileParser)
5958
ParamsList = null;
6059
}
6160

61+
for (int j = 0; j < fileParser.CurrentMatchedLines; j++)
62+
{
63+
Line = Line + fileParser.Lines[fileParser.CurrentLineCount + j];
64+
}
65+
6266
Match splitMatch = GetRegex().Match(Line);
6367

6468
if (splitMatch.NextMatch().Success) // there are multiple hooks in this line
6569
NeoDoc.WriteErrors("Multiple datastructures found", null, fileParser.relPath, fileParser.CurrentLineCount + 1, (int)NeoDoc.ERROR_CODES.MULTIPLE_DS_IN_LINE);
6670

6771
string result = Line.Substring(splitMatch.Index, Line.Length - splitMatch.Index);
6872

69-
bool mode = new Regex(@"\s*hook\.Run\s*\(").Match(Line).Success; // if false, "hook.Call(" is found
73+
bool hookRun = new Regex(@"hook\.Run\s*\(").Match(Line).Success; // if false, "hook.Call(" is found
7074

7175
List<string> tmpData = NeoDoc.GetEntriesFromString(result, out _);
7276

7377
HookName = GlobalWrapper + ":" + (name ?? tmpData[0]).Trim('"');
7478

75-
HookData = HookName + "(" + string.Join(", ", tmpData.GetRange(mode ? 1 : 2, tmpData.Count - (mode ? 1 : 2)).ToArray()) + ")"; // "hook.Call( string eventName, table gamemodeTable, vararg args )" or "hook.Run( string eventName, vararg args )"
79+
// "hook.Call( string eventName, table gamemodeTable, vararg args )" or "hook.Run( string eventName, vararg args )"
80+
HookData = HookName + "(" + string.Join(", ", tmpData.GetRange(hookRun ? 1 : 2, tmpData.Count - (hookRun ? 1 : 2)).ToArray()) + ")";
7681
}
7782

7883
public override string GetName()

source/FileParser.cs

Lines changed: 46 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System.Collections.Generic;
22
using System.IO;
33
using System.Linq;
4+
using NeoDoc.DataStructures;
45
using NeoDoc.Langs;
56
using NeoDoc.Params;
67

@@ -21,6 +22,7 @@ public class FileParser
2122

2223
internal readonly string relPath;
2324
internal int CurrentLineCount { get; set; }
25+
internal int CurrentMatchedLines { get; set; }
2426
internal List<Param> paramsList { get; set; }
2527

2628
public SortedDictionary<string, WrapperParam> WrapperDict { get; set; }
@@ -70,15 +72,41 @@ public void Process()
7072
{
7173
string line = Lines[CurrentLineCount];
7274

73-
if (string.IsNullOrEmpty(line)) // ignore empty lines
75+
if (string.IsNullOrEmpty(line))
76+
{
77+
// There shouldn't be empty lines between docs and their datastructure
78+
// we therefore discard the params list here.
79+
paramsList.Clear();
7480
continue;
81+
}
7582

76-
if (!paramMatcher.IsLineComment(line)) // if there is no comment but something else. That means the doc comment section has end
83+
// if this line is not a comment it means the doc comment section has ended
84+
// and we should now be able to find a datastructure.
85+
if (!paramMatcher.IsLineComment(line))
7786
{
78-
langMatcher.GetDataStructureType(lang, line)?.Initialize(this);
87+
line = line.TrimEnd();
88+
DataStructure dataStructure = langMatcher.GetDataStructureType(lang, line);
89+
int counter = 1;
90+
91+
// Search the next lines incrementally for a datastructure and only stop once
92+
// we find one or when the next line would be empty or a comment
93+
while(dataStructure == null
94+
&& CurrentLineCount + counter < Lines.Length
95+
&& !string.IsNullOrEmpty(Lines[CurrentLineCount + counter])
96+
&& !paramMatcher.IsLineComment(Lines[CurrentLineCount + counter]))
97+
{
98+
line = line + Lines[CurrentLineCount + counter].TrimEnd();
99+
dataStructure = langMatcher.GetDataStructureType(lang, line);
100+
counter++;
101+
}
79102

80-
// cleans the params list to be used for the next function or whatever, even if there is no dataStructure match
81-
paramsList.Clear();
103+
if (dataStructure != null)
104+
{
105+
CurrentMatchedLines = counter;
106+
dataStructure.Initialize(this);
107+
paramsList.Clear();
108+
CurrentLineCount = CurrentLineCount + counter - 1;
109+
}
82110

83111
continue;
84112
}
@@ -89,29 +117,23 @@ public void Process()
89117
{
90118
string foundLineParamString = paramMatcher.GetLineParamString(line);
91119

92-
if (!string.IsNullOrEmpty(foundLineParamString)) // if there is a not registered param
120+
if (paramMatcher.IsLineCommentStart(line)) // if matching e.g. "---"
93121
{
94-
//NeoDoc.WriteErrors("Unregistered param detected", new List<string>() {
95-
// "'" + foundLineParamString + "' ('" + line + "')"
96-
//}, relPath, CurrentLineCount + 1, (int)NeoDoc.ERROR_CODES.UNREGISTERED_PARAM);
122+
// start with a new description by default.
123+
// HINT: That means that if using e.g. `---` instead of `--` while
124+
// documenting e.g. a param addition in a new line, this line will be
125+
// handled as a new description entry instead of a continued
126+
// param addition / param description.
127+
lineParam = new DescParam();
128+
129+
paramsList.Add(lineParam);
97130
}
98-
else
131+
else if (paramsList.Count > 0) // if there are params in the list
99132
{
100-
if (paramMatcher.IsLineCommentStart(line)) // if matching e.g. "---"
101-
{
102-
paramsList.Clear(); // clear the paramsList if a new docu block starts
103-
104-
lineParam = new DescParam(); // start with a new description by default. HINT: That means that if using e.g. `---` instead of `--` while documenting e.g. a param addition in a new line, this line will be handled as a new description entry instead of a continued param addition / param description.
105-
106-
paramsList.Add(lineParam);
107-
}
108-
else if (paramsList.Count > 0) // if there are params in the list
109-
{
110-
lineParam = paramsList.ElementAt(paramsList.Count - 1); // use last param as new line param to support multiline commenting style e.g.
111-
}
112-
113-
lineParam?.ProcessAddition(paramMatcher.GetLineCommentData(line)); // add additional content
133+
lineParam = paramsList.ElementAt(paramsList.Count - 1); // use last param as new line param to support multiline commenting style e.g.
114134
}
135+
136+
lineParam?.ProcessAddition(paramMatcher.GetLineCommentData(line)); // add additional content
115137
}
116138
else
117139
{

0 commit comments

Comments
 (0)