Skip to content

Commit

Permalink
Merge pull request #493 from nevalang/regexp_submatcher
Browse files Browse the repository at this point in the history
feat(std:regexp): Submatcher (+ example)
  • Loading branch information
emil14 authored Mar 4, 2024
2 parents 03df735 + 15c4b61 commit 9175088
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 13 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# build neva cli for host OS and put to the PATH
.PHONY: install
install:
@go build -ldflags="-s -w" cmd/cli/*.go && \
@go build -ldflags="-s -w" ./cmd/cli && \
rm -rf /usr/local/bin/neva && \
mv cli /usr/local/bin/neva

Expand Down
17 changes: 17 additions & 0 deletions examples/10_regex_submatch/main.neva
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { regexp }

const { regex string = 'a(x*)b(y|z)c' }

component Main(start) (stop) {
nodes {
Printer<any>

#bind(regex)
regexp.Submatcher
}
net {
:start -> ('-axxxbyc-' -> submatcher:data)
submatcher:res -> printer:data
printer:sig -> :stop
}
}
50 changes: 50 additions & 0 deletions internal/runtime/funcs/regexp_submatcher.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package funcs

import (
"context"
"regexp"

"github.com/nevalang/neva/internal/runtime"
)

type regexpSubmatcher struct{}

func (r regexpSubmatcher) Create(io runtime.FuncIO, cfgMsg runtime.Msg) (func(ctx context.Context), error) {
regex, err := regexp.Compile(cfgMsg.Str())
if err != nil {
return nil, err
}

dataIn, err := io.In.Port("data")
if err != nil {
return nil, err
}

resOut, err := io.Out.Port("res")
if err != nil {
return nil, err
}

return func(ctx context.Context) {
for {
select {
case <-ctx.Done():
return
case dataMsg := <-dataIn:
resOut <- wrap(
regex.FindStringSubmatch(
dataMsg.String(),
),
)
}
}
}, nil
}

func wrap(ss []string) runtime.Msg {
msgs := make([]runtime.Msg, 0, len(ss))
for _, s := range ss {
msgs = append(msgs, runtime.NewStrMsg(s))
}
return runtime.NewListMsg(msgs...)
}
3 changes: 2 additions & 1 deletion internal/runtime/funcs/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ func CreatorRegistry() map[string]runtime.FuncCreator {
"line_printer": linePrinter{},
// math
"int_adder": intAdder{},
// parsing
// regexp
"regexp_submatcher": regexpSubmatcher{},
}
}
2 changes: 1 addition & 1 deletion pkg/version.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package pkg

// Version is the current version of the language and stdlib
var Version = "0.9.0" //nolint:gochecknoglobals
var Version = "0.10.0" //nolint:gochecknoglobals
3 changes: 3 additions & 0 deletions std/regexp/regexp.neva
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// bind regexp string
#extern(regexp_submatcher)
pub component Submatcher(data string) (res list<string>)
10 changes: 0 additions & 10 deletions std/time/time.neva

This file was deleted.

0 comments on commit 9175088

Please sign in to comment.