1 module coregrammars.grammars;
2 import pegged.grammar;
3 
4 //enum CoreGrammars = [ "expr", "ini", "json", "md" ];
5 enum CoreGrammarsFolder = "source/coregrammars/gen/";
6 enum CoreGrammarsPackage = "coregrammars.gen";
7 
8 version(COREGRAMMARS_MODGEN) {
9   version(unittest) {
10     // mixin each grammar inline (markdown grammar inlined crashes the compiler)
11     enum CoreGrammars = [ "terms","expr", "ini", "json" ];
12     static foreach(g;CoreGrammars) {
13       mixin(grammar(import(g~".peg")));
14     }
15   } else {
16     enum CoreGrammars = [
17       "terms": "",
18       "expr": "import coregrammars.gen.terms;\n",
19       "ini": "import coregrammars.gen.terms;\n",
20       "json": "import coregrammars.gen.terms;\n",
21       "md": ""
22     ];
23     // Main routine that regenerates each grammar module
24     void main(string[] args) {
25       import std.file : mkdirRecurse,rmdirRecurse;
26       CoreGrammarsFolder.rmdirRecurse;
27       CoreGrammarsFolder.mkdirRecurse;
28       static foreach(g,h;CoreGrammars) {
29         import std.experimental.logger;
30         sharedLog.log("Module generator: ",g);
31         asModule(CoreGrammarsPackage ~ "." ~ g,
32             CoreGrammarsFolder ~ g,
33             import(g ~ ".peg"),
34             h
35         );
36       }
37     }
38   }
39 } else {
40   public import coregrammars.gen.terms;
41   public import coregrammars.gen.expr;
42   public import coregrammars.gen.ini;
43   public import coregrammars.gen.json;
44   public import coregrammars.gen.md;
45   enum CoreGrammars = [ "terms", "expr", "ini", "json", "md" ];
46 }
47 
48 unittest {
49   //import coregrammars.gen.expr;
50 }
51 
52 unittest {
53   //import coregrammars.gen.ini;
54   enum input = `
55  [Section1]
56  testString = "stringVal"
57  testInt = 3
58  testBool = true
59  testNull = null
60  [Section2]
61  testString = "string2Val"
62  testInt = 13
63  testBool = false
64  `;
65  enum parsed = INIGrammar(input);
66  assert(parsed.successful);
67 }
68 
69 unittest {
70   //import coregrammars.gen.ini;
71   enum input = `
72  # Test comment
73  [Section1]
74  testString = "stringVal"
75  testInt = 3 # Test inline comment
76  testBool = true
77  # Test comment
78  [Section2]
79  testString = "string2Val"
80  # Test comment
81  testInt = 13
82  [Section3]
83  testBool = false
84  `;
85  enum parsed = INIGrammar(input);
86  assert(parsed.successful);
87 
88 }
89 
90 unittest {
91   //import coregrammars.gen.json;
92   alias JSON=JSONGrammar;
93     enum example2 = `
94     {
95     "Number": 42,
96     "Decimal": 123.456,
97     "String": "abc",
98     "NullString": "",
99     "Escape": "\uAAAA\n\\Hello",
100     "Array" : [0,1,2],
101     "Array2": [0, [0,1,2], "abc"],
102     "Obj"   : { "Member":0, "Member":[0,1,2] },
103     "True"  : true,
104     "False" : false,
105     "Null"  : null,
106     "Empty" : {}
107     }`;
108 
109     auto example2Tree = JSON(example2);
110     assert(example2Tree.successful);
111     assert(example2Tree[0].children.length == 12);
112 
113     assert(example2Tree[0][0][0].matches == ["Number"]);
114     assert(example2Tree[0][0][1].matches == ["42"]);
115 
116     assert(example2Tree[0][2][0].matches == ["String"]);
117     assert(example2Tree[0][2][1].matches == ["abc"]);
118 
119     enum example3 =
120         `{
121         "glossary": {
122             "title": "example glossary",
123             "GlossDiv": {
124                 "title": "S",
125                 "GlossList": {
126                     "GlossEntry": {
127                         "ID": "SGML",
128                         "SortAs": "SGML",
129                         "GlossTerm": "Standard Generalized Markup Language",
130                         "Acronym": "SGML",
131                         "Abbrev": "ISO 8879:1986",
132                         "GlossDef": {
133                             "para": "A meta-markup language, used to create markup languages such as DocBook.",
134                             "GlossSeeAlso": ["GML", "XML"]
135                         },
136                         "GlossSee": "markup"
137                     }
138                 }
139             }
140         }
141     }`;
142 
143     enum example4 =
144     `{"web-app": {
145     "servlet": [
146         {
147         "servlet-name": "cofaxCDS",
148         "servlet-class": "org.cofax.cds.CDSServlet",
149         "init-param": {
150             "configGlossary:installationAt": "Philadelphia, PA",
151             "configGlossary:adminEmail": "ksm@pobox.com",
152             "configGlossary:poweredBy": "Cofax",
153             "configGlossary:poweredByIcon": "/images/cofax.gif",
154             "configGlossary:staticPath": "/content/static",
155             "templateProcessorClass": "org.cofax.WysiwygTemplate",
156             "templateLoaderClass": "org.cofax.FilesTemplateLoader",
157             "templatePath": "templates",
158             "templateOverridePath": "",
159             "defaultListTemplate": "listTemplate.htm",
160             "defaultFileTemplate": "articleTemplate.htm",
161             "useJSP": false,
162             "jspListTemplate": "listTemplate.jsp",
163             "jspFileTemplate": "articleTemplate.jsp",
164             "cachePackageTagsTrack": 200,
165             "cachePackageTagsStore": 200,
166             "cachePackageTagsRefresh": 60,
167             "cacheTemplatesTrack": 100,
168             "cacheTemplatesStore": 50,
169             "cacheTemplatesRefresh": 15,
170             "cachePagesTrack": 200,
171             "cachePagesStore": 100,
172             "cachePagesRefresh": 10,
173             "cachePagesDirtyRead": 10,
174             "searchEngineListTemplate": "forSearchEnginesList.htm",
175             "searchEngineFileTemplate": "forSearchEngines.htm",
176             "searchEngineRobotsDb": "WEB-INF/robots.db",
177             "useDataStore": true,
178             "dataStoreClass": "org.cofax.SqlDataStore",
179             "redirectionClass": "org.cofax.SqlRedirection",
180             "dataStoreName": "cofax",
181             "dataStoreDriver": "com.microsoft.jdbc.sqlserver.SQLServerDriver",
182             "dataStoreUrl": "jdbc:microsoft:sqlserver://LOCALHOST:1433;DatabaseName=goon",
183             "dataStoreUser": "sa",
184             "dataStorePassword": "dataStoreTestQuery",
185             "dataStoreTestQuery": "SET NOCOUNT ON;select test='test';",
186             "dataStoreLogFile": "/usr/local/tomcat/logs/datastore.log",
187             "dataStoreInitConns": 10,
188             "dataStoreMaxConns": 100,
189             "dataStoreConnUsageLimit": 100,
190             "dataStoreLogLevel": "debug",
191             "maxUrlLength": 500}},
192         {
193         "servlet-name": "cofaxEmail",
194         "servlet-class": "org.cofax.cds.EmailServlet",
195         "init-param": {
196         "mailHost": "mail1",
197         "mailHostOverride": "mail2"}},
198         {
199         "servlet-name": "cofaxAdmin",
200         "servlet-class": "org.cofax.cds.AdminServlet"},
201         {
202         "servlet-name": "fileServlet",
203         "servlet-class": "org.cofax.cds.FileServlet"},
204         {
205         "servlet-name": "cofaxTools",
206         "servlet-class": "org.cofax.cms.CofaxToolsServlet",
207         "init-param": {
208             "templatePath": "toolstemplates/",
209             "log": 1,
210             "logLocation": "/usr/local/tomcat/logs/CofaxTools.log",
211             "logMaxSize": "",
212             "dataLog": 1,
213             "dataLogLocation": "/usr/local/tomcat/logs/dataLog.log",
214             "dataLogMaxSize": "",
215             "removePageCache": "/content/admin/remove?cache=pages&id=",
216             "removeTemplateCache": "/content/admin/remove?cache=templates&id=",
217             "fileTransferFolder": "/usr/local/tomcat/webapps/content/fileTransferFolder",
218             "lookInContext": 1,
219             "adminGroupID": 4,
220             "betaServer": true}}],
221     "servlet-mapping": {
222         "cofaxCDS": "/",
223         "cofaxEmail": "/cofaxutil/aemail/*",
224         "cofaxAdmin": "/admin/*",
225         "fileServlet": "/static/*",
226         "cofaxTools": "/tools/*"},
227     "taglib": {
228         "taglib-uri": "cofax.tld",
229         "taglib-location": "/WEB-INF/tlds/cofax.tld"}}}
230     `;
231 
232     assert(JSON(example3).successful);
233     assert(JSON(example4).successful);
234 }
235 
236   // Skip markdown inline test because it causes out of memory in the compilers.
237   version(COREGRAMMARS_MODGEN) {
238     // ...
239   } else {
240     unittest {
241       import coregrammars.gen.md;
242 
243     }
244   }
245 
246 version(unittest) {
247 } else {
248 }