Skip to content

Commit

Permalink
Merge pull request #15 from kasiaMarek/greedy
Browse files Browse the repository at this point in the history
Make TyRE operators greedy.
  • Loading branch information
kasiaMarek authored Dec 14, 2023
2 parents 92fee86 + f97fd17 commit 87f9755
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 5 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.*
*.worksheet.sc
!.gitignore
!.scalafmt.conf
!.github/
Expand Down
5 changes: 3 additions & 2 deletions src/main/scala/net/marek/tyre/automaton/Compiler.scala
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ private[tyre] class TyreCompiler[IN <: Tuple, R](val context: Context[R *: IN]):
is.state.next.flatMap(fixTransition[is.OS](fixableStates, _))
def test(c: Char): Boolean = is.state.test(c)
def opTail(x: IS) = is.op(x)
fixableStates ++
val continuationStates =
continuation.initStates.map:
case is: InitAcceptingState[?] =>
new InitAcceptingState((x: IS) => is.op(Nil *: x))
Expand All @@ -93,6 +93,7 @@ private[tyre] class TyreCompiler[IN <: Tuple, R](val context: Context[R *: IN]):
type OS = is.OS
lazy val state = is.state
def op(x: IS) = is.op(Nil *: x)
fixableStates ++ continuationStates

private def fixTransition[S <: Tuple](
initStates: List[RefinedInitNonAcceptingState[T, IS]],
Expand Down Expand Up @@ -126,7 +127,7 @@ private[tyre] class TyreCompiler[IN <: Tuple, R](val context: Context[R *: IN]):
case l *: e *: t =>
is.op(t) match
case Nil *: tt => (l :+ e) *: tt
withContinuation ++ looped
looped ++ withContinuation

private def transitionWithTail[S <: Tuple, A <: Tuple](
head: context.AcceptingTransition[S],
Expand Down
2 changes: 1 addition & 1 deletion src/test/scala/net/marek/tyre/AutomatonTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,6 @@ class AutomatonTest extends AnyFunSuite:
test("Star in Star"):
val tyre = ('A').rep.rep
val m = tyre.compile()
assert(m.run("AAA").contains(List(List('A'), List('A'), List('A'))))
assert(m.run("AAA").contains(List(List('A', 'A', 'A'))))
assert(m.run("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA").isDefined)
assert(m.run("AAX").isEmpty)
20 changes: 20 additions & 0 deletions src/test/scala/net/marek/tyre/GreedyTest.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package net.marek.tyre

import org.scalatest.funsuite.AnyFunSuite

class GreedyTest extends AnyFunSuite:

test("Star and option are greedy"):
val ts = tyre"a?a?"
val ms = ts.compile()
assert(ms.run("a").contains((Some('a'), None)))
val t = tyre"ab|[a-z]c"
val tc1 = tyre"$t*$t?"
val mc1 = tc1.compile()
assert(mc1.run("abab").contains((List(('a', 'b'), ('a', 'b')), None)))
val tc2 = tyre"$t*$t*"
val mc2 = tc2.compile()
assert(mc2.run("abab").contains((List(('a', 'b'), ('a', 'b')), Nil)))
val tc3 = tyre"$t?$t*"
val mc3 = tc3.compile()
assert(mc3.run("abab").contains((Some(('a', 'b')), List(('a', 'b')))))
6 changes: 4 additions & 2 deletions todo.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,20 @@ TODO
- [X] Add handling of character classes (\s . \w etc.)
- [X] Allow special character in brackets without escaping (eg. .)
- [X] Allow character classes in brackets (eg. [^\s])
- [ ] Write readme
- [X] Write readme
- [X] Stringify - allow easily flatten matched data to string
- [X] Separate API from internals (packages)

### BACKLOG

- [ ] Check if TyRE matching is greedy
- [X] Check if TyRE matching is greedy
- [X] Support for Unicode character values (\uhhhh)
- [ ] Allow escaped characters in ranges (eg. [\t-s]) and generally in brackets (eg. [^\s])
- [ ] Support for singleton types
- [ ] Helper functions for handling digits and numbers
- [ ] Unicode mode
- [ ] Scaladoc
- [ ] Prepare deployment

### TO CONSIDER

Expand Down

0 comments on commit 87f9755

Please sign in to comment.