Skip to content

Commit

Permalink
Merge pull request #4 from adriantodt/feature/v3
Browse files Browse the repository at this point in the history
Major version v3.0
  • Loading branch information
AdrianTodt authored Nov 3, 2021
2 parents d034a81 + 2f6e86e commit 90d415a
Show file tree
Hide file tree
Showing 47 changed files with 543 additions and 373 deletions.
7 changes: 4 additions & 3 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ jobs:
strategy:
matrix:
# Use these Java versions
java: [16]
java: [17]
# and run on Linux, Windows and MacOS
os: [ubuntu-20.04, windows-latest, macos-11]
os: [ubuntu-20.04, windows-latest, macos-latest]
runs-on: ${{ matrix.os }}
steps:
- name: checkout repository
Expand All @@ -32,8 +32,9 @@ jobs:
- name: validate gradle wrapper
uses: gradle/wrapper-validation-action@v1
- name: setup jdk ${{ matrix.java }}
uses: actions/setup-java@v1
uses: actions/setup-java@v2.3.1
with:
distribution: 'zulu'
java-version: ${{ matrix.java }}
- name: make gradle wrapper executable
if: ${{ runner.os != 'Windows' }}
Expand Down
9 changes: 5 additions & 4 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,19 @@ jobs:
strategy:
matrix:
# Use these Java versions
java: [16]
java: [17]
# and run on Linux, Windows and MacOS
os: [ubuntu-20.04, windows-latest, macos-11]
os: [ubuntu-20.04, windows-latest, macos-latest]
runs-on: ${{ matrix.os }}
steps:
- name: checkout repository
uses: actions/checkout@v2
- name: validate gradle wrapper
uses: gradle/wrapper-validation-action@v1
- name: setup jdk ${{ matrix.java }}
uses: actions/setup-java@v1
uses: actions/setup-java@v2.3.1
with:
distribution: 'zulu'
java-version: ${{ matrix.java }}
- name: make gradle wrapper executable
if: ${{ runner.os != 'Windows' }}
Expand All @@ -42,7 +43,7 @@ jobs:
MCD_PASSWORD: ${{ secrets.MCD_PASSWORD }}
- name: Publish to GitHub Packages (macOS)
if: ${{ runner.os == 'macOS' }}
run: gradle publishMacosX64PublicationToMavenRepository
run: gradle publishMacosX64PublicationToMavenRepository publishMacosArm64PublicationToMavenRepository
env:
MCD_USERNAME: ${{ secrets.MCD_USERNAME }}
MCD_PASSWORD: ${{ secrets.MCD_PASSWORD }}
17 changes: 9 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ To create a parser use `createParser(grammar) { ... }`, which will run the block

```kotlin
// Create a lexer as simple as a method call.
val lexer: Lexer<Token<TokenType>> = createLexer {
val lexer = Lexer.create<Token<TokenType>> {
// Implement a token type per line.
'+' { process(makeToken(PLUS)) }
// Built-in extension functions.
Expand All @@ -67,24 +67,25 @@ val lexer: Lexer<Token<TokenType>> = createLexer {
}

// Create a pratt-parser grammar. Dead simple.
val grammar: Grammar<TokenType, String> = createGrammar {
val grammar = Grammar.create<TokenType, String> {
// Create a prefix parselet as a lambda function.
prefix(STRING) { token -> token.value.removeSurrounding("") }
// Create a infix parselet, with support to precedence as a lambda function.
infix(PLUS, 1) { left, token -> left + parseExpression() }
prefix(STRING) { token -> token.value }
// Create an infix parselet, with support to precedence as a lambda function.
infix(PLUS, 1) { left, _ -> left + parseExpression() }
}

// Use your grammar to create a pratt-parser.
val parser = createParser(grammar) { // Actual code run by the parser.
val parser = SourceParser.create(lexer, grammar) { // Actual code run by the parser.
// Extension function: Throws if there's still tokens.
ensureEOF {
ensureEOF {
// Parses a expression using this parsers' grammar.
parseExpression()
}
}

// One line of code to rule them all.
val result = parser.parse(classpathSource { "input.str" }, lexer)
val result = parser.parse(Source.classpath { "input.str" })
println(result)
```

