Skip to content

Commit e625d2b

Browse files
Basile Burgbbasile
authored andcommitted
Add a ParserConfig struct, close #287
1 parent 1fdea23 commit e625d2b

File tree

5 files changed

+92
-60
lines changed

5 files changed

+92
-60
lines changed

src/dparse/ast.d

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3258,7 +3258,7 @@ unittest // issue #133
32583258
RollbackAllocator ra;
32593259
LexerConfig cf = LexerConfig("", StringBehavior.source);
32603260
StringCache ca = StringCache(16);
3261-
Module m = parseModule(getTokensForParser(src, cf, &ca), "", &ra);
3261+
Module m = ParserConfig(getTokensForParser(src, cf, &ca), "", &ra).parseModule();
32623262
TkTest t = new TkTest;
32633263
t.visit(m);
32643264
}
@@ -3276,7 +3276,7 @@ unittest // issue #165
32763276
RollbackAllocator ra;
32773277
LexerConfig cf = LexerConfig("", StringBehavior.source);
32783278
StringCache ca = StringCache(16);
3279-
Module m = parseModule(getTokensForParser(src, cf, &ca), "", &ra);
3279+
Module m = ParserConfig(getTokensForParser(src, cf, &ca), "", &ra).parseModule();
32803280
EpoTest et = new EpoTest;
32813281
et.visit(m);
32823282
assert(et.visited);
@@ -3312,15 +3312,15 @@ unittest // issue #156
33123312
{
33133313
// no colon so array.
33143314
string src1 = q{void main(){const arr = [[0]];}};
3315-
Module m = parseModule(getTokensForParser(src1, cf, &ca), "", &ra);
3315+
Module m = parseModule(ParserConfig(getTokensForParser(src1, cf, &ca), "", &ra));
33163316
Test t = new Test;
33173317
t.visit(m);
33183318
assert(t.arrValueOnly);
33193319
}
33203320
{
33213321
// simple primary before colon, assume array.
33223322
string src2 = q{void main(){const arr = [0:0];}};
3323-
Module m = parseModule(getTokensForParser(src2, cf, &ca), "", &ra);
3323+
Module m = ParserConfig(getTokensForParser(src2, cf, &ca), "", &ra).parseModule();
33243324
Test t = new Test;
33253325
t.visit(m);
33263326
assert(t.arrIndex);
@@ -3330,7 +3330,7 @@ unittest // issue #156
33303330
{
33313331
// more complex exp before colon, assume AA.
33323332
string src3 = q{void main(){const arr = [[0]:0];}};
3333-
Module m = parseModule(getTokensForParser(src3, cf, &ca), "", &ra);
3333+
Module m = ParserConfig(getTokensForParser(src3, cf, &ca), "", &ra).parseModule();
33343334
Test t = new Test;
33353335
t.visit(m);
33363336
assert(!t.arrIndex);
@@ -3363,12 +3363,12 @@ unittest // issue #170
33633363
LexerConfig cf = LexerConfig("", StringBehavior.source);
33643364
StringCache ca = StringCache(16);
33653365

3366-
Module m = parseModule(getTokensForParser(Test170_F.src, cf, &ca), "", &ra);
3366+
Module m = ParserConfig(getTokensForParser(Test170_F.src, cf, &ca), "", &ra).parseModule();
33673367
Test170_F t170_f = new Test170_F;
33683368
t170_f.visit(m);
33693369
assert(t170_f.visited);
33703370

3371-
m = parseModule(getTokensForParser(Test170_S.src, cf, &ca), "", &ra);
3371+
m = ParserConfig(getTokensForParser(Test170_S.src, cf, &ca), "", &ra).parseModule();
33723372
Test170_S t170_s = new Test170_S;
33733373
t170_s.visit(m);
33743374
assert(t170_s.visited);
@@ -3423,7 +3423,7 @@ unittest // issue #193
34233423
LexerConfig cf = LexerConfig("", StringBehavior.source);
34243424
StringCache ca = StringCache(16);
34253425

3426-
Module m = parseModule(getTokensForParser(Test193.src, cf, &ca), "", &ra);
3426+
Module m = ParserConfig(getTokensForParser(Test193.src, cf, &ca), "", &ra).parseModule();
34273427
Test193 t193 = new Test193;
34283428
t193.visit(m);
34293429
}

src/dparse/formatter.d

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3951,7 +3951,7 @@ void testFormatNode(Node)(string sourceCode)
39513951
StringCache cache = StringCache(32);
39523952
RollbackAllocator rba;
39533953
auto toks = getTokensForParser(code, config, &cache);
3954-
Module mod = parseModule(toks, "stdin", &rba);
3954+
Module mod = parseModule(ParserConfig(toks, "stdin", &rba));
39553955
(new CatchInterestingStuff).visit(mod);
39563956
}
39573957

