Skip to content

Commit cdd0cbb

Browse files
committed
Move config info into in-Rascal structure with yaml config file, step 1
1 parent 733a0ee commit cdd0cbb

File tree

13 files changed

+301
-146
lines changed

13 files changed

+301
-146
lines changed

.gitignore

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,3 @@ target
2626
# VSCode
2727
.vscode
2828

29-
# PHP Config file, should not be in GitHub
30-
src/main/rascal/lang/php/config/Config.rsc
31-

src/main/rascal/lang/php/analysis/composer/ComposerAnalysis.rsc

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -88,26 +88,26 @@ public map[loc, Composer] loadComposerFilesForCorpus() {
8888
public map[loc, ClocResult] slocForCorpus() {
8989
map[loc, ClocResult] result = ( );
9090
for (l <- composerCorpus) {
91-
cres = phpLinesOfCode(l, clocLoc);
91+
cres = phpLinesOfCode(l, clocLoc());
9292
result[l] = cres;
9393
}
9494
return result;
9595
}
9696
9797
public void saveSlocInfo(map[loc, ClocResult] results) {
98-
writeBinaryValueFile(baseLoc + "extract/clocinfo/composer-cloc.bin", results);
98+
writeBinaryValueFile(baseLoc() + "extract/clocinfo/composer-cloc.bin", results);
9999
}
100100
101101
public map[loc, ClocResult] loadSlocInfo() {
102-
return readBinaryValueFile(#map[loc, ClocResult], baseLoc + "extract/clocinfo/composer-cloc.bin");
102+
return readBinaryValueFile(#map[loc, ClocResult], baseLoc() + "extract/clocinfo/composer-cloc.bin");
103103
}
104104
105105
public void saveComposerInfo(map[loc, Composer] results) {
106-
writeBinaryValueFile(baseLoc + "extract/composer/composer-info.bin", results);
106+
writeBinaryValueFile(baseLoc() + "extract/composer/composer-info.bin", results);
107107
}
108108
109109
public map[loc, Composer] loadComposerInfo() {
110-
return readBinaryValueFile(#map[loc, Composer], baseLoc + "extract/composer/composer-info.bin");
110+
return readBinaryValueFile(#map[loc, Composer], baseLoc() + "extract/composer/composer-info.bin");
111111
}
112112
113113
@doc{

src/main/rascal/lang/php/analysis/includes/IncludesInfo.rsc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import IO;
2828
import ValueIO;
2929
import Map;
3030

31-
private loc infoLoc = baseLoc + "serialized/includeInfo";
31+
private loc infoLoc = baseLoc() + "serialized/includeInfo";
3232

3333

3434
public void buildIncludesInfo(str p, str v, loc baseloc, bool forceBuild=false) {

src/main/rascal/lang/php/analysis/signatures/Summaries.rsc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ module lang::php::analysis::signatures::Summaries
1616
import ValueIO;
1717
import lang::php::config::Config;
1818

19-
private loc summariesDir = baseLoc + "serialized/summaries";
19+
private loc summariesDir = baseLoc() + "serialized/summaries";
2020

2121
data SummaryParam
2222
= standardParam(str paramType, str paramName)
Lines changed: 216 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,216 @@
1+
@license{
2+
Copyright (c) 2013, NWO-I Centrum Wiskunde & Informatica (CWI), Mark Hills, Appalachian State University
3+
All rights reserved.
4+
5+
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
6+
7+
1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
8+
9+
2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
10+
11+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
12+
}
13+
module lang::php::config::Config
14+
15+
import lang::php::util::Option;
16+
17+
import IO;
18+
import Exception;
19+
import lang::yaml::Model;
20+
21+
public data Exception
22+
= configMissing(str key, str msg)
23+
;
24+
25+
public data Config
26+
= config(
27+
Option[bool] usePhpParserJar,
28+
Option[loc] phpLoc,
29+
Option[loc] parserLoc,
30+
Option[loc] analysisLoc,
31+
Option[str] parserMemLimit,
32+
Option[str] astToRascal,
33+
Option[loc] parserWorkingDir,
34+
Option[loc] baseLoc,
35+
Option[loc] parsedDir,
36+
Option[loc] statsDir,
37+
Option[loc] corpusRoot,
38+
Option[loc] countsDir,
39+
Option[bool] useBinaries,
40+
Option[int] logLevel,
41+
Option[loc] clocLoc)
42+
| emptyConfig()
43+
;
44+
45+
private Config c = emptyConfig();
46+
47+
public Config getConfig() {
48+
if (c is emptyConfig) {
49+
c = loadConfig();
50+
}
51+
return c;
52+
}
53+
54+
private Config loadConfig() {
55+
bool usePhpParserJar = false;
56+
loc phpLoc = |file:///opt/homebrew/bin/php|;
57+
loc parserLoc = |file:///Users/hillsma/Projects/php-analysis/PHP-Parser|;
58+
loc analysisLoc = |file:///Users/hillsma/Projects/php-analysis/php-analysis/|;
59+
str parserMemLimit = "1024M";
60+
str astToRascal = "AST2Rascal.php";
61+
loc parserWorkingDir = (parserLoc + astToRascal).parent;
62+
loc baseLoc = |home:///PHPAnalysis|;
63+
loc parsedDir = baseLoc + "serialized/parsed";
64+
loc statsDir = baseLoc + "serialized/stats";
65+
loc corpusRoot = baseLoc + "systems";
66+
loc countsDir = baseLoc + "serialized/counts";
67+
bool useBinaries = false;
68+
int logLevel = 2;
69+
loc clocLoc = |file:///opt/homebrew/bin/cloc|;
70+
71+
Config c = config(
72+
some(usePhpParserJar),
73+
some(phpLoc),
74+
some(parserLoc),
75+
some(analysisLoc),
76+
some(parserMemLimit),
77+
some(astToRascal),
78+
some(parserWorkingDir),
79+
some(baseLoc),
80+
some(parsedDir),
81+
some(statsDir),
82+
some(corpusRoot),
83+
some(countsDir),
84+
some(useBinaries),
85+
some(logLevel),
86+
some(clocLoc)
87+
);
88+
89+
return c;
90+
}
91+
92+
@doc{Indicates whether to use the parser contained in a distributed jar file or from the directory given below}
93+
public bool usePhpParserJar() {
94+
c = getConfig();
95+
return (c has usePhpParserJar && some(bool b) := c.usePhpParserJar) ? b : false;
96+
}
97+
98+
@doc{The location of the PHP executable}
99+
public loc phpLoc() {
100+
c = getConfig();
101+
if (c has phpLoc && some(loc l) := c.phpLoc) {
102+
return l;
103+
}
104+
throw configMissing("phpLoc", "Make sure to set phpLoc to a valid location in your configuration file");
105+
}
106+
107+
@doc{The base install location for the PHP-Parser project}
108+
public loc parserLoc() {
109+
c = getConfig();
110+
if (c has parserLoc && some(loc l) := c.parserLoc) {
111+
return l;
112+
}
113+
throw configMissing("parserLoc", "Make sure to set parserLoc to a valid location in your configuration file");
114+
}
115+
116+
@doc{The base install location for the php-analysis project}
117+
public loc analysisLoc() {
118+
c = getConfig();
119+
if (c has analysisLoc && some(loc l) := c.analysisLoc) {
120+
return l;
121+
}
122+
throw configMissing("analysisLoc", "Make sure to set analysisLoc to a valid location in your configuration file");
123+
}
124+
125+
@doc{The memory limit for PHP when the parser is run}
126+
public str parserMemLimit() {
127+
c = getConfig();
128+
return (c has parserMemLimit && some(str s) := c.parserMemLimit) ? s : "1024M";
129+
}
130+
131+
@doc{The location of the AST2Rascal.php file, inside the PHP-Parser directories}
132+
public str astToRascal() {
133+
c = getConfig();
134+
return (c has astToRascal && some(str s) := c.astToRascal) ? s : "AST2Rascal.php";
135+
}
136+
137+
@doc{The working directory for when the parser runs}
138+
public loc parserWorkingDir() {
139+
c = getConfig();
140+
if (c has parserWorkingDir && some(loc l) := c.parserWorkingDir) {
141+
return l;
142+
}
143+
throw configMissing("parserWorkingDir", "Make sure to set parserWorkingDir to a valid location in your configuration file");
144+
}
145+
146+
@doc{The base location for the corpus and any serialized files}
147+
public loc baseLoc() {
148+
c = getConfig();
149+
if (c has baseLoc && some(loc l) := c.baseLoc) {
150+
return l;
151+
}
152+
throw configMissing("baseLoc", "Make sure to set baseLoc to a valid location in your configuration file");
153+
}
154+
155+
@doc{Where to put the binary representations of parsed systems}
156+
public loc parsedDir() {
157+
c = getConfig();
158+
if (c has parsedDir && some(loc l) := c.parsedDir) {
159+
return l;
160+
}
161+
throw configMissing("parsedDir", "Make sure to set parsedDir to a valid location in your configuration file");
162+
}
163+
164+
@doc{Where to put the binary representations of extracted statistics}
165+
public loc statsDir() {
166+
c = getConfig();
167+
if (c has statsDir && some(loc l) := c.statsDir) {
168+
return l;
169+
}
170+
throw configMissing("statsDir", "Make sure to set statsDir to a valid location in your configuration file");
171+
}
172+
173+
@doc{Where the PHP sources for the corpus reside}
174+
public loc corpusRoot() {
175+
c = getConfig();
176+
if (c has corpusRoot && some(loc l) := c.corpusRoot) {
177+
return l;
178+
}
179+
throw configMissing("corpusRoot", "Make sure to set corpusRoot to a valid location in your configuration file");
180+
}
181+
182+
@doc{Where to put extracted counts (e.g., SLOC)}
183+
public loc countsDir() {
184+
c = getConfig();
185+
if (c has countsDir && some(loc l) := c.countsDir) {
186+
return l;
187+
}
188+
throw configMissing("countsDir", "Make sure to set countsDir to a valid location in your configuration file");
189+
}
190+
191+
@doc{This should only ever be true if we don't have source, we only have the extracted binaries for parsed systems}
192+
public bool useBinaries() {
193+
c = getConfig();
194+
return (c has useBinaries && some(bool b) := c.useBinaries) ? b : false;
195+
}
196+
197+
@doc{Debugging options
198+
@logLevel {
199+
Log level 0 => no logging;
200+
Log level 1 => main logging;
201+
Log level 2 => debug logging;
202+
}
203+
}
204+
public int logLevel() {
205+
c = getConfig();
206+
return (c has logLevel && some(int n) := c.logLevel) ? n : 2;
207+
}
208+
209+
@doc{The location of the cloc tool}
210+
public loc clocLoc() {
211+
c = getConfig();
212+
if (c has clocLoc && some(loc l) := c.clocLoc) {
213+
return l;
214+
}
215+
throw configMissing("clocLoc", "Make sure to set clocLoc to a valid location in your configuration file");
216+
}

src/main/rascal/lang/php/config/Config.rsc-dist

Lines changed: 0 additions & 53 deletions
This file was deleted.

src/main/rascal/lang/php/stats/SLOC.rsc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@ import Set;
2323
public map[str lang, tuple[int lineCount, int fileCount] counts] loadCounts(str product, str version) {
2424
map[str lang, tuple[int lineCount, int fileCount] counts] res = ( );
2525

26-
countItem = countsDir + "<toLowerCase(product)>-<version>";
26+
countItem = countsDir() + "<toLowerCase(product)>-<version>";
2727

2828
if (!exists(countItem))
29-
countItem = countsDir + "<toLowerCase(product)>_<version>";
29+
countItem = countsDir() + "<toLowerCase(product)>_<version>";
3030

3131
if (!exists(countItem))
3232
throw "Could not find counts file for <toLowerCase(product)>-<version>";

src/main/rascal/lang/php/stats/Stats.rsc

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -468,23 +468,23 @@ public void buildStats() {
468468
469469
public void writeFeatureCounts(str product, str version, map[str,int] fc) {
470470
println("Writing counts for <product>-<version>");
471-
loc fcLoc = statsDir + "<product>-<version>.fc";
471+
loc fcLoc = statsDir() + "<product>-<version>.fc";
472472
writeBinaryValueFile(fcLoc, fc);
473473
}
474474
475475
public void writeStats(str product, str version, map[str,int] fc, map[str,int] sc, map[str,int] ec) {
476-
loc fcLoc = statsDir + "<product>-<version>.fc";
477-
loc scLoc = statsDir + "<product>-<version>.sc";
478-
loc ecLoc = statsDir + "<product>-<version>.ec";
476+
loc fcLoc = statsDir() + "<product>-<version>.fc";
477+
loc scLoc = statsDir() + "<product>-<version>.sc";
478+
loc ecLoc = statsDir() + "<product>-<version>.ec";
479479
writeBinaryValueFile(fcLoc, fc);
480480
writeBinaryValueFile(scLoc, sc);
481481
writeBinaryValueFile(ecLoc, ec);
482482
}
483483
484484
public tuple[map[str,int] fc, map[str,int] sc, map[str,int] ec] getStats(str product, str version) {
485-
loc fcLoc = statsDir + "<product>-<version>.fc";
486-
loc scLoc = statsDir + "<product>-<version>.sc";
487-
loc ecLoc = statsDir + "<product>-<version>.ec";
485+
loc fcLoc = statsDir() + "<product>-<version>.fc";
486+
loc scLoc = statsDir() + "<product>-<version>.sc";
487+
loc ecLoc = statsDir() + "<product>-<version>.ec";
488488
return < readBinaryValueFile(#map[str,int],fcLoc), readBinaryValueFile(#map[str,int],scLoc), readBinaryValueFile(#map[str,int],ecLoc) >;
489489
}
490490

0 commit comments

Comments
 (0)