/
Parser.cs
1105 lines (963 loc) · 38 KB
/
Parser.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* TADS3 documentation generator.
*
* Copyright Edward L. Stauff 2003.
* This code may be freely distributed and modified for non-commercial purposes.
*
*/
using System;
using System.Collections;
using System.IO;
/*
* This file contains the parser, which reads the TADS3 source files.
* This parser is pretty stupid, and depends on the format of the source files
* nearly as much as the language syntax. It pretty much ignores anything
* it doesn't understand, and generates no error messages.
* The parser is independent of the rest of the program, and can easily be
* replaced or improved.
*/
namespace DocGen
{
/// <summary>
/// The parser.
/// </summary>
public class Parser
{
/// <summary>
/// Where to put all the stuff we collect.
/// </summary>
private SymbolTable SymbolTable;
/// <summary>
/// The current input file stream.
/// </summary>
private StreamReader CurrentFile;
/// <summary>
/// The current macro-expanded input file stream.
/// </summary>
private StreamReader CurrentExpFile = null;
/// <summary>
/// The current line in the macro-expanded file
/// </summary>
private String CurrentExpLine = null;
/// <summary>
/// The current line number in the current input file.
/// </summary>
private int LineNumber;
/// <summary>
/// Are we in an intrinsic function set?
/// </summary>
private bool InIntrinsicFuncs = false;
/// <summary>
/// Are we inside a function?
/// </summry>
private bool InFunc = false;
/// <summary>
/// The name of the current input file.
/// </summary>
private String CurrentFileName;
/// <summary>
/// The current source (input) file.
/// </summary>
private SourceFile CurrentSourceFile;
/// <summary>
/// The current class or object, or null.
/// </summary>
Modification CurrClassOrObj = null;
/// <summary>
/// The most recently parsed comment in the current input file.
/// </summary>
String LastComment = "";
/// <summary>
/// Does LastComment contains the first comment in the current input file?
/// </summary>
private bool FirstComment;
/// <summary>
/// For holding expanded multi-line macros.
/// </summary>
private ArrayList BufferedLines = new ArrayList();
/// <summary>
/// Whether to expand macros; mainly for debugging.
/// </summary>
public bool ExpandMacros = false;
/// <summary>
/// Had the parser encountered DefLangDir before?
/// </summary>
public bool DefLangDirSeen = false;
//=====================================================================
/// <summary>
/// Constructor.
/// </summary>
public Parser (SymbolTable st)
{
this.SymbolTable = st;
}
//=====================================================================
public void ParseFiles (ArrayList files)
{
int n = 1;
foreach (SourceFile f in files)
{
System.Console.Out.Write("parsing file " + n.ToString() + " of " +
files.Count.ToString() + " " + f.FullPath + " line " +
" ".Substring(0, maxProgLen));
this.ParseInputFile(f);
}
}
//=====================================================================
/// <summary>
/// Parses a single input file.
/// </summary>
public void ParseInputFile (SourceFile file)
{
this.CurrentSourceFile = file;
this.CurrentFileName = file.FullPath;
this.LineNumber = 0;
this.FirstComment = true;
this.CurrentFile = new StreamReader(file.FullPath);
String line;
if (DocGenApp.MacroExpPath != null)
{
try
{
this.CurrentExpFile = new StreamReader(
DocGenApp.MacroExpPath + "/" + file.ShortName);
}
catch (Exception)
{
CurrentExpFile = null;
CurrentExpLine = null;
}
}
while (null != (line = GetNextLine()))
ProcessLine(line, CurrentExpLine);
System.Console.Out.WriteLine();
CurrentFile.Close();
}
//=====================================================================
private void ShowProgress ()
{
String progress = LineNumber.ToString() + " of " +
this.CurrentSourceFile.NumLines.ToString() + " ";
progress = progress.Substring(0, maxProgLen);
System.Console.Out.Write(backspaces + progress);
}
static int maxProgLen = 4 + 4 + 4; // #### of ####
static String backspaces = "\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b".Substring(0, maxProgLen);
//=====================================================================
/// <summary>
/// Returns the next line in the current input file, or null on end-of-file.
/// </summary>
private String GetNextLine ()
{
if (! this.ExpandMacros)
{
LineNumber++;
this.ShowProgress();
if (CurrentExpFile != null)
this.CurrentExpLine = CurrentExpFile.ReadLine();
return CurrentFile.ReadLine();
}
String line;
if (this.BufferedLines.Count == 0)
{
LineNumber++;
this.ShowProgress();
line = CurrentFile.ReadLine();
if (line == null)
return null;
line = this.CurrentSourceFile.ExpandMacros(line);
line = line.Replace("\r\n", "\n");
line = line.Replace('\r', 'n');
int n;
while (0 <= (n = line.IndexOf('\n')))
{
this.BufferedLines.Add(line.Substring(0, n));
line = line.Substring(n+1);
}
this.BufferedLines.Add(line);
}
line = (String) this.BufferedLines[0];
this.BufferedLines.RemoveAt(0);
return line;
}
//=====================================================================
/// <summary>
/// Processes the given input line.
/// </summary>
private void ProcessLine (String origLine, String expLine)
{
String line;
bool ifcOnly = false;
bool isTransient = false;
// process everything except 'propertyset' lines from the expanded
// version
if (expLine == null || expLine.Trim().StartsWith("propertyset "))
line = origLine;
else
line = expLine;
line = line.TrimEnd(); // don't trim the beginning!
if (line == "") // ignore blank lines
return;
// count and strip leading spaces
int initialSpaces = 0;
while (line.StartsWith(" "))
{
initialSpaces++;
line = line.Substring(1);
}
// look for DMsg and BMsg definitions
if ((line.Contains("BMsg(") || line.Contains("DMsg(")) && !CurrentFileName.Contains(".h"))
ProcessMessage(line);
String token1 = GetNextToken(ref line);
// skip "transient"
if (token1 == "transient")
{
isTransient = true;
token1 = GetNextToken(ref line);
}
if (token1 == "intrinsic")
{
token1 = GetNextToken(ref line);
if (token1 == "class")
ProcessIntrinsicClass(line);
else if (token1 == "'")
InIntrinsicFuncs = true;
return;
}
if (token1 == "class")
{
InIntrinsicFuncs = InFunc = false;
ProcessClass(line, origLine);
return;
}
if (token1 == "grammar" || token1 == "VerbRule")
{
InIntrinsicFuncs = InFunc = false;
ProcessGrammar(line, origLine);
return;
}
if (token1 == "modify")
{
InIntrinsicFuncs = InFunc = false;
ProcessModify(line);
return;
}
if (token1 == "/*")
{
ProcessComment(line);
return;
}
if (this.CurrentSourceFile.IsHeaderFile && (token1 == "enum"))
{
ProcessEnums(line);
return;
}
if (token1 == "#")
{
token1 = GetNextToken(ref line);
if (this.CurrentSourceFile.IsHeaderFile && (token1 == "define"))
{
ProcessMacro(line);
return;
}
}
// check for special interface-only comments
if (token1 == "//" && initialSpaces == 4)
{
// set the 'interface only' flag, and then parse the
// rest of the line as though it were real
ifcOnly = true;
token1 = GetNextToken(ref line);
}
if (token1 == "}" && initialSpaces == 0)
InIntrinsicFuncs = InFunc = false;
String token2 = GetNextToken(ref line);
// for interface-only comments, be a little more selective, to
// avoid accidentally matching other comments
if (ifcOnly && (token2 != "=" && token2 != "(" && token2 != "{"))
return;
if (this.CurrentSourceFile.IsHeaderFile && (token2 == "template"))
{
InIntrinsicFuncs = InFunc = false;
ProcessTemplate(token1, line);
return;
}
if (token2 == "=")
{
if (initialSpaces == 4 && !InFunc)
ProcessProperty(token1, ifcOnly);
return;
}
if (token2 == ":")
{
if (initialSpaces != 0 || InFunc)
return;
ProcessObject(token1, line, isTransient, false);
return;
}
if (token2 == "(")
{
if (token1 == "DefineIAction" || token1 == "DefineTAction"
|| token1 == "DefineTIAction" || token1 == "DefineLiteralAction"
|| token1 == "DefineLiteralTAction" || token1 == "DefineTopicAction"
|| token1 == "DefineTopicTAction" || token1 == "DefineSystemAction"
|| token1 == "DefineAction")
{
ProcessActionDefinition(token1, line);
return;
}
// Certain function-like macros and statements can be ignored altogether
if (token1 == "DefDigit" || token1 == "defOrdinal"
|| token1 == "defTeen" || token1 == "defTens"
|| token1 == "defDigit" || token1 == "if" || token1 == "//")
{
return;
}
// Only process DefineLangDir the first time we see it //
if (token1 == "DefineLangDir")
{
if (DefLangDirSeen)
return;
else
DefLangDirSeen = true;
}
if (initialSpaces == 0)
{
ProcessFunction(token1, line);
InFunc = true;
}
else if (InIntrinsicFuncs)
ProcessFunction(token1, line);
else if (initialSpaces == 4 && !InFunc)
ProcessMethod(token1, line, ifcOnly);
return;
}
if (token2 == "{")
{
if (initialSpaces == 4 && !InFunc)
ProcessMethod(token1, "{" + line, ifcOnly);
return;
}
}
//=====================================================================
/// <summary>
/// Processes a line which appears to define an action.
/// DefineIdentifierAction(identifier)
/// </summary>
/// <param name="deftok"></param>
/// <param name="line"></param>
private void ProcessActionDefinition(String deftok, String line)
{
String actionClass;
String actionName;
actionClass = deftok.Substring(6, deftok.Length - 6);
actionName = GetNextToken(ref line);
ProcessObject(actionName, actionClass, false, true);
}
//=====================================================================
/// <summary>
/// Processes a line which appears to define a class.
/// "class" space identifier ":" [identifier List]
/// </summary>
private void ProcessClass (String line, String origLine)
{
// note: the keyword "class" has already been parsed
ClassDef cr = new ClassDef(Path.GetFileName(CurrentFileName), LineNumber);
cr.Name = GetNextToken(ref line);
cr.Description = LastComment;
LastComment = "";
if (cr.Name == "")
return; // must not be a class after all
if (GetNextToken(ref line) != ":")
return; // must not be a class after all
line = line.Trim();
while (line != "") // collect base class names
{
cr.BaseClasses.Add(GetNextToken(ref line));
// stop when we reach anything other than a comma
if (GetNextToken(ref line) != ",")
break;
}
// add the defined-as string to the description if applicable
String origLine2 = origLine;
String otok = GetNextToken(ref origLine2);
if (otok.StartsWith("Define")
&& (otok.EndsWith("Action") || otok.EndsWith("ActionSub")))
{
origLine = origLine.Trim();
int idx = origLine.IndexOf(")");
cr.OrigDef = origLine.Substring(0, idx + 1);
cr.IsAction = true;
}
SymbolTable.SetObjectFileName(cr);
SymbolTable.Classes.Add(cr);
this.CurrentSourceFile.Classes.Add(cr);
CurrClassOrObj = cr;
}
//=====================================================================
/// <summary>
/// Processes a line which appears to define a grammar object.
/// "grammar" identifier "(" identifier ")" ":" [token-list]
/// ":" [class-identifier-list]
/// </summary>
private void ProcessGrammar (String line, String origLine)
{
// note: the keyword "grammar" has already been parsed
ClassDef cr = new ClassDef(Path.GetFileName(CurrentFileName),
LineNumber);
cr.Name = GetNextToken(ref line);
cr.Description = LastComment;
cr.IsGrammar = true;
String origLine2 = origLine;
bool isVerbRule = (GetNextToken(ref origLine2) == "VerbRule");
LastComment = "";
if (cr.Name == "")
return; // must not be a class after all
// parse the tag, if present
if (cr.Name == "(" || GetNextToken(ref line) == "(")
{
// store the tagged version of the name
String tag = GetNextToken(ref line);
if (GetNextToken(ref line) != ")")
return;
if (isVerbRule)
cr.Name = "VerbRule";
cr.Name += "(" + tag + ")";
// if we created this with VerbRule, remember this
if (isVerbRule)
{
origLine = origLine.Trim();
int idx = origLine.IndexOf(")");
cr.OrigDef = origLine.Substring(0, idx + 1);
}
}
// check for the ":"
if (GetNextToken(ref line) != ":" && !isVerbRule)
return; // must not be a grammar statement after all
// skip the production token list, which probably spans
// multiple lines
String rule = "";
for (char qu = '\0' ; ; )
{
int idx;
bool found = false;
// scan to the ":", or to end of line
for (idx = 0 ; idx < line.Length ; ++idx)
{
switch (line[idx])
{
case '\\':
// skip the next character
++idx;
break;
case ':':
// stop unless in a string
if (qu == '\0')
found = true;
break;
case '\'':
case '"':
if (qu != '\0')
{
if (qu == line[idx])
qu = '\0';
}
else
qu = line[idx];
break;
}
// if we found the ':', stop here
if (found)
break;
}
// add up to the end of this scan to the rule
if (found)
{
// found it - add up to the ':' to the rule
rule += line.Substring(0, idx).TrimEnd();
// continue from just past the ':'
line = line.Substring(idx + 1);
// we're done scanning the rule
break;
}
// didn't find it - add this line and a newline to the rule
if (line.Trim() != "")
rule += line + "\u0001";
// continue to the next line of the rule
line = GetNextLine();
}
// now parse the class list
line = line.Trim();
while (line != "") // collect base class names
{
// get the token
cr.BaseClasses.Add(GetNextToken(ref line));
// check for and skip the comma
if (GetNextToken(ref line) != ",")
break;
}
// add the grammar list to the class
cr.GrammarRule = rule;
SymbolTable.SetObjectFileName(cr);
SymbolTable.Classes.Add(cr);
this.CurrentSourceFile.Classes.Add(cr);
CurrClassOrObj = cr;
// get the GrammarProd name - this is just the class name
// sans the (tag) part
int n;
String gpName;
if ((n = cr.Name.IndexOf('(')) >= 0)
gpName = cr.Name.Substring(0, n);
else
gpName = cr.Name;
// find the existing GrammarProd
GrammarProd g = SymbolTable.FindGrammarProd(gpName);
// if we didn't find one, create it
if (g == null)
{
g = new GrammarProd(Path.GetFileName(CurrentFileName),
LineNumber);
g.Name = gpName;
SymbolTable.SetObjectFileName(g);
SymbolTable.GrammarProds.Add(g);
}
// add me to the GrammarProd's list of match objects
g.MatchObjects.Add(cr);
cr.GrammarProdObj = g;
//if (isVerbRule)
//{
// while (!line.Contains("action"))
// {
// line = GetNextLine();
// if (line.Contains(";"))
// break;
// }
// if (line.Contains("action"))
// {
// while (GetNextToken(ref line) != "action")
// ;
// if (GetNextToken(ref line) != "=")
// return;
// String action = GetNextToken(ref line);
// }
//}
}
//====================================================================
/// <summary>
/// Extracts a DMsg() or BMsg() definition from a line and processes the result
/// </summary>
/// <param name="line"></param>
private void ProcessMessage(String line)
{
String tok;
String messName = "";
String messType;
String messText = "";
Message mes = new Message(Path.GetFileName(CurrentFileName), LineNumber);
tok = GetNextToken(ref line);
while (tok != "BMsg" && tok != "DMsg")
{
tok = GetNextToken(ref line);
}
messType = tok;
GetNextToken(ref line);
do
{
tok = GetNextToken(ref line);
if (tok == ",")
break;
else
messName += (tok + " ");
} while (tok != ",");
messText = line.Trim();
mes.Name = messName.Trim();
mes.type = messType;
while (!line.Contains(")"))
{
line = GetNextLine();
messText += (" " + line);
}
/* Replace common HTML entities */
messText = messText.Replace("&", "&");
messText = messText.Replace(">", ">");
messText = messText.Replace("<", "<");
mes.text = messText;
SymbolTable.Messages.Add(mes);
}
//=====================================================================
/// <summary>
/// Processes a line which appears to be a "modify" statement extending an intrinsic class.
/// "modify" space identifier
/// </summary>
private void ProcessModify (String line)
{
// note: the keyword "modify" has already been parsed
Modification mod = new Modification(Path.GetFileName(CurrentFileName), LineNumber);
mod.Name = GetNextToken(ref line);
mod.Description = LastComment;
LastComment = "";
if (mod.Name == "")
return; // must not be a modify statement after all
// Note: we don't set the name for the object's html file here,
// since we want to refer to the file generated for the original
// base (pre-'modify') object. We might not have parsed the
// base object yet, so we'll have to go back and fix these up
// after we've finished parsing the entire library, since
// we'll know the correct filenames at that point and can
// correlate the modification entries with the base entries
// based on the symbol name.
SymbolTable.Modifications.Add(mod);
CurrClassOrObj = mod;
}
//=====================================================================
/// <summary>
/// Processes a line which appears to define an intrinsic class.
/// "intrinsic class" space identifier "'" stuff "'" ":" [identifier List]
/// </summary>
private void ProcessIntrinsicClass (String line)
{
// note: the keyboards "intrinsic class" have already been parsed
ClassDef cr = new ClassDef(Path.GetFileName(CurrentFileName), LineNumber);
cr.Name = GetNextToken(ref line);
cr.Description = LastComment;
LastComment = "";
if (cr.Name == "")
return; // must not be a class after all
int n = line.IndexOf(':');
if (n >= 0)
{
line = line.Substring(n+1);
while (line != "") // collect base class names
{
cr.BaseClasses.Add(GetNextToken(ref line));
GetNextToken(ref line); // throw away comma
}
}
cr.IsIntrinsic = true;
SymbolTable.SetObjectFileName(cr);
SymbolTable.Classes.Add(cr);
this.CurrentSourceFile.Classes.Add(cr);
CurrClassOrObj = cr;
}
//=====================================================================
/// <summary>
/// Processes a line which appears to define a global object.
/// identifier ":" [identifier List]
/// </summary>
private void ProcessObject (String name, String line, bool isTransient, bool isAction)
{
// note: the identifer and colon have already been parsed
ObjectDef od = new ObjectDef(Path.GetFileName(CurrentFileName), LineNumber);
od.Name = name;
od.Description = LastComment;
od.IsTransient = isTransient;
od.isAction = isAction;
LastComment = "";
if (od.Name == "")
return; // must not be a class after all
line = line.Trim();
while (line != "") // collect base class names
{
String t;
t = GetNextToken(ref line);
if (t.Contains("Action"))
od.isAction = true;
od.BaseClasses.Add(t);
t = GetNextToken(ref line);
if (t != ",")
break;
}
SymbolTable.SetObjectFileName(od);
SymbolTable.GlobalObjects.Add(od);
this.CurrentSourceFile.GlobalObjects.Add(od);
CurrClassOrObj = od;
}
//=====================================================================
/// <summary>
/// Processes a line which appears to define a method.
/// identifier "(" identifierList ")"
/// </summary>
/// <param name="name">the name of the method, which has already been parsed</param>
private void ProcessMethod (String name, String line, bool ifcOnly)
{
if (CurrClassOrObj == null)
return;
MethodDef m = new MethodDef(Path.GetFileName(CurrentFileName),
LineNumber);
m.Name = name;
m.IsIfcOnly = ifcOnly;
m.Description = LastComment;
m.ClassOrObject = CurrClassOrObj;
if (! ParseArgList(ref line, m))
return;
if ((name == "dobjFor" || name == "iobjFor")
&& m.Parameters.Count == 1)
{
m.Name += "(" + m.Parameters[0] + ")";
m.Parameters = null;
}
CurrClassOrObj.Methods.Add(m);
LastComment = "";
}
//=====================================================================
/// <summary>
/// Processes a line which appears to define a global function.
/// identifier "(" identifierList ")"
/// </summary>
/// <param name="name">the name of the function, which has already been parsed</param>
private void ProcessFunction (String name, String line)
{
FunctionDef f = new FunctionDef(Path.GetFileName(CurrentFileName), LineNumber);
f.Name = name;
f.Description = LastComment;
LastComment = "";
if (! ParseArgList(ref line, f))
return;
this.SymbolTable.GlobalFunctions.Add(f);
this.CurrentSourceFile.GlobalFunctions.Add(f);
}
//=====================================================================
/// <summary>
/// Processes a line which appears to define a macro.
/// #define identifier ["(" identifierList ")"]
/// </summary>
private void ProcessMacro (String line)
{
// note: the keyword "#define" has already been parsed
MacroDef m = new MacroDef(Path.GetFileName(CurrentFileName), LineNumber);
m.Name = GetNextToken(ref line);
m.Description = LastComment;
LastComment = "";
if (line.StartsWith("(")) // look for macro args
{
line = line.Substring(1).Trim();
if (! ParseArgList(ref line, m))
return;
}
// get the body of the macro
m.Body = line.Trim();
if (m.Body == "\\")
m.Body = "";
while (line.EndsWith("\\"))
{
if (m.Body != "")
m.Body += "<br>";
line = GetNextLine().Trim();
m.Body += line;
}
SymbolTable.Macros.Add(m);
this.CurrentSourceFile.Macros.Add(m);
}
//=====================================================================
/// <summary>
/// Destructively parses an argument list from the given string and
/// puts the arguments in the given FunctionDef.
/// </summary>
/// <returns>true on success, false on error</returns>
private bool ParseArgList (ref String line, FunctionDef f)
{
line = line.Trim();
for (;;)
{
// if we're out of text on this line, fetch another
if (line == "")
line = GetNextLine().Trim();
// check what we have next
String token = GetNextToken(ref line);
if (token == ")" || token == "{")
break;
if (token == "")
return false;
if (token == "[")
{
// [arglist] parameter - build the whole string
token = GetNextToken(ref line);
if (GetNextToken(ref line) == "]")
token = "[" + token + "]";
}
if (token == "?" && f.Parameters.Count != 0)
f.Parameters[f.Parameters.Count - 1] += "?";
else if (token != ",")
f.Parameters.Add(token);
}
return true;
}
//=====================================================================
/// <summary>
/// Processes a line which appears to define a property.
/// identifier "=" value
/// </summary>
/// <param name="name">the name of the property</param>
private void ProcessProperty (String name, bool ifcOnly)
{
PropertyDef p;
if (CurrClassOrObj == null)
return;
p = new PropertyDef(name, LastComment, this.CurrClassOrObj,
Path.GetFileName(CurrentFileName), LineNumber);
p.IsIfcOnly = ifcOnly;
CurrClassOrObj.Properties.Add(p);
LastComment = "";
}
//=====================================================================
/// <summary>
/// Processes a line which appears to define one or more enums.
/// All enums defined on a single line are placed in an EnumGroup.
/// This means that some enums are not grouped as they ought to be,
/// but I did say this parser is pretty stupid.
/// "enum" identifier ["," identifier]... ";"
/// </summary>
private void ProcessEnums (String line)
{
// note: the keyword "enum" has already been parsed
EnumGroup group = new EnumGroup(Path.GetFileName(CurrentFileName), LineNumber);
group.Description = LastComment;
LastComment = "";
line = line.Trim();
while (line != "")
{
String token = GetNextToken(ref line);
if (token == ";")
break;
if (token == "")
return;
if (token != ",")
{
EnumDef e = new EnumDef(Path.GetFileName(CurrentFileName), LineNumber);
e.Name = token;
e.Group = group;
group.Enums.Add(e);
}
}
this.SymbolTable.EnumGroups.Add(group);
this.CurrentSourceFile.EnumGroups.Add(group);
}
//=====================================================================
/// <summary>
/// Processes a line which appears to define a template.
/// identifier "template" body
/// </summary>
/// <param name="name">the name of the template (already parsed)</param>
private void ProcessTemplate (String name, String line)
{
// note: the template name and keyword "template" have already been parsed
TemplateDef t = new TemplateDef(
Path.GetFileName(CurrentFileName), LineNumber);
t.Name = name;
t.Description = LastComment;
LastComment = "";
// start the token body with the current (trimmed) line
line = line.Trim();
String tBody = line.Trim();
// scan tokens until we find a semicolon
for (;;)
{
// if necessary, read a new line
if (line == "")
{
line = GetNextLine().Trim();
tBody += " " + line;
}
// get the next token
if (GetNextToken(ref line) == ";")
break;
}
// set the template body
t.Body = tBody;
// add the template to the tables
this.SymbolTable.Templates.Add(t);
this.CurrentSourceFile.Templates.Add(t);