1 using System;
2 using System.Collections;
3 using System.IO;
4
5
6
7 public void PrintUsage()
8 {
9 Console.Out() << "Soul CM Lexer Generator version " << soulcm.scmlg.LexerGeneratorVersionStr() << endl();
10 Console.Out() << "Usage: scmlg [options] <file.lexer>" << endl();
11 Console.Out() << "Options:" << endl();
12 Console.Out() << "--help | -h" << endl();
13 Console.Out() << " Print help and exit." << endl();
14 Console.Out() << "--verbose | -v" << endl();
15 Console.Out() << " Be verbose." << endl();
16 Console.Out() << "--debug | -d" << endl();
17 Console.Out() << " Print character class partition to stdout" << endl();
18 Console.Out() << "--use-ascii-identifier-classes | -a" << endl();
19 Console.Out() << " Use ASCII identifier classes." << endl();
20 Console.Out() << " By default uses Unicode identifier classes." << endl();
21 Console.Out() << "--no-identifier-classes | -n" << endl();
22 Console.Out() << " No predefined identifier classes" << endl();
23 Console.Out() << "--no-class-map-compression | -c" << endl();
24 Console.Out() << " Do not compress class maps." << endl();
25 }
26 public int main(int argc, const char** argv)
27 {
28 try
29 {
30 bool verbose = false;
31 bool debug = false;
32 bool noClassMapCompression = false;
33 soulcm.scmlg.IdentifierClassKind identifierClassKind = soulcm.scmlg.IdentifierClassKind.unicode;
34 string fileName;
35 for (int i = 1; i < argc; ++i;)
36 {
37 string arg = argv[i];
38 if (arg.StartsWith("--"))
39 {
40 if (arg == "--help")
41 {
42 PrintUsage();
43 return 1;
44 }
45 else if (arg == "--verbose")
46 {
47 verbose = true;
48 }
49 else if (arg == "--debug")
50 {
51 debug = true;
52 }
53 else if (arg == "--use-ascii-identifier-classes")
54 {
55 identifierClassKind = soulcm.scmlg.IdentifierClassKind.ascii;
56 }
57 else if (arg == "--no-identifier-classes")
58 {
59 identifierClassKind = soulcm.scmlg.IdentifierClassKind.none;
60 }
61 else if (arg == "--no-class-map-compression")
62 {
63 noClassMapCompression = true;
64 }
65 else
66 {
67 throw Exception("unknown option \'" + arg + "\'");
68 }
69 }
70 else if (arg.StartsWith("-"))
71 {
72 string options = arg.Substring(1);
73 if (options.IsEmpty())
74 {
75 throw Exception("unknown option \'-" + arg + "\'");
76 }
77 for (char o : options)
78 {
79 if (o == 'h')
80 {
81 PrintUsage();
82 return 1;
83 }
84 else if (o == 'v')
85 {
86 verbose = true;
87 }
88 else if (o == 'd')
89 {
90 debug = true;
91 }
92 else if (o == 'a')
93 {
94 identifierClassKind = soulcm.scmlg.IdentifierClassKind.ascii;
95 }
96 else if (o == 'n')
97 {
98 identifierClassKind = soulcm.scmlg.IdentifierClassKind.none;
99 }
100 else if (o == 'c')
101 {
102 noClassMapCompression = true;
103 }
104 else
105 {
106 throw Exception("unknown option \'-" + string(o, 1) + "\'");
107 }
108 }
109 }
110 else
111 {
112 fileName = GetFullPath(arg);
113 }
114 }
115 if (fileName.IsEmpty())
116 {
117 PrintUsage();
118 return 1;
119 }
120 if (verbose)
121 {
122 Console.Out() << "> " << fileName << endl();
123 }
124 string s = File.ReadAllText(fileName);
125 ustring content = ToUtf32(s);
126 LexerFileLexer lexer(content, fileName, 0);
127 ParsingContext ctx;
128 UniquePtr<soulcm.scmlg.LexerFile> lexerFile = LexerFileParser.Parse(lexer, &ctx);
129 soulcm.scmlg.LexerContext lexerContext(identifierClassKind);
130 lexerContext.SetFileName(fileName);
131 soulcm.scmlg.RegularExpressionParser regularExpressionParser;
132 lexerContext.SetParser(®ularExpressionParser);
133 string root = Path.GetDirectoryName(fileName);
134 lexerFile->Process(root, verbose, debug, noClassMapCompression, lexerContext);
135 }
136 catch (const Exception& ex)
137 {
138 Console.Error() << ex.Message() << endl();
139 return 1;
140 }
141 return 0;
142 }