Skip to content

Commit 7e832a2

Browse files
Fix issue 17392 - Add Dub file for the lexer and parser
The Dub file is intended to make the the compiler available as a library through Dub.
1 parent f546675 commit 7e832a2

6 files changed

Lines changed: 151 additions & 9 deletions

File tree

dub.sdl

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
name "dmd"
2+
description "The DMD compiler"
3+
authors "Walter Bright"
4+
copyright "Copyright © 1999-2017, Digital Mars"
5+
license "BSL-1.0"
6+
7+
targetType "none"
8+
dependency ":parser" version="*"
9+
10+
subPackage {
11+
name "idgen"
12+
targetType "executable"
13+
sourcePaths
14+
sourceFiles "src/ddmd/idgen.d"
15+
}
16+
17+
subPackage {
18+
name "root"
19+
targetType "library"
20+
sourcePaths "src/ddmd/root"
21+
}
22+
23+
subPackage {
24+
name "lexer"
25+
targetType "library"
26+
sourcePaths
27+
28+
sourceFiles \
29+
"src/ddmd/entity.d" \
30+
"src/ddmd/errors.d" \
31+
"src/ddmd/globals.d" \
32+
"src/ddmd/id.d" \
33+
"src/ddmd/identifier.d" \
34+
"src/ddmd/lexer.d" \
35+
"src/ddmd/tokens.d" \
36+
"src/ddmd/utf.d"
37+
38+
// stringImportPaths cannot be used because it will make Dub extremely slow
39+
dflags "-J$PACKAGE_DIR"
40+
41+
preBuildCommands "dub run dmd:idgen -- $PACKAGE_DIR"
42+
dependency "dmd:root" version="*"
43+
}
44+
45+
subPackage {
46+
name "parser"
47+
targetType "library"
48+
sourcePaths
49+
50+
sourceFiles \
51+
"src/ddmd/astnull.d" \
52+
"src/ddmd/parse.d"
53+
54+
dependency "dmd:lexer" version="*"
55+
}

src/ddmd/idgen.d

100644100755
Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -372,17 +372,46 @@ Msgtable[] msgtable =
372372
{ "udaSelector", "selector" },
373373
];
374374

375+
// Copied from std.string
376+
immutable(char)* toStringz(in string s) @trusted pure nothrow
377+
{
378+
if (s.length == 0) return "".ptr;
379+
380+
immutable p = s.ptr + s.length;
381+
382+
if ((cast(size_t) p & 3) && *p == 0)
383+
return &s[0];
384+
385+
auto copy = new char[s.length + 1];
386+
copy[0 .. s.length] = s[];
387+
copy[s.length] = 0;
375388

376-
int main()
389+
return &(cast(immutable(char)*)copy)[0];
390+
}
391+
392+
int main(string[] args)
377393
{
394+
// If an argument is given it's expected to be the full path of the project
395+
// root directory with a trailing slash. The argument is used when building
396+
// with Dub.
397+
immutable workingDirectory = args.length == 2 ? args[1] ~ "src/" : "./";
398+
399+
FILE* openFile(string path)
378400
{
379-
auto fp = fopen("ddmd/id.h","wb");
401+
immutable fullPath = toStringz(workingDirectory ~ path);
402+
auto fp = fopen(fullPath,"wb");
380403
if (!fp)
381404
{
382-
printf("can't open ddmd/id.h\n");
405+
printf("can't open %s\n", fullPath);
383406
exit(EXIT_FAILURE);
384407
}
385408

409+
return fp;
410+
}
411+
412+
{
413+
auto fp = openFile("ddmd/id.h");
414+
386415
fprintf(fp, "// File generated by idgen.d\n");
387416
fprintf(fp, "#ifndef DMD_ID_H\n");
388417
fprintf(fp, "#define DMD_ID_H 1\n");
@@ -404,12 +433,7 @@ int main()
404433
}
405434

406435
{
407-
auto fp = fopen("ddmd/id.d","wb");
408-
if (!fp)
409-
{
410-
printf("can't open ddmd.id.d\n");
411-
exit(EXIT_FAILURE);
412-
}
436+
auto fp = openFile("ddmd/id.d");
413437

414438
fprintf(fp, "// File generated by idgen.d\n");
415439
fprintf(fp, "\n");

test/dub_package/.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
.dub
2+
docs.json
3+
__dummy.html
4+
*.o
5+
*.obj

test/dub_package/dub.sdl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
name "dmd-dub-test"
2+
description "Test of the DMD Dub package"
3+
license "BSL 1.0"
4+
5+
dependency "dmd" path="../../"

test/dub_package/source/app.d

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// The tests in this module are highlevel and mostly indented to make sure all
2+
// necessary modules are included in the Dub package.
3+
4+
void main()
5+
{
6+
}
7+
8+
// lexer
9+
unittest
10+
{
11+
import ddmd.lexer;
12+
import ddmd.tokens;
13+
14+
immutable expected = [
15+
TOKvoid,
16+
TOKidentifier,
17+
TOKlparen,
18+
TOKrparen,
19+
TOKlcurly,
20+
TOKrcurly
21+
];
22+
23+
immutable sourceCode = "void test() {} // foobar";
24+
scope lexer = new Lexer("test", sourceCode.ptr, 0, sourceCode.length, 0, 0);
25+
lexer.nextToken;
26+
27+
TOK[] result;
28+
29+
do
30+
{
31+
result ~= lexer.token.value;
32+
} while (lexer.nextToken != TOKeof);
33+
34+
assert(result == expected);
35+
}
36+
37+
// parser
38+
unittest
39+
{
40+
import ddmd.astnull;
41+
import ddmd.parse;
42+
43+
scope parser = new Parser!ASTNull(null, null, false);
44+
assert(parser !is null);
45+
}

travis.sh

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ rebuild() {
5454

5555
# test druntime, phobos, dmd
5656
test() {
57+
test_dub_package
5758
make -j$N -C ../druntime -f posix.mak MODEL=$MODEL unittest
5859
make -j$N -C ../phobos -f posix.mak MODEL=$MODEL unittest
5960
test_dmd
@@ -69,6 +70,13 @@ test_dmd() {
6970
fi
7071
}
7172

73+
# test dub package
74+
test_dub_package() {
75+
pushd test/dub_package
76+
dub test
77+
popd
78+
}
79+
7280
for proj in druntime phobos; do
7381
if [ $TRAVIS_BRANCH != master ] && [ $TRAVIS_BRANCH != stable ] &&
7482
! git ls-remote --exit-code --heads https://github.com/dlang/$proj.git $TRAVIS_BRANCH > /dev/null; then

0 commit comments

Comments
 (0)