-
Notifications
You must be signed in to change notification settings - Fork 125
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Build preliminary CQL parsing library targeting JS (#1434)
* Try out jsRun * Compile Kotlin to JS with type definitions. Export parseToTree method.
- Loading branch information
Showing
4 changed files
with
147 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import org.antlr.v4.kotlinruntime.CharStream | ||
import org.antlr.v4.kotlinruntime.CharStreams | ||
import org.antlr.v4.kotlinruntime.CommonTokenStream | ||
import org.cqframework.cql.gen.cqlLexer | ||
import org.cqframework.cql.gen.cqlParser | ||
|
||
@ExperimentalJsExport | ||
@JsExport | ||
fun parseToTree(logic: String): String { | ||
val input: CharStream = CharStreams.fromString(logic) | ||
val tokens = CommonTokenStream(cqlLexer(input)) | ||
val parser = cqlParser(tokens) | ||
parser.buildParseTree = true | ||
val library = parser.library() | ||
|
||
// Inspect the library object in the console. | ||
// Methods like toStringTree are mangled because they have multiple overloads. | ||
// TODO: Remove in the future. | ||
console.log("Library:", library) | ||
|
||
return library.toStringTree(parser) | ||
} | ||
|
||
// This method is not exported so it won't be available in the JS library. | ||
// TODO: Remove in the future. | ||
fun someMethod() { | ||
console.log("I am not exported") | ||
} | ||
|
||
// This `main` method always just runs. | ||
// TODO: Remove in the future. | ||
fun main() { | ||
val logic = "define inIPP : AgeAt(start of MeasurementPeriod) < 18" | ||
val input: CharStream = CharStreams.fromString(logic) | ||
val tokens = CommonTokenStream(cqlLexer(input)) | ||
val parser = cqlParser(tokens) | ||
parser.buildParseTree = true | ||
val library = parser.library() | ||
|
||
val def = library.statement(0)?.expressionDefinition() | ||
console.log(def?.identifier()?.IDENTIFIER().toString()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<title>JS Client Demo</title> | ||
</head> | ||
<body> | ||
<script src="cql.js"></script> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters