Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace ANTLR with hand-rolled parser #917

Open
wants to merge 51 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
51 commits
Select commit Hold shift + click to select a range
f37a904
Start new parser
stackoverflow Dec 18, 2024
724219a
`ParserComparisonTest` inherits from `ParserComparisonTestInterface` …
KushalP Jan 16, 2025
d1fc7b9
Start parser visitor
stackoverflow Jan 16, 2025
b1be0be
Spotless apply and rebase main
stackoverflow Jan 16, 2025
6f5520e
Assert that `compare` doesn't throw, and annotate the path with `as` …
KushalP Jan 16, 2025
05c147a
Collect all test failures using `SoftAssertions` (#4)
KushalP Jan 16, 2025
e1fc451
Fix bug in parser and lexer
stackoverflow Jan 17, 2025
0afd454
Fix bug in operator resolution
stackoverflow Jan 17, 2025
95234f8
Add support for legacy syntax
stackoverflow Jan 17, 2025
4e02b30
Change spans to be faster
stackoverflow Jan 17, 2025
761c41f
Add parent to CST
stackoverflow Jan 23, 2025
9277aed
Fix CST toString and equals
stackoverflow Jan 25, 2025
7b9bae0
finish AstBuilderNew
stackoverflow Jan 29, 2025
1deb01c
switch parsers
stackoverflow Jan 29, 2025
3c8fb93
rebase main
stackoverflow Jan 30, 2025
adfd54f
improve string parsing
stackoverflow Jan 31, 2025
ef6ccd6
check object elements are separated
stackoverflow Jan 31, 2025
fc8d945
fix more parsing errors
stackoverflow Feb 1, 2025
b5a4ab7
add children to nodes
stackoverflow Feb 2, 2025
7cd101e
add partial result to parser
stackoverflow Feb 3, 2025
53046de
fix doc comment parsing
stackoverflow Feb 3, 2025
645f160
remove ad-hoc error messages
stackoverflow Feb 4, 2025
4940d2d
fix more tests
stackoverflow Feb 4, 2025
de011d8
fix delimiter tests
stackoverflow Feb 4, 2025
710431d
fix snippet tests
stackoverflow Feb 4, 2025
ec8caca
fix tests
stackoverflow Feb 4, 2025
b9bd506
add message to test failure
stackoverflow Feb 4, 2025
33d3817
fix test for windows
stackoverflow Feb 4, 2025
e38daed
make repl and docs use the new parser
stackoverflow Feb 5, 2025
0f8ed9d
add AbstractParserVisitor and accept method
stackoverflow Feb 5, 2025
c27ba72
move ImportsAndReadsParser to the new parser
stackoverflow Feb 5, 2025
0063928
remove old parser
stackoverflow Feb 5, 2025
4bdc1ee
change keyword map to array
stackoverflow Feb 5, 2025
8b440d4
change class to record
stackoverflow Feb 5, 2025
f447540
remove truffle boundaries from parsing and lexing
stackoverflow Feb 5, 2025
5599b5c
make ANTLR a test dependency
stackoverflow Feb 5, 2025
47ebcd5
rename AstBuilder
stackoverflow Feb 5, 2025
401addb
fix build
stackoverflow Feb 5, 2025
22993ec
rename field
stackoverflow Feb 6, 2025
190a4cf
create BaseParserVisitor
stackoverflow Feb 6, 2025
0496d6e
rename ident to identifier
stackoverflow Feb 6, 2025
acb7c50
remove comment storing
stackoverflow Feb 6, 2025
8766078
fix some review comments
stackoverflow Feb 6, 2025
e448fb0
fix some review comments
stackoverflow Feb 6, 2025
1669f4b
fix some review comments
stackoverflow Feb 6, 2025
080eeb9
fix some review comments
stackoverflow Feb 6, 2025
0bb9924
improve BaseParserVisitor allocations by not calling `children()`
stackoverflow Feb 6, 2025
206915f
remove duplicated classes
stackoverflow Feb 6, 2025
250c21d
refactor cst
stackoverflow Feb 7, 2025
0e99558
add suffix to expressions
stackoverflow Feb 7, 2025
e164ff5
fix some review remarks
stackoverflow Feb 7, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
.gradle/
build/
generated/
testgenerated/

# IntelliJ
.idea/
Expand Down
4 changes: 1 addition & 3 deletions bench/bench.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright © 2024 Apple Inc. and the Pkl project authors. All rights reserved.
* Copyright © 2024-2025 Apple Inc. and the Pkl project authors. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -25,8 +25,6 @@ val graal: Configuration by configurations.creating
@Suppress("UnstableApiUsage")
dependencies {
jmh(projects.pklCore)
// necessary because antlr4-runtime is declared as implementation dependency in pkl-core.gradle
jmh(libs.antlrRuntime)
truffle(libs.truffleApi)
graal(libs.graalCompiler)
}
Expand Down
1 change: 0 additions & 1 deletion bench/gradle.lockfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# This is a Gradle generated file for dependency locking.
# Manual edits can break the build and are not advised.
# This file is expected to be part of source control.
com.tunnelvisionlabs:antlr4-runtime:4.9.0=jmh,jmhCompileClasspath,jmhImplementationDependenciesMetadata,jmhRuntimeClasspath
net.bytebuddy:byte-buddy:1.15.11=testCompileClasspath,testImplementationDependenciesMetadata,testRuntimeClasspath
net.sf.jopt-simple:jopt-simple:5.0.4=jmh,jmhCompileClasspath,jmhImplementationDependenciesMetadata,jmhRuntimeClasspath
org.apache.commons:commons-math3:3.6.1=jmh,jmhCompileClasspath,jmhImplementationDependenciesMetadata,jmhRuntimeClasspath
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright © 2024 Apple Inc. and the Pkl project authors. All rights reserved.
* Copyright © 2024-2025 Apple Inc. and the Pkl project authors. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
1 change: 0 additions & 1 deletion buildSrc/src/main/kotlin/pklFatJar.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ val firstPartySourcesJarsConfiguration: Configuration =
val relocations =
mapOf(
// pkl-core dependencies
"org.antlr.v4." to "org.pkl.thirdparty.antlr.v4.",
"org.organicdesign.fp." to "org.pkl.thirdparty.paguro.",
"org.snakeyaml.engine." to "org.pkl.thirdparty.snakeyaml.engine.",
"org.msgpack." to "org.pkl.thirdparty.msgpack.",
Expand Down
15 changes: 7 additions & 8 deletions docs/src/test/kotlin/DocSnippetTests.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,14 @@ import org.pkl.core.Loggers
import org.pkl.core.SecurityManagers
import org.pkl.core.StackFrameTransformers
import org.pkl.core.module.ModuleKeyFactories
import org.pkl.core.parser.LexParseException
import org.pkl.core.parser.Parser
import org.pkl.core.parser.antlr.PklParser
import org.pkl.core.repl.ReplRequest
import org.pkl.core.repl.ReplResponse
import org.pkl.core.repl.ReplServer
import org.pkl.core.util.IoUtils
import org.antlr.v4.runtime.ParserRuleContext
import org.pkl.core.http.HttpClient
import org.pkl.core.parser.Parser
import org.pkl.core.parser.ParserError
import org.pkl.core.parser.cst.ClassProperty
import org.pkl.core.resource.ResourceReaders
import java.nio.file.Files
import kotlin.io.path.isDirectory
Expand Down Expand Up @@ -303,7 +302,7 @@ class DocSnippetTestsEngine : HierarchicalTestEngine<DocSnippetTestsEngine.Execu

override fun getType() = Type.TEST

private val parsed: ParserRuleContext by lazy {
private val parsed: org.pkl.core.parser.cst.Node by lazy {
when (language) {
"pkl" -> Parser().parseModule(code)
"pkl-expr" -> Parser().parseExpressionInput(code)
Expand All @@ -318,7 +317,7 @@ class DocSnippetTestsEngine : HierarchicalTestEngine<DocSnippetTestsEngine.Execu
if (expectError) {
throw AssertionError("Expected a parse error, but got none.")
}
} catch (e: LexParseException) {
} catch (e: ParserError) {
if (!expectError) {
throw AssertionError(e.message)
}
Expand All @@ -335,7 +334,7 @@ class DocSnippetTestsEngine : HierarchicalTestEngine<DocSnippetTestsEngine.Execu
)
)

val properties = parsed.children.filterIsInstance<PklParser.ClassPropertyContext>()
val properties = parsed.children()?.filterIsInstance<ClassProperty>() ?: emptyList()

val responses = mutableListOf<ReplResponse>()

Expand All @@ -344,7 +343,7 @@ class DocSnippetTestsEngine : HierarchicalTestEngine<DocSnippetTestsEngine.Execu
responses.addAll(context.replServer.handleRequest(
ReplRequest.Eval(
"snippet",
prop.Identifier().text,
prop.name.value,
false,
true
)
Expand Down
1 change: 0 additions & 1 deletion pkl-cli/gradle.lockfile
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ com.google.guava:listenablefuture:9999.0-empty-to-avoid-conflict-with-guava=test
com.google.j2objc:j2objc-annotations:3.0.0=testCompileClasspath,testImplementationDependenciesMetadata,testRuntimeClasspath
com.jayway.jsonpath:json-path:2.9.0=testCompileClasspath,testImplementationDependenciesMetadata,testRuntimeClasspath
com.networknt:json-schema-validator:1.5.4=testCompileClasspath,testImplementationDependenciesMetadata,testRuntimeClasspath
com.tunnelvisionlabs:antlr4-runtime:4.9.0=runtimeClasspath,testRuntimeClasspath
commons-fileupload:commons-fileupload:1.5=testCompileClasspath,testImplementationDependenciesMetadata,testRuntimeClasspath
commons-io:commons-io:2.11.0=testCompileClasspath,testImplementationDependenciesMetadata,testRuntimeClasspath
net.bytebuddy:byte-buddy:1.15.11=testCompileClasspath,testImplementationDependenciesMetadata,testRuntimeClasspath
Expand Down
3 changes: 0 additions & 3 deletions pkl-cli/pkl-cli.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,6 @@ tasks.shadowJar {
exclude("META-INF/maven/**")
exclude("META-INF/upgrade/**")

// org.antlr.v4.runtime.misc.RuleDependencyProcessor
exclude("META-INF/services/javax.annotation.processing.Processor")

exclude("module-info.*")
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright © 2024 Apple Inc. and the Pkl project authors. All rights reserved.
* Copyright © 2024-2025 Apple Inc. and the Pkl project authors. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -21,15 +21,15 @@
import com.oracle.svm.core.annotate.TargetClass;
import com.oracle.svm.truffle.TruffleFeature;
import java.util.Map;
import org.pkl.core.ast.builder.AstBuilder;

/**
* Workaround to prevent the native-image build error "Detected a started Thread in the image
* heap.". The cause of this error is the use of {@link org.graalvm.polyglot.Context} in the
* (intentionally) statically reachable class {@link org.pkl.core.runtime.StdLibModule}.
*
* <p>A cleaner solution would be to have a separate {@link org.pkl.core.ast.builder.AstBuilder} for
* stdlib modules that produces a fully initialized module object without executing any Truffle
* nodes.
* <p>A cleaner solution would be to have a separate {@link AstBuilder} for stdlib modules that
* produces a fully initialized module object without executing any Truffle nodes.
*
* <p>This class is automatically discovered by native-image; no registration is required.
*/
Expand Down
1 change: 0 additions & 1 deletion pkl-codegen-java/gradle.lockfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
com.github.ajalt.clikt:clikt-jvm:3.5.4=compileClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
com.github.ajalt.clikt:clikt:3.5.4=apiDependenciesMetadata,compileClasspath,implementationDependenciesMetadata,runtimeClasspath,testCompileClasspath,testImplementationDependenciesMetadata,testRuntimeClasspath
com.palantir.javapoet:javapoet:0.6.0=compileClasspath,implementationDependenciesMetadata,runtimeClasspath,testCompileClasspath,testImplementationDependenciesMetadata,testRuntimeClasspath
com.tunnelvisionlabs:antlr4-runtime:4.9.0=runtimeClasspath,testRuntimeClasspath
io.leangen.geantyref:geantyref:1.3.16=testRuntimeClasspath
net.bytebuddy:byte-buddy:1.15.11=testCompileClasspath,testImplementationDependenciesMetadata,testRuntimeClasspath
org.apiguardian:apiguardian-api:1.1.2=testCompileClasspath,testImplementationDependenciesMetadata,testRuntimeOnlyDependenciesMetadata
Expand Down
1 change: 0 additions & 1 deletion pkl-codegen-kotlin/gradle.lockfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
com.github.ajalt.clikt:clikt-jvm:3.5.4=compileClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
com.github.ajalt.clikt:clikt:3.5.4=apiDependenciesMetadata,compileClasspath,implementationDependenciesMetadata,runtimeClasspath,testCompileClasspath,testImplementationDependenciesMetadata,testRuntimeClasspath
com.squareup:kotlinpoet:1.6.0=compileClasspath,implementationDependenciesMetadata,runtimeClasspath,testCompileClasspath,testImplementationDependenciesMetadata,testRuntimeClasspath
com.tunnelvisionlabs:antlr4-runtime:4.9.0=runtimeClasspath,testRuntimeClasspath
io.leangen.geantyref:geantyref:1.3.16=testRuntimeClasspath
net.bytebuddy:byte-buddy:1.15.11=testCompileClasspath,testImplementationDependenciesMetadata,testRuntimeClasspath
org.apiguardian:apiguardian-api:1.1.2=testCompileClasspath,testImplementationDependenciesMetadata,testRuntimeOnlyDependenciesMetadata
Expand Down
1 change: 0 additions & 1 deletion pkl-commons-cli/gradle.lockfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
# This file is expected to be part of source control.
com.github.ajalt.clikt:clikt-jvm:3.5.4=compileClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
com.github.ajalt.clikt:clikt:3.5.4=apiDependenciesMetadata,compileClasspath,implementationDependenciesMetadata,runtimeClasspath,testCompileClasspath,testImplementationDependenciesMetadata,testRuntimeClasspath
com.tunnelvisionlabs:antlr4-runtime:4.9.0=runtimeClasspath,testRuntimeClasspath
net.bytebuddy:byte-buddy:1.15.11=testCompileClasspath,testImplementationDependenciesMetadata,testRuntimeClasspath
org.apiguardian:apiguardian-api:1.1.2=testCompileClasspath,testImplementationDependenciesMetadata,testRuntimeOnlyDependenciesMetadata
org.assertj:assertj-core:3.27.2=testCompileClasspath,testImplementationDependenciesMetadata,testRuntimeClasspath
Expand Down
1 change: 0 additions & 1 deletion pkl-config-java/gradle.lockfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
com.github.ajalt.clikt:clikt-jvm:3.5.4=pklCodegenJava
com.github.ajalt.clikt:clikt:3.5.4=pklCodegenJava
com.palantir.javapoet:javapoet:0.6.0=pklCodegenJava
com.tunnelvisionlabs:antlr4-runtime:4.9.0=pklCodegenJava,runtimeClasspath,testRuntimeClasspath
io.leangen.geantyref:geantyref:1.3.16=compileClasspath,implementationDependenciesMetadata,runtimeClasspath,testCompileClasspath,testImplementationDependenciesMetadata,testRuntimeClasspath
javax.inject:javax.inject:1=testCompileClasspath,testImplementationDependenciesMetadata,testRuntimeClasspath
net.bytebuddy:byte-buddy:1.15.11=testCompileClasspath,testImplementationDependenciesMetadata,testRuntimeClasspath
Expand Down
1 change: 0 additions & 1 deletion pkl-config-kotlin/gradle.lockfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
com.github.ajalt.clikt:clikt-jvm:3.5.4=pklCodegenKotlin
com.github.ajalt.clikt:clikt:3.5.4=pklCodegenKotlin
com.squareup:kotlinpoet:1.6.0=pklCodegenKotlin
com.tunnelvisionlabs:antlr4-runtime:4.9.0=pklCodegenKotlin,pklConfigJava,runtimeClasspath,testRuntimeClasspath
io.leangen.geantyref:geantyref:1.3.16=pklConfigJava,runtimeClasspath,testCompileClasspath,testImplementationDependenciesMetadata,testRuntimeClasspath
net.bytebuddy:byte-buddy:1.15.11=testCompileClasspath,testImplementationDependenciesMetadata,testRuntimeClasspath
org.apiguardian:apiguardian-api:1.1.2=testCompileClasspath,testImplementationDependenciesMetadata,testRuntimeOnlyDependenciesMetadata
Expand Down
2 changes: 1 addition & 1 deletion pkl-core/gradle.lockfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
com.google.code.findbugs:jsr305:3.0.2=compileClasspath,compileOnlyDependenciesMetadata
com.palantir.javapoet:javapoet:0.6.0=generatorCompileClasspath,generatorImplementationDependenciesMetadata,generatorRuntimeClasspath
com.tunnelvisionlabs:antlr4-annotations:4.9.0=antlr
com.tunnelvisionlabs:antlr4-runtime:4.9.0=antlr,compileClasspath,implementationDependenciesMetadata,runtimeClasspath,testCompileClasspath,testImplementationDependenciesMetadata,testRuntimeClasspath
com.tunnelvisionlabs:antlr4-runtime:4.9.0=antlr,testCompileClasspath,testImplementationDependenciesMetadata,testRuntimeClasspath
com.tunnelvisionlabs:antlr4:4.9.0=antlr
net.bytebuddy:byte-buddy:1.15.11=testCompileClasspath,testImplementationDependenciesMetadata,testRuntimeClasspath
org.abego.treelayout:org.abego.treelayout.core:1.0.1=antlr
Expand Down
21 changes: 12 additions & 9 deletions pkl-core/pkl-core.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright © 2024 Apple Inc. and the Pkl project authors. All rights reserved.
* Copyright © 2024-2025 Apple Inc. and the Pkl project authors. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -27,15 +27,16 @@ plugins {

val generatorSourceSet = sourceSets.register("generator")

sourceSets { main { java { srcDir(file("generated/antlr")) } } }
sourceSets { test { java { srcDir(file("testgenerated/antlr")) } } }

idea {
module {
// mark src/main/antlr as source dir
// mark generated/antlr as generated source dir
// mark generated/truffle as generated source dir
sourceDirs = sourceDirs + files("src/main/antlr", "generated/antlr", "generated/truffle")
generatedSourceDirs = generatedSourceDirs + files("generated/antlr", "generated/truffle")
sourceDirs = sourceDirs + files("generated/truffle")
generatedSourceDirs = generatedSourceDirs + files("testgenerated/antlr", "generated/truffle")
testSources.from(files("src/test/antlr", "testgenerated/antlr"))
}
}

Expand All @@ -56,7 +57,6 @@ dependencies {
// pkl-core implements pkl-executor's ExecutorSpi, but the SPI doesn't ship with pkl-core
compileOnly(projects.pklExecutor)

implementation(libs.antlrRuntime)
implementation(libs.msgpack)
implementation(libs.truffleApi)
implementation(libs.graalSdk)
Expand All @@ -65,6 +65,7 @@ dependencies {

implementation(libs.snakeYaml)

testImplementation(libs.antlrRuntime)
testImplementation(projects.pklCommonsTest)

add("generatorImplementation", libs.javaPoet)
Expand Down Expand Up @@ -98,12 +99,14 @@ tasks.generateGrammarSource {
// generate only visitor
arguments = arguments + listOf("-visitor", "-no-listener")

source = fileTree("src/test/antlr") { include("*.g4") }

// Due to https://github.com/antlr/antlr4/issues/2260,
// we can't put .g4 files into src/main/antlr/org/pkl/core/parser/antlr.
// Instead, we put .g4 files into src/main/antlr, adapt output dir below,
// and use @header directives in .g4 files (instead of setting `-package` argument here)
// and task makeIntelliJAntlrPluginHappy to fix up the IDE story.
outputDirectory = file("generated/antlr/org/pkl/core/parser/antlr")
outputDirectory = file("testgenerated/antlr/org/pkl/core/parser/antlr")
}

tasks.compileJava { dependsOn(tasks.generateGrammarSource) }
Expand All @@ -119,8 +122,8 @@ tasks.named("generateGeneratorGrammarSource") { enabled = false }
val makeIntelliJAntlrPluginHappy by
tasks.registering(Copy::class) {
dependsOn(tasks.generateGrammarSource)
into("src/main/antlr")
from("generated/antlr/org/pkl/core/parser/antlr") { include("PklLexer.tokens") }
into("test/antlr")
from("testgenerated/antlr/org/pkl/core/parser/antlr") { include("PklLexer.tokens") }
}

tasks.processResources {
Expand All @@ -141,7 +144,7 @@ tasks.processResources {
mapOf(
"version" to buildInfo.pklVersion,
"commitId" to buildInfo.commitId,
"stdlibModules" to stdlibModules.joinToString(",")
"stdlibModules" to stdlibModules.joinToString(","),
)
)
}
Expand Down
2 changes: 1 addition & 1 deletion pkl-core/src/main/java/org/pkl/core/PcfRenderer.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright © 2024 Apple Inc. and the Pkl project authors. All rights reserved.
* Copyright © 2024-2025 Apple Inc. and the Pkl project authors. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
Loading