• Stars
    star
    161
  • Rank 224,718 (Top 5 %)
  • Language
    Scala
  • License
    Apache License 2.0
  • Created about 6 years ago
  • Updated about 1 month ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Statically-checked inline matching on regular expressions in Scala

GitHub Workflow

Kaleidoscope

Statically-checked inline matching on regular expressions

Kaleidoscope is a small library which provides pattern matching using regular expressions, and extraction of capturing groups into values, which are typed according to the repetition of the group. Patterns can be written inline, directly in a case pattern, and do not need to be predefined.

Features

  • pattern match strings against regular expressions
  • regular expressions can be written inline in patterns
  • extraction of capturing groups in patterns
  • typed extraction (into Lists or Options) of variable-length capturing groups
  • static verification of regular expression syntax
  • simpler "glob" syntax is also provided

Availability

Kaleidoscope has not yet been published as a binary.

Getting Started

To use Kaleidoscope, first import its package,

import kaleidoscope.*

and you can then use a Kaleidoscope regular expression—a string prefixed with the letter r—anywhere you can use a pattern in Scala. For example,

path match
  case r"/images/.*" => println("image")
  case r"/styles/.*" => println("stylesheet")
  case _             => println("something else")

or,

email match
  case r"^[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,6}$$" => Some(email)
  case _                                            => None

Such patterns will either match or not, however should they match, it is possible to extract parts of the matched string using capturing groups. The pattern syntax is exactly as described in the Java Standard Library, with the exception that a capturing group (enclosed within ( and )) may be bound to an identifier by placing it, like an interpolated string substitution, immediately prior to the capturing group, as $identifier or ${identifier}.

Here is an example:

path match
  case r"/images/${img}(.*)"  => Image(img)
  case r"/styles/$styles(.*)" => Stylesheet(styles)

Alternatively, this can be extracted directly in a val definition, like so:

val r"^[a-z0-9._%+-]+@$domain([a-z0-9.-]+\.$tld([a-z]{2,6})$$" = "[email protected]"
> domain: String = "example.com"
> tld: String = "com"

In addition, the syntax of the regular expressionwill be checked at compile-time, and any issues will be reported then.

Repeated and optional capture groups

A normal, unitary capturing group will extract into a Text value. But if a capturing group has a repetition suffix, such as * or +, then the extracted type will be a List[Text]. This also applies to repetition ranges, such as {3}, {2,} or {1,9}. Note that {1} will still extract a Text value.

A capture group may be marked as optional, meaning it can appear either zero or one times. This will extract a value with the type Option[Text].

For example, see how init is extracted as a List[Text], below:

"parsley, sage, rosemary, and thyme" match
  case r"$only([a-z]+)"                      => List(only)
  case r"$first([a-z]+) and $second([a-z]+)" => List(first, second)
  case r"$init([a-z]+, )*and $last([a-z]+)"  => init.map(_.drop(2, Rtl)) :+ last

Escaping

Note that inside an extractor pattern string, whether it is single- (r"...") or triple-quoted (r"""..."""), special characters, notably \, do not need to be escaped, with the exception of $ which should be written as $$. It is still necessary, however, to follow the regular expression escaping rules, for example, an extractor matching a single opening parenthesis would be written as r"\(" or r"""\(""".

Status

Kaleidoscope is classified as maturescent. For reference, Scala One projects are categorized into one of the following five stability levels:

  • embryonic: for experimental or demonstrative purposes only, without any guarantees of longevity
  • fledgling: of proven utility, seeking contributions, but liable to significant redesigns
  • maturescent: major design decisions broady settled, seeking probatory adoption and refinement
  • dependable: production-ready, subject to controlled ongoing maintenance and enhancement; tagged as version 1.0.0 or later
  • adamantine: proven, reliable and production-ready, with no further breaking changes ever anticipated

Projects at any stability level, even embryonic projects, are still ready to be used, but caution should be taken if there is a mismatch between the project's stability level and the importance of your own project.

Kaleidoscope is designed to be small. Its entire source code currently consists of 509 lines of code.

Building

Kaleidoscope can be built on Linux or Mac OS with Fury, however the approach to building is currently in a state of flux, and is likely to change.

Contributing

Contributors to Kaleidoscope are welcome and encouraged. New contributors may like to look for issues marked beginner.

We suggest that all contributors read the Contributing Guide to make the process of contributing to Kaleidoscope easier.