src/dparse/parser.d

Lines changed: 81 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -17,50 +17,86 @@ import std.string : format;
1717
// Caution: generates 180 megabytes of logging for std.datetime
1818
//version = dparse_verbose;
1919

20+
2021
/**
22+
* Prototype for a custom parser message function or delegate.
2123
* Params:
22-
* tokens = the tokens parsed by dparse.lexer
23-
* fileName = the name of the file being parsed
24-
* messageFunction = a function to call on error or warning messages.
25-
* The parameters are the file name, line number, column number,
26-
* the error or warning message, and a boolean (true means error, false
27-
* means warning).
28-
* Returns: the parsed module
24+
* fileName = The source file name.
25+
* line = The line in the source, 0 based.
26+
* column = The column in the source, 0 based.
27+
* message = Datailed message.
28+
* isError = Indicates if the message is a warning (`false`) or a if it's an error (`true`).
2929
*/
30-
Module parseModule(const(Token)[] tokens, string fileName, RollbackAllocator* allocator,
31-
void delegate(string, size_t, size_t, string, bool) messageFunction = null,
32-
uint* errorCount = null, uint* warningCount = null)
30+
alias MessageFunction = void function(string fileName , size_t line, size_t column, string message, bool isError);
31+
32+
/// ditto
33+
alias MessageDelegate = void delegate(string, size_t, size_t, string, bool);
34+
35+
/**
36+
* Parser configuration struct
37+
*/
38+
struct ParserConfig
39+
{
40+
/// The tokens parsed by dparse.lexer.
41+
const(Token)[] tokens;
42+
/// The name of the file being parsed
43+
string fileName;
44+
/// A pointer to a rollback allocator.
45+
RollbackAllocator* allocator;
46+
/// An optional function used to handle warnings and errors.
47+
MessageFunction messageFunction;
48+
/// An optional delegate used to handle warnings and errors.
49+
/// Set either this one or messageFunction, not both.
50+
MessageDelegate messageDelegate;
51+
/// An optional pointer to a variable receiving the error count.
52+
uint* errorCount;
53+
/// An optional pointer to a variable receiving the warning count.
54+
uint* warningCount;
55+
}
56+
57+
58+
/**
59+
* Params:
60+
* parserConfig = a parser configuration.
61+
* Returns:
62+
* The parsed module
63+
*/
64+
Module parseModule()(auto ref ParserConfig parserConfig)
3365
{
3466
auto parser = new Parser();
35-
parser.fileName = fileName;
36-
parser.tokens = tokens;
37-
parser.messageDg = messageFunction;
38-
parser.allocator = allocator;
39-
auto mod = parser.parseModule();
40-
if (warningCount !is null)
41-
*warningCount = parser.warningCount;
42-
if (errorCount !is null)
43-
*errorCount = parser.errorCount;
67+
with (parserConfig)
68+
{
69+
parser.fileName = fileName;
70+
parser.tokens = tokens;
71+
parser.messageFunction = messageFunction;
72+
parser.messageDelegate = messageDelegate;
73+
parser.allocator = allocator;
74+
}
75+
Module mod = parser.parseModule();
76+
with (parserConfig)
77+
{
78+
if (warningCount !is null)
79+
*warningCount = parser.warningCount;
80+
if (errorCount !is null)
81+
*errorCount = parser.errorCount;
82+
}
4483
return mod;
4584
}
4685