See the full code [here](https://github.com/adriantodt/tartar/blob/master/src/test/java/examples/StringJoiner.kt),
Expand Down
27 changes: 18 additions & 9 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
plugins {
kotlin("multiplatform") version "1.5.30"
kotlin("multiplatform") version "1.5.31"
`maven-publish`
id("org.jetbrains.dokka") version "1.5.0"
id("org.jetbrains.dokka") version "1.5.31"
}

group = "com.github.adriantodt"
version = "2.3"
version = "3.0"

repositories {
mavenCentral()
}

kotlin {
explicitApi()

jvm {
compilations.all {
kotlinOptions.jvmTarget = "13"
Expand All @@ -24,10 +26,11 @@ kotlin {
browser()
nodejs()
}

linuxX64("linuxX64")
macosX64("macosX64")
mingwX64("mingwX64")
linuxX64()
linuxArm64()
macosX64()
macosArm64()
mingwX64()

sourceSets {
val commonMain by getting
Expand All @@ -51,28 +54,34 @@ kotlin {
val linuxX64Main by getting {
dependsOn(nativeMain)
}
val linuxArm64Main by getting {
dependsOn(nativeMain)
}
val mingwX64Main by getting {
dependsOn(nativeMain)
}
val macosX64Main by getting {
dependsOn(nativeMain)
}
val macosArm64Main by getting {
dependsOn(nativeMain)
}
}
}


tasks {
register<Jar>("dokkaJar") {
from(dokkaHtml)
dependsOn(dokkaHtml)
archiveClassifier.set("javadoc")
}
}

publishing {
publications.withType<MavenPublication> {
artifact(tasks["dokkaJar"])
}
// select the repositories you want to publish to

repositories {
maven {
url = uri("https://maven.cafeteria.dev/releases")
Expand Down
53 changes: 0 additions & 53 deletions src/commonMain/kotlin/com/github/adriantodt/tartar/Tartar.kt

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.github.adriantodt.tartar.api.dsl

/**
* Represents a predicate (boolean-valued function) of one [Char]-valued argument.
*
* @since 3.0
*/
public fun interface CharPredicate {
/**
* Evaluates this predicate on the given argument.
*
* @param value the input argument
* @return true if the input argument matches the predicate, otherwise false.
*/
public fun test(value: Char): Boolean

public companion object {
/**
* [CharPredicate] which predicate is the function [Char.isLetter].
*/
public val isLetter: CharPredicate = CharPredicate(Char::isLetter)

/**
* [CharPredicate] which predicate is the function [Char.isDigit].
*/
public val isDigit: CharPredicate = CharPredicate(Char::isDigit)

/**
* [CharPredicate] which predicate is the function [Char.isLetterOrDigit].
*/
public val isLetterOrDigit: CharPredicate = CharPredicate(Char::isLetterOrDigit)
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.github.adriantodt.tartar.api
package com.github.adriantodt.tartar.api.dsl

import com.github.adriantodt.tartar.api.parser.Grammar
import com.github.adriantodt.tartar.api.parser.InfixParser
import com.github.adriantodt.tartar.api.parser.PrefixParser
import com.github.adriantodt.tartar.api.grammar.Grammar
import com.github.adriantodt.tartar.api.grammar.InfixParselet
import com.github.adriantodt.tartar.api.grammar.PrefixParselet

/**
* A builder of [Grammars][com.github.adriantodt.tartar.api.parser.Grammar], as a domain-specific language (DSL).
Expand All @@ -11,44 +11,44 @@ import com.github.adriantodt.tartar.api.parser.PrefixParser
* @param E The grammar's expression result.
* @author AdrianTodt
*/
interface GrammarDSL<T, E> {
public interface GrammarDSL<T, E> {
/**
* Imports all parselets from other grammars.
* @param override If set to true, imported parselets overrides existing ones. If false, they throw.
* @param grammars The grammars to import.
*/
fun import(override: Boolean = false, vararg grammars: Grammar<T, E>)
public fun import(override: Boolean = false, vararg grammars: Grammar<T, E>)

/**
* Registers a prefix parselets into the grammar.
* @param type The token type to associate the parselet with.
* @param parselet The prefix parselet to register.
* @param override If set to true, imported parselets overrides existing ones. If false, they throw.
*/
fun prefix(type: T, parselet: PrefixParser<T, E>, override: Boolean = false)
public fun prefix(type: T, parselet: PrefixParselet<T, E>, override: Boolean = false)

/**
* Registers a prefix parselets into the grammar.
* @param type The token type to associate the parselet with.
* @param override If set to true, imported parselets overrides existing ones. If false, they throw.
* @param block The code to execute when the type matches.
*/
fun prefix(type: T, override: Boolean = false, block: PrefixFunction<T, E>)
public fun prefix(type: T, override: Boolean = false, block: PrefixFunction<T, E>)

/**
* Registers a infix parselets into the grammar.
* @param type The token type to associate the parselet with.
* @param parselet The infix parselet to register.
* @param override If set to true, imported parselets overrides existing ones. If false, they throw.
*/
fun infix(type: T, parselet: InfixParser<T, E>, override: Boolean = false)
public fun infix(type: T, parselet: InfixParselet<T, E>, override: Boolean = false)

/**
* Registers a infix parselets into the grammar.
* @param type The token type to associate the parselet with.
* @param override If set to true, imported parselets overrides existing ones. If false, they throw.
* @param block The code to execute when the type matches.
*/
fun infix(type: T, precedence: Int, override: Boolean = false, block: InfixFunction<T, E>)
public fun infix(type: T, precedence: Int, override: Boolean = false, block: InfixFunction<T, E>)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.github.adriantodt.tartar.api.dsl

import com.github.adriantodt.tartar.api.parser.ParserContext
import com.github.adriantodt.tartar.api.parser.Token

/**
* Function used by [GrammarDSL] to configure a
* [Prefix Parselet][com.github.adriantodt.tartar.api.parser.PrefixParselet]
* in a functional way.
*/
public typealias PrefixFunction<T, E> = ParserContext<T, E>.(token: Token<T>) -> E

/**
* Function used by [GrammarDSL] to configure a
* [Infix Parselet][com.github.adriantodt.tartar.api.parser.InfixParselet]
* in a functional way.
*/
public typealias InfixFunction<T, E> = ParserContext<T, E>.(left: E, token: Token<T>) -> E

/**
* Function which receives a [LexerDSL] as its receiver.
*/
public typealias GrammarConfig<T, E> = GrammarDSL<T, E>.() -> Unit
Loading

0 comments on commit 90d415a

Please sign in to comment.