Please do not contact project maintainers privately with questions unless there is a good reason to keep them private. While it can be tempting to repsond to such questions, private answers cannot be shared with a wider audience, and it can result in duplication of effort.

Author

Kaleidoscope was designed and developed by Jon Pretty, and commercial support and training is available from Propensive OÜ.

Name

Kaleidoscope is named after the optical instrument which shows pretty patterns to its user, while the library also works closely with patterns.

In general, Scala One project names are always chosen with some rationale, however it is usually frivolous. Each name is chosen for more for its uniqueness and intrigue than its concision or catchiness, and there is no bias towards names with positive or "nice" meanings—since many of the libraries perform some quite unpleasant tasks.

Names should be English words, though many are obscure or archaic, and it should be noted how willingly English adopts foreign words. Names are generally of Greek or Latin origin, and have often arrived in English via a romance language.

License

Kaleidoscope is copyright © 2023 Jon Pretty & Propensive OÜ, and is made available under the Apache 2.0 License.

More Repositories

1

fury-old

A new build tool for JVM languages
Scala
405
star
2

contextual

Statically-checked string interpolation in Scala
Scala
245
star
3

rapture

Rapture
Scala
184
star
4

probably

To probe what we can't prove, so the unprovable may become probable; testing for Scala
Scala
55
star
5

mercator-old

Automatic typeclass-based abstraction over monad-like types
Scala
54
star
6

totalitarian

Totalitarian: typesafe data structures for working with total functions
Scala
49
star
7

impromptu

Lightweight and typesafe asynchronous computation graphs
Scala
41
star
8

mutatus

Lightweight API for working with cloud data
Scala
40
star
9

one

A platform of libraries for Scala 3
Shell
33
star
10

xylophone

Working with XML in Scala
Scala
24
star
11

adversaria

Typeclass interfaces to access user-defined Scala annotations
Scala
24
star
12

gastronomy

Simple generically-derived cryptographic functions for Scala
Scala
20
star
13

polyvinyl

Typesafe record types for Scala
Scala
18
star
14

pyroclastic

Functional dataflow through composable computations
Scala
18
star
15

cosmopolite

Typesafe internationalization of strings in Scala
Scala
18
star
16

fury

A build tool for Scala
Scala
16
star
17

guillotine

Making the execution of system processes easier in Scala
Scala
12
star
18

exoskeleton

Interfaces for interacting with the shell from Scala
Scala
12
star
19

caesura

Simple parsing of CSV into case classes in Scala
Scala
10
star
20

quarantine

Safe and total handling of domain-specific exceptions
Scala
9
star
21

acyclicity

Monadic directed acyclic graph datastructures for Scala
Scala
9
star
22

escritoire

A library for writing tables for Scala
Scala
9
star
23

scintillate

A lightweight HTTP client and server for the Loom generation using Scala
Scala
9
star
24

wisteria

Easy, fast, transparent generic derivation of typeclass instances in Scala
Scala
8
star
25

profanity

A library for realtime interactive terminal software in Scala
Scala
6
star
26

rudiments

A collection of basic, useful methods to complement the Scala stardard library
Scala
6
star
27

iridescence

Sophisticated representation of color in Scala
Scala
5
star
28

honeycomb

A lightweight and typesafe DSL for embedding HTML in Scala code
Scala
5
star
29

optometry

Universal lenses with intuitive syntax
Scala
5
star
30

harlequin

Syntax highlighting for Scala code
Scala
5
star
31

turbulence

Simple tools for working with data streams in LazyLists in Scala
Scala
4
star
32

eucalyptus

Versatile logging with static configuration for Scala
Scala
4
star
33

merino

A JSON parser and AST
Scala
4
star
34

scalaworld

Scala World website
Scala
3
star
35

cataclysm

Typesafe CSS in Scala
Scala
3
star
36

umbrageous

Scala compiler plugin to shade packages at compiletime
Scala
3
star
37

serpentine

Precise navigation of path structures in Scala
Scala
3
star
38

galilei

Simple interfaces for I/O operations in Scala
Scala
3
star
39

ethereal

Run Scala applications as daemons
Scala
3
star
40

punctuation

Typesafe parsing and rendering of markdown in Scala
Scala
3
star
41

anticipation

Minimal typeclass definitions for seamless integration without hard dependencies for Scala
Scala
3
star
42

savagery

Scala-ble Vector Graphics
Scala
3
star
43

