Skip to main content
Version: 1.0.1

How to Parse CL Code

This is the most basic of examples: it shows how to parse some CL code into a CLCompilationUnit.

Add Dependencies​

repositories {
mavenLocal()
mavenCentral()
flatDir {
dirs("deps")
}
}

dependencies {
implementation(files("deps/clparser-with-dependencies-1.0.1-all.jar"))
}

Parse a file​

Create a CLKolasuParser, register your license, and call parse. The root of a successful parse is a CLCompilationUnit. Pass parseComments = true when you need block comments (/* … */) attached to the compilation unit.

import com.strumenta.cl.model.CLCompilationUnit
import com.strumenta.cl.model.DeclareVariableCommand
import com.strumenta.cl.model.Procedure
import com.strumenta.cl.parser.CLKolasuParser
import com.strumenta.kolasu.commercial.LicenseManager
import com.strumenta.kolasu.parsing.ParsingResult
import com.strumenta.kolasu.traversing.walkDescendants
import java.io.File

fun processCLFile(clFile: File, license: File) {
println("== Processing CL file ${clFile.path} ==\n")
if (!clFile.exists() || !clFile.isFile) {
println("File not found or not a file, skipping")
return
}

LicenseManager.registerLicense(license)
val parser = CLKolasuParser(parseComments = true)
val result: ParsingResult<CLCompilationUnit> = parser.parse(clFile)

if (result.issues.isNotEmpty()) {
println("Issues in file: ${result.issues.size}")
result.issues.forEach { println(" - $it") }
}

val root = result.root
if (root == null) {
println("Compilation unit not built, skipping")
return
}

println("Procedures (PGM…ENDPGM): ${root.procedures.size}")
println("Top-level commands: ${root.commands.size}")
println("Top-level subroutines: ${root.subroutines.size}")
if (root.comments.isNotEmpty()) {
println("Comments collected: ${root.comments.size}")
}

val dclCount = root.walkDescendants(DeclareVariableCommand::class).count()
println("DCL variables: $dclCount")
}

You can use the method we just created very easily.

fun main() {
processCLFile(
File("examples/arithmetic.cl"),
File("licenses/strumenta.license"),
)
}

Supported extensions include .cl, .clp, and .clle. The same parse overloads accept a String, a File, or an InputStream with an explicit source.