Search in sources :

Example 1 with DtsParser

use of org.eclipse.n4js.dts.DtsParser in project n4js by eclipse.

the class N4JSResource method doLoad.

/**
 * Ensures that state is discarded before loading (i.e. the second slot is unloaded).
 */
@Override
protected void doLoad(InputStream inputStream, Map<?, ?> options) throws IOException {
    if (options != null && options.containsKey(OPTION_CLEAR_FUNCTION_BODIES)) {
        optionClearFunctionBodies = Boolean.TRUE == options.get(OPTION_CLEAR_FUNCTION_BODIES);
    }
    if (contents != null && !contents.isEmpty()) {
        discardStateFromDescription(true);
    }
    if (isOpaque()) {
        IParseResult result = new JSParseResult(inputStream);
        updateInternalState(this.getParseResult(), result);
    } else {
        ResourceType resourceType = ResourceType.getResourceType(getURI());
        if (resourceType == ResourceType.DTS) {
            setValidationDisabled(true);
            try (Reader reader = createReader(inputStream)) {
                IParseResult result = new DtsParser().parse(reader, this);
                updateInternalState(this.getParseResult(), result);
            }
        } else {
            super.doLoad(inputStream, options);
        }
    }
}
Also used : DtsParser(org.eclipse.n4js.dts.DtsParser) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) ResourceType(org.eclipse.n4js.utils.ResourceType) IParseResult(org.eclipse.xtext.parser.IParseResult)

Example 2 with DtsParser

use of org.eclipse.n4js.dts.DtsParser in project n4js by eclipse.

the class DtsParsesDefinitelyTypedTest method assertParseCounts.

static void assertParseCounts(Path repoRoot, int minPass, int maxFail, int maxError) throws IOException {
    Path typesFolder = repoRoot.resolve("types");
    List<Path> files = Files.walk(typesFolder, FileVisitOption.FOLLOW_LINKS).filter(file -> {
        if (!file.toString().endsWith(".d.ts")) {
            return false;
        }
        String relPath = typesFolder.relativize(file).toString();
        for (String exclude : EXCLUDES) {
            if (exclude != null && !exclude.isBlank() && relPath.startsWith(exclude)) {
                return false;
            }
        }
        return true;
    }).collect(Collectors.toList());
    Collections.sort(files, (p1, p2) -> p1.toString().compareTo(p2.toString()));
    int filesCount = files.size();
    int pass = 0;
    int fail = 0;
    int error = 0;
    System.out.println("Processing " + filesCount + " files ...");
    Stopwatch sw = Stopwatch.createStarted();
    for (Path file : files) {
        try (BufferedReader buf = new BufferedReader(new InputStreamReader(new FileInputStream(file.toFile())))) {
            N4JSResource resource = new N4JSResource();
            resource.setURI(new FileURI(file).toURI());
            DtsParseResult parseResult = new DtsParser().parse(buf, resource);
            if (parseResult.hasSyntaxErrors()) {
                fail++;
            } else {
                pass++;
            }
        } catch (Throwable e) {
            e.printStackTrace();
            if (e instanceof Error) {
                throw e;
            }
            error++;
        }
    }
    System.out.println("Done processing " + filesCount + " files in " + sw.elapsed(TimeUnit.SECONDS) + "s");
    System.out.println("  passed:  " + pass + " (" + percent(pass, filesCount) + ")");
    System.out.println("  failed:  " + fail + " (" + percent(fail, filesCount) + ")");
    System.out.println("  errors:  " + error + " (" + percent(error, filesCount) + ")");
    if (error > maxError) {
        Assert.fail("More errors detected than expected: " + error);
    }
    if (fail > maxFail) {
        Assert.fail("More failures detected than expected: " + fail);
    }
    if (pass < minPass) {
        Assert.fail("Less passes detected than expected: " + pass);
    }
}
Also used : Path(java.nio.file.Path) DtsParseResult(org.eclipse.n4js.dts.DtsParseResult) FileURI(org.eclipse.n4js.workspace.locations.FileURI) Files(java.nio.file.Files) Stopwatch(com.google.common.base.Stopwatch) GitUtils.hardReset(org.eclipse.n4js.tests.helper.git.GitUtils.hardReset) IOException(java.io.IOException) Test(org.junit.Test) FileInputStream(java.io.FileInputStream) InputStreamReader(java.io.InputStreamReader) Collectors(java.util.stream.Collectors) DtsParser(org.eclipse.n4js.dts.DtsParser) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) N4JSResource(org.eclipse.n4js.resource.N4JSResource) StandardSystemProperty(com.google.common.base.StandardSystemProperty) FileVisitOption(java.nio.file.FileVisitOption) BufferedReader(java.io.BufferedReader) Assert(org.junit.Assert) Path(java.nio.file.Path) Collections(java.util.Collections) InputStreamReader(java.io.InputStreamReader) DtsParseResult(org.eclipse.n4js.dts.DtsParseResult) Stopwatch(com.google.common.base.Stopwatch) FileInputStream(java.io.FileInputStream) DtsParser(org.eclipse.n4js.dts.DtsParser) FileURI(org.eclipse.n4js.workspace.locations.FileURI) BufferedReader(java.io.BufferedReader) N4JSResource(org.eclipse.n4js.resource.N4JSResource)

Aggregations

InputStreamReader (java.io.InputStreamReader)2 DtsParser (org.eclipse.n4js.dts.DtsParser)2 StandardSystemProperty (com.google.common.base.StandardSystemProperty)1 Stopwatch (com.google.common.base.Stopwatch)1 BufferedReader (java.io.BufferedReader)1 FileInputStream (java.io.FileInputStream)1 IOException (java.io.IOException)1 Reader (java.io.Reader)1 FileVisitOption (java.nio.file.FileVisitOption)1 Files (java.nio.file.Files)1 Path (java.nio.file.Path)1 Collections (java.util.Collections)1 List (java.util.List)1 TimeUnit (java.util.concurrent.TimeUnit)1 Collectors (java.util.stream.Collectors)1 DtsParseResult (org.eclipse.n4js.dts.DtsParseResult)1 N4JSResource (org.eclipse.n4js.resource.N4JSResource)1 GitUtils.hardReset (org.eclipse.n4js.tests.helper.git.GitUtils.hardReset)1 ResourceType (org.eclipse.n4js.utils.ResourceType)1 FileURI (org.eclipse.n4js.workspace.locations.FileURI)1