Skip to content

Commit 46c49bd

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 46c49bd

7 files changed

Lines changed: 157 additions & 9 deletions

File tree

dub.sdl

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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 "library"
8+
9+
// This is required because otherwise Dub will include all source files in src.
10+
// Just picked an existing directory which doesn't contain any D source files.
11+
sourcePaths "docs"
12+
13+
// This is required because Dub needs to have at lease one source file
14+
sourceFiles "src/ddmd/package.d"
15+
16+
dependency ":parser" version="*"
17+
18+
subPackage {
19+
name "idgen"
20+
targetType "executable"
21+
sourcePaths "docs"
22+
sourceFiles "src/ddmd/idgen.d"
23+
}
24+
25+
subPackage {
26+
name "root"
27+
targetType "library"
28+
sourcePaths "src/ddmd/root"
29+
}
30+
31+
subPackage {
32+
name "lexer"
33+
targetType "library"
34+
sourcePaths "docs"
35+
36+
sourceFiles \
37+
"src/ddmd/entity.d" \
38+
"src/ddmd/errors.d" \
39+
"src/ddmd/globals.d" \
40+
"src/ddmd/id.d" \
41+
"src/ddmd/identifier.d" \
42+
"src/ddmd/lexer.d" \
43+
"src/ddmd/tokens.d" \
44+
"src/ddmd/utf.d"
45+
46+
// stringImportPaths cannot be used because it will make Dub extremely slow
47+
dflags "-J$PACKAGE_DIR"
48+
49+
preBuildCommands "dub run dmd:idgen -- $PACKAGE_DIR"
50+
dependency "dmd:root" version="*"
51+
}
52+
53+
subPackage {
54+
name "parser"
55+
targetType "library"
56+
sourcePaths "docs"
57+
58+
sourceFiles \
59+
"src/ddmd/astnull.d" \
60+
"src/ddmd/parse.d"
61+
62+
dependency "dmd:lexer" version="*"
63+
}

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");

src/ddmd/package.d

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module ddmd;

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: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
void main()
2+
{
3+
}
4+
5+
// lexer
6+
unittest
7+
{
8+
import ddmd.lexer;
9+
import ddmd.tokens;
10+
11+
immutable expected = [
12+
TOKvoid,
13+
TOKidentifier,
14+
TOKlparen,
15+
TOKrparen,
16+
TOKlcurly,
17+
TOKrcurly
18+
];
19+
20+
immutable sourceCode = "void test() {} // foobar";
21+
scope lexer = new Lexer("test", sourceCode.ptr, 0, sourceCode.length, 0, 0);
22+
lexer.nextToken;
23+
24+
TOK[] result;
25+
26+
do
27+
{
28+
result ~= lexer.token.value;
29+
} while (lexer.nextToken != TOKeof);
30+
31+
assert(result == expected);
32+
}
33+
34+
// parser
35+
unittest
36+
{
37+
import ddmd.astnull;
38+
import ddmd.parse;
39+
40+
scope parser = new Parser!ASTNull(null, null, false);
41+
assert(parser !is null);
42+
}

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)