简体   繁体   中英

How to create an Rascal AST from file

I've found the documentation on the Rascal site: http://tutor.rascal-mpl.org/Rascal/Libraries/lang/java/m3/AST/createAstFromFile/createAstFromFile.html But I honestly have no idea how to apply this. Can someone give me an example of how to apply this in code? I've tried some things, but either get missing modifier "java" or undeclared variables errors, which don't help me at all.

I'm currently stuck at:

ast = createAstFromFile(l,true,"1.8");

Two additional questions: what does "Declaration" add here? And what does the collectBindings boolean mean in this context? (the documentation is not that clear related to this part of Rascal)

Additional information:

rascal>import lang::java::jdt::m3::Core;
ok
rascal>createM3FromEclipseFile(|project://smallsql/src/smallsql/tools/CommandLine.java|);
|plugin://rascal_eclipse/src/org/rascalmpl/eclipse/library/lang/java/jdt/Project.rsc|(276,127,<11,0>,<12,47>): IO("**smallsql does not exist**")
        at somewhere plugin://rascal_eclipse/src/org/rascalmpl/eclipse/library/lang/java/jdt/Project.rsc|(276,127,<11,0>,<12,47>))
        at classPathForProject(|plugin://rascal_eclipse/src/org/rascalmpl/eclipse/library/lang/java/jdt/m3/Core.rsc|(1776,2,<54,55>,<54,57>))
        at $root$(|prompt:///|(0,82,<1,0>,<1,82>))


rascal>projects();
set[loc]: {
  |project://smallsql|,
  |project://MetricsTest|,
  |project://Analysis|,
  |project://hsqldb|
}

I've had similar luck, but found the following workaround:

import lang::java::jdt::m3::AST;
createAstsFromEclipseProject(|project://myProject|,true)

This will give you all the asts for the project and you can select the one you need from that.

The issue is around compiler parameters (classpath etc). When given a project, the function can derive meta information from the project configuration which makes it work correctly.

  • collectBindings means the files are not just parsed but also name binding and type binding is executed. This is useful but it takes more time.
  • Declaration is just the general type of things that declare something.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM