Code Generation
Add Dependencies
- Kotlin
- Java
repositories {
mavenLocal()
mavenCentral()
flatDir {
dirs("deps")
}
}
dependencies {
implementation(files("deps/sas-code-generator-with-dependencies-1.7.0-all.jar"))
}
repositories {
mavenLocal()
mavenCentral()
flatDir {
dirs("deps")
}
}
dependencies {
implementation(files("deps/sas-code-generator-with-dependencies-1.7.0-all.jar"))
implementation "com.strumenta.kolasu:kolasu-javalib:1.5.96"
}
Output code
The SAS parser has a module that allows to output code, i.e., to format an AST and create the corresponding text. This module is in an early stage of development. All you need to do is to pass an AST to the method printToString and you will the textual representation of the code. You might want to use it to format some code or to generate a code file from an AST that you created.
- Kotlin
- Java
import com.strumenta.kolasu.traversing.walk
import com.strumenta.sas.ast.SourceFile
import com.strumenta.kolasu.commercial.LicenseManager
import com.strumenta.sas.parser.SASLanguage
import java.io.File
import com.strumenta.sas.generator.SASPrinter
fun formatAst((sasFile: File, license: File)) {
LicenseManager.registerLicense(File("licenses/strumenta.SAS.license"))
val sas = SASLanguage()
sas.parseNativeSQL = true
sas.parseComments = true
val result = sas.parse(File("example.sas"))
val root = result.root
val formattedCode = sasPrinter.printToString(result.root!!)
}
import com.strumenta.kolasu.javalib.Traversing;
import com.strumenta.sas.ast.SourceFile;
import com.strumenta.kolasu.commercial.LicenseManager;
import com.strumenta.sas.parser.SASLanguage;
import com.strumenta.kolasu.model.Node;
import com.strumenta.kolasu.parsing.ParsingResult;
import java.io.File;
public class WalkAst {
public static void formatAst(File sasFile, File license) {
LicenseManager.INSTANCE.registerLicense(license);
SASLanguage sas = new SASLanguage();
sas.setParseNativeSQL(true);
sas.setParseComments(true);
ParsingResult<SourceFile> result = sas.parse(sasFile);
SourceFile root = result.getRoot();
if (root == null) {
return;
}
// create the printer
SASPrinter sasPrinter = new SASPrinter();
String formattedCode = sasPrinter.printToString(result.getRoot());
}
}