47-
/// Ditto
48-
deprecated("Use the overload accepting a delegate instead of a function")
86+
deprecated("Use the parseModule overload that takes a ParserConfig instead")
4987
Module parseModule(const(Token)[] tokens, string fileName, RollbackAllocator* allocator,
50-
void function(string, size_t, size_t, string, bool) messageFunction,
51-
uint* errorCount = null, uint* warningCount = null)
88+
MessageFunction messageFunction, uint* errorCount = null, uint* warningCount = null)
5289
{
53-
auto parser = new Parser();
54-
parser.fileName = fileName;
55-
parser.tokens = tokens;
56-
parser.messageFunction = messageFunction;
57-
parser.allocator = allocator;
58-
auto mod = parser.parseModule();
59-
if (warningCount !is null)
60-
*warningCount = parser.warningCount;
61-
if (errorCount !is null)
62-
*errorCount = parser.errorCount;
63-
return mod;
90+
return ParserConfig(tokens, fileName, allocator, messageFunction, null,
91+
errorCount, warningCount).parseModule();
92+
}
93+
94+
deprecated("Use the parseModule overload that takes a ParserConfig instead")
95+
Module parseModule(const(Token)[] tokens, string fileName, RollbackAllocator* allocator,
96+
MessageDelegate messageDelegate, uint* errorCount = null, uint* warningCount = null)
97+
{
98+
return ParserConfig(tokens, fileName, allocator, null, messageDelegate,
99+
errorCount, warningCount).parseModule();
64100
}
65101

66102
/**
@@ -7047,18 +7083,14 @@ class Parser
70477083
RollbackAllocator* allocator;
70487084

70497085
/**
7050-
* Function that is called when a warning or error is encountered.
7086+
* Function or delegate that is called when a warning or error is encountered.
70517087
* The parameters are the file name, line number, column number,
70527088
* and the error or warning message.
70537089
*/
7054-
deprecated("Use 'messageDg' instead")
7055-
public alias messageFunction = messageFunction_;
7056-
7057-
/// Ditto
7058-
public void delegate(string, size_t, size_t, string, bool) messageDg;
7090+
MessageFunction messageFunction;
70597091

70607092
/// Ditto
7061-
private void function(string, size_t, size_t, string, bool) messageFunction_;
7093+
MessageDelegate messageDelegate;
70627094

70637095
void setTokens(const(Token)[] tokens)
70647096
{
@@ -7528,10 +7560,10 @@ protected: final:
75287560
++warningCount;
75297561
auto column = index < tokens.length ? tokens[index].column : 0;
75307562
auto line = index < tokens.length ? tokens[index].line : 0;
7531-
if (messageDg !is null)
7532-
messageDg(fileName, line, column, message, false);
7533-
else if (messageFunction_ !is null)
7534-
messageFunction_(fileName, line, column, message, false);
7563+
if (messageDelegate !is null)
7564+
messageDelegate(fileName, line, column, message, false);
7565+
else if (messageFunction !is null)
7566+
messageFunction(fileName, line, column, message, false);
75357567
else
75367568
stderr.writefln("%s(%d:%d)[warn]: %s", fileName, line, column, message);
75377569
}
@@ -7544,10 +7576,10 @@ protected: final:
75447576
++errorCount;
75457577
auto column = index < tokens.length ? tokens[index].column : tokens[$ - 1].column;
75467578
auto line = index < tokens.length ? tokens[index].line : tokens[$ - 1].line;
7547-
if (messageDg !is null)
7548-
messageDg(fileName, line, column, message, true);
7549-
else if (messageFunction_ !is null)
7550-
messageFunction_(fileName, line, column, message, true);
7579+
if (messageDelegate !is null)
7580+
messageDelegate(fileName, line, column, message, true);
7581+
else if (messageFunction !is null)
7582+
messageFunction(fileName, line, column, message, true);
75517583
else
75527584
stderr.writefln("%s(%d:%d)[error]: %s", fileName, line, column, message);
75537585
}

subprojects/stdx-allocator.wrap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
[wrap-git]
22
directory = stdx-allocator
33
url = https://github.com/dlang-community/stdx-allocator.git
4-
revision = head
4+
revision = v2.77.4

test/tester.d

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ int main(string[] args)
309309
config.fileName = arg;
310310
const(Token)[] tokens = getTokensForParser(fileBytes, config, &cache);
311311
RollbackAllocator rba;
312-
parseModule(tokens, arg, &rba, &messageFunction);
312+
parseModule(ParserConfig(tokens, arg, &rba, &messageFunction));
313313
}
314314
writefln("Finished parsing with %d errors and %d warnings.",
315315
errorCount, warningCount);

0 commit comments

Comments
 (0)