ferocity

TypeScript
3
star
44

jacinta

Simple interfaces for reading, processing and writing JSON in Scala
Scala
3
star
45

telekinesis

A lightweight URI representation and HTTP client
Scala
3
star
46

bifurcate

Deserialize binary data in Scala
Scala
3
star
47

quantitative

Statically-checked physical units with seamless syntax
Scala
2
star
48

dissonance

An implementation of Myers' diff algorithm for Scala
Scala
2
star
49

nettlesome

Typesafe representations of network concepts in Scala
Scala
2
star
50

tarantula

Drive a web browser with the WebDriver protocol for Scala
Scala
2
star
51

panopticon

Versatile and composable lenses for Scala
Scala
2
star
52

plutocrat

Typesafe representations of monetary values in Scala
Scala
2
star
53

spectacular

Scala typeclasses for rendering different values as text for different audiences
Scala
2
star
54

gossamer

Lightweight and typesafe strings in Scala
Scala
2
star
55

larceny

Scala 3 compiler plugin for testing compiler errors
Scala
2
star
56

wrath

Simplistic build script for bootstrapping Scala projects
Shell
2
star
57

mitigation

Mitigation
Scala
2
star
58

feudalism

Controlled borrowing of mutable values for safe concurrency
Scala
2
star
59

mercator

Autogeneration of functor and monad instances for Scala types
Scala
2
star
60

parasite

Structured asynchronous task management in Scala
Scala
2
star
61

escapade

Adventures in ANSI escape codes in Scala
Scala
2
star
62

vacuous

A simple representation of optional values
Scala
2
star
63

hypotenuse

A rigorous and consistent foundation for numerical and arithmetic programming
Scala
2
star
64

superlunary

Exploiting lightweight modular staging in Scala
Scala
2
star
65

embarcadero

Management of Docker containers for Scala
Scala
2
star
66

codl

The COllaborative Data Language is a lightweight data definition language for human/computer collaboration
Shell
1
star
67

nextgen-scala

Makefile
1
star
68

scala3-training

Scala
1
star
69

cellulose

A comprehensive Scala library for working with CoDL files
Scala
1
star
70

mosquito

Typesafe vector algebra for Scala
Scala
1
star
71

imperial

Access UNIX's standard directory structure from Scala
Scala
1
star
72

inimitable

UUIDs for Scala
Scala
1
star
73

denominative

An experiment in numeric semantics to eradicate off-by-one errors
Scala
1
star
74

zeppelin

Work with ZIP files in Scala
Scala
1
star
75

ulysses

An implementation of Bloom filters for Scala
Scala
1
star
76

oubliette

Quickly launch new JVMs by remote control from Scala
Shell
1
star
77

hyperbole

Assintance with Scala 3 metaprogramming
Scala
1
star
78

phoenicia

Read TrueType and OpenType fonts in Scala
Scala
1
star
79

yossarian

The insanity of terminal emulation
Scala
1
star
80

dendrology

Rendering of trees and DAGs in the console
Scala
1
star
81

nectary

Automatic OpenAPI endpoint generation
Scala
1
star
82

fulminate

Rich user messages for Scala
Scala
1
star
83

contingency

Safe and seamless validation and error aggregation
Scala
1
star
84

anthology

A typesafe interface to the Scala compiler
Scala
1
star
85

camouflage

Caching data structures for Scala
Scala
1
star
86

aviation

Representations of time, in its various forms, for Scala
Scala
1
star
87

villainy

Record types from JSON schemata in Scala
Scala
1
star
88

digression

Utilities for working with exceptions in Scala
Scala
1
star
89

amok

A comprehensive API documentation system
Scala
1
star
90

frontier

Scala
1
star
91

gesticulate

Safe representations of MIME types in Scala
Scala
1
star
92

coaxial

Socket handling for Scala
Scala
1
star
93

modicum

Scala
1
star
94

aristotle

A library and language for producing MathML
Scala
1
star
95

charisma

Represent chemical formulae in Scala
Scala
1
star
96

chiaroscuro

Provides structural comparisons between values
Scala
1
star
97

cardinality

Dependently-typed range-checked numbers for Scala
Scala
1
star
98

baroque

Make coding with complex numbers more real
Scala
1
star
99

hieroglyph

Calculate Unicode character display widths in Scala
Scala
1
star
100

ambience

Safely access environment variables and system properties in Scala
Scala
1
star