├── app ├── .gitignore ├── src │ └── main │ │ ├── res │ │ ├── mipmap-hdpi │ │ │ ├── ic_launcher.png │ │ │ └── ic_launcher_round.png │ │ ├── mipmap-mdpi │ │ │ ├── ic_launcher.png │ │ │ └── ic_launcher_round.png │ │ ├── mipmap-xhdpi │ │ │ ├── ic_launcher.png │ │ │ └── ic_launcher_round.png │ │ ├── mipmap-xxhdpi │ │ │ ├── ic_launcher.png │ │ │ └── ic_launcher_round.png │ │ ├── mipmap-xxxhdpi │ │ │ ├── ic_launcher.png │ │ │ └── ic_launcher_round.png │ │ ├── values │ │ │ ├── strings.xml │ │ │ ├── colors.xml │ │ │ └── styles.xml │ │ ├── mipmap-anydpi-v26 │ │ │ ├── ic_launcher.xml │ │ │ └── ic_launcher_round.xml │ │ ├── layout │ │ │ └── activity_home.xml │ │ ├── drawable-v24 │ │ │ └── ic_launcher_foreground.xml │ │ └── drawable │ │ │ └── ic_launcher_background.xml │ │ ├── java │ │ └── com │ │ │ └── github │ │ │ └── alexandrepiveteau │ │ │ └── parsers │ │ │ └── sample │ │ │ └── HomeActivity.kt │ │ └── AndroidManifest.xml ├── proguard-rules.pro └── build.gradle ├── parser-combinators ├── .gitignore ├── src │ └── main │ │ └── java │ │ └── com │ │ └── github │ │ └── alexandrepiveteau │ │ └── parsers │ │ ├── ExperimentalParser.kt │ │ ├── Parser.factory.kt │ │ ├── Parser.kt │ │ └── Parser.combinators.kt └── build.gradle ├── parser-combinators-primitives ├── .gitignore ├── src │ └── main │ │ └── java │ │ └── com │ │ └── github │ │ └── alexandrepiveteau │ │ └── parsers │ │ └── primitives │ │ └── Char.factory.kt └── build.gradle ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── .gitignore ├── gradle.properties ├── LICENSE.md ├── settings.gradle ├── gradlew.bat ├── README.md └── gradlew /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /parser-combinators/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /parser-combinators-primitives/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/alexandrepiveteau/parser-combinators-kotlin/HEAD/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /app/src/main/res/mipmap-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/alexandrepiveteau/parser-combinators-kotlin/HEAD/app/src/main/res/mipmap-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/alexandrepiveteau/parser-combinators-kotlin/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/alexandrepiveteau/parser-combinators-kotlin/HEAD/app/src/main/res/mipmap-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/alexandrepiveteau/parser-combinators-kotlin/HEAD/app/src/main/res/mipmap-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/alexandrepiveteau/parser-combinators-kotlin/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-hdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/alexandrepiveteau/parser-combinators-kotlin/HEAD/app/src/main/res/mipmap-hdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-mdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/alexandrepiveteau/parser-combinators-kotlin/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/alexandrepiveteau/parser-combinators-kotlin/HEAD/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/alexandrepiveteau/parser-combinators-kotlin/HEAD/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/alexandrepiveteau/parser-combinators-kotlin/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /parser-combinators/src/main/java/com/github/alexandrepiveteau/parsers/ExperimentalParser.kt: -------------------------------------------------------------------------------- 1 | package com.github.alexandrepiveteau.parsers 2 | 3 | /** 4 | * An annotation indicating that a certain API for parser handling is still experimental. Experimental APIs that are 5 | * released are not guaranteed to be maintained over time, and therefore an explicit opt-in from the client will be 6 | * required when using the functionality. 7 | */ 8 | @MustBeDocumented 9 | @Retention(value = AnnotationRetention.BINARY) 10 | @Experimental(level = Experimental.Level.WARNING) 11 | annotation class ExperimentalParser -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # macOS-specific files 2 | .DS_Store 3 | 4 | # User-specific properties (keys, etc.) 5 | signing.properties 6 | 7 | # Built application files 8 | *.apk 9 | *.ap_ 10 | 11 | # Files for the Dalvik VM 12 | *.dex 13 | 14 | # Java class files 15 | *.class 16 | 17 | # Generated files 18 | bin/ 19 | gen/ 20 | out/ 21 | 22 | # Gradle files 23 | .gradle/ 24 | build/ 25 | 26 | # Local configuration file (sdk path, etc) 27 | local.properties 28 | 29 | # Proguard folder generated by Eclipse 30 | proguard/ 31 | 32 | # Log Files 33 | *.log 34 | 35 | # Android Studio Navigation editor temp files 36 | .navigation/ 37 | 38 | # Android Studio captures folder 39 | captures/ 40 | /captures 41 | 42 | # Intellij 43 | *.idea 44 | /.idea/workspace.xml 45 | /.idea/libraries 46 | *.iml 47 | -------------------------------------------------------------------------------- /parser-combinators/src/main/java/com/github/alexandrepiveteau/parsers/Parser.factory.kt: -------------------------------------------------------------------------------- 1 | package com.github.alexandrepiveteau.parsers 2 | 3 | /** 4 | * A convenience function for the [Parser.succeed] method, that lets you also specify the [value] that should be 5 | * produced by the created [Parser]. Internally, this simply maps a [Unit] [Parser.succeed] instance with the value you 6 | * provide. 7 | * 8 | * @param I The type of the input provided to the [Parser]. 9 | * @param O The type of the output provided by the [Parser]. 10 | * @param E The type of errors generated by the parser. 11 | * 12 | * @param value The value that should be offered by the [Parser] each time it is called. 13 | */ 14 | @ExperimentalParser 15 | fun Parser.Factory.succeedWith(value: O): Parser = 16 | Parser.succeed().map { value } -------------------------------------------------------------------------------- /app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # You can control the set of applied configuration files using the 3 | # proguardFiles setting in build.gradle. 4 | # 5 | # For more details, see 6 | # http://developer.android.com/guide/developing/tools/proguard.html 7 | 8 | # If your project uses WebView with JS, uncomment the following 9 | # and specify the fully qualified class name to the JavaScript interface 10 | # class: 11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 12 | # public *; 13 | #} 14 | 15 | # Uncomment this to preserve the line number information for 16 | # debugging stack traces. 17 | #-keepattributes SourceFile,LineNumberTable 18 | 19 | # If you keep the line number information, uncomment this to 20 | # hide the original source file name. 21 | #-renamesourcefileattribute SourceFile 22 | -------------------------------------------------------------------------------- /gradle.properties: -------------------------------------------------------------------------------- 1 | # Project-wide Gradle settings. 2 | # IDE (e.g. Android Studio) users: 3 | # Gradle settings configured through the IDE *will override* 4 | # any settings specified in this file. 5 | # For more details on how to configure your build environment visit 6 | # http://www.gradle.org/docs/current/userguide/build_environment.html 7 | # Specifies the JVM arguments used for the daemon process. 8 | # The setting is particularly useful for tweaking memory settings. 9 | org.gradle.jvmargs=-Xmx1536m 10 | # When configured, Gradle will run in incubating parallel mode. 11 | # This option should only be used with decoupled projects. More details, visit 12 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects 13 | # org.gradle.parallel=true 14 | 15 | 16 | # AndroidX support. 17 | android.enableJetifier=true 18 | android.useAndroidX=true -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 Alexandre Piveteau 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2018 Alexandre Piveteau 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | include ':app' 26 | include ':parser-combinators' 27 | include ':parser-combinators-primitives' 28 | -------------------------------------------------------------------------------- /app/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 24 | 25 | 26 | parser-combinators-kotlin 27 | 28 | -------------------------------------------------------------------------------- /parser-combinators-primitives/src/main/java/com/github/alexandrepiveteau/parsers/primitives/Char.factory.kt: -------------------------------------------------------------------------------- 1 | package com.github.alexandrepiveteau.parsers.primitives 2 | 3 | import com.github.alexandrepiveteau.functional.monads.Maybe 4 | import com.github.alexandrepiveteau.functional.monads.eitherError 5 | import com.github.alexandrepiveteau.functional.monads.eitherValue 6 | import com.github.alexandrepiveteau.functional.monads.toMaybe 7 | import com.github.alexandrepiveteau.parsers.ExperimentalParser 8 | import com.github.alexandrepiveteau.parsers.Parser 9 | 10 | /** 11 | * Returns a [Parser] that takes as input a [String] and returns the first [Char] if it corresponds to the character 12 | * that was provided as a parameter. 13 | * 14 | * @param E The type of the errors that will be generated by this [Parser]. 15 | * 16 | * @param c The character that should be at the beginning of the [String] for the [Parser] to succeed. 17 | * @param f The function that will generate an [E] if the [Char] was not found at the right place. Offers the [Char] 18 | * t that was founs instead. 19 | */ 20 | @ExperimentalParser 21 | fun Char.Companion.parserOfSome(c: Char, f: (Maybe) -> E): Parser = 22 | Parser { s -> 23 | when (val xs = s.firstOrNull()) { 24 | c -> eitherValue(c to s.drop(1)) 25 | else -> eitherError(f(xs.toMaybe())) 26 | } 27 | } -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | # 2 | # MIT License 3 | # 4 | # Copyright (c) 2018 Alexandre Piveteau 5 | # 6 | # Permission is hereby granted, free of charge, to any person obtaining a copy 7 | # of this software and associated documentation files (the "Software"), to deal 8 | # in the Software without restriction, including without limitation the rights 9 | # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | # copies of the Software, and to permit persons to whom the Software is 11 | # furnished to do so, subject to the following conditions: 12 | # 13 | # The above copyright notice and this permission notice shall be included in all 14 | # copies or substantial portions of the Software. 15 | # 16 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | # SOFTWARE. 23 | # 24 | 25 | #Fri Sep 07 23:16:49 CEST 2018 26 | distributionBase=GRADLE_USER_HOME 27 | distributionPath=wrapper/dists 28 | zipStoreBase=GRADLE_USER_HOME 29 | zipStorePath=wrapper/dists 30 | distributionUrl=https\://services.gradle.org/distributions/gradle-4.10-all.zip 31 | -------------------------------------------------------------------------------- /app/src/main/res/values/colors.xml: -------------------------------------------------------------------------------- 1 | 2 | 25 | 26 | 27 | #3F51B5 28 | #303F9F 29 | #FF4081 30 | 31 | -------------------------------------------------------------------------------- /app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml: -------------------------------------------------------------------------------- 1 | 2 | 25 | 26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml: -------------------------------------------------------------------------------- 1 | 2 | 25 | 26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /parser-combinators/build.gradle: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2018 Alexandre Piveteau 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | apply plugin: 'java-library' 26 | apply plugin: 'kotlin' 27 | apply plugin: 'com.github.dcendents.android-maven' 28 | 29 | group='com.github.alexandrepiveteau' 30 | 31 | dependencies { 32 | implementation "org.jetbrains.kotlin:kotlin-stdlib:1.3.0" 33 | api "com.github.alexandrepiveteau.functional-kotlin:functional-monads:0.3.0" 34 | } 35 | 36 | sourceCompatibility = "1.7" 37 | targetCompatibility = "1.7" 38 | -------------------------------------------------------------------------------- /app/src/main/java/com/github/alexandrepiveteau/parsers/sample/HomeActivity.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2018 Alexandre Piveteau 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | package com.github.alexandrepiveteau.parsers.sample 26 | 27 | import androidx.appcompat.app.AppCompatActivity 28 | import android.os.Bundle 29 | 30 | class HomeActivity : AppCompatActivity() { 31 | 32 | override fun onCreate(savedInstanceState: Bundle?) { 33 | super.onCreate(savedInstanceState) 34 | setContentView(R.layout.activity_home) 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /app/src/main/res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 24 | 25 | 26 | 27 | 28 | 34 | 35 | 36 | -------------------------------------------------------------------------------- /parser-combinators-primitives/build.gradle: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2018 Alexandre Piveteau 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | apply plugin: 'java-library' 26 | apply plugin: 'kotlin' 27 | apply plugin: 'com.github.dcendents.android-maven' 28 | 29 | group='com.github.alexandrepiveteau' 30 | 31 | dependencies { 32 | implementation "org.jetbrains.kotlin:kotlin-stdlib:1.3.0" 33 | api "com.github.alexandrepiveteau.functional-kotlin:functional-monads:0.3.0" 34 | 35 | api project(':parser-combinators') 36 | } 37 | 38 | sourceCompatibility = "1.7" 39 | targetCompatibility = "1.7" 40 | -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 25 | 26 | 28 | 29 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_home.xml: -------------------------------------------------------------------------------- 1 | 24 | 25 | 31 | 32 | 40 | 41 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2018 Alexandre Piveteau 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | apply plugin: 'com.android.application' 26 | apply plugin: 'kotlin-android' 27 | 28 | android { 29 | compileSdkVersion 28 30 | defaultConfig { 31 | applicationId "com.github.alexandrepiveteau.parsers.sample" 32 | minSdkVersion 15 33 | targetSdkVersion 28 34 | versionCode 1 35 | versionName "1.1.0" 36 | testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" 37 | } 38 | buildTypes { 39 | release { 40 | minifyEnabled false 41 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 42 | } 43 | } 44 | } 45 | 46 | dependencies { 47 | implementation fileTree(dir: 'libs', include: ['*.jar']) 48 | implementation "org.jetbrains.kotlin:kotlin-stdlib:1.3.0" 49 | implementation 'androidx.appcompat:appcompat:1.0.0' 50 | implementation 'androidx.constraintlayout:constraintlayout:2.0.0-alpha2' 51 | implementation project(':parser-combinators') 52 | } 53 | -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | set DIRNAME=%~dp0 12 | if "%DIRNAME%" == "" set DIRNAME=. 13 | set APP_BASE_NAME=%~n0 14 | set APP_HOME=%DIRNAME% 15 | 16 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 17 | set DEFAULT_JVM_OPTS= 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windows variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | 53 | :win9xME_args 54 | @rem Slurp the command line arguments. 55 | set CMD_LINE_ARGS= 56 | set _SKIP=2 57 | 58 | :win9xME_args_slurp 59 | if "x%~1" == "x" goto execute 60 | 61 | set CMD_LINE_ARGS=%* 62 | 63 | :execute 64 | @rem Setup the command line 65 | 66 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 67 | 68 | @rem Execute Gradle 69 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 70 | 71 | :end 72 | @rem End local scope for the variables with windows NT shell 73 | if "%ERRORLEVEL%"=="0" goto mainEnd 74 | 75 | :fail 76 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 77 | rem the _cmd.exe /c_ return code! 78 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 79 | exit /b 1 80 | 81 | :mainEnd 82 | if "%OS%"=="Windows_NT" endlocal 83 | 84 | :omega 85 | -------------------------------------------------------------------------------- /parser-combinators/src/main/java/com/github/alexandrepiveteau/parsers/Parser.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2018 Alexandre Piveteau 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | package com.github.alexandrepiveteau.parsers 26 | 27 | import com.github.alexandrepiveteau.functional.monads.* 28 | 29 | class Parser(private val f: (I) -> Either>) { 30 | 31 | fun parse(i: I): Either> = f(i) 32 | 33 | companion object Factory { 34 | 35 | /** 36 | * Returns a [Parser] that will chomp the first [Char] of a given [String]. If the [Char] does not correspond, 37 | * an error message will be generated instead. 38 | * 39 | * @param char The value of the character that will be consumed by this [Parser]. 40 | * @param f The function that generates an error message, with as parameter the [Char] that was not found. 41 | * 42 | * @param E The type of errors generated by the parser. 43 | * 44 | * This function will be removed in the next major library update. 45 | */ 46 | @Deprecated( 47 | message = "This function has been removed from the core Parser types, as it is type-specific.", 48 | level = DeprecationLevel.WARNING) 49 | fun char(char: Char, f: (Char) -> E): Parser = 50 | Parser { text -> 51 | return@Parser if (text.firstOrNull() == char) 52 | eitherValue>(char to text.drop(1)) 53 | else 54 | eitherError(f(char)) 55 | } 56 | 57 | fun fail(f: () -> E): Parser = Parser { eitherError(f()) } 58 | fun lazy(f: () -> Parser): Parser = Parser { input -> f().parse(input) } 59 | fun succeed(): Parser = Parser { eitherValue(Unit to it) } 60 | } 61 | } -------------------------------------------------------------------------------- /app/src/main/res/drawable-v24/ic_launcher_foreground.xml: -------------------------------------------------------------------------------- 1 | 24 | 25 | 31 | 36 | 37 | 43 | 46 | 49 | 50 | 51 | 52 | 58 | 59 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # parser-combinators 2 | 3 | [![](https://jitpack.io/v/alexandrepiveteau/parser-combinators-kotlin.svg)](https://jitpack.io/#alexandrepiveteau/parser-combinators-kotlin) 4 | 5 | This repository contains some utilies for parser combinators in the Kotlin programming language. 6 | The OSS license can be found in the LICENSE.md file of the repository. 7 | 8 | ## Installation 9 | This library is available on [JitPack.io](https://jitpack.io/#alexandrepiveteau/parser-combinators-kotlin). Make 10 | sure to add the following Maven repository in your root **build.gradle** file : 11 | 12 | ``` 13 | allprojects { 14 | repositories { 15 | ... 16 | maven { url 'https://jitpack.io' } 17 | } 18 | } 19 | ``` 20 | 21 | You can now add the library modules in your application **build.gradle** file : 22 | 23 | ``` 24 | dependencies { 25 | implementation "com.github.alexandrepiveteau.parser-combinators-kotlin:parser-combinators:1.1.0" 26 | implementation "com.github.alexandrepiveteau.parser-combinators-kotlin:parser-combinators-primitives:1.1.0" 27 | } 28 | ``` 29 | 30 | ## Usage 31 | The library contains a single module, versioned using **semantic versioning** : 32 | 33 | - **parser-combinators** - Offers some primitives for building `Parser` instances, and combinators for manipulating `Parser` instances. 34 | - **parser-combinators-primitives** - Offers some functions for building `Parser` on primitive data types in Kotlin. 35 | 36 | ### parser-combinators 37 | 38 | A `Parser` is a structure accepting a `Input` as input, and returning an `Either>`, where the `Error` case of the `Either` represents a problem that occurred while parsing the input, and the `Value` case of the `Either` represents a pair of the parser output, and the remaining `Input` that has not been parsed yet. A parser combinator is a function that accepts one or multiple parsers as input and outputs a different parser. 39 | 40 | Each `Parser` instance is just an immutable wrapper around a parsing function. Therefore, it is easily possible to create your own instances of `Parser` from scratch. Nevertheless, some default `Parser` implementations are provided : 41 | 42 | ```kotlin 43 | /* 44 | * This Parser will always fail, no matter what input sequence is provided to it. This can 45 | * be useful when combining multiple Parsers together. 46 | */ 47 | val l = Parser.fail { IllegalArgumentException("This parser always fail.") } 48 | 49 | /* 50 | * This Parser uses a lazily evaluated lambda as its argument. It can easily be used to 51 | * recursively call itself. The lambda has the type () -> Parser in this 52 | * example. 53 | */ 54 | val e = Parser.lazy { TODO("Make a recursive call, lazily evaluated.") } 55 | 56 | /* 57 | * This Parser always succeeds. It will produce a Parser instance, and therefore 58 | * has can be mapped to produce a "default" value of any type. 59 | */ 60 | val x = Parser.succeed().map { 34 } // Parser 61 | ``` 62 | 63 | Multiple combinators are provided as **extension functions** in the library. For instance, the `map { }` function (used at the end of the previous snippet) is a combinator that transforms the value produced by a `Parser`. These high-level functions can be used to build high-level `Parser` objects, which can for instance contain some custom types. 64 | 65 | The following parser combinators are provided as **extension functions** (in each one of these, the first argument is always the current `Parser` instance) : 66 | 67 | - `map(f: (O1) -> O2)` - Returns a `Parser` that transforms the value produced using a mapping function. 68 | - `flatMap(f: (O1) -> Either)` - Returns a `Parser` that, like `map {}`, transforms the value produced using a mapping function. It can also transform the value into an `Either.Error`, which will make the `Parser` fail. This can be used when you want to validate your model with some logic that can't be easily built into parsers otherwise. 69 | - `and(other: Parser)` - Returns a `Parser` that pairs the responses of the two combined parsers. 70 | - `after(other: Parser)` - Same as `and`, but returns only the second value of the pair. 71 | - `before(other: Parser)` - Same as `and`, but returns only the first value of the pair. 72 | - `or(other: Parser)` - Tries the first `Parser` instance, and, if it fails, tries the `other` instance. Returns a `Parser` formed of an `Either` based on the result of the parsing. 73 | - `flatOr(other: Parser)` - Same as `or`, but because both `Parser` have the same type, the resulting `Either` can safely be flattened. 74 | - `loop()` - Returns a `Parser` that applies itself repeatedly, until it fails. Returns a `List` of the results of each iteration. 75 | -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env sh 2 | 3 | ############################################################################## 4 | ## 5 | ## Gradle start up script for UN*X 6 | ## 7 | ############################################################################## 8 | 9 | # Attempt to set APP_HOME 10 | # Resolve links: $0 may be a link 11 | PRG="$0" 12 | # Need this for relative symlinks. 13 | while [ -h "$PRG" ] ; do 14 | ls=`ls -ld "$PRG"` 15 | link=`expr "$ls" : '.*-> \(.*\)$'` 16 | if expr "$link" : '/.*' > /dev/null; then 17 | PRG="$link" 18 | else 19 | PRG=`dirname "$PRG"`"/$link" 20 | fi 21 | done 22 | SAVED="`pwd`" 23 | cd "`dirname \"$PRG\"`/" >/dev/null 24 | APP_HOME="`pwd -P`" 25 | cd "$SAVED" >/dev/null 26 | 27 | APP_NAME="Gradle" 28 | APP_BASE_NAME=`basename "$0"` 29 | 30 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 31 | DEFAULT_JVM_OPTS="" 32 | 33 | # Use the maximum available, or set MAX_FD != -1 to use that value. 34 | MAX_FD="maximum" 35 | 36 | warn () { 37 | echo "$*" 38 | } 39 | 40 | die () { 41 | echo 42 | echo "$*" 43 | echo 44 | exit 1 45 | } 46 | 47 | # OS specific support (must be 'true' or 'false'). 48 | cygwin=false 49 | msys=false 50 | darwin=false 51 | nonstop=false 52 | case "`uname`" in 53 | CYGWIN* ) 54 | cygwin=true 55 | ;; 56 | Darwin* ) 57 | darwin=true 58 | ;; 59 | MINGW* ) 60 | msys=true 61 | ;; 62 | NONSTOP* ) 63 | nonstop=true 64 | ;; 65 | esac 66 | 67 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 68 | 69 | # Determine the Java command to use to start the JVM. 70 | if [ -n "$JAVA_HOME" ] ; then 71 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 72 | # IBM's JDK on AIX uses strange locations for the executables 73 | JAVACMD="$JAVA_HOME/jre/sh/java" 74 | else 75 | JAVACMD="$JAVA_HOME/bin/java" 76 | fi 77 | if [ ! -x "$JAVACMD" ] ; then 78 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 79 | 80 | Please set the JAVA_HOME variable in your environment to match the 81 | location of your Java installation." 82 | fi 83 | else 84 | JAVACMD="java" 85 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 86 | 87 | Please set the JAVA_HOME variable in your environment to match the 88 | location of your Java installation." 89 | fi 90 | 91 | # Increase the maximum file descriptors if we can. 92 | if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then 93 | MAX_FD_LIMIT=`ulimit -H -n` 94 | if [ $? -eq 0 ] ; then 95 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 96 | MAX_FD="$MAX_FD_LIMIT" 97 | fi 98 | ulimit -n $MAX_FD 99 | if [ $? -ne 0 ] ; then 100 | warn "Could not set maximum file descriptor limit: $MAX_FD" 101 | fi 102 | else 103 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 104 | fi 105 | fi 106 | 107 | # For Darwin, add options to specify how the application appears in the dock 108 | if $darwin; then 109 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 110 | fi 111 | 112 | # For Cygwin, switch paths to Windows format before running java 113 | if $cygwin ; then 114 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 115 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 116 | JAVACMD=`cygpath --unix "$JAVACMD"` 117 | 118 | # We build the pattern for arguments to be converted via cygpath 119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 120 | SEP="" 121 | for dir in $ROOTDIRSRAW ; do 122 | ROOTDIRS="$ROOTDIRS$SEP$dir" 123 | SEP="|" 124 | done 125 | OURCYGPATTERN="(^($ROOTDIRS))" 126 | # Add a user-defined pattern to the cygpath arguments 127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 129 | fi 130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 131 | i=0 132 | for arg in "$@" ; do 133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 135 | 136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 138 | else 139 | eval `echo args$i`="\"$arg\"" 140 | fi 141 | i=$((i+1)) 142 | done 143 | case $i in 144 | (0) set -- ;; 145 | (1) set -- "$args0" ;; 146 | (2) set -- "$args0" "$args1" ;; 147 | (3) set -- "$args0" "$args1" "$args2" ;; 148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;; 149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 154 | esac 155 | fi 156 | 157 | # Escape application args 158 | save () { 159 | for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done 160 | echo " " 161 | } 162 | APP_ARGS=$(save "$@") 163 | 164 | # Collect all arguments for the java command, following the shell quoting and substitution rules 165 | eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS" 166 | 167 | # by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong 168 | if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then 169 | cd "$(dirname "$0")" 170 | fi 171 | 172 | exec "$JAVACMD" "$@" 173 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_launcher_background.xml: -------------------------------------------------------------------------------- 1 | 2 | 25 | 26 | 31 | 34 | 39 | 44 | 49 | 54 | 59 | 64 | 69 | 74 | 79 | 84 | 89 | 94 | 99 | 104 | 109 | 114 | 119 | 124 | 129 | 134 | 139 | 144 | 149 | 154 | 159 | 164 | 169 | 174 | 179 | 184 | 189 | 194 | 195 | -------------------------------------------------------------------------------- /parser-combinators/src/main/java/com/github/alexandrepiveteau/parsers/Parser.combinators.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2018 Alexandre Piveteau 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | package com.github.alexandrepiveteau.parsers 26 | 27 | import com.github.alexandrepiveteau.functional.monads.* 28 | 29 | /** 30 | * Returns the combination of two [Parser]s but ignores the value returned by the first one. Implemented as a 31 | * combination of an [and] and a [map]. 32 | * 33 | * @param I The type of the input provided to the [Parser]. 34 | * @param O1 The type of the output provided by the original [Parser]. 35 | * @param O2 The type of the output provided by the new [Parser]. 36 | * @param E The type of errors generated by the parser. 37 | * 38 | * @param other The instance of [Parser] that this instance is combined with. 39 | */ 40 | fun Parser.after(other: Parser): Parser = 41 | and(other).map { (_, b) -> b } 42 | 43 | /** 44 | * Returns the combinator of two [Parser]s with as generated output a [Pair] of the result produced by the first 45 | * [Parser], followed by the output produced by the second [Parser] immediately after. 46 | * 47 | * If either of the two [Parser]s fail, the first [Parser] to fail will return its [E] error. 48 | * 49 | * @param I The type of the input provided to the [Parser]. 50 | * @param O1 The type of the output provided by the original [Parser]. 51 | * @param O2 The type of the output provided by the new [Parser]. 52 | * @param E The type of errors generated by the parser. 53 | * 54 | * @param other The instance of [Parser] that this instance is combined with. 55 | */ 56 | fun Parser.and(other: Parser): Parser, E> = 57 | Parser { input -> 58 | val result1 = this.parse(input) 59 | return@Parser when (result1) { 60 | is Either.Error -> eitherError, I>>(result1.error) 61 | is Either.Value -> { 62 | val result2 = other.parse(result1.value.second) 63 | return@Parser when (result2) { 64 | is Either.Error -> eitherError, I>>(result2.error) 65 | is Either.Value -> eitherValue(result1.value.first to result2.value.first to result2.value.second) 66 | } 67 | } 68 | } 69 | } 70 | 71 | /** 72 | * Returns the combination of two [Parser]s but ignores the value returned by the second one. Implemented as a 73 | * combination of an [and] and a [map]. 74 | * 75 | * @param I The type of the input provided to the [Parser]. 76 | * @param O1 The type of the output provided by the original [Parser]. 77 | * @param O2 The type of the output provided by the new [Parser]. 78 | * @param E The type of errors generated by the parser. 79 | * 80 | * @param other The instance of [Parser] that this instance is combined with. 81 | */ 82 | fun Parser.before(other: Parser): Parser = 83 | and(other).map { (a, _) -> a } 84 | 85 | /** 86 | * Returns a [Parser] that will perform the same operation as a [map], but flattens the result. 87 | * 88 | * @param I The type of the input provided to the [Parser]. 89 | * @param O1 The type of the output provided by the original [Parser]. 90 | * @param O2 The type of the output provided by the new [Parser]. 91 | * @param E The type of errors generated by the parser. 92 | */ 93 | fun Parser.flatMap(f: (O1) -> Either): Parser = 94 | Parser { input -> 95 | val result = this.parse(input) 96 | return@Parser when (result) { 97 | is Either.Error -> eitherError>(result.error) 98 | is Either.Value -> { 99 | val mappedResult = f(result.value.first) 100 | return@Parser when (mappedResult) { 101 | is Either.Error -> eitherError>(mappedResult.error) 102 | is Either.Value -> eitherValue(mappedResult.value to result.value.second) 103 | } 104 | } 105 | } 106 | } 107 | 108 | /** 109 | * Returns a [Parser] that will perform the same operation as a [or], but flattens the result. 110 | * 111 | * @param I The type of the input provided to the [Parser]. 112 | * @param O The type of the output provided by the [Parser]. 113 | * @param E The type of errors generated by the parser. 114 | */ 115 | fun Parser.flatOr(other: Parser): Parser = 116 | or(other).map { either -> 117 | return@map when (either) { 118 | is Either.Error -> either.error 119 | is Either.Value -> either.value 120 | } 121 | } 122 | 123 | 124 | /** 125 | * Returns a new instance of [Parser] that will have a bijection relationship to its child parser. The original [Parser] 126 | * will be used underneath, and a bi-directional mapping of values will occur. This operator acts on input values. 127 | * 128 | * This is a bit the opposite of the [map] operator that acts on output values. 129 | * 130 | * @param I1 The type of the input of the original [Parser]. 131 | * @param I2 The type of the input of the new [Parser]. 132 | * @param O The type of the output provided by the parsers. 133 | * @param E The type of th errors generated by the parser. 134 | * 135 | * @param f The mapping function from [I2] to [I1]. 136 | * @param g The mapping function from [I1] to [I2]. 137 | */ 138 | fun Parser.local(f: (I2) -> I1, g: (I1) -> I2): Parser = 139 | Parser { a: I2 -> parse(f(a)).toValue().map { (x, y) -> x to g(y) }.either } 140 | 141 | /** 142 | * Returns a [Parser] that will try to compose the original [Parser] into a [Parser] of a never-ending sequence of 143 | * outputs produced by the original [Parser]. The implementation of this [Parser] should be stack-safe, as it avoids 144 | * using recursion. 145 | * 146 | * @param I The type of the input provided to the [Parser]. 147 | * @param O The type of the output provided by the [Parser]. 148 | * @param E The type of errors generated by the parser. 149 | */ 150 | fun Parser.loop(): Parser, E> = 151 | Parser { input -> 152 | val elements = mutableListOf() 153 | var remainder = input 154 | val out: Either, I>> 155 | iterator@ while (true) { 156 | val result = this.parse(remainder) 157 | when (result) { 158 | is Either.Error -> { 159 | out = eitherValue(elements to remainder) 160 | break@iterator 161 | } 162 | is Either.Value -> { 163 | elements += result.value.first 164 | remainder = result.value.second 165 | } 166 | } 167 | } 168 | return@Parser out 169 | } 170 | 171 | /** 172 | * Returns a [Parser] where the original output value is mapped on to produce another type of output value. If you 173 | * want to act on the input type of the [Parser], you should use [local] instead. 174 | * 175 | * @param I The type of the input provided to the [Parser]. 176 | * @param O1 The type of the output provided by the original [Parser]. 177 | * @param O2 The type of the output provided by the new [Parser]. 178 | * @param E The type of errors generated by the parser. 179 | * 180 | * @param f The mapping function from [O1] to [O2]. 181 | */ 182 | fun Parser.map(f: (O1) -> O2): Parser = 183 | Parser { input -> parse(input).toValue().map { (o, r) -> f(o) to r }.either } 184 | 185 | /** 186 | * Returns a new instance of [Parser] that will map the content to a [Maybe] instance. Would the original [Parser] fail, 187 | * the new [Parser] will return an empty [Maybe] instance. Would the [Parser] succeed, the resulting value will simply 188 | * be wrapped in a [Maybe] type. 189 | * 190 | * @param I The type of the input provided to the [Parser]. 191 | * @param O The type of the output provided by the [Parser]. 192 | * @param E The type of errors generated by the parser. 193 | */ 194 | fun Parser.optional(): Parser, E> = 195 | map { maybeOf(it) } 196 | .flatOr(Parser.succeed().map { emptyMaybe() }) 197 | 198 | /** 199 | * Returns a new instance of [Parser] that will combine two different instances of [Parser] with the same input type and 200 | * a potentially different output type. The first [Parser] instance will be tried, and if it fails, the second [Parser] 201 | * instance will be tried instead. If both fail, the error message will be created by the second [Parser] instance. 202 | * 203 | * @param I The type of the input provided to the [Parser]. 204 | * @param O1 The type of the output provided by the original [Parser]. 205 | * @param O2 The type of the output provided by the new [Parser]. 206 | * @param E The type of errors generated by the parser. 207 | * 208 | * @param other The instance of [Parser] that this instance is combined with. 209 | */ 210 | fun Parser.or(other: Parser): Parser, E> = 211 | Parser { input -> 212 | val result1 = this.parse(input) 213 | return@Parser when (result1) { 214 | is Either.Error -> { 215 | val result2 = other.parse(input) 216 | return@Parser when (result2) { 217 | is Either.Error -> eitherError, I>>(result2.error) 218 | is Either.Value -> eitherValue(eitherError(result2.value.first) to result2.value.second) 219 | } 220 | } 221 | is Either.Value -> eitherValue, I>>(eitherValue(result1.value.first) to result1.value.second) 222 | } 223 | } --------------------------------------------------------------------------------