@@ -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" )
4987Module 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 }
0 commit comments